Skip to content

Commit 9c84042

Browse files
committed
fix(oauth/nous): fail closed on refresh-intent hardening, honor device-flow deadline, classify missing access_token as terminal
1 parent cb8ea6e commit 9c84042

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎src/oauth/nous.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ function writeRefreshIntent(refreshToken: string, status: RefreshIntentStatus):
186186
// failure so the caller can fail closed instead of refreshing blind.
187187
mkdirSync(dir, { recursive: true, mode: 0o700 });
188188
// mkdirSync only applies the mode at creation; re-apply owner-only on an
189-
// existing directory so a permissive pre-existing dir is corrected.
190-
try { chmodSync(dir, 0o700); } catch { /* best-effort */ }
189+
// existing directory so a permissive pre-existing dir is corrected. Fail
190+
// closed: if the directory cannot be hardened to owner-only, do not write
191+
// refresh-intent data into a directory a local attacker may observe.
192+
chmodSync(dir, 0o700);
191193
hardenConfigDir();
192194
const path = refreshIntentPath(refreshToken);
193195
try {
@@ -558,13 +560,15 @@ async function pollForToken(
558560
// current wait interval. The device-code grant is idempotent for the
559561
// pending case, so a retried poll is safe (unlike the refresh path).
560562
if (signal?.aborted) throw new Error("Login cancelled");
561-
await sleep(waitMs, signal);
563+
const remainingMs = deadline - Date.now();
564+
if (remainingMs <= 0) break;
565+
await sleep(Math.min(waitMs, remainingMs), signal);
562566
continue;
563567
}
564568
// Parse once and pass the payload through to the failure path (review #8),
565569
// so we never try to re-read a body that has already been consumed.
566570
const payload = (await response.json().catch(() => ({}))) as NousTokenResponse;
567-
if (response.ok && nonEmptyString(payload.access_token)) return parseTokenPayload(payload, "");
571+
if (response.ok) return parseTokenPayload(payload, "");
568572
const error = payload.error;
569573
if (error === "authorization_pending") {
570574
await sleep(waitMs, signal);

‎tests/nous-oauth.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,27 @@ describe("Nous device-flow error handling", () => {
315315
expect((err as { oauthError?: string }).oauthError).toBe("invalid_token");
316316
expect((err as { oauthError?: string }).oauthError).not.toBe("refresh_token_reused");
317317
});
318+
319+
test("a successful device-flow response missing access_token is a terminal invalid_token error", async () => {
320+
// A 200 token response that omits access_token must be routed through
321+
// parseTokenPayload so it is classified as terminal invalid_token, not
322+
// silently treated as a transient/unknown error that retries the login.
323+
globalThis.fetch = deviceFlowFetch(() =>
324+
new Response(JSON.stringify({ refresh_token: "device-refresh" }), { status: 200 }),
325+
);
326+
const ctrl: OAuthController = { onAuth() {} };
327+
let err: unknown;
328+
try {
329+
await loginNous(ctrl);
330+
} catch (e) {
331+
err = e;
332+
}
333+
expect(err).toBeInstanceOf(Error);
334+
expect((err as Error).message).toContain("did not include an access token");
335+
expect((err as { name?: string }).name).toBe("NousTokenError");
336+
expect((err as { oauthError?: string }).oauthError).toBe("invalid_token");
337+
expect((err as { terminal?: boolean }).terminal).toBe(true);
338+
});
318339
});
319340

320341
describe("Nous Portal base URL hardening", () => {

0 commit comments

Comments
 (0)