Download tasks/codeql_javascript_benchmark_top30_v1/morefixes-1cf55be2336b09a515a6/patch.diff from Benchanything/checkerbench: direct link, hf CLI and curl.
- Browser
- Download file 39.6 kB
-
https://huggingface.co/datasets/Benchanything/checkerbench/resolve/main/tasks/codeql_javascript_benchmark_top30_v1/morefixes-1cf55be2336b09a515a6/patch.diff
- Command line
-
hf download hf://datasets/Benchanything/checkerbench/tasks/codeql_javascript_benchmark_top30_v1/morefixes-1cf55be2336b09a515a6/patch.diff
-
curl -L -o patch.diff https://huggingface.co/datasets/Benchanything/checkerbench/resolve/main/tasks/codeql_javascript_benchmark_top30_v1/morefixes-1cf55be2336b09a515a6/patch.diff
39.6 kB
| diff --git a/src/server/auth-client.test.ts b/src/server/auth-client.test.ts | |
| index 459088b4..097b90fd 100644 | |
| --- a/src/server/auth-client.test.ts | |
| +++ b/src/server/auth-client.test.ts | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const sessionCookie = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const sessionCookie = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${sessionCookie}`); | |
| const request = new NextRequest( | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| updatedSessionCookie!.value, | |
| secret | |
| ); | |
| - expect(updatedSessionCookieValue).toEqual({ | |
| + expect(updatedSessionCookieValue).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: DEFAULT.sub | |
| }, | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sid: DEFAULT.sid, | |
| createdAt: expect.any(Number) | |
| } | |
| - }); | |
| + })); | |
| // assert that the session expiry has been extended by the inactivity duration | |
| expect(updatedSessionCookie?.maxAge).toEqual(1800); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| ); | |
| expect(transactionCookie).toBeDefined(); | |
| expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual( | |
| - { | |
| + expect.objectContaining({ | |
| nonce: authorizationUrl.searchParams.get("nonce"), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state: authorizationUrl.searchParams.get("state"), | |
| returnTo: "/" | |
| - } | |
| + }) | |
| ); | |
| }); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| expect(transactionCookie).toBeDefined(); | |
| expect( | |
| (await decrypt(transactionCookie!.value, secret)).payload | |
| - ).toEqual({ | |
| + ).toEqual(expect.objectContaining({ | |
| nonce: authorizationUrl.searchParams.get("nonce"), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state: authorizationUrl.searchParams.get("state"), | |
| returnTo: "/" | |
| - }); | |
| + })); | |
| }); | |
| it("should forward the configured authorization parameters to the authorization server", async () => { | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| ); | |
| expect(transactionCookie).toBeDefined(); | |
| expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual( | |
| - { | |
| + expect.objectContaining({ | |
| nonce: authorizationUrl.searchParams.get("nonce"), | |
| maxAge: 3600, | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state: authorizationUrl.searchParams.get("state"), | |
| returnTo: "/" | |
| - } | |
| + }) | |
| ); | |
| }); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| ); | |
| expect(transactionCookie).toBeDefined(); | |
| expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual( | |
| - { | |
| + expect.objectContaining({ | |
| nonce: authorizationUrl.searchParams.get("nonce"), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state: authorizationUrl.searchParams.get("state"), | |
| returnTo: "https://example.com/dashboard" | |
| - } | |
| + }) | |
| ); | |
| }); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| ); | |
| expect(transactionCookie).toBeDefined(); | |
| expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual( | |
| - { | |
| + expect.objectContaining({ | |
| nonce: authorizationUrl.searchParams.get("nonce"), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state: authorizationUrl.searchParams.get("state"), | |
| returnTo: "/" | |
| - } | |
| + }) | |
| ); | |
| }); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| expect(transactionCookie).toBeDefined(); | |
| expect( | |
| (await decrypt(transactionCookie!.value, secret)).payload | |
| - ).toEqual({ | |
| + ).toEqual(expect.objectContaining({ | |
| nonce: expect.any(String), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state, | |
| returnTo: "/" | |
| - }); | |
| + })); | |
| }); | |
| describe("custom parameters to the authorization server", async () => { | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| expect(transactionCookie).toBeDefined(); | |
| expect( | |
| (await decrypt(transactionCookie!.value, secret)).payload | |
| - ).toEqual({ | |
| + ).toEqual(expect.objectContaining({ | |
| nonce: expect.any(String), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state, | |
| returnTo: "/" | |
| - }); | |
| + })); | |
| }); | |
| it("should forward custom parameters set in the configuration to the authorization server", async () => { | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| expect(transactionCookie).toBeDefined(); | |
| expect( | |
| (await decrypt(transactionCookie!.value, secret)).payload | |
| - ).toEqual({ | |
| + ).toEqual(expect.objectContaining({ | |
| nonce: expect.any(String), | |
| codeVerifier: expect.any(String), | |
| responseType: "code", | |
| state, | |
| returnTo: "/" | |
| - }); | |
| + })); | |
| }); | |
| }); | |
| }); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const sessionCookie = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const sessionCookie = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${sessionCookie}`); | |
| const request = new NextRequest( | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const sessionCookie = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const sessionCookie = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${sessionCookie}`); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const sessionCookie = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const sessionCookie = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${sessionCookie}`); | |
| const request = new NextRequest( | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| const sessionCookie = response.cookies.get("__session"); | |
| expect(sessionCookie).toBeDefined(); | |
| const { payload: session } = await decrypt(sessionCookie!.value, secret); | |
| - expect(session).toEqual({ | |
| + expect(session).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: DEFAULT.sub | |
| }, | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sid: expect.any(String), | |
| createdAt: expect.any(Number) | |
| } | |
| - }); | |
| + })); | |
| // validate the transaction cookie has been removed | |
| const transactionCookie = response.cookies.get(`__txn_${state}`); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| const sessionCookie = response.cookies.get("__session"); | |
| expect(sessionCookie).toBeDefined(); | |
| const { payload: session } = await decrypt(sessionCookie!.value, secret); | |
| - expect(session).toEqual({ | |
| + expect(session).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: DEFAULT.sub | |
| }, | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sid: expect.any(String), | |
| createdAt: expect.any(Number) | |
| } | |
| - }); | |
| + })); | |
| // validate the transaction cookie has been removed | |
| const transactionCookie = response.cookies.get(`__txn_${state}`); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_does-not-exist=${await encrypt(transactionState, secret)}` | |
| + `__txn_does-not-exist=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sessionCookie!.value, | |
| secret | |
| ); | |
| - expect(session).toEqual(expectedSession); | |
| + expect(session).toEqual(expect.objectContaining(expectedSession)); | |
| }); | |
| it("should be called with an error if the state parameter is missing", async () => { | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_non-existent-state=${await encrypt(transactionState, secret)}` | |
| + `__txn_non-existent-state=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sessionCookie!.value, | |
| secret | |
| ); | |
| - expect(session).toEqual({ | |
| + expect(session).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: DEFAULT.sub, | |
| name: "John Doe", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sid: expect.any(String), | |
| createdAt: expect.any(Number) | |
| } | |
| - }); | |
| + })); | |
| }); | |
| it("should not call the hook if the session is not established", async () => { | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| state: state, | |
| returnTo: "/dashboard" | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| headers.set( | |
| "cookie", | |
| - `__txn_${state}=${await encrypt(transactionState, secret)}` | |
| + `__txn_${state}=${await encrypt(transactionState, secret, expiration)}` | |
| ); | |
| const request = new NextRequest(url, { | |
| method: "GET", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sessionCookie!.value, | |
| secret | |
| ); | |
| - expect(session).toEqual({ | |
| + expect(session).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: DEFAULT.sub, | |
| name: "John Doe", | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| sid: expect.any(String), | |
| createdAt: expect.any(Number) | |
| } | |
| - }); | |
| + })); | |
| }); | |
| }); | |
| }); | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const sessionCookie = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const sessionCookie = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${sessionCookie}`); | |
| const request = new NextRequest( | |
| ca/T0LLtgmbMmxSv/MmzIg== | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const sessionCookie = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const sessionCookie = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${sessionCookie}`); | |
| const request = new NextRequest( | |
| diff --git a/src/server/cookies.test.ts b/src/server/cookies.test.ts | |
| index f943b9aa..0f895d4e 100644 | |
| --- a/src/server/cookies.test.ts | |
| +++ b/src/server/cookies.test.ts | |
| describe("encrypt/decrypt", async () => { | |
| it("should encrypt/decrypt a payload with the correct secret", async () => { | |
| const payload = { key: "value" }; | |
| - const encrypted = await encrypt(payload, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const encrypted = await encrypt(payload, secret, expiration); | |
| const decrypted = await decrypt(encrypted, secret); | |
| - expect(decrypted.payload).toEqual(payload); | |
| + expect(decrypted.payload).toEqual(expect.objectContaining(payload)); | |
| }); | |
| it("should fail to decrypt a payload with the incorrect secret", async () => { | |
| const payload = { key: "value" }; | |
| - const encrypted = await encrypt(payload, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const encrypted = await encrypt(payload, secret, expiration); | |
| await expect(() => | |
| decrypt(encrypted, incorrectSecret) | |
| ).rejects.toThrowError(); | |
| }); | |
| + it("should fail to decrypt when expired", async () => { | |
| + const payload = { key: "value" }; | |
| + const expiration = Math.floor(Date.now() / 1000 - 60); // 60 seconds in the past | |
| + const encrypted = await encrypt(payload, secret, expiration); | |
| + await expect(() => | |
| + decrypt(encrypted, secret) | |
| + ).rejects.toThrowError(`"exp" claim timestamp check failed`); | |
| + }); | |
| + | |
| it("should fail to encrypt if a secret is not provided", async () => { | |
| const payload = { key: "value" }; | |
| - await expect(() => encrypt(payload, "")).rejects.toThrowError(); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + | |
| + await expect(() => encrypt(payload, "", expiration)).rejects.toThrowError(); | |
| }); | |
| it("should fail to decrypt if a secret is not provided", async () => { | |
| const payload = { key: "value" }; | |
| - const encrypted = await encrypt(payload, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + | |
| + const encrypted = await encrypt(payload, secret, expiration); | |
| await expect(() => decrypt(encrypted, "")).rejects.toThrowError(); | |
| }); | |
| }); | |
| diff --git a/src/server/cookies.ts b/src/server/cookies.ts | |
| index be31bbe7..757bf947 100644 | |
| --- a/src/server/cookies.ts | |
| +++ b/src/server/cookies.ts | |
| const ENCRYPTION_INFO = "JWE CEK"; | |
| export async function encrypt( | |
| payload: jose.JWTPayload, | |
| secret: string, | |
| + expiration: number, | |
| additionalHeaders?: { | |
| iat: number; | |
| uat: number; | |
| export async function encrypt( | |
| const encryptedCookie = await new jose.EncryptJWT(payload) | |
| .setProtectedHeader({ enc: ENC, alg: ALG, ...additionalHeaders }) | |
| + .setExpirationTime(expiration) | |
| .encrypt(encryptionSecret); | |
| return encryptedCookie.toString(); | |
| diff --git a/src/server/session/stateful-session-store.test.ts b/src/server/session/stateful-session-store.test.ts | |
| index 6e690756..d8551656 100644 | |
| --- a/src/server/session/stateful-session-store.test.ts | |
| +++ b/src/server/session/stateful-session-store.test.ts | |
| describe("Stateful Session Store", async () => { | |
| set: vi.fn(), | |
| delete: vi.fn() | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| { | |
| id: sessionId | |
| }, | |
| - secret | |
| + secret, | |
| + expiration, | |
| ); | |
| const headers = new Headers(); | |
| describe("Stateful Session Store", async () => { | |
| set: vi.fn(), | |
| delete: vi.fn() | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| { | |
| id: sessionId | |
| }, | |
| - secret | |
| + secret, | |
| + expiration, | |
| ); | |
| const headers = new Headers(); | |
| describe("Stateful Session Store", async () => { | |
| set: vi.fn(), | |
| delete: vi.fn() | |
| }; | |
| - | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| { | |
| id: sessionId | |
| }, | |
| - secret | |
| + secret, | |
| + expiration, | |
| ); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${encryptedCookieValue}`); | |
| describe("Stateful Session Store", async () => { | |
| set: vi.fn(), | |
| delete: vi.fn() | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| { | |
| id: sessionId | |
| }, | |
| - secret | |
| + secret, | |
| + expiration, | |
| ); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${encryptedCookieValue}`); | |
| diff --git a/src/server/session/stateful-session-store.ts b/src/server/session/stateful-session-store.ts | |
| index 04206981..802cebdc 100644 | |
| --- a/src/server/session/stateful-session-store.ts | |
| +++ b/src/server/session/stateful-session-store.ts | |
| export class StatefulSessionStore extends AbstractSessionStore { | |
| if (!sessionId) { | |
| sessionId = generateId(); | |
| } | |
| - | |
| + | |
| + const maxAge = this.calculateMaxAge(session.internal.createdAt); | |
| + const expiration = Date.now() / 1000 + maxAge; | |
| const jwe = await cookies.encrypt( | |
| { | |
| id: sessionId | |
| }, | |
| - this.secret | |
| + this.secret, | |
| + expiration, | |
| ); | |
| - const maxAge = this.calculateMaxAge(session.internal.createdAt); | |
| - | |
| + | |
| resCookies.set(this.sessionCookieName, jwe.toString(), { | |
| ...this.cookieConfig, | |
| maxAge | |
| diff --git a/src/server/session/stateless-session-store.test.ts b/src/server/session/stateless-session-store.test.ts | |
| index 23f853d7..45bf7e58 100644 | |
| --- a/src/server/session/stateless-session-store.test.ts | |
| +++ b/src/server/session/stateless-session-store.test.ts | |
| describe("Stateless Session Store", async () => { | |
| createdAt: Math.floor(Date.now() / 1000) | |
| } | |
| }; | |
| - const encryptedCookieValue = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const encryptedCookieValue = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${encryptedCookieValue}`); | |
| describe("Stateless Session Store", async () => { | |
| secret | |
| }); | |
| - expect(await sessionStore.get(requestCookies)).toEqual(session); | |
| + expect(await sessionStore.get(requestCookies)).toEqual(expect.objectContaining(session)); | |
| }); | |
| it("should return null if no session cookie exists", async () => { | |
| describe("Stateless Session Store", async () => { | |
| uat: Math.floor(Date.now() / 1000), | |
| exp: Math.floor(Date.now() / 1000) | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| legacySession, | |
| secret, | |
| + expiration, | |
| legacyHeader | |
| ); | |
| describe("Stateless Session Store", async () => { | |
| uat: Math.floor(Date.now() / 1000), | |
| exp: Math.floor(Date.now() / 1000) | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| legacySession, | |
| secret, | |
| + expiration, | |
| legacyHeader | |
| ); | |
| describe("Stateless Session Store", async () => { | |
| uat: Math.floor(Date.now() / 1000), | |
| exp: Math.floor(Date.now() / 1000) | |
| }; | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| const encryptedCookieValue = await encrypt( | |
| legacySession, | |
| secret, | |
| + expiration, | |
| legacyHeader | |
| ); | |
| describe("Stateless Session Store", async () => { | |
| } | |
| ] | |
| }; | |
| - const encryptedCookieValue = await encrypt(session, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const encryptedCookieValue = await encrypt(session, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__session=${encryptedCookieValue}`); | |
| describe("Stateless Session Store", async () => { | |
| secret | |
| }); | |
| - expect(await sessionStore.get(requestCookies)).toEqual(session); | |
| + expect(await sessionStore.get(requestCookies)).toEqual(expect.objectContaining(session)); | |
| }); | |
| }); | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("__session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session)); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| expect(cookie?.sameSite).toEqual("lax"); | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("__session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| - expect(cookie?.path).toEqual("/"); | |
| - expect(cookie?.httpOnly).toEqual(true); | |
| - expect(cookie?.sameSite).toEqual("lax"); | |
| - expect(cookie?.maxAge).toEqual(0); // cookie should expire immediately | |
| - expect(cookie?.secure).toEqual(false); | |
| + | |
| + await expect( | |
| + decrypt(cookie!.value, secret) | |
| + ).rejects.toThrow(`"exp" claim timestamp check failed`); | |
| }); | |
| it("should delete the legacy cookie if it exists", async () => { | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("__session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session)); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| expect(cookie?.sameSite).toEqual("lax"); | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("__session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session)); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| expect(cookie?.sameSite).toEqual("lax"); | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("__session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session)); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| expect(cookie?.sameSite).toEqual("strict"); | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("__session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session)); | |
| expect(cookie?.path).toEqual("/custom-path"); | |
| }); | |
| describe("Stateless Session Store", async () => { | |
| const cookie = responseCookies.get("custom-session"); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual(session); | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session)); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| expect(cookie?.sameSite).toEqual("lax"); | |
| diff --git a/src/server/session/stateless-session-store.ts b/src/server/session/stateless-session-store.ts | |
| index 2e804830..29310b81 100644 | |
| --- a/src/server/session/stateless-session-store.ts | |
| +++ b/src/server/session/stateless-session-store.ts | |
| export class StatelessSessionStore extends AbstractSessionStore { | |
| session: SessionData | |
| ) { | |
| const { connectionTokenSets, ...originalSession } = session; | |
| - const jwe = await cookies.encrypt(originalSession, this.secret); | |
| const maxAge = this.calculateMaxAge(session.internal.createdAt); | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const jwe = await cookies.encrypt(originalSession, this.secret, expiration); | |
| const cookieValue = jwe.toString(); | |
| const options: CookieOptions = { | |
| ...this.cookieConfig, | |
| export class StatelessSessionStore extends AbstractSessionStore { | |
| cookieName: string, | |
| maxAge: number | |
| ) { | |
| - const jwe = await cookies.encrypt(session, this.secret); | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const jwe = await cookies.encrypt(session, this.secret, expiration); | |
| const cookieValue = jwe.toString(); | |
| diff --git a/src/server/transaction-store.test.ts b/src/server/transaction-store.test.ts | |
| index 94a3ea56..c6fd0a85 100644 | |
| --- a/src/server/transaction-store.test.ts | |
| +++ b/src/server/transaction-store.test.ts | |
| describe("Transaction Store", async () => { | |
| state, | |
| returnTo: "/dashboard" | |
| }; | |
| - const encryptedCookieValue = await encrypt(transactionState, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const encryptedCookieValue = await encrypt(transactionState, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__txn_${state}=${encryptedCookieValue}`); | |
| describe("Transaction Store", async () => { | |
| expect( | |
| (await transactionStore.get(requestCookies, state))?.payload | |
| - ).toEqual(transactionState); | |
| + ).toEqual(expect.objectContaining(transactionState)); | |
| }); | |
| it("should return null if no transaction cookie with a matching state exists", async () => { | |
| describe("Transaction Store", async () => { | |
| state, | |
| returnTo: "/dashboard" | |
| }; | |
| - const encryptedCookieValue = await encrypt(transactionState, secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + const encryptedCookieValue = await encrypt(transactionState, secret, expiration); | |
| const headers = new Headers(); | |
| headers.append("cookie", `__txn_incorrect-state=${encryptedCookieValue}`); | |
| describe("Transaction Store", async () => { | |
| expect(cookie).toBeDefined(); | |
| expect((await decrypt(cookie!.value, secret)).payload).toEqual( | |
| - transactionState | |
| + expect.objectContaining(transactionState) | |
| ); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| describe("Transaction Store", async () => { | |
| expect(cookie).toBeDefined(); | |
| expect((await decrypt(cookie!.value, secret)).payload).toEqual( | |
| - transactionState | |
| + expect.objectContaining(transactionState) | |
| ); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| describe("Transaction Store", async () => { | |
| expect(cookie).toBeDefined(); | |
| expect((await decrypt(cookie!.value, secret)).payload).toEqual( | |
| - transactionState | |
| + expect.objectContaining(transactionState) | |
| ); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| describe("Transaction Store", async () => { | |
| expect(cookie).toBeDefined(); | |
| expect((await decrypt(cookie!.value, secret)).payload).toEqual( | |
| - transactionState | |
| + expect.objectContaining(transactionState) | |
| ); | |
| expect(cookie?.path).toEqual("/custom-path"); | |
| }); | |
| describe("Transaction Store", async () => { | |
| const cookie = responseCookies.get(cookieName); | |
| expect(cookie).toBeDefined(); | |
| - expect((await decrypt(cookie!.value, secret)).payload).toEqual( | |
| + expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining( | |
| transactionState | |
| - ); | |
| + )); | |
| expect(cookie?.path).toEqual("/"); | |
| expect(cookie?.httpOnly).toEqual(true); | |
| expect(cookie?.sameSite).toEqual("lax"); | |
| diff --git a/src/server/transaction-store.ts b/src/server/transaction-store.ts | |
| index 9a20ea4e..45ff2de0 100644 | |
| --- a/src/server/transaction-store.ts | |
| +++ b/src/server/transaction-store.ts | |
| export class TransactionStore { | |
| resCookies: cookies.ResponseCookies, | |
| transactionState: TransactionState | |
| ) { | |
| - const jwe = await cookies.encrypt(transactionState, this.secret); | |
| + | |
| + const expiration = Math.floor(Date.now() / 1000 + this.cookieConfig.maxAge!); | |
| + const jwe = await cookies.encrypt(transactionState, this.secret, expiration); | |
| if (!transactionState.state) { | |
| throw new Error("Transaction state is required"); | |
| diff --git a/src/testing/generate-session-cookie.test.ts b/src/testing/generate-session-cookie.test.ts | |
| index a3a83040..8e433a0e 100644 | |
| --- a/src/testing/generate-session-cookie.test.ts | |
| +++ b/src/testing/generate-session-cookie.test.ts | |
| describe("generateSessionCookie", async () => { | |
| }; | |
| const sessionCookie = await generateSessionCookie(session, config); | |
| expect(sessionCookie).toEqual(expect.any(String)); | |
| - expect((await decrypt(sessionCookie, secret)).payload).toEqual({ | |
| + expect((await decrypt(sessionCookie, secret)).payload).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: "user_123" | |
| }, | |
| describe("generateSessionCookie", async () => { | |
| sid: "auth0-sid", | |
| createdAt: createdAt | |
| } | |
| - }); | |
| + })); | |
| }); | |
| it("should populate the internal property if it was not provided", async () => { | |
| describe("generateSessionCookie", async () => { | |
| }; | |
| const sessionCookie = await generateSessionCookie(session, config); | |
| expect(sessionCookie).toEqual(expect.any(String)); | |
| - expect((await decrypt(sessionCookie, secret)).payload).toEqual({ | |
| + expect((await decrypt(sessionCookie, secret)).payload).toEqual(expect.objectContaining({ | |
| user: { | |
| sub: "user_123" | |
| }, | |
| describe("generateSessionCookie", async () => { | |
| sid: "auth0-sid", | |
| createdAt: expect.any(Number) | |
| } | |
| - }); | |
| + })); | |
| }); | |
| it("should not populate the internal property if a null was provided", async () => { | |
| describe("generateSessionCookie", async () => { | |
| }; | |
| const sessionCookie = await generateSessionCookie(session, config); | |
| expect(sessionCookie).toEqual(expect.any(String)); | |
| - expect((await decrypt(sessionCookie, secret)).payload).toEqual({ | |
| - user: { | |
| - sub: "user_123" | |
| - }, | |
| - tokenSet: { | |
| - accessToken: "at_123", | |
| - refreshToken: "rt_123", | |
| - expiresAt: 123456 | |
| - }, | |
| - internal: null | |
| - }); | |
| + expect((await decrypt(sessionCookie, secret)).payload).not.toEqual(expect.objectContaining({ | |
| + internal: expect.anything() | |
| + })); | |
| }); | |
| - it("should not populate the internal property if a undefine was provided", async () => { | |
| + it("should not populate the internal property if a undefined was provided", async () => { | |
| const session: Partial<SessionData> = { | |
| user: { sub: "user_123" }, | |
| tokenSet: { | |
| describe("generateSessionCookie", async () => { | |
| }; | |
| const sessionCookie = await generateSessionCookie(session, config); | |
| expect(sessionCookie).toEqual(expect.any(String)); | |
| - expect((await decrypt(sessionCookie, secret)).payload).toEqual({ | |
| - user: { | |
| - sub: "user_123" | |
| - }, | |
| - tokenSet: { | |
| - accessToken: "at_123", | |
| - refreshToken: "rt_123", | |
| - expiresAt: 123456 | |
| - }, | |
| - internal: undefined | |
| - }); | |
| + expect((await decrypt(sessionCookie, secret)).payload).not.toEqual(expect.objectContaining({ | |
| + internal: expect.anything() | |
| + })); | |
| }); | |
| }); | |
| diff --git a/src/testing/generate-session-cookie.ts b/src/testing/generate-session-cookie.ts | |
| index 371a3aec..0e4ba05f 100644 | |
| --- a/src/testing/generate-session-cookie.ts | |
| +++ b/src/testing/generate-session-cookie.ts | |
| export const generateSessionCookie = async ( | |
| }; | |
| } | |
| - return encrypt(session, config.secret); | |
| + const maxAge = 60 * 60; // 1 hour in seconds | |
| + const expiration = Math.floor(Date.now() / 1000 + maxAge); | |
| + | |
| + return encrypt(session, config.secret, expiration); | |
| }; | |