Skip to content
Back to log

Verifying package origins with npm provenance

Chainabit's Chao · · 7 min read

In short

npm provenance links a package to its build instructions and repository. This post explains how to publish with provenance and verify downloaded packages.

npm provenance provides verifiable information about where and how a package was built. npm uses a provenance attestation to link the package to its source code and build instructions. A publish attestation records that an authorized user published the package through the registry. The two attestations capture different information about the build and publish stages.

According to npm, Sigstore signs packages published with provenance and records them in a public transparency ledger that users can inspect. Developers can verify where and how a package was built before downloading it.

What does provenance show?

GitHub describes provenance as a verifiable way to link a published package to its source repository and the specific build instructions used to publish it. That link shows which code and build process are associated with the published package.

In GitHub's example, provenance includes metadata about the repository and build as well as the package. The example includes environment fields such as GITHUB_REPOSITORY, GITHUB_SHA, GITHUB_RUN_ID, and GITHUB_WORKFLOW_REF. It also records the repository URI and commit digest.

In the SLSA provenance model, subject identifies the artifact or artifacts produced by the build. buildDefinition describes the inputs used for the build. runDetails contains information about a specific build run. According to SLSA, this structure lets you verify that the artifact was built as expected and rebuild it if needed.

In the SLSA model, buildType identifies the template that defines how the build runs and how to interpret its parameters and dependencies. externalParameters contains parameters set outside the build platform. SLSA treats these values as untrusted inputs and requires that they be included in the provenance and verified downstream.

internalParameters contains parameters controlled by the entity identified by builder.id. SLSA defines this field for debugging, incident response, and vulnerability management. Its values may also be needed to reproduce the build.

According to SLSA, externalParameters and internalParameters are top-level inputs to the template that are independent of other inputs. Each field is defined as a JSON object. The parameters contain the values passed to the build platform's interface. Metadata such as digests for artifacts referenced by those parameters can go in resolvedDependencies.

resolvedDependencies is an unordered collection of artifacts needed during the build. SLSA defines this field to support recursive analysis of the software supply chain. When a build uses a Git repository URI, the specific Git commit it resolves to can be recorded as a dependency if known. SLSA describes the completeness of the dependency list as a best-effort property.

Within runDetails, builder identifies the build platform that carried out the run. metadata holds information about that build run. The invocationId field identifies the specific run and can be used to find its logs. According to SLSA, builder.id determines the meaning and format of this value, which is case-sensitive by default.

SLSA's builderDependencies field covers dependencies that the orchestrator uses outside the workload. They have no effect on the build itself, but they can affect how provenance is generated or what security guarantees it provides. byproducts holds additional artifacts generated during the build that fall outside its declared outputs. Examples include build logs and the digest of the evaluated build configuration.

Signing and registry verification

According to the npm provenance project, the signing process requests an OIDC ID token from the CI/CD environment and generates a temporary public/private key pair. Proof of possession of the private key is sent to Fulcio along with the ID token and public key. Fulcio issues the signing certificate. The provenance statement is signed and uploaded to Rekor, and the keys are deleted.

npm describes Sigstore as a collection of tools and services for signing software with short-lived certificates. The certificate authority verifies the integrity of the OIDC token and issues a signing certificate that includes build information. The transparency log provides a public, verifiable, tamper-evident ledger for signed attestations.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

According to GitHub, the signing certificate binds the key to the CI job's identity. Anyone who receives the certificate can verify the signature and see the identity of the CI job that created it. After the provenance attestation is sent to Rekor, it is submitted to the npm registry along with the package being published.

According to the npm provenance project, the registry performs server-side verification and integrity checks on the provenance bundle before accepting a publish request. It checks whether the certificate's Issuer extension value is supported. It also compares the Runner Environment value against a list of allowed values to verify that the build ran on a cloud-hosted runner.

The registry also compares the Source Repository Visibility At Signing extension value against a list of allowed values. It checks whether the extension values in the signing certificate match the information in the SLSA provenance statement. The npm provenance project notes that changing environment variables during the build can alter information in the statement, while the certificate extension values are resistant to that form of spoofing.

The name of the package being published and the tarball's sha-512 digest are checked against the subject information in the signed statement. The registry also verifies that the repository or repository.url value in the uploaded package.json matches the Source Repository URI extension value in the signing certificate. Once the checks pass, npm signs a publish attestation to attest to the publication.

Publishing with provenance using GitHub Actions

According to npm, publishing with provenance requires a cloud-hosted runner on a supported cloud CI/CD provider. npm lists GitHub Actions and GitLab CI/CD among the supported providers. The repository entry in package.json must point to a public repository and match the location from which you publish with provenance, including capitalization.

The following configuration grants permission to generate an ID token in a GitHub Actions workflow.

permissions:
  id-token: write

npm's publishing instructions for GitHub Actions use a GitHub-hosted runner. To generate provenance, add --provenance to the publish command.

npm publish --provenance

According to npm, when publishing a package for the first time, you must also explicitly set access to public.

npm publish --provenance --access public

With trusted publishing, npm automatically generates provenance attestations without requiring the --provenance flag. npm also describes configuration options for third-party publishing tools that do not call npm publish directly. You can set the NPM_CONFIG_PROVENANCE environment variable to true in the GitHub Actions workflow.

You can also add the publishConfig block shown by npm to package.json.

"publishConfig": {
  "provenance": true
},

npm also says you can add the following entry to the project's .npmrc file.

provenance=true

Verifying downloaded packages

npm provides the following command to verify provenance attestations for downloaded packages.

npm audit signatures

npm's sample output shows separate counts for verified registry signatures and attestations in a project. According to the npm provenance project, the command runs sigstore.verify on each downloaded bundle. It compares the in-toto subject name and digest in the attestation with the installed package's details.

Verification checks the artifact signature using the public key in the signing certificate. It also confirms that the signing certificate was issued by a trusted Sigstore root certificate. The npm provenance project says verification also checks that Rekor received the signed attestation while the certificate was valid.

A trusted public key is used to verify that the registry accepted the publication through an authorized user or token. According to the npm provenance project, public keys for signed publish attestations are distributed through the Sigstore Trust Root under a target that matches the registry hostname.

Limitations

npm explicitly states that provenance offers no guarantee that a package is free of malicious code. Provenance provides a verifiable link between the package, its source code, and its build instructions. Developers can review this information to decide whether to trust the package.

Publishing with provenance requires a supported cloud CI/CD provider and a cloud-hosted runner. In the SLSA model, the resolvedDependencies field is populated as completely as possible on a best-effort basis.

Ready to start?

Download the app and continue from your phone.

End of Entry