@@ -46,67 +46,79 @@ export class RestrictedBackend implements Backend {
4646 signal : AbortSignal
4747 ) : Promise < string > {
4848 signal . throwIfAborted ( ) ;
49- const child = spawn (
50- this . runtime . guard ,
51- [ job , this . runtime . python , this . runtime . worker , mode ] ,
52- {
53- detached : true ,
54- stdio : [ 'ignore' , 'pipe' , 'pipe' ] ,
55- env : { } ,
56- }
49+ const status = await open (
50+ join ( job , 'status' ) ,
51+ constants . O_CREAT |
52+ constants . O_EXCL |
53+ constants . O_NOFOLLOW |
54+ constants . O_RDWR ,
55+ 0o600
5756 ) ;
58- let output = '' ;
59- let count = 0 ;
60- let overflow = false ;
61- const kill = ( ) => {
62- if (
63- child . pid &&
64- child . exitCode === null &&
65- child . signalCode === null
66- ) {
67- try {
68- process . kill ( - child . pid , 'SIGKILL' ) ;
69- } catch ( error ) {
70- if ( ( error as NodeJS . ErrnoException ) . code !== 'ESRCH' )
71- this . healthy = false ;
57+ try {
58+ signal . throwIfAborted ( ) ;
59+ const child = spawn (
60+ this . runtime . guard ,
61+ [ job , this . runtime . python , this . runtime . worker , mode ] ,
62+ {
63+ detached : true ,
64+ stdio : [ 'ignore' , status . fd , 'ignore' ] ,
65+ env : { } ,
7266 }
73- }
74- } ;
75- signal . addEventListener ( 'abort' , kill , { once : true } ) ;
76- if ( signal . aborted ) kill ( ) ;
77- let spawnError = false ;
78- child . on ( 'error' , ( ) => {
79- spawnError = true ;
80- } ) ;
81- child . stdout . on ( 'data' , ( chunk : Buffer ) => {
82- count += chunk . length ;
83- if ( count > 1024 ) {
84- overflow = true ;
85- kill ( ) ;
86- } else output += chunk . toString ( 'ascii' ) ;
87- } ) ;
88- child . stderr . on ( 'data' , ( chunk : Buffer ) => {
89- count += chunk . length ;
90- if ( count > 2048 ) {
91- overflow = true ;
92- kill ( ) ;
93- }
94- } ) ;
95- const code = await new Promise < number | null > ( resolve =>
96- child . once ( 'close' , resolve )
97- ) ;
98- signal . removeEventListener ( 'abort' , kill ) ;
99- signal . throwIfAborted ( ) ;
100- if ( spawnError || overflow ) throw new ExtractionError ( 'UNAVAILABLE' ) ;
101- if ( code !== 0 ) {
102- const value = output . trim ( ) ;
103- if ( parserErrors . has ( value ) )
104- throw new ExtractionError ( value as 'INVALID_DOCUMENT' ) ;
105- throw new ExtractionError (
106- code === null ? 'RESOURCE_LIMIT' : 'UNAVAILABLE'
10767 ) ;
68+ const kill = ( ) => {
69+ if (
70+ child . pid &&
71+ child . exitCode === null &&
72+ child . signalCode === null
73+ ) {
74+ try {
75+ process . kill ( - child . pid , 'SIGKILL' ) ;
76+ } catch ( error ) {
77+ if ( ( error as NodeJS . ErrnoException ) . code !== 'ESRCH' )
78+ this . healthy = false ;
79+ }
80+ }
81+ } ;
82+ signal . addEventListener ( 'abort' , kill , { once : true } ) ;
83+ if ( signal . aborted ) kill ( ) ;
84+ let spawnError = false ;
85+ child . on ( 'error' , ( ) => {
86+ spawnError = true ;
87+ } ) ;
88+ const code = await new Promise < number | null > ( resolve =>
89+ child . once ( 'close' , resolve )
90+ ) ;
91+ signal . removeEventListener ( 'abort' , kill ) ;
92+ signal . throwIfAborted ( ) ;
93+ const stat = await status . stat ( ) ;
94+ if (
95+ spawnError ||
96+ ! stat . isFile ( ) ||
97+ stat . nlink !== 1 ||
98+ stat . size > 1024
99+ )
100+ throw new ExtractionError ( 'UNAVAILABLE' ) ;
101+ const buffer = Buffer . alloc ( 1025 ) ;
102+ const { bytesRead } = await status . read (
103+ buffer ,
104+ 0 ,
105+ buffer . length ,
106+ 0
107+ ) ;
108+ if ( bytesRead > 1024 ) throw new ExtractionError ( 'UNAVAILABLE' ) ;
109+ const output = buffer . toString ( 'ascii' , 0 , bytesRead ) ;
110+ if ( code !== 0 ) {
111+ const value = output . trim ( ) ;
112+ if ( parserErrors . has ( value ) )
113+ throw new ExtractionError ( value as 'INVALID_DOCUMENT' ) ;
114+ throw new ExtractionError (
115+ code === null ? 'RESOURCE_LIMIT' : 'UNAVAILABLE'
116+ ) ;
117+ }
118+ return output ;
119+ } finally {
120+ await status . close ( ) ;
108121 }
109- return output ;
110122 }
111123 async probe ( ) : Promise < void > {
112124 const job = await this . create ( ) ;
@@ -131,7 +143,7 @@ export class RestrictedBackend implements Backend {
131143 await this . execute ( job , format , signal ) ;
132144 const file = await open (
133145 join ( job , 'result' ) ,
134- constants . O_RDONLY | constants . O_NOFOLLOW
146+ constants . O_RDONLY | constants . O_NOFOLLOW | constants . O_NONBLOCK
135147 ) ;
136148 try {
137149 const stat = await file . stat ( ) ;
@@ -142,7 +154,11 @@ export class RestrictedBackend implements Backend {
142154 ) {
143155 throw new ExtractionError ( 'OUTPUT_LIMIT' ) ;
144156 }
145- const bytes = await file . readFile ( ) ;
157+ const buffer = Buffer . alloc ( stat . size + 1 ) ;
158+ const { bytesRead } = await file . read ( buffer , 0 , buffer . length , 0 ) ;
159+ if ( bytesRead > policy . resultBytes )
160+ throw new ExtractionError ( 'OUTPUT_LIMIT' ) ;
161+ const bytes = buffer . subarray ( 0 , bytesRead ) ;
146162 signal . throwIfAborted ( ) ;
147163 validateResult ( bytes , format , digest ) ;
148164 return bytes ;
0 commit comments