Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: npm pack marks the wrong files as executable #409

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kchindam-infy
Copy link

This PR fixes an issue where npm pack incorrectly marks files as executable if their paths contain the bin path as a substring. Specifically, the problem occurs when files located in directories like src/bin/ are erroneously marked as executable because their paths include the bin directory name.

Changes made:

  • Updated lib/util/is-package-bin.js:

    • Changed the parameter name from path to filePath for clarity.
    • Removed the unreliable regular expression used to manipulate the path.
    • Introduced proper path handling by:
      • Removing the 'package/' prefix from filePath to get the relative path.
      • Normalizing both filePath and binPath using path.posix.normalize.
      • Comparing the normalized paths for an exact match.
  • Ensured Cross-Platform Compatibility:

    • Used path.posix to handle paths consistently across different operating systems, particularly important for paths within tarballs.

    The issue was caused by improper path manipulation and comparison in is-package-bin.js. The original code did not correctly handle nested directories or different path formats, leading to unintended files being marked as executable.

By accurately processing and comparing paths, we ensure that only the exact files specified in the bin field of package.json are marked as executable. This aligns the behavior of npm pack with the expected outcome and prevents potential security risks or execution of unintended scripts.

References

@kchindam-infy kchindam-infy requested a review from a team as a code owner October 16, 2024 19:38
if (kv[1] === p) {

// Remove 'package/' prefix if present
const relativeFilePath = filePath.startsWith('package/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are filePaths that start with package "special", why does this need to be removed? this prefix seems arbitrary and permissible

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we need to remove the 'package/' prefix from filePath is that this prefix is added to all the file paths during the tarball creation process due to the prefix 'package/' option in tar-create-options.js. As a result filePath in the filter function includes this prefix e.g. 'package/bin/index.js'.

Since the paths specified in the bin field of package.json (e.g. 'bin/index.js') do not include this prefix, we need to adjust filePath to make an accurate comparison. By removing the 'package/' prefix (or better yet computing the relative path), we align filePath with how path are represented in package.json.

To avoid hardcoding the prefix, we can update the code to use path.relative. (const relativePath = path.relative('package', filePath); )

Let me know if this makes sense or if you'd like me to adjust code further

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants