|
2 | 2 | import { describe, expect, it, vi } from "vitest"; |
3 | 3 | import { loginChutes } from "./oauth.js"; |
4 | 4 |
|
5 | | -function boundedErrorResponse(body: string, status = 500): { |
| 5 | +function boundedErrorResponse( |
| 6 | + body: string, |
| 7 | + status = 500, |
| 8 | +): { |
6 | 9 | response: Response; |
7 | 10 | cancel: ReturnType<typeof vi.fn>; |
8 | 11 | releaseLock: ReturnType<typeof vi.fn>; |
@@ -112,4 +115,64 @@ describe("chutes plugin OAuth", () => { |
112 | 115 | expect(errorResponse.cancel).toHaveBeenCalledTimes(1); |
113 | 116 | expect(errorResponse.releaseLock).toHaveBeenCalledTimes(1); |
114 | 117 | }); |
| 118 | + |
| 119 | + it("cancels oversized token exchange JSON body via the 16 MiB provider cap", async () => { |
| 120 | + const ONE_MIB = 1024 * 1024; |
| 121 | + const TOTAL_CHUNKS = 32; |
| 122 | + const chunk = new Uint8Array(ONE_MIB); |
| 123 | + |
| 124 | + let bytesPulled = 0; |
| 125 | + let canceled = false; |
| 126 | + const oversizedTokenJson = new Response( |
| 127 | + new ReadableStream<Uint8Array>({ |
| 128 | + pull(controller) { |
| 129 | + if (bytesPulled >= TOTAL_CHUNKS * ONE_MIB) { |
| 130 | + controller.close(); |
| 131 | + return; |
| 132 | + } |
| 133 | + bytesPulled += chunk.length; |
| 134 | + controller.enqueue(chunk); |
| 135 | + }, |
| 136 | + cancel() { |
| 137 | + canceled = true; |
| 138 | + }, |
| 139 | + }), |
| 140 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 141 | + ); |
| 142 | + |
| 143 | + const fetchFn = vi.fn(async (input: RequestInfo | URL) => { |
| 144 | + const url = |
| 145 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 146 | + if (url === "https://api.chutes.ai/idp/userinfo") { |
| 147 | + return new Response(JSON.stringify({ login: "test", name: "Test" }), { |
| 148 | + status: 200, |
| 149 | + headers: { "Content-Type": "application/json" }, |
| 150 | + }); |
| 151 | + } |
| 152 | + if (url === "https://api.chutes.ai/idp/token") { |
| 153 | + return oversizedTokenJson; |
| 154 | + } |
| 155 | + return new Response("not found", { status: 404 }); |
| 156 | + }); |
| 157 | + |
| 158 | + await expect( |
| 159 | + loginChutes({ |
| 160 | + app: { |
| 161 | + clientId: "cid_test", |
| 162 | + redirectUri: "http://127.0.0.1:1456/oauth-callback", |
| 163 | + scopes: ["openid"], |
| 164 | + }, |
| 165 | + manual: true, |
| 166 | + createState: () => "state_test", |
| 167 | + onAuth: vi.fn(async () => {}), |
| 168 | + onPrompt: vi.fn( |
| 169 | + async () => "http://127.0.0.1:1456/oauth-callback?code=code_test&state=state_test", |
| 170 | + ), |
| 171 | + fetchFn, |
| 172 | + }), |
| 173 | + ).rejects.toThrow(/Chutes token exchange: JSON response exceeds 16777216 bytes/); |
| 174 | + |
| 175 | + expect(canceled).toBe(true); |
| 176 | + expect(bytesPulled).toBeLessThan(TOTAL_CHUNKS * ONE_MIB); |
| 177 | + }); |
115 | 178 | }); |
0 commit comments