Refresh classification: revoke only on a 400 invalid_grant; any other answer keeps the tokens

A grant verdict is read from a 400 body by exact code, never from a 401, a
5xx or a 2xx. A 2xx that is not an object with a non-empty string
access_token and no error is refused as provider_body_not_token_set, so a
malformed success keeps the tokens and surfaces as the typed 502 for every
provider, rotating or not. A refresh answer without expires_in stores the
expiry as unknown rather than keeping the already-due one; an unknown expiry
is never due, so the next resolve does not refresh again.

Pins Soul's three surviving mutants: substring code match, the dropped
expiry rule, and the snapshot refresh token on a non-rotating provider.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-19 12:34:17 +02:00
commit d989860163
5 changed files with 216 additions and 12 deletions

View file

@ -113,7 +113,10 @@ export async function refreshManagedCredential(
refreshTokenEncrypted = locked.refreshTokenEncrypted!;
}
const tokenExpiresAt = refreshed.expiresAt ?? locked.tokenExpiresAt;
// An answer without expires_in leaves the expiry unknown. The old expiry is
// already due, so keeping it would refresh again on every resolve; an
// unknown expiry is never due (dueForRefresh).
const tokenExpiresAt = refreshed.expiresAt;
return {
kind: "rotate",
accessTokenEncrypted: ctx.tokenCustody.encrypt(refreshed.accessToken),

View file

@ -89,7 +89,7 @@ function readOAuthErrorCode(text: string): OAuthErrorCode | undefined {
*/
export class ProviderResponseError extends Error {
readonly status: number;
readonly code: "provider_status" | "provider_body_not_json";
readonly code: "provider_status" | "provider_body_not_json" | "provider_body_not_token_set";
/**
* The RFC 6749 section 5.2 error code of a refusal, when the provider sent
* one from that closed set. The only value taken from a provider's body.
@ -109,7 +109,14 @@ export class ProviderResponseError extends Error {
async function readProviderJson<T>(response: Response, failure: string): Promise<T> {
const text = await response.text();
if (!response.ok) {
throw new ProviderResponseError(response.status, "provider_status", failure, readOAuthErrorCode(text));
// RFC 6749 section 5.2 sends a grant verdict as 400 only. The same JSON
// behind a 401, a 5xx or a gateway page is not one.
throw new ProviderResponseError(
response.status,
"provider_status",
failure,
response.status === 400 ? readOAuthErrorCode(text) : undefined
);
}
if (!text) {
return {} as T;
@ -173,13 +180,28 @@ export async function requestProviderTokens(
init.signal = signal;
}
const label = grant.grant_type === "refresh_token" ? "refresh" : "exchange";
const tokenResponse = await fetchJson<{
const failure = `${descriptor.displayName} token ${label} failed`;
const response = await fetch(descriptor.tokenEndpoint, init);
const answer = await readProviderJson<unknown>(response, failure);
// A 2xx is a token set only when it is an object holding a string access
// token and no error.
if (
typeof answer !== "object" ||
answer === null ||
Array.isArray(answer) ||
typeof (answer as { access_token?: unknown }).access_token !== "string" ||
!(answer as { access_token: string }).access_token ||
(answer as { error?: unknown }).error !== undefined
) {
throw new ProviderResponseError(response.status, "provider_body_not_token_set", failure);
}
const tokenResponse = answer as {
access_token: string;
refresh_token?: string;
token_type?: string;
expires_in?: number;
scope?: string | string[];
}>(descriptor.tokenEndpoint, init, `${descriptor.displayName} token ${label} failed`);
};
const tokenSet: OAuthTokenSet = {
accessToken: tokenResponse.access_token,