Modern JavaScript applications depend on hundreds or sometimes thousands of third-party packages. These dependencies help developers build applications faster, but they also introduce security risks that are not always easy to identify.
Most developers check whether an npm package has known vulnerabilities before installing it. However, a package can be free from reported vulnerabilities and still be dangerous.
One possible reason is a compromised build pipeline.
A build pipeline is responsible for converting source code into a package that developers can download and install. It may include source checkout, dependency installation, compilation, testing, packaging, and publishing.
If an attacker gains access to the build environment, publishing credentials, or CI/CD workflow, they may be able to publish a modified package without making the corresponding changes to the public source code.
This creates a serious supply chain security problem. Developers may review the source repository and find nothing suspicious while the published npm package contains unexpected or malicious code.
In this article, we will explore how compromised npm build pipelines work, how to investigate package provenance, how to verify published package contents, and how to identify warning signs before trusting a dependency.
Understanding How an npm Build Pipeline Can Be Compromised
An npm package normally moves through several stages before reaching developers.
A typical publishing workflow looks like this:
Developer Changes Source Code
|
v
Source Repository
|
v
CI/CD Workflow Starts
|
v
Dependencies Installed
|
v
Application Build and Tests
|
v
Package Created
|
v
npm Registry Publishing
|
v
Developers Install Package
Each stage introduces a potential security risk.
For example, an attacker might compromise a GitHub Actions workflow, steal an npm publishing token, modify a build artifact, or inject malicious code during the packaging process.
The published package may then contain files that do not exist in the corresponding source repository.
Common Build Pipeline Attack Scenarios
Attack Scenario | What Happens | Potential Impact |
|---|
Compromised CI/CD credentials | Attacker obtains publishing permissions | Unauthorized package release |
Modified workflow | Build instructions are changed | Malicious commands execute during publishing |
Dependency compromise | A build dependency executes malicious code | Build output becomes untrusted |
Artifact tampering | Package contents are modified before publishing | Published package differs from expected output |
Stolen npm token | Attacker publishes a new package version | Unauthorized release |
Compromised maintainer account | Attacker changes release configuration | Malicious package publication |
An important point is that these attacks do not always require modifying the main application source code.
An attacker who controls the publishing process may be able to modify the final package independently.
Understanding npm Package Provenance
npm provenance provides information about where and how a package was built.
It connects a published package with its source repository and build environment through cryptographically signed attestations.
This information helps developers investigate whether a package was produced by an expected repository and CI/CD workflow.
npm provenance uses Sigstore technology to establish verifiable build information.
It includes two important types of attestations:
Provenance attestation: Provides information about the source repository, build process, and environment used to create the package.
Publish attestation: Provides evidence that the npm registry accepted the package publication through an authorized publishing process.
These attestations are useful when investigating suspicious package releases.
However, provenance does not guarantee that a package is safe. If an attacker compromises the trusted build pipeline itself, the pipeline may produce a valid attestation for malicious output.
Therefore, developers should verify provenance and independently investigate the source code, workflow configuration, and published package contents.
Checking npm Package Provenance
Let's investigate how to examine an npm package before adding it to an application.
For this example, assume that an application depends on a package named sample-library.
The goal is to understand whether its published version came from the expected source repository and build workflow.
Step 1: Inspect Package Information
Start by checking the package metadata.
Run the following command:
npm view sample-library
This displays information such as the package version, description, dependencies, maintainers, and other metadata.
To inspect more specific information, use:
npm view sample-library \
name version repository maintainers \
time dist.integrity dist.tarball
Review the following details:
Package name and version.
Repository information.
Maintainer information.
Publication timestamps.
Distribution integrity value.
Package download location.
Unexpected repository information, unfamiliar maintainers, or unusual release activity should trigger further investigation.
However, package metadata alone cannot prove that the publishing process was trustworthy.
Step 2: Check Whether the Package Has Provenance
npm provides a command for verifying registry signatures and available provenance attestations.
Run:
npm audit signatures
The command checks the signatures of downloaded packages and verifies available provenance attestations.
A successful verification provides evidence that the package matches the signed information associated with its publication.
For example, the output may report:
audited 125 packages
125 packages have verified registry signatures
18 packages have verified attestations
The numbers above are illustrative.
A verified registry signature and a verified provenance attestation provide different information.
The registry signature helps verify the integrity and authenticity of the published package through the registry's signing mechanism.
The provenance attestation provides information about the build environment and source repository.
If a package has a verified registry signature but no provenance attestation, you have evidence about registry publication integrity but less information about how the package was built.
Do not treat missing provenance as automatic proof of compromise. Investigate the package's publishing history and trust requirements.
Step 3: Examine the Provenance Information
When provenance is available, inspect the npm package's provenance details.
Pay attention to:
The most important question is whether the package was built from the repository and workflow expected by the maintainers.
For example, assume a package claims to originate from:
Repository:
https://github.com/example/sample-library
Workflow:
.github/workflows/release.yml
If the package maintainer normally publishes releases from this repository but the provenance identifies an unfamiliar repository or workflow, investigate before trusting the package.
Also verify that the source repository and workflow were authorized to publish the package.
A valid provenance signature only proves that the attestation was signed through the trusted signing process. It does not independently prove that the workflow itself was free from malicious modifications.
Comparing Published Package Contents With Source Code
One of the most useful investigation techniques is comparing the downloaded package with the expected source code.
A package may contain additional files that are not present in the public repository.
For example, an attacker might inject an unexpected JavaScript file into the package before publishing it.
Step 4: Download the Published Package
Use npm pack to download the package without installing it.
npm pack [email protected]
This creates a compressed package archive in the current directory.
Inspect its contents:
tar -tzf sample-library-1.2.0.tgz
Look for unexpected files, including:
Unknown JavaScript files.
Additional executable scripts.
Unexpected configuration files.
Modified package entry points.
Unusual installation scripts.
Pay particular attention to package.json.
For example:
{
"name": "sample-library",
"version": "1.2.0",
"main": "index.js",
"scripts": {
"postinstall": "node scripts/setup.js"
}
}
If a new release introduces an installation script that did not exist previously, investigate its purpose.
An installation script is not automatically malicious, but unexpected changes deserve attention.
Step 5: Extract and Inspect the Package
Extract the downloaded archive:
mkdir extracted-package
tar -xzf sample-library-1.2.0.tgz \
-C extracted-package
Inspect the package files:
find extracted-package -type f
Compare the extracted contents with the corresponding source repository.
Pay attention to generated files, compiled output, and files that are normally included during publishing.
Not every difference indicates an attack. Some projects intentionally publish compiled JavaScript, generated declarations, or bundled assets that are not committed to source control.
The important task is determining whether each difference is expected and can be explained by the documented build process.
Investigating the CI/CD Workflow
A package can have valid provenance and still originate from a compromised build environment.
This is why reviewing the actual publishing workflow is essential.
Step 6: Inspect the GitHub Actions Workflow
If the package uses GitHub Actions, inspect its workflow files.
A typical publishing workflow may look like this:
name: Publish npm Package
on:
release:
types:
- published
permissions:
contents: read
id-token: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 24
registry-url: https://registry.npmjs.org
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build package
run: npm run build
- name: Publish package
run: npm publish --provenance --access public
This is an illustrative workflow, not a complete production security configuration.
When reviewing the actual workflow, investigate:
Unexpected changes to publishing commands.
New external scripts downloaded during the build.
Unfamiliar GitHub Actions.
Excessive workflow permissions.
Unexpected environment variables.
Unusual publishing triggers.
Changes to the package build process.
Also inspect the workflow revision associated with the release.
A workflow may have been modified immediately before a suspicious publication.
Step 7: Check the Source Commit
Provenance information can identify the source commit associated with a package build.
Inspect that commit in the repository.
Look for unexpected changes involving:
Package entry points.
Build scripts.
Dependency versions.
Installation scripts.
Obfuscated JavaScript.
Network requests.
File system operations.
Credential access.
Compare the suspicious release with earlier versions.
A sudden change in package contents or build instructions may indicate an unauthorized modification.
Also examine whether the commit was reviewed and whether the publishing workflow used the expected branch and release tag.
Checking Package Integrity Against the Published Version
npm package-lock.json files contain integrity information for downloaded dependencies.
For example:
{
"node_modules/sample-library": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/sample-library/-/sample-library-1.2.0.tgz",
"integrity": "sha512-EXAMPLE_HASH"
}
}
The integrity field contains a cryptographic digest used to verify the downloaded package content.
This helps detect unexpected changes to the package archive after the lockfile was generated.
However, there is an important limitation.
If an attacker publishes a malicious package and the project subsequently generates a new lockfile from that package, the lockfile may contain the correct integrity value for the malicious archive.
Therefore, integrity verification should be combined with provenance verification and source review.
Warning Signs of a Potentially Compromised Build Pipeline
The following indicators deserve additional investigation.
Warning Sign | Why It Matters |
|---|
Package version published outside the normal release schedule | May indicate an unauthorized release |
Repository does not match the expected maintainer repository | Source identity may be incorrect |
Unexpected provenance workflow | Package may have been built through an unfamiliar process |
New installation scripts | Additional code may execute during installation |
Published files differ unexpectedly from source | Package may have been modified during build |
Unusual dependency changes | Build environment may have introduced unexpected code |
Unexpected publishing permissions | CI/CD workflow may have excessive access |
Maintainer account changes | Publishing authority may have been compromised |
No individual warning sign proves that a package has been compromised.
Developers should evaluate multiple indicators together and compare them with the project's normal release process.
Advantages and Disadvantages of Provenance Verification
Advantages
Better source visibility: Developers can identify the repository associated with a published package.
Build transparency: Provenance provides information about the CI/CD environment and workflow.
Cryptographic verification: Signed attestations help detect changes to published artifacts and their associated metadata.
Easier investigations: Developers can compare package versions, source commits, and publishing workflows.
Supply chain visibility: Teams can make more informed decisions before trusting third-party dependencies.
Disadvantages
Not a complete security guarantee: A compromised trusted workflow can produce valid provenance for malicious packages.
Requires investigation: Developers still need to review source code and build configuration.
Not available everywhere: Some packages do not publish provenance information.
Workflow complexity: Maintaining secure publishing pipelines requires additional configuration.
False confidence: A verified signature does not prove that the package contains no malicious code.
Best Practices for Production Applications
Verify package signatures. Run npm audit signatures regularly and investigate failed verification results.
Prefer packages with provenance. Give preference to packages that provide verifiable source and build information.
Review source repositories. Confirm that the repository and maintainers match the expected package publisher.
Inspect release changes. Compare suspicious versions with previous releases, especially when installation scripts or build configurations change.
Pin dependency versions. Use package-lock.json and npm ci to make dependency installation reproducible.
Review CI/CD permissions. Publishing workflows should receive only the permissions required for their operations.
Use trusted publishing. Prefer OIDC-based trusted publishing where supported instead of long-lived npm publishing tokens.
Protect publishing workflows. Require code review for workflow changes and restrict who can trigger release operations.
Separate build and deployment credentials. Do not expose production deployment secrets to ordinary dependency installation steps.
Establish an incident response process. If a compromised package is suspected, stop using the affected version, investigate exposure, rotate potentially compromised credentials, and review affected builds.
Summary
A compromised npm build pipeline can introduce malicious code into a published package even when the corresponding source repository appears trustworthy.
This makes package verification an important part of JavaScript application security.
npm provenance provides valuable information about where and how a package was built. The npm audit signatures command helps verify registry signatures and available provenance attestations.
However, these mechanisms should not be treated as a guarantee that a package is safe. Developers must also inspect published package contents, compare source commits, review CI/CD workflows, and investigate unexpected release changes.
For production applications, combine provenance verification with dependency locking, trusted publishing, restricted pipeline permissions, and regular security reviews.
The goal is to verify not only which package was downloaded but also whether it came from the expected source and whether the process that produced it can be trusted.
Join the conversation! Your thoughts help the community grow.