55 * It is only intended for CLI use, not browser environments.
66 */
77
8+ import {
9+ readProviderJsonResponse ,
10+ readResponseTextLimited ,
11+ } from "openclaw/plugin-sdk/provider-http" ;
812import {
913 parseOAuthAuthorizationInput ,
1014 resolveOAuthTokenExpiresAt ,
1115 resolveOAuthTokenLifetimeMs ,
1216} from "openclaw/plugin-sdk/provider-oauth-runtime" ;
17+ import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime" ;
1318import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime" ;
1419import { resolveCodexAuthIdentity } from "./openai-chatgpt-auth-identity.js" ;
1520import {
@@ -27,6 +32,8 @@ import type {
2732import { generatePKCE } from "./openai-chatgpt-pkce.runtime.js" ;
2833
2934const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann" ;
35+ const OPENAI_OAUTH_RESPONSE_MAX_BYTES = 16 * 1024 * 1024 ;
36+ const OPENAI_OAUTH_DIAGNOSTIC_MAX_BYTES = 4 * 1024 ;
3037const AUTHORIZE_URL = "https://auth.openai.com/oauth/authorize" ;
3138const TOKEN_URL = "https://auth.openai.com/oauth/token" ;
3239const CALLBACK_PORT = 1455 ;
@@ -178,8 +185,24 @@ async function postTokenForm(
178185 auditContext : "openai-chatgpt-oauth-token" ,
179186 } ) ;
180187 try {
181- const responseBody = await response . arrayBuffer ( ) ;
182- return new Response ( responseBody , {
188+ // Read the original response body under a byte cap *before* release(),
189+ // so a hostile or buggy token endpoint cannot exhaust memory via the
190+ // unbounded arrayBuffer() path.
191+ const responseBody = await readResponseWithLimit ( response , OPENAI_OAUTH_RESPONSE_MAX_BYTES , {
192+ onOverflow : ( { maxBytes } ) =>
193+ new Error ( `OpenAI Codex token response exceeds ${ maxBytes } bytes` ) ,
194+ } ) ;
195+ // `readResponseWithLimit` returns a `Buffer` (Node Uint8Array view). The
196+ // global `Response` constructor accepts `BufferSource` (Uint8Array /
197+ // ArrayBuffer) as a body; cast through `BodyInit` because `Buffer.buffer`
198+ // is typed as `ArrayBufferLike`. Same wrap-shape as
199+ // extensions/google/oauth.http.ts:454.
200+ const bodyBytes = new Uint8Array (
201+ responseBody . buffer ,
202+ responseBody . byteOffset ,
203+ responseBody . byteLength ,
204+ ) ;
205+ return new Response ( bodyBytes as unknown as BodyInit , {
183206 status : response . status ,
184207 statusText : response . statusText ,
185208 headers : response . headers ,
@@ -216,15 +239,20 @@ async function exchangeAuthorizationCode(
216239 }
217240
218241 if ( ! response . ok ) {
219- const text = await response . text ( ) . catch ( ( ) => "" ) ;
242+ const text = await readResponseTextLimited ( response , OPENAI_OAUTH_DIAGNOSTIC_MAX_BYTES ) . catch (
243+ ( ) => "" ,
244+ ) ;
220245 return {
221246 type : "failed" ,
222247 status : response . status ,
223248 message : `OpenAI Codex token exchange failed (${ response . status } ): ${ text || response . statusText } ` ,
224249 } ;
225250 }
226251
227- const json = ( await response . json ( ) ) as TokenResponseJson ;
252+ const json = await readProviderJsonResponse < TokenResponseJson > (
253+ response ,
254+ "openai-codex-token-exchange" ,
255+ ) ;
228256
229257 const expires = resolveOAuthTokenExpiresAt ( json . expires_in ) ;
230258 if ( ! json . access_token || ! json . refresh_token || expires === undefined ) {
@@ -258,15 +286,20 @@ async function refreshAccessToken(
258286 ) ;
259287
260288 if ( ! response . ok ) {
261- const text = await response . text ( ) . catch ( ( ) => "" ) ;
289+ const text = await readResponseTextLimited ( response , OPENAI_OAUTH_DIAGNOSTIC_MAX_BYTES ) . catch (
290+ ( ) => "" ,
291+ ) ;
262292 return {
263293 type : "failed" ,
264294 status : response . status ,
265295 message : `OpenAI Codex token refresh failed (${ response . status } ): ${ text || response . statusText } ` ,
266296 } ;
267297 }
268298
269- const json = ( await response . json ( ) ) as TokenResponseJson ;
299+ const json = await readProviderJsonResponse < TokenResponseJson > (
300+ response ,
301+ "openai-codex-token-refresh" ,
302+ ) ;
270303
271304 const expires = resolveOAuthTokenExpiresAt ( json . expires_in ) ;
272305 if ( ! json . access_token || ! json . refresh_token || expires === undefined ) {
0 commit comments