Skip to content

Commit

Permalink
Add ":" separator between the hash components
Browse files Browse the repository at this point in the history
mirroring HTTP digest authentication
  • Loading branch information
tmistele committed Dec 31, 2024
1 parent a5bd471 commit 43bb20c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions crates/tinymist/src/tool/preview/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ pub async fn try_auth_websocket_client(
.context("auth response 1 missing")??;
let response: AuthMsgResponseClient = serde_json::from_str(response.to_text()?)?;

if sha512hex(format!("{}{}{}", secret, challenge, response.cnonce).as_str()) == response.hash {
if sha512hex(format!("{}:{}:{}", secret, challenge, response.cnonce).as_str()) == response.hash
{
// ... then we authenticate to the client
let snonce = generate_token();
let hash = sha512hex(format!("{}{}{}", secret, response.challenge, snonce).as_str());
let hash = sha512hex(format!("{}:{}:{}", secret, response.challenge, snonce).as_str());
let json = serde_json::to_string(&AuthMsgResponseServer {
snonce: &snonce,
hash: &hash,
Expand Down
4 changes: 2 additions & 2 deletions tools/typst-preview-frontend/src/ws/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function getAuthenticatedSocket(url: string, secret: string, dec: TextDec
const cnonce = generateCryptoRandom(32);
prews.next(enc.encode(JSON.stringify({
'cnonce': cnonce,
'hash': await digestHex(secret + message.challenge + cnonce),
'hash': await digestHex(secret + ":" + message.challenge + ":" + cnonce),
'challenge': challengeForServer
})));

Expand Down Expand Up @@ -122,7 +122,7 @@ export function getAuthenticatedSocket(url: string, secret: string, dec: TextDec
// Server liked our 'hash'. Now we check if the server is malicious or not
if(message.snonce === undefined || message.hash === undefined)
throw new Error("Missing snonce or hash.");
if(message.hash !== await digestHex(secret + challengeForServer + message.snonce))
if(message.hash !== await digestHex(secret + ":" + challengeForServer + ":" + message.snonce))
throw new Error("Malicious server detected?!");

// Authentication succeeded!
Expand Down

0 comments on commit 43bb20c

Please sign in to comment.