Skip to content

Commit 60f8039

Browse files
authored
fix(pwa): reject malformed signed cookies (#68)
1 parent d6ef72b commit 60f8039

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

apps/pwa/src/lib/crypto.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export function sign(value) {
3737

3838
export function unsign(signed) {
3939
if (!signed || typeof signed !== "string" || !signed.includes(".")) return null;
40-
const [payload, mac] = signed.split(".");
40+
const parts = signed.split(".");
41+
if (parts.length !== 2) return null;
42+
const [payload, mac] = parts;
4143
const expected = crypto.createHmac("sha256", config.sessionSecret).update(payload).digest("base64url");
4244
const a = Buffer.from(mac);
4345
const b = Buffer.from(expected);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
import { sign, unsign } from "../src/lib/crypto.mjs";
4+
5+
test("unsign accepts an untouched signed cookie value", () => {
6+
const signed = sign({ step: "passkey", nonce: "abc" });
7+
8+
assert.deepEqual(unsign(signed), { step: "passkey", nonce: "abc" });
9+
});
10+
11+
test("unsign rejects signed cookie values with extra segments", () => {
12+
const signed = sign({ step: "passkey", nonce: "abc" });
13+
14+
assert.equal(unsign(`${signed}.junk`), null);
15+
});

0 commit comments

Comments
 (0)