@@ -129,10 +129,13 @@ def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch):
129129 'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n ' \
130130 'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n '
131131
132- def _add_tar_member (tf , name , linkname = None , linktype = tarfile .SYMTYPE ):
132+ def _add_tar_member (tf , name , linkname = None , linktype = tarfile .SYMTYPE , dirmode = None ):
133133 info = tarfile .TarInfo (name )
134134 data = b''
135- if linkname is not None :
135+ if dirmode is not None :
136+ info .type = tarfile .DIRTYPE
137+ info .mode = dirmode
138+ elif linkname is not None :
136139 info .type = linktype
137140 info .linkname = linkname
138141 else :
@@ -226,3 +229,28 @@ def test_unpack_package_hardlink_missing_target(tmpdir):
226229 assert source_found
227230 assert os .path .isfile (os .path .join (temp_path , 'pkg' , 'main.c' ))
228231 assert not os .path .lexists (os .path .join (temp_path , 'pkg' , 'copy.c' ))
232+
233+ def test_unpack_package_link_in_readonly_dir (tmpdir ):
234+ work_path = str (tmpdir )
235+ outside_path = os .path .join (work_path , 'outside' )
236+ os .mkdir (outside_path )
237+ with open (os .path .join (outside_path , 'secret.c' ), 'w' , encoding = 'utf-8' ) as f :
238+ f .write ('int secret;\n ' )
239+ tgz = os .path .join (work_path , 'temp.tgz' )
240+ with tarfile .open (tgz , 'w:gz' ) as tf :
241+ _add_tar_member (tf , 'pkg/main.c' )
242+ # redirected out of temp by the next member, like in test_unpack_package_link_redirected_later
243+ _add_tar_member (tf , 'pkg/x.h/l.h' , linkname = 'd.h/../../../outside' )
244+ _add_tar_member (tf , 'pkg/x.h/d.h' , linkname = '.' )
245+ # a directory member with a header-like name makes the folder read-only after the links exist
246+ _add_tar_member (tf , 'pkg/x.h' , dirmode = 0o555 )
247+
248+ temp_path , source_found = unpack_package (work_path , tgz )
249+ assert source_found
250+ assert os .path .isfile (os .path .join (temp_path , 'pkg' , 'main.c' ))
251+ real_temp_path = os .path .realpath (temp_path )
252+ for name in ('l.h' , 'd.h' ):
253+ path = os .path .join (temp_path , 'pkg' , 'x.h' , name )
254+ if os .path .lexists (path ):
255+ assert os .path .commonpath ([real_temp_path , os .path .realpath (path )]) == real_temp_path
256+ assert os .listdir (outside_path ) == ['secret.c' ]
0 commit comments