@@ -126,3 +126,31 @@ def test_unpack_package_duplicate_name(tmpdir):
126126 inc_path = os .path .realpath (os .path .join (temp_path , 'pkg' , 'inc.h' ))
127127 assert os .path .commonpath ([real_temp_path , inc_path ]) == real_temp_path
128128 assert os .listdir (outside_path ) == ['target.c' ]
129+
130+ def test_unpack_package_link_redirected_later (tmpdir ):
131+ work_path = str (tmpdir )
132+ outside_path = os .path .join (work_path , 'outside' )
133+ os .mkdir (outside_path )
134+ with open (os .path .join (outside_path , 'secret.c' ), 'w' , encoding = 'utf-8' ) as f :
135+ f .write ('int secret;\n ' )
136+ tgz = os .path .join (work_path , 'temp.tgz' )
137+ with tarfile .open (tgz , 'w:gz' ) as tf :
138+ _add_tar_member (tf , 'pkg/src/main.c' )
139+ # 'd.h' does not exist yet, so this target is resolved as temp/outside and looks harmless
140+ _add_tar_member (tf , 'pkg/l.h' , linkname = 'd.h/../../outside' )
141+ # turns the missing component into a link, which redirects 'l.h' out of temp
142+ _add_tar_member (tf , 'pkg/d.h' , linkname = '.' )
143+ # a link that stays inside the package is still extracted
144+ _add_tar_member (tf , 'pkg/include/main.c' , linkname = '../src/main.c' )
145+
146+ temp_path , source_found = unpack_package (work_path , tgz )
147+ assert source_found
148+ real_temp_path = os .path .realpath (temp_path )
149+ for name in ('l.h' , 'd.h' ):
150+ path = os .path .join (temp_path , 'pkg' , name )
151+ if os .path .lexists (path ):
152+ assert os .path .commonpath ([real_temp_path , os .path .realpath (path )]) == real_temp_path
153+ inc_path = os .path .join (temp_path , 'pkg' , 'include' , 'main.c' )
154+ if os .path .islink (inc_path ):
155+ assert os .path .samefile (inc_path , os .path .join (temp_path , 'pkg' , 'src' , 'main.c' ))
156+ assert os .listdir (outside_path ) == ['secret.c' ]
0 commit comments