@@ -29,6 +29,22 @@ function mockTokenResponseText(body: string, status = 200): void {
2929 } ) ;
3030}
3131
32+ function mockTokenErrorBodyWithBodyTraps ( body : string , status = 500 ) {
33+ const response = new Response ( body , {
34+ status,
35+ headers : { "Content-Type" : "text/plain" } ,
36+ } ) ;
37+ const text = vi
38+ . spyOn ( response , "text" )
39+ . mockRejectedValue ( new Error ( "unexpected response.text() call" ) ) ;
40+ const arrayBuffer = vi
41+ . spyOn ( response , "arrayBuffer" )
42+ . mockRejectedValue ( new Error ( "unexpected response.arrayBuffer() call" ) ) ;
43+ const release = vi . fn ( async ( ) => undefined ) ;
44+ ssrfMocks . fetchWithSsrFGuard . mockResolvedValueOnce ( { response, release } ) ;
45+ return { arrayBuffer, release, text } ;
46+ }
47+
3248afterEach ( ( ) => {
3349 ssrfMocks . fetchWithSsrFGuard . mockReset ( ) ;
3450} ) ;
@@ -143,6 +159,27 @@ describe("OpenAI Codex OAuth flow", () => {
143159 } ) ;
144160 } ) ;
145161
162+ it ( "bounds token exchange error bodies without materializing the full response" , async ( ) => {
163+ const body = "x" . repeat ( 32 * 1024 ) ;
164+ const { arrayBuffer, release, text } = mockTokenErrorBodyWithBodyTraps ( body , 500 ) ;
165+
166+ const result = await testing . exchangeAuthorizationCode (
167+ "code" ,
168+ "verifier" ,
169+ testing . resolveRedirectUri ( "localhost" ) ,
170+ { timeoutMs : 5 } ,
171+ ) ;
172+
173+ expect ( result ) . toEqual ( {
174+ type : "failed" ,
175+ status : 500 ,
176+ message : `OpenAI Codex token exchange failed (500): ${ "x" . repeat ( 8 * 1024 ) } ` ,
177+ } ) ;
178+ expect ( arrayBuffer ) . not . toHaveBeenCalled ( ) ;
179+ expect ( text ) . not . toHaveBeenCalled ( ) ;
180+ expect ( release ) . toHaveBeenCalledTimes ( 1 ) ;
181+ } ) ;
182+
146183 it ( "times out token refresh requests" , async ( ) => {
147184 ssrfMocks . fetchWithSsrFGuard . mockRejectedValueOnce ( timeoutError ( ) ) ;
148185
@@ -174,4 +211,20 @@ describe("OpenAI Codex OAuth flow", () => {
174211 message : "OpenAI Codex token refresh response missing fields: expires_in" ,
175212 } ) ;
176213 } ) ;
214+
215+ it ( "bounds token refresh error bodies without materializing the full response" , async ( ) => {
216+ const body = "y" . repeat ( 32 * 1024 ) ;
217+ const { arrayBuffer, release, text } = mockTokenErrorBodyWithBodyTraps ( body , 500 ) ;
218+
219+ const result = await testing . refreshAccessToken ( "old-refresh-token" , { timeoutMs : 5 } ) ;
220+
221+ expect ( result ) . toEqual ( {
222+ type : "failed" ,
223+ status : 500 ,
224+ message : `OpenAI Codex token refresh failed (500): ${ "y" . repeat ( 8 * 1024 ) } ` ,
225+ } ) ;
226+ expect ( arrayBuffer ) . not . toHaveBeenCalled ( ) ;
227+ expect ( text ) . not . toHaveBeenCalled ( ) ;
228+ expect ( release ) . toHaveBeenCalledTimes ( 1 ) ;
229+ } ) ;
177230} ) ;
0 commit comments