@@ -49,6 +49,93 @@ function hasOwnField(container: Record<string, unknown>, field: string): boolean
4949 return Object . prototype . hasOwnProperty . call ( container , field ) ;
5050}
5151
52+ type JsonScanResult = {
53+ next : number ;
54+ duplicate : boolean ;
55+ } ;
56+
57+ function skipJsonWhitespace ( text : string , index : number ) : number {
58+ while ( index < text . length && / [ \t \n \r ] / . test ( text [ index ] ?? "" ) ) index += 1 ;
59+ return index ;
60+ }
61+
62+ function scanJsonStringEnd ( text : string , index : number ) : number {
63+ if ( text [ index ] !== '"' ) throw new SyntaxError ( "expected JSON string" ) ;
64+ for ( let cursor = index + 1 ; cursor < text . length ; cursor += 1 ) {
65+ const char = text [ cursor ] ;
66+ if ( char === '"' ) return cursor + 1 ;
67+ if ( char === "\\" ) cursor += 1 ;
68+ }
69+ throw new SyntaxError ( "unterminated JSON string" ) ;
70+ }
71+
72+ function scanJsonValue ( text : string , index : number ) : JsonScanResult {
73+ const start = skipJsonWhitespace ( text , index ) ;
74+ if ( text [ start ] === "{" ) return scanJsonObject ( text , start ) ;
75+ if ( text [ start ] === "[" ) return scanJsonArray ( text , start ) ;
76+ if ( text [ start ] === '"' ) return { next : scanJsonStringEnd ( text , start ) , duplicate : false } ;
77+
78+ for ( const literal of [ "true" , "false" , "null" ] ) {
79+ if ( text . startsWith ( literal , start ) ) {
80+ return { next : start + literal . length , duplicate : false } ;
81+ }
82+ }
83+ const number = text . slice ( start ) . match ( / ^ - ? (?: 0 | [ 1 - 9 ] \d * ) (?: \. \d + ) ? (?: [ e E ] [ + - ] ? \d + ) ? / ) ;
84+ if ( ! number ) throw new SyntaxError ( "expected JSON value" ) ;
85+ return { next : start + number [ 0 ] . length , duplicate : false } ;
86+ }
87+
88+ function scanJsonObject ( text : string , index : number ) : JsonScanResult {
89+ const keys = new Set < string > ( ) ;
90+ let duplicate = false ;
91+ let cursor = skipJsonWhitespace ( text , index + 1 ) ;
92+ if ( text [ cursor ] === "}" ) return { next : cursor + 1 , duplicate : false } ;
93+
94+ while ( cursor < text . length ) {
95+ const keyEnd = scanJsonStringEnd ( text , cursor ) ;
96+ const key = JSON . parse ( text . slice ( cursor , keyEnd ) ) as unknown ;
97+ if ( typeof key !== "string" ) throw new SyntaxError ( "invalid JSON object key" ) ;
98+ if ( keys . has ( key ) ) duplicate = true ;
99+ keys . add ( key ) ;
100+
101+ cursor = skipJsonWhitespace ( text , keyEnd ) ;
102+ if ( text [ cursor ] !== ":" ) throw new SyntaxError ( "expected JSON object colon" ) ;
103+ const value = scanJsonValue ( text , cursor + 1 ) ;
104+ duplicate ||= value . duplicate ;
105+ cursor = skipJsonWhitespace ( text , value . next ) ;
106+ if ( text [ cursor ] === "}" ) return { next : cursor + 1 , duplicate } ;
107+ if ( text [ cursor ] !== "," ) throw new SyntaxError ( "expected JSON object separator" ) ;
108+ cursor = skipJsonWhitespace ( text , cursor + 1 ) ;
109+ }
110+ throw new SyntaxError ( "unterminated JSON object" ) ;
111+ }
112+
113+ function scanJsonArray ( text : string , index : number ) : JsonScanResult {
114+ let duplicate = false ;
115+ let cursor = skipJsonWhitespace ( text , index + 1 ) ;
116+ if ( text [ cursor ] === "]" ) return { next : cursor + 1 , duplicate : false } ;
117+
118+ while ( cursor < text . length ) {
119+ const value = scanJsonValue ( text , cursor ) ;
120+ duplicate ||= value . duplicate ;
121+ cursor = skipJsonWhitespace ( text , value . next ) ;
122+ if ( text [ cursor ] === "]" ) return { next : cursor + 1 , duplicate } ;
123+ if ( text [ cursor ] !== "," ) throw new SyntaxError ( "expected JSON array separator" ) ;
124+ cursor = skipJsonWhitespace ( text , cursor + 1 ) ;
125+ }
126+ throw new SyntaxError ( "unterminated JSON array" ) ;
127+ }
128+
129+ function hasDuplicateJsonObjectKeys ( text : string ) : boolean {
130+ try {
131+ const result = scanJsonValue ( text , 0 ) ;
132+ return result . duplicate || skipJsonWhitespace ( text , result . next ) !== text . length ;
133+ } catch {
134+ // Scanner disagreement is untrusted input, just like JSON.parse failure.
135+ return true ;
136+ }
137+ }
138+
52139function exactResetEligibleCode (
53140 container : Record < string , unknown > ,
54141) : CodexResetEligibleExhaustionCode | undefined {
@@ -88,9 +175,13 @@ async function resetEligibleCodeFromResponse(
88175 signal ?: AbortSignal ,
89176) : Promise < CodexResetEligibleExhaustionCode | undefined > {
90177 try {
91- const body = await readBoundedResponseBody ( response . clone ( ) , { signal } ) ;
178+ const body = await readBoundedResponseBody ( response . clone ( ) , { signal, fatalUtf8 : true } ) ;
92179 if ( ! body . displaySafe || body . truncated || ! body . text . trim ( ) ) return undefined ;
93- return structuredResetEligibleCode ( JSON . parse ( body . text ) as unknown ) ;
180+ const payload = JSON . parse ( body . text ) as unknown ;
181+ // JSON.parse silently keeps the last duplicate key, making contradictory
182+ // payloads order-dependent. Reject any duplicate at any object depth.
183+ if ( hasDuplicateJsonObjectKeys ( body . text ) ) return undefined ;
184+ return structuredResetEligibleCode ( payload ) ;
94185 } catch {
95186 // Classification must fail closed. A malformed, oversized, consumed, or
96187 // cancelled body cannot authorize an irreversible reset-credit operation.
0 commit comments