Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,11 @@ The following options are supported:
symlink that would affect the location of an extracted file. [Alias:
`U`]
- `strip` Remove the specified number of leading path elements.
Pathnames with fewer elements will be silently skipped. Note that
the pathname is edited after applying the filter, but before
security checks. [Alias: `strip-components`, `stripComponents`]
Pathnames with fewer elements, or whose names become empty after
stripping, are silently skipped and do not change the extraction
directory. Note that the pathname is edited after applying the
filter, but before security checks. [Alias: `strip-components`,
`stripComponents`]
- `preserveOwner` If true, tar will set the `uid` and `gid` of
extracted entries to the `uid` and `gid` fields in the archive.
This defaults to true when run as root, and false otherwise. If
Expand Down Expand Up @@ -856,9 +858,10 @@ Most unpack errors will cause a `warn` event to be emitted. If the
With this option, existing hardlinks will be broken, as will any
symlink that would affect the location of an extracted file.
- `strip` Remove the specified number of leading path elements.
Pathnames with fewer elements will be silently skipped. Note that
the pathname is edited after applying the filter, but before
security checks.
Pathnames with fewer elements, or whose names become empty after
stripping, are silently skipped and do not change the extraction
directory. Note that the pathname is edited after applying the
filter, but before security checks.
- `umask` Filter the modes of entries like `process.umask()`.
- `dmode` Default mode for directories
- `fmode` Default mode for files
Expand Down
3 changes: 2 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export interface TarOptions {
* entry path. For example, with `{strip: 2}`, the entry `a/b/c/d` would be
* extracted to `{cwd}/c/d`.
*
* Any entry whose entire path is stripped will be excluded.
* Any entry whose entire path is stripped will be excluded, and will
* not change ownership, mode, or mtime of the extraction directory.
*/
strip?: number

Expand Down
8 changes: 8 additions & 0 deletions src/unpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ export class Unpack extends Parser {
}
}
parts.splice(0, this.strip)
// GNU tar --strip-components skips members whose names become
// empty. Applying those entries would mutate cwd ownership,
// mode, and mtime (#294).
if (parts.every(part => part === '' || part === '.')) {
return false
}
entry.path = parts.join('/')
}

Expand Down Expand Up @@ -383,6 +389,8 @@ export class Unpack extends Parser {

// an archive can set properties on the extraction directory, but it
// may not replace the cwd with a different kind of thing entirely.
// Fully-stripped directory entries never reach here; they are
// skipped above so strip does not mutate cwd (#294).
if (
entry.absolute === this.cwd &&
entry.type !== 'Directory' &&
Expand Down
Loading