Why npm Lockfiles Do Not Secure Git Dependencies by Commit

Why npm Lockfiles Do Not Secure Git Dependencies by Commit EduTechBuddy.com © edutechbuddy.com
Why npm Lockfiles Do Not Secure Git Dependencies by Commit © edutechbuddy.com
Many developers assume the npm lockfile pins every dependency, but Git-based packages work differently. Learn why package.json, not the lockfile, determines which Git commit is installed and what this means for your project's security.

Plenty of developers trust npm lockfiles to keep every dependency locked down as intended. But with Git-based dependencies, the lockfile doesn't offer the same security or pinning as it does for registry packages. npm's official documentation says package-lock.json records the dependency tree for reproducible installs, yet it treats Git dependencies differently by logging the Git URL with a commit SHA instead of a registry version and integrity hash. This difference matters for anyone managing JavaScript projects with custom or third-party Git repositories. For educators and students focused on software supply chain security, knowing this distinction is key to building secure and reproducible projects.

How npm Handles Git Dependencies

Adding a Git dependency to your project prompts npm to create a lockfile entry with a resolved URL that points to the repository and a specific commit hash. The npm documentation explains that for Git dependencies, the lockfile entry is the full Git URL with the commit SHA. Unlike registry packages, this entry skips the integrity field. The commit hash follows the # in the URL, serving as the only pointer to the code version.

Here's an example of a lockfile entry for a Git dependency:

"node_modules/gitdep-pkg": {
  "version": "1.0.0",
  "resolved": "git+file:///path/to/repo-a#c48c5632d59d77c1a4c9f6e2a5511402af93c217"
}

Notice there's no integrity field. npm doesn't check the downloaded code against a cryptographic hash as it does for registry packages. The lockfile's resolved field just notes where the package was fetched from, and for Git dependencies, that's the Git URL with the commit SHA.

What Happens When the Lockfile Is Tampered With?

To see the impact, take a scenario with two local Git repositories, each exporting a package with different code. The project depends on one repository at a specific commit. A test with npm 10.9.4 showed that if you change only the resolved URL in the lockfile to point to the other repository, npm still installs the original code specified in package.json. Even adding a fake integrity field doesn't trigger an error—npm ignores it for Git dependencies. This shows npm installs from the Git specification in package.json, not from the lockfile's resolved field (detailed test results).

If you change both package.json and the lockfile to point to a different repository and commit, npm installs the new code without complaint. If the commit hash in package.json doesn't exist in the new repository, the install fails with a clear error. npm's documented approach lines up with this: the Git specification in package.json is what npm trusts for installation.

Why package.json Is the Real Pin

The main point is that for Git dependencies, npm relies on the Git specification in package.json instead of the lockfile. The lockfile's resolved field is advisory and doesn't control which code gets installed. Reviewing changes to package.json is crucial for security, especially when Git URLs or commit hashes change. npm documentation and independent tests confirm that changing only the lockfile doesn't affect which repository is installed, but changing package.json does.

Integrity Checks and Security Tools

Git dependencies miss out on integrity checks in the lockfile, unlike registry packages. npm's schema treats the integrity field as an artifact-level field for registry packages, while Git dependencies are pinned by the commit SHA in the URL. Trying to enforce host restrictions with tools like lockfile-lint may not catch tampering if the URL uses file:// or other protocols. Both original and tampered lockfiles can pass or fail these checks together, so automated tools alone may not spot unauthorized changes.

Best Practices for Managing Git Dependencies

Since the true pin for a Git dependency is in package.json, code reviewers should watch any pull request that changes a Git URL or commit hash. Treat these changes with the same care as version updates for registry packages. Automated tools might not offer enough protection, so manual review stays important. For educators teaching secure software development, pointing out this difference helps students see why reviewing manifest files matters and where automated dependency management tools fall short.

For more on how Git configuration details can affect repository safety, see the earlier breakdown of Git's boolean parsing and its impact on execution.

Camila Ferreira Editor EduTechBuddy.com
Co-Founder

Camila Ferreira

Camila Ferreira is the Co-Founder of EduTechBuddy, where her editorial focus is programming education and practical software development skills. Her primary subjects include Python programming, Git version control, and the fundamental concepts beginners need to understand when working with code.