Quick revision
The short answer
This is dependency confusion: the build looks in both internal and public repositories, finds the attacker's public artifact with the same coordinates, and resolves it instead of the internal one, often because it has a higher version. We prevent it by reserving our groupId namespace on public repositories where possible, configuring the repository manager so internal group ids are served only from the hosted repository, binding group ids to one repository in Gradle with content filtering or exclusiveContent, and pinning versions while verifying checksums or signatures, for example with Gradle dependency verification.
What to remember
In this answer, we saw that dependency confusion works when a build can find the same coordinates in both internal and public repositories and is allowed to choose the attacker's copy, usually through a higher version. We prevent it by reserving our namespace publicly, routing internal group ids only to hosted repositories in the repository manager, binding group ids to one repository with Gradle's exclusiveContent or content filters, avoiding dynamic versions, and verifying checksums or signatures with Gradle dependency verification.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
This is dependency confusion: the build looks in both internal and public repositories, finds the attacker's public artifact with the same coordinates, and resolves it instead of the internal one, often because it has a higher version. We prevent it by reserving our groupId namespace on public repositories where possible, configuring the repository manager so internal group ids are served only from the hosted repository, binding group ids to one repository in Gradle with content filtering or exclusiveContent, and pinning versions while verifying checksums or signatures, for example with Gradle dependency verification.
Why this question is asked
In 2021, a researcher showed that dozens of large companies could be made to run his code simply by publishing packages with the names of their internal libraries to public registries. Builds that searched public and internal repositories together picked up his packages without anyone noticing. The best-known cases were in npm and PyPI, but the mechanism applies to Maven and Gradle builds too. Interviewers ask this scenario to see whether we understand how resolution actually chooses an artifact, and which controls close the gap. In this answer, we will see how the attack works, why it can succeed in Java builds, and then go through the defences in the repository manager, in Gradle and in Maven, and the verification that backs them up.
1How the Attack Works
Our services depend on com.acme:payments-client, an internal library published to the company's hosted repository at version 3.2.0. The attacker learns the name, for example from a leaked pom, a stack trace in a public bug report or a job posting. They publish com.acme:payments-client at version 99.0.0 to a public repository that our builds can reach. The artifact contains code that runs when the class is loaded, or in a test, and sends environment variables, including CI secrets, to the attacker.
The build now has two candidate sources for the same coordinates. If it is allowed to pick the highest available version, or if the public repository is searched first, it takes the attacker's artifact. Nothing in the build output looks unusual; the build may even fail later in a way that looks like a normal incompatibility.
7 messages in order. Play them, or step through them one at a time.
The build did nothing wrong by its own rules; it was allowed to look in both places.
2Why Java Builds Can Be Exposed
Maven Central requires publishers to prove they own a namespace, usually through a domain they control, before publishing under a groupId such as com.acme, which makes this attack much harder on Central itself. But builds often reach other public repositories too: vendor repositories, JitPack, community mirrors or old repositories added years ago, which may not verify namespaces in the same way. And not every company's group ids match a domain it owns.
The conditions that let the attack succeed are on our side. A repository manager group that merges proxies and hosted repositories for every group id, version ranges or dynamic versions such as 3.+ or LATEST that accept whatever is highest, and Gradle builds that declare several repositories without saying which group comes from which. Each defence below removes one of these conditions.
3Defence 1: Reserve the Namespace and Route Internal Groups
The first defence is to make sure nobody else can publish under our name. We register our groupId namespace on Maven Central, even if we publish nothing there, so the attacker cannot verify it. Where other public repositories allow namespace reservation, we do the same.
The second, and the most important, is in the repository manager. Internal group ids must be served only from hosted repositories. Nexus has routing rules that block requests for given path prefixes, such as com/acme/, from being sent to proxy repositories, and Artifactory has include and exclude patterns on remote repositories with a similar effect (check your product version for the exact feature names). With this in place, a request for com.acme anything never leaves the company, so the attacker's artifact cannot be found even if it exists.
4Defence 2: Bind Group Ids to One Repository in Gradle
Gradle lets the build itself declare which repository may serve which group. Repository content filtering restricts a repository to certain groups, and exclusiveContent goes further: the named groups are searched for only in that repository, and every other repository is skipped for them.
Notice that this works even if the repository manager is misconfigured, because the build never asks the public proxy about com.acme at all. Maven has no equivalent per-group binding in the pom; we rely on the repository manager routing and on pointing the mirror at a group whose order and rules we control.
5Defence 3: Pin Versions and Verify What We Download
The attack depends on the attacker's version being chosen. If our builds use exact versions, managed in a BOM, a version 99.0.0 is never requested. Version ranges and dynamic versions should not be used for internal libraries; Gradle's dependency locking records the exact resolved versions when dynamic versions cannot be avoided.
Verification is the last layer. Gradle dependency verification records the expected checksums, and optionally the PGP signing keys, of every artifact in a verification-metadata.xml file committed to the repository. If an artifact changes, or a new one appears that is not listed, the build fails.
Maven can check that downloaded files match the checksums published next to them, but those come from the same repository as the artifact, so they do not detect a malicious publisher. For stronger guarantees in Maven, we rely on repository manager routing, pinned versions and signature checking with third-party plugins (check what your toolchain supports).
6Follow-up questions
Would the attack also work against a developer's laptop?
Yes, if the laptop's build can reach public repositories directly or through a merged group. That is why the Maven mirror with mirrorOf set to star and Gradle's central repository settings matter: they make laptops and CI use the same controlled repository manager. Laptops often hold credentials too, so they are an attractive target.
How would you detect that it already happened?
Look for internal group ids in the proxy repositories' caches, which should never contain them, and search the repository manager's request logs for internal coordinates fetched from remote repositories. Comparing SBOMs of recent builds against the list of versions actually published internally also shows unexpected versions, such as 99.0.0.
Is this only a problem for private group ids?
Mostly. A public library under a verified namespace on Maven Central cannot easily be impersonated there. The risk is highest for internal names that are not reserved publicly and for builds that search several public repositories with weaker namespace checks.
7Conclusion
In this answer, we saw that dependency confusion works when a build can find the same coordinates in both internal and public repositories and is allowed to choose the attacker's copy, usually through a higher version. We prevent it by reserving our namespace publicly, routing internal group ids only to hosted repositories in the repository manager, binding group ids to one repository with Gradle's exclusiveContent or content filters, avoiding dynamic versions, and verifying checksums or signatures with Gradle dependency verification.
Open with
This is a dependency confusion attack. The attacker publishes our internal coordinates to a public repository with a higher version, and if our build searches internal and public repositories together and is allowed to take the highest version, it resolves the attacker's artifact, whose code then runs in our CI with our secrets.
Then, if they want more
To prevent it, I would first reserve our groupId namespace on Maven Central and anywhere else that allows it, so the coordinates cannot be published by someone else there.
The most important control is in the repository manager: internal group ids are served only from hosted repositories, using Nexus routing rules or Artifactory exclude patterns, so requests for com.acme never reach a proxy.
In Gradle, I would declare repositories centrally and use exclusiveContent so com.acme groups are resolved only from the internal repository, which protects us even if the repository manager is misconfigured. I would also pin exact versions through a BOM rather than ranges or dynamic versions, because the attack relies on a higher version being chosen.
Finally, Gradle dependency verification records checksums, and optionally signing keys, in verification-metadata.xml, so a swapped or unexpected artifact fails the build.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides, Gradle user manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An attacker publishes a package to a public repository with the same coordinates as one...": https://scrivio-interview-library.netlify.app/java/dependency-and-supply-chain/40d531bff7/