@@ -120,6 +120,8 @@ exports.extract = function extract (cwd, opts) {
120120 if ( ! cwd ) cwd = '.'
121121 if ( ! opts ) opts = { }
122122
123+ cwd = path . resolve ( cwd )
124+
123125 const xfs = opts . fs || fs
124126 const ignore = opts . ignore || opts . filter || noop
125127 const mapStream = opts . mapStream || echo
@@ -216,18 +218,21 @@ exports.extract = function extract (cwd, opts) {
216218 function onsymlink ( ) {
217219 if ( win32 ) return next ( ) // skip symlinks on win for now before it can be tested
218220 xfs . unlink ( name , function ( ) {
221+ const dst = path . resolve ( path . dirname ( name ) , header . linkname )
222+ if ( ! inCwd ( dst ) ) return next ( new Error ( name + ' is not a valid symlink' ) )
223+
219224 xfs . symlink ( header . linkname , name , stat )
220225 } )
221226 }
222227
223228 function onlink ( ) {
224229 if ( win32 ) return next ( ) // skip links on win for now before it can be tested
225230 xfs . unlink ( name , function ( ) {
226- const srcpath = path . join ( cwd , path . join ( '/' , header . linkname ) )
231+ const dst = path . join ( cwd , path . join ( '/' , header . linkname ) )
227232
228- xfs . link ( srcpath , name , function ( err ) {
233+ xfs . link ( dst , name , function ( err ) {
229234 if ( err && err . code === 'EPERM' && opts . hardlinkAsFilesFallback ) {
230- stream = xfs . createReadStream ( srcpath )
235+ stream = xfs . createReadStream ( dst )
231236 return onfile ( )
232237 }
233238
@@ -236,6 +241,10 @@ exports.extract = function extract (cwd, opts) {
236241 } )
237242 }
238243
244+ function inCwd ( dst ) {
245+ return dst . startsWith ( cwd )
246+ }
247+
239248 function onfile ( ) {
240249 const ws = xfs . createWriteStream ( name )
241250 const rs = mapStream ( stream , header )
0 commit comments