isaacs / isaacs/node-tar

[BUG] extract with strip can change permissions on existing files when running as root

Open
#294 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
JavaScript
Stars
922
Forks
281
PR merge metrics
No merged PRs in 30d

Description

What / Why

When the tar utility runs with --strip-components and -p (or as root), no existing directories have their permissions changed. However, when node-tar does the equivalent, the existing filesystem can be mutated.

How

Steps to Reproduce

In an empty directory, run

mkdir dir
sudo chown 501 dir
tar -czf tarball.tgz dir
sudo node - <<JS
const tar = require('tar')
tar.x({file: 'tarball.tgz',strip:1})
JS
ls -an
Expected Behavior

When I run

mkdir dir
sudo chown 501 dir
tar -czf tarball.tgz dir
sudo tar --strip-components=1 -xzf tarball.tgz
ls -an

which should be roughly equivalent, the output is

total 8
drwxr-xr-x   4 502  20   128 17 Sep 18:08 .
drwxr-xr-x  46 502  20  1472 17 Sep 18:08 ..
drwxr-xr-x   2 501  20    64 17 Sep 18:08 dir
-rw-r--r--   1 502  20   110 17 Sep 18:08 tarball.tgz
Actual Behavior

When I run the above script using node-tar, the output is

total 8
drwxr-xr-x   4 501  20   128 17 Sep 18:09 .
drwxr-xr-x  46 502  20  1472 17 Sep 18:09 ..
drwxr-xr-x   2 501  20    64 17 Sep 18:09 dir
-rw-r--r--   1 502  20   110 17 Sep 18:09 tarball.tgz

The results are almost identical, except in the node-tar case the current directory has changed ownership from UID 502 to UID 501.

References

I encountered this while investigating a problem with running backstage's tests.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the documented node-tar extraction with --strip-components while running as root, then inspect the extraction path that handles existing directories and ownership. Done means the current directory remains owned by UID 502, matching the system tar behavior, with coverage for the reported command.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.