Files
co-lite/solvway-admin-portal/app/lib/sessionSeal.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

103 lines
4.4 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { sealSession, openSession } from "./sessionSeal";
import { encodeSession } from "./sessionCodec";
// sign-session-cookies (F32): the session cookie is HMAC-signed; anything edited, unsigned or forged reads as no session.
const SAMPLE = { token: "jwt-abc", resetPasswordRequired: true, absoluteExpiry: 1900000000 };
let saved: { secret?: string; key?: string };
beforeEach(() => {
saved = { secret: process.env.SESSION_SECRET, key: process.env.MOBILE_API_KEY };
process.env.SESSION_SECRET = "unit-test-secret";
});
afterEach(() => {
process.env.SESSION_SECRET = saved.secret;
process.env.MOBILE_API_KEY = saved.key;
if (saved.secret === undefined) delete process.env.SESSION_SECRET;
if (saved.key === undefined) delete process.env.MOBILE_API_KEY;
});
function edit(raw: string, change: (o: Record<string, unknown>) => void): string {
const obj = JSON.parse(decodeURIComponent(raw));
change(obj);
return encodeURIComponent(JSON.stringify(obj));
}
describe("sealSession / openSession", () => {
it("round-trips the session", async () => {
expect(await openSession(await sealSession(SAMPLE))).toEqual(SAMPLE);
});
it("keeps the cookie readable JSON with the signature as an extra field", async () => {
const obj = JSON.parse(decodeURIComponent(await sealSession(SAMPLE)));
expect(typeof obj.sig).toBe("string");
expect(obj.token).toBe(SAMPLE.token);
});
it("rejects an edited flag", async () => {
const raw = edit(await sealSession(SAMPLE), o => { o.resetPasswordRequired = !o.resetPasswordRequired; });
expect(await openSession(raw)).toBeNull();
});
it("rejects an edited token", async () => {
const raw = edit(await sealSession(SAMPLE), o => { o.token = "someone-else"; });
expect(await openSession(raw)).toBeNull();
});
it("rejects a stripped or forged signature", async () => {
const sealed = await sealSession(SAMPLE);
expect(await openSession(edit(sealed, o => { delete o.sig; }))).toBeNull();
expect(await openSession(edit(sealed, o => { o.sig = "AAAA"; }))).toBeNull();
});
it("rejects an unsigned cookie written before this change", async () => {
expect(await openSession(encodeSession(SAMPLE))).toBeNull();
});
it("rejects a cookie signed with another key", async () => {
const sealed = await sealSession(SAMPLE);
process.env.SESSION_SECRET = "another-secret";
expect(await openSession(sealed)).toBeNull();
});
it("rejects garbage and empty input", async () => {
expect(await openSession("not-json")).toBeNull();
expect(await openSession("")).toBeNull();
expect(await openSession(undefined)).toBeNull();
});
it("derives the key from MOBILE_API_KEY when SESSION_SECRET is unset", async () => {
delete process.env.SESSION_SECRET;
process.env.MOBILE_API_KEY = "server-api-key";
const sealed = await sealSession(SAMPLE);
expect(await openSession(sealed)).toEqual(SAMPLE);
process.env.MOBILE_API_KEY = "rotated-key";
expect(await openSession(sealed)).toBeNull();
});
it("fails closed with no key at all", async () => {
const sealed = await sealSession(SAMPLE);
delete process.env.SESSION_SECRET;
delete process.env.MOBILE_API_KEY;
await expect(sealSession(SAMPLE)).rejects.toThrow();
expect(await openSession(sealed)).toBeNull();
});
});
// Review follow-up: the key carries a per-portal label, so a cookie signed for the other portal (or with the bare
// secret) never verifies here, even when both portals share SESSION_SECRET or MOBILE_API_KEY.
describe("per-portal key", () => {
it("rejects a cookie signed with the bare secret or another portal's label", async () => {
process.env.SESSION_SECRET = "shared-secret";
const sealed = JSON.parse(decodeURIComponent(await sealSession(SAMPLE)));
delete sealed.sig;
const payload = JSON.stringify(sealed);
for (const keyText of ["shared-secret", "bb-session-v1:shared-secret", "bb-member-session-v1:shared-secret"]) {
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(keyText), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
const sig = new Uint8Array(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(payload)));
const forged = encodeURIComponent(JSON.stringify({ ...sealed, sig: btoa(String.fromCharCode(...sig)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "") }));
expect(await openSession(forged)).toBeNull();
}
});
});