66 * / `--human` override.
77 */
88
9- import { promises as fs } from "node:fs" ;
9+ import { constants as fsConstants , promises as fs } from "node:fs" ;
1010import { resolve as resolvePath } from "node:path" ;
1111import type { Command } from "commander" ;
1212import {
13+ MAX_JSONC_INPUT_BYTES ,
1314 OcEmitSentinelError ,
1415 OcPathError ,
1516 REDACTED_SENTINEL ,
@@ -50,6 +51,15 @@ interface PathCommandOptions {
5051
5152type OutputMode = "human" | "json" ;
5253
54+ // Keep every parser behind the shipped JSONC ceiling so user-selected files
55+ // cannot allocate an unbounded input before format-specific validation runs.
56+ const MAX_OC_PATH_INPUT_BYTES = MAX_JSONC_INPUT_BYTES ;
57+
58+ type LoadedOcPathFile = {
59+ readonly ast : OcAst ;
60+ readonly raw : string ;
61+ } ;
62+
5363const SCRUB_PLACEHOLDER = "[REDACTED]" ;
5464
5565const defaultRuntime : OutputRuntimeEnv = {
@@ -160,14 +170,47 @@ function catchSentinel<T>(
160170 }
161171}
162172
163- async function loadAst (
173+ async function loadOcPathFile (
164174 absPath : string ,
165175 fileName : string ,
166176 runtime : OutputRuntimeEnv ,
167177 mode : OutputMode ,
168- ) : Promise < OcAst | null > {
169- const raw = await fs . readFile ( absPath , "utf-8" ) ;
178+ ) : Promise < LoadedOcPathFile | null > {
170179 const kind = inferKind ( fileName ) ;
180+ // A blocking open can hang on a FIFO before stat can reject it. O_NONBLOCK
181+ // preserves regular-file and symlink reads while making that check reachable.
182+ const handle = await fs . open ( absPath , fsConstants . O_RDONLY | fsConstants . O_NONBLOCK ) ;
183+ let raw : string ;
184+ try {
185+ const stat = await handle . stat ( ) ;
186+ if ( ! stat . isFile ( ) ) {
187+ emitError ( runtime , mode , `not a regular file: ${ absPath } ` , "OC_PATH_FILE_NOT_REGULAR" ) ;
188+ runtime . exit ( 2 ) ;
189+ return null ;
190+ }
191+ // `end` is inclusive, so a raced growth can return at most the cap plus one byte.
192+ const bytes =
193+ stat . size > MAX_OC_PATH_INPUT_BYTES
194+ ? null
195+ : Buffer . concat (
196+ await handle
197+ . createReadStream ( { autoClose : false , end : MAX_OC_PATH_INPUT_BYTES , start : 0 } )
198+ . toArray ( ) ,
199+ ) ;
200+ if ( bytes === null || bytes . length > MAX_OC_PATH_INPUT_BYTES ) {
201+ emitError (
202+ runtime ,
203+ mode ,
204+ `input exceeds ${ MAX_OC_PATH_INPUT_BYTES } bytes${ stat . size > MAX_OC_PATH_INPUT_BYTES ? `; got ${ stat . size } ` : "" } ` ,
205+ kind === "jsonc" ? "OC_JSONC_INPUT_TOO_LARGE" : "OC_PATH_INPUT_TOO_LARGE" ,
206+ ) ;
207+ runtime . exit ( 2 ) ;
208+ return null ;
209+ }
210+ raw = bytes . toString ( "utf8" ) ;
211+ } finally {
212+ await handle . close ( ) ;
213+ }
171214 if ( kind === "jsonc" ) {
172215 const result = parseJsonc ( raw ) ;
173216 const sizeDiagnostic = result . diagnostics . find (
@@ -178,15 +221,15 @@ async function loadAst(
178221 runtime . exit ( 2 ) ;
179222 return null ;
180223 }
181- return result . ast ;
224+ return { ast : result . ast , raw } ;
182225 }
183226 if ( kind === "jsonl" ) {
184- return parseJsonl ( raw ) . ast ;
227+ return { ast : parseJsonl ( raw ) . ast , raw } ;
185228 }
186229 if ( kind === "yaml" ) {
187- return parseYaml ( raw ) . ast ;
230+ return { ast : parseYaml ( raw ) . ast , raw } ;
188231 }
189- return parseMd ( raw ) . ast ;
232+ return { ast : parseMd ( raw ) . ast , raw } ;
190233}
191234
192235function emitForKind ( ast : OcAst , fileName ?: string ) : string {
@@ -297,13 +340,13 @@ async function pathResolveCommand(
297340 if ( ocPath === null ) {
298341 return ;
299342 }
300- const ast = await loadAst ( resolveFsPath ( ocPath , options ) , ocPath . file , runtime , mode ) ;
301- if ( ast === null ) {
343+ const loaded = await loadOcPathFile ( resolveFsPath ( ocPath , options ) , ocPath . file , runtime , mode ) ;
344+ if ( loaded === null ) {
302345 return ;
303346 }
304347 let match : OcMatch | null ;
305348 try {
306- match = resolveOcPath ( ast , ocPath ) ;
349+ match = resolveOcPath ( loaded . ast , ocPath ) ;
307350 } catch ( err ) {
308351 if ( err instanceof OcPathError ) {
309352 // resolveOcPath throws on wildcard patterns — point at find.
@@ -349,14 +392,13 @@ async function pathSetCommand(
349392 return ;
350393 }
351394 const fsPath = resolveFsPath ( ocPath , options ) ;
352- const oldBytes = await fs . readFile ( fsPath , "utf-8" ) ;
353- const ast = await loadAst ( fsPath , ocPath . file , runtime , mode ) ;
354- if ( ast === null ) {
395+ const loaded = await loadOcPathFile ( fsPath , ocPath . file , runtime , mode ) ;
396+ if ( loaded === null ) {
355397 return ;
356398 }
357399
358400 const result = catchSentinel ( "set" , runtime , mode , ( ) =>
359- setOcPath ( ast , ocPath , value , { valueJson : options . valueJson === true } ) ,
401+ setOcPath ( loaded . ast , ocPath , value , { valueJson : options . valueJson === true } ) ,
360402 ) ;
361403 if ( result === null ) {
362404 return ;
@@ -381,7 +423,8 @@ async function pathSetCommand(
381423 const byteLength = Buffer . byteLength ( newBytes , "utf8" ) ;
382424
383425 if ( options . dryRun === true ) {
384- const diff = options . diff === true ? formatUnifiedDiff ( oldBytes , newBytes , fsPath ) : undefined ;
426+ const diff =
427+ options . diff === true ? formatUnifiedDiff ( loaded . raw , newBytes , fsPath ) : undefined ;
385428 emit (
386429 runtime ,
387430 mode ,
@@ -427,11 +470,11 @@ async function pathFindCommand(
427470 runtime . exit ( 2 ) ;
428471 return ;
429472 }
430- const ast = await loadAst ( resolveFsPath ( pattern , options ) , pattern . file , runtime , mode ) ;
431- if ( ast === null ) {
473+ const loaded = await loadOcPathFile ( resolveFsPath ( pattern , options ) , pattern . file , runtime , mode ) ;
474+ if ( loaded === null ) {
432475 return ;
433476 }
434- const matches = findOcPaths ( ast , pattern ) ;
477+ const matches = findOcPaths ( loaded . ast , pattern ) ;
435478 emit (
436479 runtime ,
437480 mode ,
@@ -529,16 +572,16 @@ async function pathEmitCommand(
529572 ? resolvePath ( options . file )
530573 : resolvePath ( options . cwd ?? process . cwd ( ) , fileArg ) ;
531574 const fileName = fsPath . split ( / [ \\ / ] / ) . pop ( ) ?? fileArg ;
532- const ast = await loadAst ( fsPath , fileName , runtime , mode ) ;
533- if ( ast === null ) {
575+ const loaded = await loadOcPathFile ( fsPath , fileName , runtime , mode ) ;
576+ if ( loaded === null ) {
534577 return ;
535578 }
536- const bytes = catchSentinel ( "emit" , runtime , mode , ( ) => emitForKind ( ast , fileName ) ) ;
579+ const bytes = catchSentinel ( "emit" , runtime , mode , ( ) => emitForKind ( loaded . ast , fileName ) ) ;
537580 if ( bytes === null ) {
538581 return ;
539582 }
540583 if ( mode === "json" ) {
541- runtime . writeStdout ( scrubSentinel ( JSON . stringify ( { ok : true , kind : ast . kind , bytes } ) ) ) ;
584+ runtime . writeStdout ( scrubSentinel ( JSON . stringify ( { ok : true , kind : loaded . ast . kind , bytes } ) ) ) ;
542585 return ;
543586 }
544587 runtime . writeStdout ( bytes ) ;
0 commit comments