-
Notifications
You must be signed in to change notification settings - Fork 0
fix(auth): reject invalid optional credentials #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+182
−20
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { publicAccessContext } from "@/lib/public-api-access"; | ||
| import { AuthenticationError, getOptionalAuthenticatedUser, resolveOptionalAuthentication } from "@/lib/supabase/auth"; | ||
|
|
||
| function authClient(result: unknown) { | ||
| return { | ||
| auth: { | ||
| getUser: vi.fn(async () => result), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("optional authentication", () => { | ||
| it("distinguishes absent credentials without calling Supabase auth", async () => { | ||
| const client = authClient({ data: { user: null }, error: null }); | ||
| const request = new Request("http://localhost/api/search"); | ||
|
|
||
| await expect(resolveOptionalAuthentication(request, client as never)).resolves.toEqual({ status: "absent" }); | ||
| expect(client.auth.getUser).not.toHaveBeenCalled(); | ||
| await expect(getOptionalAuthenticatedUser(request, client as never)).resolves.toBeNull(); | ||
| }); | ||
|
|
||
| it("returns a valid bearer user with immutable app metadata", async () => { | ||
| const client = authClient({ | ||
| data: { user: { id: "user-1", app_metadata: { site_role: "administrator" } } }, | ||
| error: null, | ||
| }); | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { authorization: "Bearer valid-token" }, | ||
| }); | ||
|
|
||
| await expect(resolveOptionalAuthentication(request, client as never)).resolves.toEqual({ | ||
| status: "valid", | ||
| user: { id: "user-1", appMetadata: { site_role: "administrator" } }, | ||
| }); | ||
| expect(client.auth.getUser).toHaveBeenCalledWith("valid-token"); | ||
| }); | ||
|
|
||
| it("rejects a presented bearer credential that Supabase reports as invalid", async () => { | ||
| const client = authClient({ | ||
| data: { user: null }, | ||
| error: { message: "Invalid token" }, | ||
| }); | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { authorization: "Bearer expired-token" }, | ||
| }); | ||
|
|
||
| await expect(getOptionalAuthenticatedUser(request, client as never)).rejects.toBeInstanceOf(AuthenticationError); | ||
| }); | ||
|
|
||
| it("rejects a malformed authorization header without trying cookie fallback", async () => { | ||
| const client = authClient({ | ||
| data: { user: { id: "user-1", app_metadata: {} } }, | ||
| error: null, | ||
| }); | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { | ||
| authorization: "Basic credentials", | ||
| cookie: "sb-access-token=valid-cookie-token", | ||
| }, | ||
| }); | ||
|
|
||
| await expect(resolveOptionalAuthentication(request, client as never)).resolves.toEqual({ status: "invalid" }); | ||
| expect(client.auth.getUser).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not let a valid cookie override an invalid bearer credential", async () => { | ||
| const client = { | ||
| auth: { | ||
| getUser: vi.fn(async (token: string) => | ||
| token === "valid-cookie-token" | ||
| ? { data: { user: { id: "user-1", app_metadata: {} } }, error: null } | ||
| : { data: { user: null }, error: { message: "Invalid token" } }, | ||
| ), | ||
| }, | ||
| }; | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { | ||
| authorization: "Bearer expired-token", | ||
| cookie: "sb-access-token=valid-cookie-token", | ||
| }, | ||
| }); | ||
|
|
||
| await expect(resolveOptionalAuthentication(request, client as never)).resolves.toEqual({ status: "invalid" }); | ||
| expect(client.auth.getUser).toHaveBeenCalledTimes(1); | ||
| expect(client.auth.getUser).toHaveBeenCalledWith("expired-token"); | ||
| }); | ||
|
|
||
| it("validates a legacy session cookie when no authorization header is present", async () => { | ||
| const client = authClient({ data: { user: { id: "user-1", app_metadata: {} } }, error: null }); | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { cookie: "sb-access-token=valid-cookie-token" }, | ||
| }); | ||
|
|
||
| await expect(resolveOptionalAuthentication(request, client as never)).resolves.toMatchObject({ | ||
| status: "valid", | ||
| user: { id: "user-1" }, | ||
| }); | ||
| expect(client.auth.getUser).toHaveBeenCalledWith("valid-cookie-token"); | ||
| }); | ||
|
|
||
| it("rejects a legacy session cookie that Supabase reports as invalid", async () => { | ||
| const client = authClient({ data: { user: null }, error: { message: "Invalid token" } }); | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { cookie: "sb-access-token=expired-cookie-token" }, | ||
| }); | ||
|
|
||
| await expect(getOptionalAuthenticatedUser(request, client as never)).rejects.toBeInstanceOf(AuthenticationError); | ||
| expect(client.auth.getUser).toHaveBeenCalledWith("expired-cookie-token"); | ||
| }); | ||
|
|
||
| it("propagates dependency failures instead of treating them as anonymous", async () => { | ||
| const client = { | ||
| auth: { getUser: vi.fn(async () => Promise.reject(new Error("Supabase unavailable"))) }, | ||
| }; | ||
| const request = new Request("http://localhost/api/search", { | ||
| headers: { authorization: "Bearer valid-token" }, | ||
| }); | ||
|
|
||
| await expect(getOptionalAuthenticatedUser(request, client as never)).rejects.toThrow("Supabase unavailable"); | ||
| }); | ||
|
|
||
| it("never constructs anonymous access for invalid credentials", async () => { | ||
| const client = authClient({ data: { user: null }, error: { message: "Invalid token" } }); | ||
| const invalidRequest = new Request("http://localhost/api/search", { | ||
| headers: { authorization: "Bearer expired-token", "x-real-ip": "198.51.100.10" }, | ||
| }); | ||
| const anonymousRequest = new Request("http://localhost/api/search", { | ||
| headers: { "x-real-ip": "198.51.100.10" }, | ||
| }); | ||
|
|
||
| await expect(publicAccessContext(invalidRequest, client as never)).rejects.toBeInstanceOf(AuthenticationError); | ||
| await expect(publicAccessContext(anonymousRequest, client as never)).resolves.toMatchObject({ | ||
| authenticated: false, | ||
| ownerId: undefined, | ||
| rateLimitSubject: { kind: "anonymous" }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.