Protect virtual repositories from dependency confusion

  • Tier: Premium, Ultimate
  • Offering: GitLab.com
  • Status: Beta

A virtual repository serves your internal packages and public packages from one URL. That’s convenient, but it opens a risk called dependency confusion. Someone publishes a package to a public registry with the same name as one of your internal packages. If your build resolves that name through the virtual repository, it can install the public package instead of yours.

Upstream order alone does not protect you. Use the best practices covered in the following sections together for the best results.

How a public version can reach your build

For npm and Maven, a virtual repository merges the version lists of every upstream that has the package. It does not stop at the first upstream. For the full rules, see how a virtual repository picks a version.

For example, a virtual npm repository lists a hosted repository first and an npmjs.com remote repository second. The hosted repository has your internal is-number@1.0.0. npmjs.com has a public package with the same name, at versions up to 7.0.0. The virtual repository returns:

  • Version 1.0.0 from your hosted repository
  • Every other version from npmjs.com
  • The latest dist-tag from your hosted repository

What the npm client installs depends on the version it asks for:

RequestInstalled package
is-number@1.0.0Yours
is-number@^1.0.0 or is-numberYours, because npm prefers latest when it matches the range.
is-number@^1.1.0Public 1.1.2
is-number@7.0.0Public 7.0.0

This table describes the npm client. Yarn and pnpm might resolve a request differently, for example by picking the highest matching version instead of the latest dist-tag. Then is-number@^1.0.0 installs the public 1.1.2.

So upstream order protects a version you published. It does not protect a version you didn’t publish. An attacker needs to publish only a public version that matches a range your build asks for.

Put hosted repositories first

List your hosted repositories before any remote repository that proxies a public registry. Then, when an internal and a public upstream have the same version, your version wins.

To set the order, see set the upstreams of a virtual repository.

Use a name the public registry reserves for you

Publish internal packages under a namespace that only you can publish to on the public registry. Then no one else can publish a public package with the same name.

Unscoped npm names, unregistered npm scopes, and unregistered Maven group IDs are at risk.

Send internal names to the hosted repository

Configure your client to fetch internal packages directly from the hosted repository. Then requests for those names never reach the public registry.

In .npmrc, route your scope to the hosted repository, and everything else to the virtual repository:

ini
registry=<virtual_repository_url>
@example:registry=<hosted_repository_url>

In .yarnrc.yml, route your scope to the hosted repository, and everything else to the virtual repository:

yaml
npmRegistryServer: "<virtual_repository_url>"
npmScopes:
  example:
    npmRegistryServer: "<hosted_repository_url>"

In settings.gradle, use exclusiveContent so that your group IDs resolve only from the hosted repository:

groovy
dependencyResolutionManagement {
    repositories {
        exclusiveContent {
            forRepository {
                maven { url = uri("<hosted_repository_url>") }
            }
            filter {
                includeGroupByRegex("com\\.example(\\..*)?")
            }
        }
        maven { url = uri("<virtual_repository_url>") }
    }
}

In settings.gradle.kts, use exclusiveContent so that your group IDs resolve only from the hosted repository:

kotlin
dependencyResolutionManagement {
    repositories {
        exclusiveContent {
            forRepository {
                maven { url = uri("<hosted_repository_url>") }
            }
            filter {
                includeGroupByRegex("com\\.example(\\..*)?")
            }
        }
        maven { url = uri("<virtual_repository_url>") }
    }
}

sbt can’t send some group IDs to one repository and the rest to another.

mvn 3.9 and later can limit a repository to a list of group IDs, with remote repository filtering. It’s an allowlist, not a denylist. To keep your group IDs off the virtual repository, you would list every public group ID your build uses for that repository. Few teams can keep that list current.

Instead:

  • Use a group ID you registered on Maven Central, so no one else can publish that name there.
  • Pin exact versions, as described in the next section.
  • For builds that use only internal packages, point the build at a virtual repository that has no public upstream.

For authentication, see npm and Maven.

Pin versions and commit lockfiles

A lockfile records the exact version and checksum of every dependency. With a lockfile, your build installs the same package every time, and fails if the checksum changes.

ClientWhat to do
npmCommit package-lock.json, and run npm ci in CI/CD.
YarnCommit yarn.lock, and run yarn install --immutable in CI/CD.
pnpmCommit pnpm-lock.yaml, and run pnpm install --frozen-lockfile in CI/CD.
GradleTurn on dependency locking and dependency verification.
MavenUse exact versions. Do not use version ranges, LATEST, or RELEASE.

Limit who can publish

Give the Artifact Contributor role on the hosted repository only to the identities that publish releases, such as your CI/CD jobs. Assign the role on the repository, not on the whole registry. The Artifact Manager and Artifact Admin roles can also publish, so limit who holds them.

Everyone else needs only the Artifact Viewer role to install packages.

For more information, see assign a role.

Known issues

  • You cannot limit an upstream to certain package names. For example, you cannot tell a virtual repository to never fetch @example/* from npmjs.com. Allow and deny rules are proposed in issue 1177.
  • For Maven, any upstream can set <latest> and <release>, whatever its position. Do not resolve dependencies with LATEST or RELEASE.
  • If two npm upstreams have the same version with different contents, the package metadata and the tarball can come from different upstreams. npm then rejects the tarball with an integrity error, and your build fails.