uv: Prevent fallback to PyPi when private index returns 401/403 (to mitigate dependency confusion risk)

I’m using uv with a setup where a private package index requires both authentication and a VPN connection. My intention is to always prefer the private index and only fall back to PyPI if the package truly does not exist on the private index.

This works as expected when the VPN and credentials are valid.

However, in cases where the VPN is disconnected or credentials are invalid (resulting in a 401 or 403), uv silently falls back to PyPI if the package is found there. This exposes a potential dependency confusion attack, where a malicious package on PyPI could be installed if the private index is temporarily unavailable.

Ideal behavior: If the private index returns a 401 or 403, I would expect uv to not fall back to PyPI. Instead, it should treat this as a hard failure and return an error that it couldn’t reach index 401/403.

I’m not 100% sure if this should be a feature request or a bug report. After reading the Package Indexes documentation I assumed it would work as as explained above. However if this is intentional, could an alternative be a stricter --index-strategy with this behaviour?

Current setup: uv.toml

[[index]]
url = "https://private-repo.com/simple"
authenticate = "always"

.netrc

machine private-repo.com
    login user
    password PW

Example behaviour:

  1. Private index unavailable and package not on pypi
$ uv pip install private-package-not-on-pypi
× No solution found when resolving dependencies:
╰─▶ Because private-package-not-on-pypi was not found in the package registry and you require it, your requirements are unsatisfiable.

hint: An index URL (https://private-repo.com/simple) could not be queried due to a lack of valid authentication credentials (401 Unauthorized).
  1. Private index unavailable and package also on pyp
$ uv pip install private-package-also-on-pypi
→ Installs package from PyPI silently

Platform

Linux 6.11.0-19-generic x86_64 GNU/Linux

Version

0.6.9

Python version

Python 3.12.9

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@bendikhk I am actually working on a design to address this very problem. I hope to begin work on the implementation in the near future.

Yeah, default = true also solves this problem. Some users need PyPI for some of their packages still.

@bendikhk I am actually working on a design to address this very problem. I hope to begin work on the implementation in the near future.

Great! Let me know if I can help with some testing! 😃

Hi again,

I see now that this is properly documented in the documentation, which is good!

If the discovered credentials are not valid (i.e., the index returns a HTTP 401 or 403), then uv will treat packages as unavailable and query the next configured index as described in the index strategy section.

However, the current implementation is still a huge risk factor for us. If someone forgets to enable vpn, or have an invalid password they are open for “dependency confusion” attacks. And I would still argue that with the “first-index” strategy and authenticate always it should fail hard when not being able to access the index. Are there any plans to change this behavior in the near future, or perhaps introduce a setting that allows users to opt into stricter index access checks for cases like ours?

Thanks for providing these logs. Would you mind also sharing your pyproject.toml?

In the examples above I only used uv pip install simplecalc without any pyproject.toml.

I’ve added the code to reproduce different scenarios here