|
| 1 | +import { betterAuth } from "better-auth"; |
| 2 | +import { memoryAdapter } from "better-auth/adapters/memory"; |
| 3 | +import { admin, customSession } from "better-auth/plugins"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { projectSessionPayload, projectSessionRow } from "../session-projection"; |
| 6 | + |
| 7 | +const storedRow = { |
| 8 | + id: "s1", |
| 9 | + userId: "u1", |
| 10 | + token: "fixture-not-a-real-token", |
| 11 | + expiresAt: new Date("2030-01-01T00:00:00.000Z"), |
| 12 | + createdAt: new Date(0), |
| 13 | + updatedAt: new Date(0), |
| 14 | + ipAddress: "203.0.113.7", |
| 15 | + userAgent: "fixture-agent/1.0", |
| 16 | + impersonatedBy: null, |
| 17 | +}; |
| 18 | + |
| 19 | +describe("projectSessionRow", () => { |
| 20 | + it("drops the session token, the stored IP address and the user agent", () => { |
| 21 | + const projected = projectSessionRow(storedRow); |
| 22 | + expect(projected).not.toHaveProperty("token"); |
| 23 | + expect(projected).not.toHaveProperty("ipAddress"); |
| 24 | + expect(projected).not.toHaveProperty("userAgent"); |
| 25 | + expect(JSON.stringify(projected)).not.toContain("fixture-not-a-real-token"); |
| 26 | + expect(JSON.stringify(projected)).not.toContain("203.0.113.7"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("keeps the fields callers read", () => { |
| 30 | + expect(projectSessionRow(storedRow)).toEqual({ |
| 31 | + id: "s1", |
| 32 | + userId: "u1", |
| 33 | + expiresAt: storedRow.expiresAt, |
| 34 | + createdAt: storedRow.createdAt, |
| 35 | + updatedAt: storedRow.updatedAt, |
| 36 | + impersonatedBy: null, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("keeps the impersonation marker the admin banner reads", () => { |
| 41 | + expect(projectSessionRow({ ...storedRow, impersonatedBy: "admin-1" }).impersonatedBy).toBe( |
| 42 | + "admin-1", |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does not let an unknown upstream column through", () => { |
| 47 | + const withExtra = { ...storedRow, futureSecret: "leak-me" }; |
| 48 | + expect(JSON.stringify(projectSessionRow(withExtra))).not.toContain("leak-me"); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("projectSessionPayload", () => { |
| 53 | + it("passes the user object through untouched", () => { |
| 54 | + const user = { id: "u1", name: "Ada", email: "ada@example.com", role: "admin" }; |
| 55 | + const result = projectSessionPayload({ user, session: storedRow }); |
| 56 | + expect(result.user).toBe(user); |
| 57 | + expect(result.session).not.toHaveProperty("token"); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +function buildTestAuth() { |
| 62 | + const options = { |
| 63 | + database: memoryAdapter({ user: [], session: [], account: [], verification: [] }), |
| 64 | + baseURL: "http://localhost:3000", |
| 65 | + secret: "session-projection-test-secret-not-a-real-credential", |
| 66 | + emailAndPassword: { enabled: true, autoSignIn: true }, |
| 67 | + plugins: [admin()], |
| 68 | + }; |
| 69 | + return betterAuth({ |
| 70 | + ...options, |
| 71 | + plugins: [ |
| 72 | + ...options.plugins, |
| 73 | + customSession(async ({ user, session }) => projectSessionPayload({ user, session }), options), |
| 74 | + ], |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +describe("the session endpoint", () => { |
| 79 | + it("does not return the session token, IP address or user agent", async () => { |
| 80 | + const testAuth = buildTestAuth(); |
| 81 | + |
| 82 | + const signUp = await testAuth.api.signUpEmail({ |
| 83 | + body: { name: "Ada", email: "ada@example.com", password: "fixture-password-1234" }, |
| 84 | + asResponse: true, |
| 85 | + }); |
| 86 | + const cookie = signUp.headers |
| 87 | + .getSetCookie() |
| 88 | + .map((entry) => entry.split(";")[0]) |
| 89 | + .join("; "); |
| 90 | + expect(cookie).not.toBe(""); |
| 91 | + |
| 92 | + const result = await testAuth.api.getSession({ headers: new Headers({ cookie }) }); |
| 93 | + |
| 94 | + expect(result).not.toBeNull(); |
| 95 | + expect(result?.user.email).toBe("ada@example.com"); |
| 96 | + expect(result?.session).not.toHaveProperty("token"); |
| 97 | + expect(result?.session).not.toHaveProperty("ipAddress"); |
| 98 | + expect(result?.session).not.toHaveProperty("userAgent"); |
| 99 | + expect(JSON.stringify(result)).not.toContain(signUp.headers.get("set-cookie") ?? ""); |
| 100 | + }); |
| 101 | + |
| 102 | + it("still sets no-store on the session response", async () => { |
| 103 | + const testAuth = buildTestAuth(); |
| 104 | + const signUp = await testAuth.api.signUpEmail({ |
| 105 | + body: { name: "Ada", email: "ada2@example.com", password: "fixture-password-1234" }, |
| 106 | + asResponse: true, |
| 107 | + }); |
| 108 | + const cookie = signUp.headers |
| 109 | + .getSetCookie() |
| 110 | + .map((entry) => entry.split(";")[0]) |
| 111 | + .join("; "); |
| 112 | + |
| 113 | + const response = await testAuth.api.getSession({ |
| 114 | + headers: new Headers({ cookie }), |
| 115 | + asResponse: true, |
| 116 | + }); |
| 117 | + |
| 118 | + expect(response.headers.get("cache-control")).toContain("no-store"); |
| 119 | + }); |
| 120 | +}); |
0 commit comments