@@ -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
@@ -216,15 +219,23 @@ async function exchangeAuthorizationCode(
216219 }
217220
218221 if ( ! response . ok ) {
219- const text = await response . text ( ) . catch ( ( ) => "" ) ;
222+ const text = await readResponseWithLimit ( response , OPENAI_OAUTH_RESPONSE_MAX_BYTES , {
223+ onOverflow : ( { maxBytes } ) =>
224+ new Error ( `OpenAI OAuth token response exceeds ${ maxBytes } bytes` ) ,
225+ } )
226+ . then ( ( buf ) => new TextDecoder ( ) . decode ( buf ) )
227+ . catch ( ( ) => "" ) ;
220228 return {
221229 type : "failed" ,
222230 status : response . status ,
223231 message : `OpenAI Codex token exchange failed (${ response . status } ): ${ text || response . statusText } ` ,
224232 } ;
225233 }
226234
227- const json = ( await response . json ( ) ) as TokenResponseJson ;
235+ const json = await readProviderJsonResponse < TokenResponseJson > (
236+ response ,
237+ "OpenAI Codex token exchange" ,
238+ ) ;
228239
229240 const expires = resolveOAuthTokenExpiresAt ( json . expires_in ) ;
230241 if ( ! json . access_token || ! json . refresh_token || expires === undefined ) {
@@ -258,15 +269,23 @@ async function refreshAccessToken(
258269 ) ;
259270
260271 if ( ! response . ok ) {
261- const text = await response . text ( ) . catch ( ( ) => "" ) ;
272+ const text = await readResponseWithLimit ( response , OPENAI_OAUTH_RESPONSE_MAX_BYTES , {
273+ onOverflow : ( { maxBytes } ) =>
274+ new Error ( `OpenAI OAuth token response exceeds ${ maxBytes } bytes` ) ,
275+ } )
276+ . then ( ( buf ) => new TextDecoder ( ) . decode ( buf ) )
277+ . catch ( ( ) => "" ) ;
262278 return {
263279 type : "failed" ,
264280 status : response . status ,
265281 message : `OpenAI Codex token refresh failed (${ response . status } ): ${ text || response . statusText } ` ,
266282 } ;
267283 }
268284
269- const json = ( await response . json ( ) ) as TokenResponseJson ;
285+ const json = await readProviderJsonResponse < TokenResponseJson > (
286+ response ,
287+ "OpenAI Codex token refresh" ,
288+ ) ;
270289
271290 const expires = resolveOAuthTokenExpiresAt ( json . expires_in ) ;
272291 if ( ! json . access_token || ! json . refresh_token || expires === undefined ) {
0 commit comments