@@ -185,3 +185,31 @@ def test_unpack_package_duplicate_name(tmpdir):
185185 inc_path = os .path .realpath (os .path .join (temp_path , 'pkg' , 'inc.h' ))
186186 assert os .path .commonpath ([real_temp_path , inc_path ]) == real_temp_path
187187 assert os .listdir (outside_path ) == ['target.c' ]
188+
189+ def test_unpack_package_link_redirected_later (tmpdir ):
190+ work_path = str (tmpdir )
191+ outside_path = os .path .join (work_path , 'outside' )
192+ os .mkdir (outside_path )
193+ with open (os .path .join (outside_path , 'secret.c' ), 'w' , encoding = 'utf-8' ) as f :
194+ f .write ('int secret;\n ' )
195+ tgz = os .path .join (work_path , 'temp.tgz' )
196+ with tarfile .open (tgz , 'w:gz' ) as tf :
197+ _add_tar_member (tf , 'pkg/src/main.c' )
198+ # 'd.h' does not exist yet, so this target is resolved as temp/outside and looks harmless
199+ _add_tar_member (tf , 'pkg/l.h' , linkname = 'd.h/../../outside' )
200+ # turns the missing component into a link, which redirects 'l.h' out of temp
201+ _add_tar_member (tf , 'pkg/d.h' , linkname = '.' )
202+ # a link that stays inside the package is still extracted
203+ _add_tar_member (tf , 'pkg/include/main.c' , linkname = '../src/main.c' )
204+
205+ temp_path , source_found = unpack_package (work_path , tgz )
206+ assert source_found
207+ real_temp_path = os .path .realpath (temp_path )
208+ for name in ('l.h' , 'd.h' ):
209+ path = os .path .join (temp_path , 'pkg' , name )
210+ if os .path .lexists (path ):
211+ assert os .path .commonpath ([real_temp_path , os .path .realpath (path )]) == real_temp_path
212+ inc_path = os .path .join (temp_path , 'pkg' , 'include' , 'main.c' )
213+ if os .path .islink (inc_path ):
214+ assert os .path .samefile (inc_path , os .path .join (temp_path , 'pkg' , 'src' , 'main.c' ))
215+ assert os .listdir (outside_path ) == ['secret.c' ]
0 commit comments