@@ -10,6 +10,8 @@ import {
1010 resolveOAuthTokenExpiresAt ,
1111 resolveOAuthTokenLifetimeMs ,
1212} from "openclaw/plugin-sdk/provider-oauth-runtime" ;
13+ import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http" ;
14+ import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime" ;
1315import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime" ;
1416import { resolveCodexAuthIdentity } from "./openai-chatgpt-auth-identity.js" ;
1517import {
@@ -36,6 +38,7 @@ const LOOPBACK_CALLBACK_HOSTS = new Set(["localhost", "127.0.0.1", "::1"]);
3638const CALLBACK_HOST = resolveCallbackHost ( ) ;
3739const REDIRECT_URI = resolveRedirectUri ( CALLBACK_HOST ) ;
3840const MANUAL_PROMPT_FALLBACK_MS = 15_000 ;
41+ const OPENAI_OAUTH_RESPONSE_MAX_BYTES = 16 * 1024 * 1024 ;
3942const TOKEN_REQUEST_TIMEOUT_MS = 30_000 ;
4043const SCOPE = "openid profile email offline_access" ;
4144
@@ -178,8 +181,12 @@ async function postTokenForm(
178181 auditContext : "openai-chatgpt-oauth-token" ,
179182 } ) ;
180183 try {
181- const responseBody = await response . arrayBuffer ( ) ;
182- return new Response ( responseBody , {
184+ const responseBody = await readResponseWithLimit ( response , OPENAI_OAUTH_RESPONSE_MAX_BYTES , {
185+ onOverflow : ( { maxBytes } ) =>
186+ new Error ( `OpenAI OAuth token response exceeds ${ maxBytes } bytes` ) ,
187+ } ) ;
188+ const bodyBuffer = new Uint8Array ( responseBody ) ;
189+ return new Response ( bodyBuffer , {
183190 status : response . status ,
184191 statusText : response . statusText ,
185192 headers : response . headers ,
@@ -216,15 +223,23 @@ async function exchangeAuthorizationCode(
216223 }
217224
218225 if ( ! response . ok ) {
219- const text = await response . text ( ) . catch ( ( ) => "" ) ;
226+ const text = await readResponseWithLimit ( response , OPENAI_OAUTH_RESPONSE_MAX_BYTES , {
227+ onOverflow : ( { maxBytes } ) =>
228+ new Error ( `OpenAI OAuth token response exceeds ${ maxBytes } bytes` ) ,
229+ } )
230+ . then ( ( buf ) => new TextDecoder ( ) . decode ( buf ) )
231+ . catch ( ( ) => "" ) ;
220232 return {
221233 type : "failed" ,
222234 status : response . status ,
223235 message : `OpenAI Codex token exchange failed (${ response . status } ): ${ text || response . statusText } ` ,
224236 } ;
225237 }
226238
227- const json = ( await response . json ( ) ) as TokenResponseJson ;
239+ const json = await readProviderJsonResponse < TokenResponseJson > (
240+ response ,
241+ "OpenAI Codex token exchange" ,
242+ ) ;
228243
229244 const expires = resolveOAuthTokenExpiresAt ( json . expires_in ) ;
230245 if ( ! json . access_token || ! json . refresh_token || expires === undefined ) {
@@ -258,15 +273,23 @@ async function refreshAccessToken(
258273 ) ;
259274
260275 if ( ! response . ok ) {
261- const text = await response . text ( ) . catch ( ( ) => "" ) ;
276+ const text = await readResponseWithLimit ( response , OPENAI_OAUTH_RESPONSE_MAX_BYTES , {
277+ onOverflow : ( { maxBytes } ) =>
278+ new Error ( `OpenAI OAuth token response exceeds ${ maxBytes } bytes` ) ,
279+ } )
280+ . then ( ( buf ) => new TextDecoder ( ) . decode ( buf ) )
281+ . catch ( ( ) => "" ) ;
262282 return {
263283 type : "failed" ,
264284 status : response . status ,
265285 message : `OpenAI Codex token refresh failed (${ response . status } ): ${ text || response . statusText } ` ,
266286 } ;
267287 }
268288
269- const json = ( await response . json ( ) ) as TokenResponseJson ;
289+ const json = await readProviderJsonResponse < TokenResponseJson > (
290+ response ,
291+ "OpenAI Codex token refresh" ,
292+ ) ;
270293
271294 const expires = resolveOAuthTokenExpiresAt ( json . expires_in ) ;
272295 if ( ! json . access_token || ! json . refresh_token || expires === undefined ) {
0 commit comments