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
5 changes: 5 additions & 0 deletions .changeset/oauth-device-timing-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Handle invalid OAuth device authorization timing fields safely.
11 changes: 9 additions & 2 deletions packages/oauth/src/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ function tokenFromResponse(payload: Record<string, unknown>): TokenInfo {
};
}

function positiveNumber(value: unknown): number | undefined {
if (typeof value !== 'number' && typeof value !== 'string') return undefined;
if (typeof value === 'string' && value.trim().length === 0) return undefined;
const n = Number(value);
return Number.isFinite(n) && n > 0 ? n : undefined;
}

/** HTTP client default timeout for OAuth requests. */
const DEFAULT_HTTP_TIMEOUT_MS = 30_000;

Expand Down Expand Up @@ -152,8 +159,8 @@ export async function requestDeviceAuthorization(
deviceCode,
verificationUri: typeof data['verification_uri'] === 'string' ? data['verification_uri'] : '',
verificationUriComplete,
expiresIn: data['expires_in'] !== undefined ? Number(data['expires_in']) : null,
interval: Number(data['interval'] ?? 5),
expiresIn: positiveNumber(data['expires_in']) ?? null,
interval: positiveNumber(data['interval']) ?? 5,
};
}

Expand Down
16 changes: 16 additions & 0 deletions packages/oauth/test/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ describe('requestDeviceAuthorization', () => {
expect(auth.interval).toBe(5);
});

it('normalizes invalid timing fields from device authorization responses', async () => {
server.enqueue('/api/oauth/device_authorization', {
status: 200,
body: {
user_code: 'U',
device_code: 'D',
verification_uri_complete: 'https://x/y',
expires_in: true,
interval: 'soon',
},
});
const auth = await requestAuth();
expect(auth.expiresIn).toBeNull();
expect(auth.interval).toBe(5);
});

it('throws OAuthError on non-200 response', async () => {
server.enqueue('/api/oauth/device_authorization', {
status: 500,
Expand Down