[{"data":1,"prerenderedAt":575,"buildId":576},["ShallowReactive",2],{"blog-dependency-confusion":3},{"id":4,"title":5,"body":6,"cluster":564,"date":565,"description":566,"draft":567,"extension":568,"keyword":569,"meta":570,"navigation":392,"path":571,"seo":572,"stem":573,"__hash__":574},"blog\u002Fblog\u002Fdependency-confusion.md","Dependency confusion: how a public package hijacks a build",{"type":7,"value":8,"toc":544},"minimark",[9,13,22,27,30,62,85,95,105,109,112,117,136,139,143,153,157,160,169,173,177,198,201,205,221,240,243,247,250,299,303,307,313,348,357,406,410,422,430,436,440,453,471,485,489,502,506,514,537,540],[10,11,12],"p",{},"Dependency confusion happens when a build that should install your internal package installs someone else's public package with the same name instead. The attacker never touches your repository or your private package index. They publish one package to a public registry and wait for your build to choose it.",[10,14,15,16,21],{},"It is one of the cheapest software supply chain attacks to run, and one of the cheapest to prevent. This post covers how the attack works, why package managers allow it, two real cases, and the configuration that closes it. For the wider picture of supply chain attacks and build controls, see ",[17,18,20],"a",{"href":19},"\u002Fblog\u002Fsoftware-supply-chain-attacks","Software supply chain attacks and how builds stop them",".",[23,24,26],"h2",{"id":25},"how-a-dependency-confusion-attack-works","How a dependency confusion attack works",[10,28,29],{},"The attack needs three conditions:",[31,32,33,50,56],"ol",{},[34,35,36,40,41,45,46,49],"li",{},[37,38,39],"strong",{},"An internal package name the attacker can learn."," Names leak through ",[42,43,44],"code",{},"package.json"," and ",[42,47,48],{},"requirements.txt"," files in public repositories, bundled JavaScript served to browsers, error messages and job posts.",[34,51,52,55],{},[37,53,54],{},"A build that can see both a private package index and a public one."," For example, pip configured with an internal index plus PyPI, or a registry proxy that merges internal and public packages under one URL.",[34,57,58,61],{},[37,59,60],{},"A resolver that picks the \"best\" version across all sources."," If the public package has a higher version number than the internal one, the resolver installs the public one.",[10,63,64,65,68,69,72,73,75,76,79,80,82,83,21],{},"When those line up, the attacker publishes ",[42,66,67],{},"acme-billing"," version ",[42,70,71],{},"99.0.0"," to the public registry. The next build resolves ",[42,74,67],{},", sees ",[42,77,78],{},"1.4.2"," internally and ",[42,81,71],{}," publicly, and installs ",[42,84,71],{},[10,86,87,88,45,91,94],{},"The attacker does not need your code to import the package. npm runs ",[42,89,90],{},"preinstall",[42,92,93],{},"postinstall"," scripts at install time, and Python source distributions can run code while they build. Code execution happens on the developer laptop or the CI runner the moment the install runs, with whatever credentials that machine holds.",[10,96,97,98,104],{},"OWASP lists dependency confusion as the first attack vector under ",[17,99,103],{"href":100,"rel":101},"https:\u002F\u002Fowasp.github.io\u002Fwww-project-top-10-ci-cd-security-risks\u002FCICD-SEC-03-Dependency-Chain-Abuse",[102],"nofollow","CICD-SEC-3, Dependency Chain Abuse",": \"publication of malicious packages in public repositories with the same name as internal package names.\"",[23,106,108],{"id":107},"why-package-managers-choose-the-public-package","Why package managers choose the public package",[10,110,111],{},"Package managers were built to find the newest compatible version of a name. Most have no concept of \"this name belongs to my organisation, never look for it anywhere else\" unless you configure one.",[113,114,116],"h3",{"id":115},"pip-and-extra-index-url","pip and --extra-index-url",[10,118,119,122,123,126,127,132,133,135],{},[42,120,121],{},"--index-url"," sets the main index and ",[42,124,125],{},"--extra-index-url"," adds more. pip collects candidate versions from every index it is given and picks the best match; it does not treat your private index as the authority for your package names. The ",[17,128,131],{"href":129,"rel":130},"https:\u002F\u002Fpip.pypa.io\u002Fen\u002Fstable\u002Fcli\u002Fpip_install\u002F",[102],"pip documentation"," is direct about it: \"Using the ",[42,134,125],{}," option to search for packages which are not in the main repository (for example, private packages) is unsafe.\"",[10,137,138],{},"The common setup, PyPI as the main index and the internal server as an extra, is exactly the exposed one.",[113,140,142],{"id":141},"npm-and-unscoped-names","npm and unscoped names",[10,144,145,146,148,149,152],{},"An unscoped npm package name such as ",[42,147,67],{}," lives in one global namespace. If your project resolves it through a registry that also proxies the public npm registry, a public package with that name and a higher version can win. Scoped names such as ",[42,150,151],{},"@acme\u002Fbilling"," can be tied to a single registry, which is why scopes are part of the fix below.",[113,154,156],{"id":155},"private-registries-that-proxy-a-public-one","Private registries that proxy a public one",[10,158,159],{},"Many teams run Artifactory, Nexus, Azure Artifacts or a similar server as a single endpoint that serves internal packages and caches public ones. That looks safe because the build only has one URL. Whether it is safe depends on what the server does when the same name exists on both sides. If it merges versions from both, you have moved the confusion into the registry rather than removed it.",[10,161,162,163,168],{},"Azure Artifacts, for example, lets a feed owner decide per package whether versions from a public registry may be saved to the feed, and its ",[17,164,167],{"href":165,"rel":166},"https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fazure\u002Fdevops\u002Fartifacts\u002Fconcepts\u002Fupstream-behavior",[102],"documentation"," says that option is off by default. Check what your own registry does; do not assume.",[23,170,172],{"id":171},"real-dependency-confusion-attacks","Real dependency confusion attacks",[113,174,176],{"id":175},"alex-birsans-research-2021","Alex Birsan's research, 2021",[10,178,179,180,185,186,188,189,191,192,197],{},"In ",[17,181,184],{"href":182,"rel":183},"https:\u002F\u002Fmedium.com\u002F@alex.birsan\u002Fdependency-confusion-4a5d60fec610",[102],"February 2021",", Alex Birsan published the research that named the attack. Working under bug bounty programmes and agreed tests, he found internal package names in ",[42,187,44],{}," files and published public packages with those names. A ",[42,190,90],{}," script reported back over DNS, which showed the company IP address the install came from, the username and the home directory. ",[17,193,196],{"href":194,"rel":195},"https:\u002F\u002Fwww.bleepingcomputer.com\u002Fnews\u002Fsecurity\u002Fresearcher-hacks-over-35-tech-firms-in-novel-supply-chain-attack\u002F",[102],"BleepingComputer"," reported that the packages ran inside more than 35 companies, including Microsoft, Apple, PayPal, Shopify, Netflix, Tesla, Yelp and Uber, and earned him over $130,000.",[10,199,200],{},"Microsoft assigned CVE-2021-24105 to Azure Artifacts and described the root cause as a design flaw in package managers rather than a bug, which can only be fixed through configuration. That framing still holds: there is no patch to install, only settings to get right.",[113,202,204],{"id":203},"pytorch-nightly-and-torchtriton-2022","PyTorch nightly and torchtriton, 2022",[10,206,207,208,211,212,214,215,220],{},"Between 25 and 30 December 2022, anyone who installed PyTorch nightly on Linux with pip also installed a malicious ",[42,209,210],{},"torchtriton"," from PyPI. PyTorch shipped ",[42,213,210],{}," from its own nightly index, and someone registered the same name on PyPI. The ",[17,216,219],{"href":217,"rel":218},"https:\u002F\u002Fpytorch.org\u002Fblog\u002Fcompromised-nightly-dependency\u002F",[102],"PyTorch post-mortem"," says: \"Since the PyPI index takes precedence, this malicious package was being installed instead of the version from our official repository.\"",[10,222,223,224,227,228,231,232,235,236,239],{},"The malicious binary collected system information, ",[42,225,226],{},"\u002Fetc\u002Fpasswd",", ",[42,229,230],{},"$HOME\u002F.gitconfig",", files under ",[42,233,234],{},"$HOME\u002F.ssh"," and the first 1,000 files in the home directory, and sent them out through DNS queries. PyTorch removed the dependency, renamed it to ",[42,237,238],{},"pytorch-triton",", and registered a placeholder package on PyPI so the name could not be taken again.",[10,241,242],{},"Neither case needed a stolen credential or a code change. Both used ordinary resolver behaviour.",[23,244,246],{"id":245},"how-to-check-whether-your-build-is-exposed","How to check whether your build is exposed",[10,248,249],{},"You can answer this in an afternoon:",[251,252,253,259,265,287,293],"ul",{},[34,254,255,258],{},[37,256,257],{},"List every internal package name"," your builds install, across npm, PyPI, Maven, NuGet and anything else you publish internally.",[34,260,261,264],{},[37,262,263],{},"Check whether each name exists publicly."," A name that is free on the public registry is a name an attacker can take.",[34,266,267,270,271,227,274,227,277,227,280,227,283,286],{},[37,268,269],{},"Read the install configuration your CI actually uses",": ",[42,272,273],{},"pip.conf",[42,275,276],{},"PIP_EXTRA_INDEX_URL",[42,278,279],{},".npmrc",[42,281,282],{},"settings.xml",[42,284,285],{},"nuget.config",". Look for more than one source, or a public registry listed alongside the internal one.",[34,288,289,292],{},[37,290,291],{},"Check what your registry proxy does"," when the same name exists internally and publicly.",[34,294,295,298],{},[37,296,297],{},"Check your lockfiles"," for internal package names resolved from a public URL.",[23,300,302],{"id":301},"how-to-prevent-dependency-confusion","How to prevent dependency confusion",[113,304,306],{"id":305},"use-a-single-source-and-make-it-the-authority","Use a single source, and make it the authority",[10,308,309,310,312],{},"Point every client at one index that you control, and remove ",[42,311,125],{}," from CI. That index can proxy public packages, but it should refuse public versions of names you own.",[314,315,320],"pre",{"className":316,"code":317,"language":318,"meta":319,"style":319},"language-bash shiki shiki-themes github-dark","pip install --index-url https:\u002F\u002Fpypi.internal.example.com\u002Fsimple -r requirements.txt\n","bash","",[42,321,322],{"__ignoreMap":319},[323,324,327,331,335,339,342,345],"span",{"class":325,"line":326},"line",1,[323,328,330],{"class":329},"svObZ","pip",[323,332,334],{"class":333},"sU2Wk"," install",[323,336,338],{"class":337},"sDLfK"," --index-url",[323,340,341],{"class":333}," https:\u002F\u002Fpypi.internal.example.com\u002Fsimple",[323,343,344],{"class":337}," -r",[323,346,347],{"class":333}," requirements.txt\n",[10,349,350,351,356],{},"If you use uv, its ",[17,352,355],{"href":353,"rel":354},"https:\u002F\u002Fdocs.astral.sh\u002Fuv\u002Fconcepts\u002Findexes\u002F",[102],"default index strategy"," stops at the first index that has a given package, and the docs say the intent is \"to prevent 'dependency confusion' attacks.\" You can also pin a package to one index so it is never looked up anywhere else:",[314,358,362],{"className":359,"code":360,"language":361,"meta":319,"style":319},"language-toml shiki shiki-themes github-dark","[[tool.uv.index]]\nname = \"internal\"\nurl = \"https:\u002F\u002Fpypi.internal.example.com\u002Fsimple\"\nexplicit = true\n\n[tool.uv.sources]\nacme-billing = { index = \"internal\" }\n","toml",[42,363,364,369,375,381,387,394,400],{"__ignoreMap":319},[323,365,366],{"class":325,"line":326},[323,367,368],{},"[[tool.uv.index]]\n",[323,370,372],{"class":325,"line":371},2,[323,373,374],{},"name = \"internal\"\n",[323,376,378],{"class":325,"line":377},3,[323,379,380],{},"url = \"https:\u002F\u002Fpypi.internal.example.com\u002Fsimple\"\n",[323,382,384],{"class":325,"line":383},4,[323,385,386],{},"explicit = true\n",[323,388,390],{"class":325,"line":389},5,[323,391,393],{"emptyLinePlaceholder":392},true,"\n",[323,395,397],{"class":325,"line":396},6,[323,398,399],{},"[tool.uv.sources]\n",[323,401,403],{"class":325,"line":402},7,[323,404,405],{},"acme-billing = { index = \"internal\" }\n",[113,407,409],{"id":408},"scope-internal-names-and-reserve-them-publicly","Scope internal names and reserve them publicly",[10,411,412,413,418,419,421],{},"On npm, publish internal packages under an organisation scope and map that scope to your registry, as the ",[17,414,417],{"href":415,"rel":416},"https:\u002F\u002Fdocs.npmjs.com\u002Fcli\u002Fv10\u002Fusing-npm\u002Fscope",[102],"npm scope docs"," describe. Commit the ",[42,420,279],{}," to each repository so CI uses the same mapping:",[314,423,428],{"className":424,"code":426,"language":427,"meta":319},[425],"language-text","@acme:registry=https:\u002F\u002Fnpm.internal.example.com\u002F\n","text",[42,429,426],{"__ignoreMap":319},[10,431,432,433,435],{},"Where scopes do not exist, register your internal names on the public registry as empty placeholder packages, as PyTorch did with ",[42,434,210],{},". The name cannot be taken if you hold it.",[113,437,439],{"id":438},"lock-versions-and-hashes","Lock versions and hashes",[10,441,442,443,448,449,452],{},"A lockfile with hashes means a new version on any registry cannot enter the build without a reviewed change. With pip, ",[17,444,447],{"href":445,"rel":446},"https:\u002F\u002Fpip.pypa.io\u002Fen\u002Fstable\u002Ftopics\u002Fsecure-installs\u002F",[102],"hash-checking mode"," requires every requirement to be pinned with ",[42,450,451],{},"=="," and carry a hash:",[314,454,456],{"className":316,"code":455,"language":318,"meta":319,"style":319},"pip install --require-hashes -r requirements.txt\n",[42,457,458],{"__ignoreMap":319},[323,459,460,462,464,467,469],{"class":325,"line":326},[323,461,330],{"class":329},[323,463,334],{"class":333},[323,465,466],{"class":337}," --require-hashes",[323,468,344],{"class":337},[323,470,347],{"class":333},[10,472,473,474,477,478,481,482,484],{},"With npm, ",[42,475,476],{},"npm ci"," installs exactly what ",[42,479,480],{},"package-lock.json"," says and fails if it does not match ",[42,483,44],{},". In both cases, review lockfile diffs for a changed source URL, not just a changed version.",[113,486,488],{"id":487},"treat-package-installation-as-code-execution","Treat package installation as code execution",[10,490,491,492,495,496,498,499,501],{},"Install steps run third-party code. Run them in a job that holds no deploy secrets, add ",[42,493,494],{},"--ignore-scripts"," to ",[42,497,476],{}," where your dependencies allow it, and restrict the runner's outbound network to the registries it needs. Birsan's packages and the ",[42,500,210],{}," binary both reported back over DNS, so egress rules should cover DNS too.",[23,503,505],{"id":504},"practise-dependency-confusion-in-a-lab","Practise dependency confusion in a lab",[10,507,508,509,513],{},"Module 5 of our course ",[17,510,512],{"href":511},"\u002Fcourses\u002Fdevsecops","DevSecOps: CI\u002FCD Pipeline Security"," covers dependencies hands-on, in the browser, against a real package index and CI runner:",[251,515,516,523,530],{},[34,517,518,522],{},[17,519,521],{"href":520},"\u002Fcourses\u002Fdevsecops\u002Flabs\u002Fdependency-confusion-against-a-private-package-index","Dependency confusion against a private package index",": a company pulls internal packages from a private index that falls back to a public one, and you get your package installed in its build.",[34,524,525,529],{},[17,526,528],{"href":527},"\u002Fcourses\u002Fdevsecops\u002Flabs\u002Ftyposquatting-and-install-scripts-that-run-on-install","Typosquatting and install scripts that run on install",": code execution on the runner from a name one keystroke away from a popular package.",[34,531,532,536],{},[17,533,535],{"href":534},"\u002Fcourses\u002Fdevsecops\u002Flabs\u002Flockfiles-hash-pinning-and-pinning-third-party-actions","Lockfiles, hash pinning and pinning third-party actions to a commit",": you lock and hash every dependency, then replay both attacks and watch the build refuse them.",[10,538,539],{},"The attack lab shows how little it takes. The defence lab shows the build saying no.",[541,542,543],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}",{"title":319,"searchDepth":371,"depth":371,"links":545},[546,547,552,556,557,563],{"id":25,"depth":371,"text":26},{"id":107,"depth":371,"text":108,"children":548},[549,550,551],{"id":115,"depth":377,"text":116},{"id":141,"depth":377,"text":142},{"id":155,"depth":377,"text":156},{"id":171,"depth":371,"text":172,"children":553},[554,555],{"id":175,"depth":377,"text":176},{"id":203,"depth":377,"text":204},{"id":245,"depth":371,"text":246},{"id":301,"depth":371,"text":302,"children":558},[559,560,561,562],{"id":305,"depth":377,"text":306},{"id":408,"depth":377,"text":409},{"id":438,"depth":377,"text":439},{"id":487,"depth":377,"text":488},{"id":504,"depth":371,"text":505},"supply-chain","2026-11-26","How a dependency confusion attack swaps a private package for a public one, why package managers allow it, and how to set up a private index safely.",false,"md","dependency confusion",{},"\u002Fblog\u002Fdependency-confusion",{"title":5,"description":566},"blog\u002Fdependency-confusion","2v7zwrCPhGz9bDNso6Ug-LMboMw13BKgCxHqqLF1WLc",1795688863060,"486f93ba-69c2-419b-a828-addbe3303942"]