@@ -184,3 +184,78 @@ def test_copy_file_from_worker_failed(self):
184184 self .assertEqual (0 , len (list (response )))
185185 context .set_trailing_metadata .assert_called_with ([('result' ,
186186 'invalid-path' )])
187+
188+ def test_rejects_paths_outside_worker_roots (self ):
189+ """Test file_impl rejects paths outside worker-owned roots."""
190+ os .makedirs ('/worker/bot_tmp' )
191+ os .makedirs ('/secret' )
192+ self .fs .create_file ('/secret/file' , contents = 'secret' )
193+
194+ with mock .patch .dict (
195+ os .environ , {
196+ 'WORKER_ROOT_DIR' : '/worker' ,
197+ 'WORKER_BOT_TMPDIR' : '/worker/bot_tmp' ,
198+ }):
199+ response = file_impl .create_directory (
200+ untrusted_runner_pb2 .CreateDirectoryRequest (
201+ path = '/secret/new_dir' , create_intermediates = True ), None )
202+ self .assertFalse (response .result )
203+ self .assertFalse (os .path .exists ('/secret/new_dir' ))
204+
205+ response = file_impl .remove_directory (
206+ untrusted_runner_pb2 .RemoveDirectoryRequest (
207+ path = '/secret' , recreate = False ), None )
208+ self .assertFalse (response .result )
209+ self .assertTrue (os .path .isdir ('/secret' ))
210+
211+ response = file_impl .list_files (
212+ untrusted_runner_pb2 .ListFilesRequest (path = '/secret' ), None )
213+ self .assertEqual ([], response .file_paths )
214+
215+ response = file_impl .stat (
216+ untrusted_runner_pb2 .StatRequest (path = '/secret/file' ), None )
217+ self .assertFalse (response .result )
218+
219+ context = mock .MagicMock ()
220+ context .invocation_metadata .return_value = (('path-bin' ,
221+ b'/secret/out' ),)
222+ response = file_impl .copy_file_to_worker (
223+ (untrusted_runner_pb2 .FileChunk (data = b'A' ),), context )
224+ self .assertFalse (response .result )
225+ self .assertFalse (os .path .exists ('/secret/out' ))
226+
227+ context = mock .MagicMock ()
228+ response = file_impl .copy_file_from_worker (
229+ untrusted_runner_pb2 .CopyFileFromRequest (path = '/secret/file' ),
230+ context )
231+ self .assertEqual ([], list (response ))
232+ context .set_trailing_metadata .assert_called_with ([('result' ,
233+ 'invalid-path' )])
234+
235+ response = file_impl .get_fuzz_targets (
236+ untrusted_runner_pb2 .GetFuzzTargetsRequest (path = '/secret' ), None )
237+ self .assertEqual ([], response .fuzz_target_paths )
238+
239+ def test_rejects_worker_root_symlink_escape (self ):
240+ """Test file_impl rejects worker-root paths resolving outside the root."""
241+ os .makedirs ('/worker' )
242+ os .makedirs ('/secret' )
243+ self .fs .create_file ('/secret/file' , contents = 'secret' )
244+ os .symlink ('/secret' , '/worker/link' )
245+
246+ with mock .patch .dict (os .environ , {'WORKER_ROOT_DIR' : '/worker' }):
247+ context = mock .MagicMock ()
248+ context .invocation_metadata .return_value = (('path-bin' ,
249+ b'/worker/link/out' ),)
250+ response = file_impl .copy_file_to_worker (
251+ (untrusted_runner_pb2 .FileChunk (data = b'A' ),), context )
252+ self .assertFalse (response .result )
253+ self .assertFalse (os .path .exists ('/secret/out' ))
254+
255+ context = mock .MagicMock ()
256+ response = file_impl .copy_file_from_worker (
257+ untrusted_runner_pb2 .CopyFileFromRequest (path = '/worker/link/file' ),
258+ context )
259+ self .assertEqual ([], list (response ))
260+ context .set_trailing_metadata .assert_called_with ([('result' ,
261+ 'invalid-path' )])
0 commit comments