mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-08-13 22:37:29 +00:00
Reject extra session token segments (#18)
This commit is contained in:
parent
aa7eb32c36
commit
d558554da3
2 changed files with 5 additions and 2 deletions
|
|
@ -376,7 +376,8 @@ describe("session signing", () => {
|
||||||
process.env.LOGICSRC_SESSION_SECRET = "session_secret_for_tests";
|
process.env.LOGICSRC_SESSION_SECRET = "session_secret_for_tests";
|
||||||
const token = signSession({ provider: "coinpay", sub: "merchant-123" });
|
const token = signSession({ provider: "coinpay", sub: "merchant-123" });
|
||||||
expect(verifySession(token)).toMatchObject({ provider: "coinpay", sub: "merchant-123" });
|
expect(verifySession(token)).toMatchObject({ provider: "coinpay", sub: "merchant-123" });
|
||||||
expect(verifySession(`${token}tampered`)).toBeNull();
|
expect(verifySession(`tampered`)).toBeNull();
|
||||||
|
expect(verifySession(`.extra`)).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,9 @@ export function signSession(payload: Record<string, unknown>): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function verifySession(value: string): Record<string, unknown> | null {
|
export function verifySession(value: string): Record<string, unknown> | null {
|
||||||
const [encoded, signature] = value.split(".");
|
const parts = value.split(".");
|
||||||
|
if (parts.length !== 2) return null;
|
||||||
|
const [encoded, signature] = parts;
|
||||||
if (!encoded || !signature) return null;
|
if (!encoded || !signature) return null;
|
||||||
|
|
||||||
const expected = createHmac("sha256", getSessionSecret()).update(encoded).digest("base64url");
|
const expected = createHmac("sha256", getSessionSecret()).update(encoded).digest("base64url");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue