Edge Function returns 401 comparing Bearer JWT with SUPABASE_SERVICE_ROLE_KEY #53426
Replies: 5 comments 1 reply
|
The 401 came back with your function's own
To check whether they match without printing either value, log the length and the first 8 hex characters of a SHA-256 of each: const h = async (s: string) => Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(s)))).slice(0, 4).map(b => b.toString(16).padStart(2, '0')).join('');
console.log(token.length, await h(token), expected.length, await h(expected));If the hashes differ, it's one of the two causes above. Longer term I'd avoid using the service-role key as the caller's password at all. Every function in the project receives it, it bypasses RLS, and it's painful to rotate. A dedicated secret is simpler: I wrote up the other ways to lock down who can call an Edge Function here, if useful (my own site): https://redproof.dev/guides/secure-supabase-edge-functions |
|
Adding to @HowlinMan24's answer, which covers the most likely causes (whitespace, rotation) — your four questions, answered against the current Supabase docs: 1. The contract (Q1/Q2)
2. What reaching your handler tells you Your own So, without logging either value, check which shape arrived: const token = req.headers.get('Authorization')?.replace(/^Bearer\s+/i, '').trim() ?? '';
console.log('is_new_secret_key:', token.startsWith('sb_'));
console.log('is_legacy_jwt:', token.startsWith('eyJ'));If it starts with 3. Verifying without exposing values (Q3) @HowlinMan24's SHA-256-prefix comparison is the right tool for "are these two strings equal". The two shape checks above complement it: prefix and 4. Gateway differences (Q4) The new publishable/secret keys are not JWTs. Longer term I agree with @HowlinMan24: don't use the service-role key as a caller password. Every function in the project receives it, it bypasses RLS, and comparing raw secrets in a handler is exactly the pattern that produces this class of bug. A dedicated secret ( Source: https://supabase.com/docs/guides/getting-started/migrating-to-new-api-keys |
|
Check the response body first: if it says "Invalid JWT", "Invalid Token or Protected Header formatting" or "Missing authorization header", the platform's built-in check rejected the request before your code ran. If the body is your own "denied" message, your function ran and returned the 401 itself. The official fix is to turn off JWT verification for that function in the Functions Dashboard, or redeploy it with the no-verify-jwt flag, so your own check is the only one verifying the caller. This guide walks through the causes and the official fix steps: https://vibe-fixer.com/guides/supabase-edge-function-401-invalid-jwt?src=s-gh-2b538c If this doesn't sort it, our AI agent can fix it in your code and re-test the live app. Free check first. (I'm building VibeFix) https://vibe-fixer.com/?src=s-gh-2b538c |
|
Good point — and worth underlining for the asker: with That's also why the prefix check matters here: it diagnoses the mismatch without touching the gateway setting at all, so the asker can keep platform verification on, which is what they said they want. If the token starts with |
|
@John-Smith57 — the On the rejection source. With platform JWT verification enabled, a 401 carrying your own handler's body means the gateway accepted the presented credential and your code ran. Per what is already established in this thread, only two kinds of value get that far: a currently-valid legacy service-role JWT for this project, or one of the newer On verifying without exposing either value (your Q3). The supported pattern is the one this thread converged on: length plus a truncated SHA-256 of each side. Two refinements worth writing into the check: The constant-time compare is over the digests and protects the comparison, not the gateway — with The contract behind the check (your Q4). A bearer that equals Durable items now that the function is deleted. The digest check answers Q3 without touching the gateway setting, which keeps |
Uh oh!
There was an error while loading. Please reload this page.
We're investigating authentication behavior in a temporary Supabase Edge Function running in the managed hosted environment.
Configuration:
Authorization: Bearer <token>SUPABASE_SERVICE_ROLE_KEYObserved behavior:
The request returned HTTP 401 with the function's sanitized
deniedresponse. This suggests the function's own restricted-invocation check rejected the request, although we have not conclusively established the rejection source.Offline tests confirmed that the comparison logic accepts matching credential bytes and rejects mismatches.
The temporary function was deleted after testing.
Questions:
SUPABASE_SERVICE_ROLE_KEYguaranteed to contain the project's current legacy service-role JWT in hosted Edge Functions?We want to preserve strict authentication and avoid disabling JWT verification or exposing credentials.
Any guidance on the supported authentication contract or a secure diagnostic procedure would be appreciated.
All reactions