-
Notifications
You must be signed in to change notification settings - Fork 0
feat: loginview should honor nextauth callbackurl from the query string (middleware-friendly) (#955) #959
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
Merged
feat: loginview should honor nextauth callbackurl from the query string (middleware-friendly) (#955) #959
Changes from all commits
Commits
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
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,47 @@ | ||
| import { getQueryCallbackUrl } from "../src/hooks/authentication/session/useSessionActive"; | ||
|
|
||
| describe("getQueryCallbackUrl", () => { | ||
| it("returns the string when query is a non-empty same-origin path", () => { | ||
| expect(getQueryCallbackUrl("/atlases")).toBe("/atlases"); | ||
| }); | ||
|
|
||
| it("preserves query string and fragment in the returned path", () => { | ||
| expect(getQueryCallbackUrl("/atlases?tab=metadata#row-1")).toBe( | ||
| "/atlases?tab=metadata#row-1", | ||
| ); | ||
| }); | ||
|
|
||
| it("returns the first element when query is a string array", () => { | ||
| expect(getQueryCallbackUrl(["/atlases", "/other"])).toBe("/atlases"); | ||
| }); | ||
|
|
||
| it("returns undefined when query is undefined", () => { | ||
| expect(getQueryCallbackUrl(undefined)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined when query is an empty string", () => { | ||
| expect(getQueryCallbackUrl("")).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined when query is an empty array", () => { | ||
| expect(getQueryCallbackUrl([])).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined when the first array element is empty", () => { | ||
| expect(getQueryCallbackUrl(["", "/other"])).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("rejects absolute http URLs (open-redirect guard)", () => { | ||
| expect(getQueryCallbackUrl("https://evil.example/phish")).toBeUndefined(); | ||
| expect(getQueryCallbackUrl("http://evil.example/phish")).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("rejects protocol-relative URLs (browsers treat as absolute)", () => { | ||
| expect(getQueryCallbackUrl("//evil.example/phish")).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("rejects values that do not start with '/'", () => { | ||
| expect(getQueryCallbackUrl("atlases")).toBeUndefined(); | ||
| expect(getQueryCallbackUrl("javascript:alert(1)")).toBeUndefined(); | ||
| }); | ||
| }); |
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,89 @@ | ||
| import { jest } from "@jest/globals"; | ||
| import { act, renderHook } from "@testing-library/react"; | ||
| import { TransformRouteFn } from "../src/hooks/useRouteHistory"; | ||
|
|
||
| const ROOT_PATH = "/"; | ||
| const ROUTES = ["/login", "/route1", "/route2"]; | ||
| const PROVIDER_ID = "google"; | ||
|
|
||
| let mockRouterQuery: Record<string, string | string[] | undefined> = {}; | ||
|
|
||
| jest.unstable_mockModule("next/router", () => ({ | ||
| ...jest.requireActual<typeof import("next/router")>("next/router"), | ||
| useRouter: jest.fn(() => ({ query: mockRouterQuery })), | ||
| })); | ||
| jest.unstable_mockModule("../src/hooks/useRouteHistory", () => ({ | ||
| useRouteHistory: jest.fn(), | ||
| })); | ||
| jest.unstable_mockModule("../src/nextauth/service", () => ({ | ||
| service: { login: jest.fn(), logout: jest.fn() }, | ||
| })); | ||
|
|
||
| const { useRouteHistory } = await import("../src/hooks/useRouteHistory"); | ||
| const { service } = await import("../src/nextauth/service"); | ||
| const { useNextAuthService } = | ||
| await import("../src/nextauth/hooks/useNextAuthService"); | ||
|
|
||
| const MOCK_USE_ROUTE_HISTORY = useRouteHistory as jest.MockedFunction< | ||
| typeof useRouteHistory | ||
| >; | ||
| const MOCK_LOGIN = service.login as jest.MockedFunction<typeof service.login>; | ||
|
|
||
| describe("useNextAuthService", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockRouterQuery = {}; | ||
| MOCK_LOGIN.mockReset(); | ||
| MOCK_USE_ROUTE_HISTORY.mockReset(); | ||
| MOCK_USE_ROUTE_HISTORY.mockReturnValue({ | ||
| callbackUrl: jest.fn( | ||
| (transformFn?: TransformRouteFn | undefined) => | ||
| transformFn?.(ROUTES) ?? ROOT_PATH, | ||
| ), | ||
| }); | ||
| }); | ||
|
|
||
| test("requestLogin uses route-history fallback when query.callbackUrl is absent", () => { | ||
| const { result } = renderHook(() => useNextAuthService()); | ||
| act(() => result.current.requestLogin(PROVIDER_ID)); | ||
| expect(MOCK_LOGIN).toHaveBeenCalledWith(PROVIDER_ID, { | ||
| callbackUrl: ROUTES[1], | ||
| }); | ||
| }); | ||
|
|
||
| test("requestLogin prefers a same-origin query.callbackUrl over route history", () => { | ||
| mockRouterQuery = { callbackUrl: "/from-query" }; | ||
| const { result } = renderHook(() => useNextAuthService()); | ||
| act(() => result.current.requestLogin(PROVIDER_ID)); | ||
| expect(MOCK_LOGIN).toHaveBeenCalledWith(PROVIDER_ID, { | ||
| callbackUrl: "/from-query", | ||
| }); | ||
| }); | ||
|
|
||
| test("requestLogin falls back to route history when query.callbackUrl is empty", () => { | ||
| mockRouterQuery = { callbackUrl: "" }; | ||
| const { result } = renderHook(() => useNextAuthService()); | ||
| act(() => result.current.requestLogin(PROVIDER_ID)); | ||
| expect(MOCK_LOGIN).toHaveBeenCalledWith(PROVIDER_ID, { | ||
| callbackUrl: ROUTES[1], | ||
| }); | ||
| }); | ||
|
|
||
| test("requestLogin rejects an absolute URL in query.callbackUrl (open-redirect guard)", () => { | ||
| mockRouterQuery = { callbackUrl: "https://evil.example/phish" }; | ||
| const { result } = renderHook(() => useNextAuthService()); | ||
| act(() => result.current.requestLogin(PROVIDER_ID)); | ||
| expect(MOCK_LOGIN).toHaveBeenCalledWith(PROVIDER_ID, { | ||
| callbackUrl: ROUTES[1], | ||
| }); | ||
| }); | ||
|
|
||
| test("requestLogin rejects a protocol-relative URL in query.callbackUrl", () => { | ||
| mockRouterQuery = { callbackUrl: "//evil.example/phish" }; | ||
| const { result } = renderHook(() => useNextAuthService()); | ||
| act(() => result.current.requestLogin(PROVIDER_ID)); | ||
| expect(MOCK_LOGIN).toHaveBeenCalledWith(PROVIDER_ID, { | ||
| callbackUrl: ROUTES[1], | ||
| }); | ||
| }); | ||
| }); | ||
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.