Skip to content

Commit 33ae0ed

Browse files
committed
Fix race conditions when creating payloads - fixes porsager#430 porsager#668
1 parent 428475a commit 33ae0ed

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/connection.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -656,27 +656,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
656656

657657
/* c8 ignore next 5 */
658658
async function AuthenticationCleartextPassword() {
659+
const payload = await Pass()
659660
write(
660-
b().p().str(await Pass()).z(1).end()
661+
b().p().str(payload).z(1).end()
661662
)
662663
}
663664

664665
async function AuthenticationMD5Password(x) {
665-
write(
666-
b().p().str(
667-
'md5' +
668-
(await md5(Buffer.concat([
666+
const payload = 'md5' + (
667+
await md5(
668+
Buffer.concat([
669669
Buffer.from(await md5((await Pass()) + user)),
670670
x.subarray(9)
671-
])))
672-
).z(1).end()
671+
])
672+
)
673+
)
674+
write(
675+
b().p().str(payload).z(1).end()
673676
)
674677
}
675678

676679
async function SASL() {
680+
nonce = (await crypto.randomBytes(18)).toString('base64')
677681
b().p().str('SCRAM-SHA-256' + b.N)
678682
const i = b.i
679-
nonce = (await crypto.randomBytes(18)).toString('base64')
680683
write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())
681684
}
682685

@@ -698,12 +701,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
698701

699702
serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')
700703

704+
const payload = 'c=biws,r=' + res.r + ',p=' + xor(
705+
clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))
706+
).toString('base64')
707+
701708
write(
702-
b().p().str(
703-
'c=biws,r=' + res.r + ',p=' + xor(
704-
clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))
705-
).toString('base64')
706-
).end()
709+
b().p().str(payload).end()
707710
)
708711
}
709712

0 commit comments

Comments
 (0)