Secret leaks often occur when a sensitive piece of authentication data is stored with the source code of an application. Considering the source
code is intended to be deployed across multiple assets, including source code repositories or application hosting servers, the secrets might get
exposed to an unintended audience.
Why is this an issue?
In most cases, trust boundaries are violated when a secret is exposed in a source code repository or an uncontrolled deployment environment.
Unintended people who don’t need to know the secret might get access to it. They might then be able to use it to gain unwanted access to associated
services or resources.
The trust issue can be more or less severe depending on the people’s role and entitlement.
What is the potential impact?
The exact consequences of a PyPI API token compromise can vary depending on the scope of the affected token. Depending on this factor, the attacker
might get access to the full account the token is bound to or only to a project belonging to that user.
In any case, such a compromise can lead to source code leaks, data leaks and even serious supply chain attacks. In general, a reputational loss is
also a common threat.
Compromise of sensitive source code
The affected service is used to store private packages and repositories. If a token is leaked, it can be used by unauthorized individuals to gain
access to your sensitive code, proprietary libraries, and other confidential resources. This can lead to intellectual property theft, unauthorized
modifications, or even sabotage of your software.
If these private packages contain other secrets, it might even lead to further breaches in the organization’s services.
Supply chain attacks
If the leaked secret gives an attacker the ability to publish code to private packages or repositories under the name of the organization, then
there may exist grave consequences beyond the compromise of source code. The attacker may inject malware, backdoors, or other harmful code into these
private repositories.
This can cause further security breaches inside the organization, but will also affect clients if the malicious code gets added to any products.
Distributing code that (unintentionally) contains backdoors or malware can lead to widespread security vulnerabilities, reputational damage, and
potential legal liabilities.
How to fix it
Revoke the secret
Revoke any leaked secrets and remove them from the application source code.
Before revoking the secret, ensure that no other applications or processes are using it. Other usages of the secret will also be impacted when the
secret is revoked.
Use a secret vault
A secret vault should be used to generate and store the new secret. This will ensure the secret’s security and prevent any further unexpected
disclosure.
Depending on the development platform and the leaked secret type, multiple solutions are currently available.
For PyPI, keyring
is a recommended solution to securely store secrets. Further explanation is given in the example below.
Code examples
Noncompliant code example
PyPI API tokens can be used to authenticate with PyPI by setting the token as a password in .pypirc
.
[pypi]
username = __token__
password = pypi-YBf3ZAIKOMPwNZ1VaQ0RAtjww5lI1az1CMLEOWgDQN56EPADfzRmgsENVcmIUh2mSBwYlTtyNKGmVlLm2MZD2aJOTWmD2EO5PMyWjvUY3Ii2CjsidALCNCNmvX8N8gcijBliFN2ciBCLgQdi2YYfGjA1kz19z1UBKg
Compliant solution
Instead, Python’s keyring
package can be used to securely authenticate with PyPI. Once
keyring
is installed using pip, it should be used for authentication automatically. It is also possible to configure it manually using
the following:
pip config set --global global.keyring-provider subprocess
Going the extra mile
Reducing the permission scope per secret
By reducing the permission scope, the token is granted only the minimum set of permissions required to perform its intended tasks. This follows the
principle of least privilege, which states that a user or token should have only the necessary privileges to carry out its specific functions. By
adhering to this principle, the potential attack surface is minimized, reducing the risk of unauthorized access or misuse of sensitive resources.
Additionally, if a token is compromised, the reduced permissions scope limits the potential damage that can be done. With fewer permissions, the
attacker’s ability to access or modify critical resources is restricted, reducing the impact of the compromise.
Resources
Documentation
Standards