Skip to content
Open
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
4 changes: 3 additions & 1 deletion studio/src/features/auth/providers/boundedFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export const platformFetch: Fetcher = (input, init) => fetch(input, init);
export async function fetchJson(fetcher: Fetcher, url: string, init: RequestInit): Promise<Result<unknown, FetchFailure>> {
let response: Response;
try {
response = await fetcher(url, { ...init, redirect: 'error', signal: AbortSignal.timeout(FETCH_LIMITS.timeoutMs) });
// `manual`, not `error`: the Workers runtime throws on `redirect: 'error'` ("won't be implemented"), which made
// every discovery, key and token fetch a network failure. A 3xx answer is not ok, so it is still refused below.
response = await fetcher(url, { ...init, redirect: 'manual', signal: AbortSignal.timeout(FETCH_LIMITS.timeoutMs) });
} catch {
return err({ kind: 'network' });
}
Expand Down
27 changes: 27 additions & 0 deletions studio/test/boundedFetchRedirect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Goal: identity-service fetches work on the Workers runtime, which rejects `redirect: 'error'`, and still refuse
// redirects. Method: call fetchJson with a fetcher that records its init and answers a JSON body or a 302.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { fetchJson } from '../src/features/auth/providers/boundedFetch';

test('identity fetches ask for redirect: manual, which the Workers runtime accepts', async () => {
let seen: RequestInit | undefined;
const answer = await fetchJson(
async (_url, init) => {
seen = init;
return Response.json({ issuer: 'https://idp.example' });
},
'https://idp.example/.well-known/openid-configuration',
{},
);
assert.equal(seen?.redirect, 'manual');
assert.deepEqual(answer, { ok: true, value: { issuer: 'https://idp.example' } });
});
test('a redirect answer is refused as a status failure', async () => {
const answer = await fetchJson(
async () => new Response(null, { status: 302, headers: { Location: 'https://elsewhere.example/' } }),
'https://idp.example/jwks',
{},
);
assert.deepEqual(answer, { ok: false, error: { kind: 'status', status: 302 } });
});