@@ -6,7 +6,7 @@ import { getCredential, listAccounts, saveCredential } from "../src/oauth/store" |
6 | 6 | import type { OAuthController } from "../src/oauth/types"; |
7 | 7 |
|
8 | 8 | const TEST_DIR = join(import.meta.dir, ".tmp-nous-oauth-test"); |
9 | | -const TEST_PORTAL = "http://portal.test"; |
| 9 | +const TEST_PORTAL = "https://portal.test"; |
10 | 10 | let previousOpencodexHome: string | undefined; |
11 | 11 | let previousPortalBase: string | undefined; |
12 | 12 |
|
@@ -159,15 +159,33 @@ describe("Nous device-flow error handling", () => { |
159 | 159 | new Response(JSON.stringify({ error: "access_denied", error_description: "User denied the request" }), { status: 400 }), |
160 | 160 | ); |
161 | 161 | const ctrl: OAuthController = { onAuth() {} }; |
162 | | - await expect(loginNous(ctrl)).rejects.toThrow("Nous Portal device authorization denied"); |
| 162 | + let err: unknown; |
| 163 | + try { |
| 164 | + await loginNous(ctrl); |
| 165 | + } catch (e) { |
| 166 | + err = e; |
| 167 | + } |
| 168 | + expect(err).toBeInstanceOf(Error); |
| 169 | + expect((err as Error).message).toContain("Nous Portal device authorization denied"); |
| 170 | + expect((err as { name?: string }).name).toBe("NousTokenError"); |
| 171 | + expect((err as { oauthError?: string }).oauthError).toBe("access_denied"); |
163 | 172 | }); |
164 | 173 |
|
165 | 174 | test("expired_token surfaces as a terminal NousTokenError", async () => { |
166 | 175 | globalThis.fetch = deviceFlowFetch(() => |
167 | 176 | new Response(JSON.stringify({ error: "expired_token", error_description: "Code expired" }), { status: 400 }), |
168 | 177 | ); |
169 | 178 | const ctrl: OAuthController = { onAuth() {} }; |
170 | | - await expect(loginNous(ctrl)).rejects.toThrow("Nous Portal device authorization expired"); |
| 179 | + let err: unknown; |
| 180 | + try { |
| 181 | + await loginNous(ctrl); |
| 182 | + } catch (e) { |
| 183 | + err = e; |
| 184 | + } |
| 185 | + expect(err).toBeInstanceOf(Error); |
| 186 | + expect((err as Error).message).toContain("Nous Portal device authorization expired"); |
| 187 | + expect((err as { name?: string }).name).toBe("NousTokenError"); |
| 188 | + expect((err as { oauthError?: string }).oauthError).toBe("expired_token"); |
171 | 189 | }); |
172 | 190 |
|
173 | 191 | test("slow_down backs off and resumes polling until success", async () => { |
@@ -213,7 +231,68 @@ describe("Nous device-flow error handling", () => { |
213 | 231 | }, 15_000); |
214 | 232 | }); |
215 | 233 |
|
216 | | -describe("Nous refresh fallback", () => { |
| 234 | +describe("Nous Portal base URL hardening", () => { |
| 235 | + test("an HTTP override fails before fetch is invoked", async () => { |
| 236 | + process.env.NOUS_PORTAL_BASE_URL = "http://portal.test"; |
| 237 | + let fetchCalled = false; |
| 238 | + const realFetch = globalThis.fetch; |
| 239 | + globalThis.fetch = (async () => { |
| 240 | + fetchCalled = true; |
| 241 | + return new Response("{}", { status: 200 }); |
| 242 | + }) as typeof fetch; |
| 243 | + try { |
| 244 | + const ctrl: OAuthController = { onAuth() {} }; |
| 245 | + await expect(loginNous(ctrl)).rejects.toThrow(/must use HTTPS/); |
| 246 | + await expect(refreshNousToken("old-refresh")).rejects.toThrow(/must use HTTPS/); |
| 247 | + expect(fetchCalled).toBe(false); |
| 248 | + } finally { |
| 249 | + globalThis.fetch = realFetch; |
| 250 | + delete process.env.NOUS_PORTAL_BASE_URL; |
| 251 | + } |
| 252 | + }); |
| 253 | + |
| 254 | + test("a non-URL override fails before fetch is invoked", async () => { |
| 255 | + process.env.NOUS_PORTAL_BASE_URL = "not a url"; |
| 256 | + let fetchCalled = false; |
| 257 | + const realFetch = globalThis.fetch; |
| 258 | + globalThis.fetch = (async () => { |
| 259 | + fetchCalled = true; |
| 260 | + return new Response("{}", { status: 200 }); |
| 261 | + }) as typeof fetch; |
| 262 | + try { |
| 263 | + await expect(refreshNousToken("old-refresh")).rejects.toThrow(/not a valid URL/); |
| 264 | + expect(fetchCalled).toBe(false); |
| 265 | + } finally { |
| 266 | + globalThis.fetch = realFetch; |
| 267 | + delete process.env.NOUS_PORTAL_BASE_URL; |
| 268 | + } |
| 269 | + }); |
| 270 | + |
| 271 | + test("embedded credentials / query / fragment in the override are rejected", async () => { |
| 272 | + for (const bad of [ |
| 273 | + |
| 274 | + "https://portal.test?x=1", |
| 275 | + "https://portal.test#frag", |
| 276 | + ]) { |
| 277 | + process.env.NOUS_PORTAL_BASE_URL = bad; |
| 278 | + const realFetch = globalThis.fetch; |
| 279 | + let fetchCalled = false; |
| 280 | + globalThis.fetch = (async () => { |
| 281 | + fetchCalled = true; |
| 282 | + return new Response("{}", { status: 200 }); |
| 283 | + }) as typeof fetch; |
| 284 | + try { |
| 285 | + await expect(refreshNousToken("old-refresh")).rejects.toThrow(/base URL/); |
| 286 | + expect(fetchCalled).toBe(false); |
| 287 | + } finally { |
| 288 | + globalThis.fetch = realFetch; |
| 289 | + delete process.env.NOUS_PORTAL_BASE_URL; |
| 290 | + } |
| 291 | + } |
| 292 | + }); |
| 293 | +}); |
| 294 | + |
| 295 | +describe("Nous refresh token safety", () => { |
217 | 296 | const realFetch = globalThis.fetch; |
218 | 297 |
|
219 | 298 | beforeEach(() => { |
@@ -227,7 +306,7 @@ describe("Nous refresh fallback", () => { |
227 | 306 | else process.env.NOUS_PORTAL_BASE_URL = previousPortalBase; |
228 | 307 | }); |
229 | 308 |
|
230 | | - test("keeps the previous refresh token when the response omits a new one", async () => { |
| 309 | + test("rejecting a missing replacement refresh token does not reuse the consumed one", async () => { |
231 | 310 | const access = jwtWithClaims({ sub: "wired-user", exp: Math.floor(Date.now() / 1000) + 3600 }); |
232 | 311 | globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { |
233 | 312 | const observedHeader = (init?.headers as Record<string, string> | undefined)?.["x-nous-refresh-token"]; |
@@ -239,10 +318,41 @@ describe("Nous refresh fallback", () => { |
239 | 318 | }), { status: 200 }); |
240 | 319 | }) as typeof fetch; |
241 | 320 |
|
242 | | - const cred = await refreshNousToken("old-refresh"); |
| 321 | + await expect(refreshNousToken("old-refresh")).rejects.toMatchObject({ |
| 322 | + name: "NousTokenError", |
| 323 | + oauthError: "refresh_token_reused", |
| 324 | + }); |
| 325 | + }); |
| 326 | + |
| 327 | + test("rejecting a replacement equal to the submitted token (consumed-credential reuse)", async () => { |
| 328 | + const access = jwtWithClaims({ sub: "wired-user", exp: Math.floor(Date.now() / 1000) + 3600 }); |
| 329 | + globalThis.fetch = (async () => new Response(JSON.stringify({ |
| 330 | + access_token: access, |
| 331 | + refresh_token: "old-refresh", // identical to what was submitted |
| 332 | + expires_in: 3600, |
| 333 | + }), { status: 200 })) as typeof fetch; |
| 334 | + |
| 335 | + await expect(refreshNousToken("old-refresh")).rejects.toMatchObject({ |
| 336 | + name: "NousTokenError", |
| 337 | + oauthError: "refresh_token_reused", |
| 338 | + }); |
| 339 | + }); |
243 | 340 |
|
| 341 | + test("a rotated replacement refresh token is kept", async () => { |
| 342 | + const access = jwtWithClaims({ sub: "wired-user", exp: Math.floor(Date.now() / 1000) + 3600 }); |
| 343 | + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { |
| 344 | + const observedHeader = (init?.headers as Record<string, string> | undefined)?.["x-nous-refresh-token"]; |
| 345 | + expect(observedHeader).toBe("old-refresh"); |
| 346 | + return new Response(JSON.stringify({ |
| 347 | + access_token: access, |
| 348 | + refresh_token: "rotated-refresh", |
| 349 | + expires_in: 3600, |
| 350 | + }), { status: 200 }); |
| 351 | + }) as typeof fetch; |
| 352 | + |
| 353 | + const cred = await refreshNousToken("old-refresh"); |
244 | 354 | expect(cred.access).toBe(access); |
245 | | - expect(cred.refresh).toBe("old-refresh"); |
| 355 | + expect(cred.refresh).toBe("rotated-refresh"); |
246 | 356 | expect(cred.accountId).toBe("wired-user"); |
247 | 357 | }); |
248 | 358 | }); |
|