(expectedPassword: string)
| 1002 | |
| 1003 | describe("PgPool SCRAM credential isolation", () => { |
| 1004 | const scramServer = (expectedPassword: string) => { |
| 1005 | const saltText = "DBRmN4Xi9iMOo1tZfsi+Hg==" |
| 1006 | const iterations = 4096 |
| 1007 | const salted = pbkdf2Sync(expectedPassword, Buffer.from(saltText, "base64"), iterations, 32, "sha256") |
| 1008 | const clientKey = createHmac("sha256", salted).update("Client Key").digest() |
| 1009 | const storedKey = createHash("sha256").update(clientKey).digest() |
| 1010 | const serverKey = createHmac("sha256", salted).update("Server Key").digest() |
| 1011 | |
| 1012 | return () => { |
| 1013 | let step: "startup" | "saslInit" | "saslFinal" = "startup" |
| 1014 | let clientFirstBare = "" |
| 1015 | let serverFirst = "" |
| 1016 | const socket = new Duplex({ |
| 1017 | read() {}, |
| 1018 | write(chunk: Buffer, _encoding, callback) { |
| 1019 | if (step === "startup") { |
| 1020 | step = "saslInit" |
| 1021 | const payload = Buffer.concat([Buffer.alloc(4), Buffer.from("SCRAM-SHA-256\0\0")]) |
| 1022 | payload.writeInt32BE(10, 0) |
| 1023 | queueMicrotask(() => socket.push(backendMessage("R", payload))) |
| 1024 | } else if (step === "saslInit") { |
| 1025 | step = "saslFinal" |
| 1026 | const mechEnd = chunk.indexOf(0, 5) |
| 1027 | const initial = chunk.subarray(mechEnd + 5).toString("utf8") |
| 1028 | clientFirstBare = initial.slice(3) |
| 1029 | const clientNonce = clientFirstBare.slice("n=,r=".length) |
| 1030 | serverFirst = `r=${clientNonce}server,s=${saltText},i=${iterations}` |
| 1031 | const payload = Buffer.concat([Buffer.alloc(4), Buffer.from(serverFirst)]) |
| 1032 | payload.writeInt32BE(11, 0) |
| 1033 | queueMicrotask(() => socket.push(backendMessage("R", payload))) |
| 1034 | } else if (step === "saslFinal") { |
| 1035 | const [withoutProof, proof] = chunk.subarray(5).toString("utf8").split(",p=") |
| 1036 | const authMessage = `${clientFirstBare},${serverFirst},${withoutProof}` |
| 1037 | const clientSig = createHmac("sha256", storedKey).update(authMessage).digest() |
| 1038 | const expectedProof = Buffer.from(clientKey.map((byte, i) => byte ^ clientSig[i])).toString("base64") |
| 1039 | if (proof === expectedProof) { |
| 1040 | const serverSig = createHmac("sha256", serverKey).update(authMessage).digest("base64") |
| 1041 | const saslFinal = Buffer.concat([Buffer.alloc(4), Buffer.from(`v=${serverSig}`)]) |
| 1042 | saslFinal.writeInt32BE(12, 0) |
| 1043 | queueMicrotask(() => socket.push(Buffer.concat([backendMessage("R", saslFinal), ready]))) |
| 1044 | } else { |
| 1045 | queueMicrotask(() => |
| 1046 | socket.push(backendMessage("E", Buffer.from("SFATAL\0C28P01\0Mpassword authentication failed\0\0"))) |
| 1047 | ) |
| 1048 | } |
| 1049 | } |
| 1050 | callback() |
| 1051 | } |
| 1052 | }) |
| 1053 | return socket |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | it.effect("binds cached salted passwords to the credential that produced them", () => |
| 1058 | Effect.scoped(Effect.gen(function*() { |
no test coverage detected