Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/api/auth/oidc/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
discoverOidcProvider,
exchangeAuthorizationCode,
openOidcTransaction,
safeRelativePath,
sealOidcReauthGrant,
verifyOidcIdToken,
} from "@/lib/auth/oidc";
Expand Down Expand Up @@ -56,7 +57,7 @@ function escapeHtmlAttribute(value: string) {
}

function sessionHandoff(appOrigin: string, next: string) {
const destination = new URL(next, appOrigin).toString();
const destination = new URL(safeRelativePath(next), appOrigin).toString();
const serializedDestination = serializeForInlineScript(destination);
const escapedDestination = escapeHtmlAttribute(destination);
const nonce = randomBytes(16).toString("base64");
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/oidc-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ vi.mock("@/lib/auth/oidc", () => ({
discoverOidcProvider: vi.fn(),
exchangeAuthorizationCode: vi.fn(),
openOidcTransaction: vi.fn(),
safeRelativePath: vi.fn((value: string | null | undefined) => value ?? "/"),
sealOidcReauthGrant: vi.fn(),
sealOidcTransaction: vi.fn(),
verifyOidcIdToken: vi.fn(),
Expand Down Expand Up @@ -47,6 +48,7 @@ import {
discoverOidcProvider,
exchangeAuthorizationCode,
openOidcTransaction,
safeRelativePath,
sealOidcReauthGrant,
sealOidcTransaction,
verifyOidcIdToken,
Expand Down Expand Up @@ -277,6 +279,28 @@ describe("OIDC routes", () => {
);
});

test("callback re-sanitizes a stored redirect target before the handoff", async () => {
vi.mocked(openOidcTransaction).mockResolvedValueOnce({
...transaction,
next: "https://evil.example.test/phish",
});
vi.mocked(safeRelativePath).mockReturnValueOnce("/");

const response = await callbackGet(
request(
`/api/auth/oidc/callback?state=${transaction.state}&code=one-use-code`,
),
);

expect(safeRelativePath).toHaveBeenCalledWith(
"https://evil.example.test/phish",
);
expect(response.status).toBe(200);
expect(await response.text()).toContain(
'window.location.replace("https://wealth.example.test/")',
);
});

test("disabled internal users receive a generic access denial", async () => {
vi.mocked(resolveOidcLogin).mockReturnValueOnce(null);

Expand Down
Loading