Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Risks of Hardcoded Secrets | Understanding Python Vulnerabilities
Python Security Best Practices

bookRisks of Hardcoded Secrets

Hardcoding secrets, such as passwords, API keys, or cryptographic tokens, directly into your source code is a common but dangerous practice. When you embed sensitive information in your code, you increase the risk of accidental exposure. This can happen if you share your code with others, push it to a public repository, or even through code review tools that index your files. Attackers often scan public code repositories for these secrets, and once found, they can exploit them to gain unauthorized access to systems, data, or services.

12345678910
# Example of a hardcoded API key (INSECURE) import requests API_KEY = "sk_live_1234567890abcdef" # Hardcoded secret def get_user_data(user_id): url = f"https://api.example.com/users/{user_id}" headers = {"Authorization": f"Bearer {API_KEY}"} response = requests.get(url, headers=headers) return response.json()
copy

In the code above, the API key is directly written into the script. If this file is ever committed to a version control system like Git, anyone with access to the repository could retrieve the API key. Even private repositories are not immune—secrets can still leak through accidental sharing, backups, or misconfigured permissions. Hardcoded secrets are also difficult to rotate or revoke, since you must update every copy of the code and ensure all deployments are synchronized. This makes your systems more vulnerable to attacks and complicates incident response.

123456789101112
# Secure pattern: using environment variables (simulated here with a variable assignment) import requests import os # Simulate retrieving the API key from an environment variable API_KEY = os.getenv("API_KEY", "REPLACE_ME") # In practice, set API_KEY in your environment def get_user_data(user_id): url = f"https://api.example.com/users/{user_id}" headers = {"Authorization": f"Bearer {API_KEY}"} response = requests.get(url, headers=headers) return response.json()
copy

By retrieving the API key from an environment variable rather than hardcoding it, you keep sensitive information out of your source code. This approach makes it much less likely that secrets will be accidentally exposed through code sharing or version control. It also allows you to manage and rotate secrets independently of your codebase, reducing operational risk and improving your security posture.

Note
Definition

In software security, "secrets" are pieces of sensitive information—such as passwords, API keys, cryptographic keys, or tokens—that grant access to protected resources or systems. Keeping secrets secure is critical to preventing unauthorized access and data breaches.

1. Why should secrets never be hardcoded in source code?

2. What is a safer alternative to hardcoding secrets?

question mark

Why should secrets never be hardcoded in source code?

Select the correct answer

question mark

What is a safer alternative to hardcoding secrets?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5.56

bookRisks of Hardcoded Secrets

Svep för att visa menyn

Hardcoding secrets, such as passwords, API keys, or cryptographic tokens, directly into your source code is a common but dangerous practice. When you embed sensitive information in your code, you increase the risk of accidental exposure. This can happen if you share your code with others, push it to a public repository, or even through code review tools that index your files. Attackers often scan public code repositories for these secrets, and once found, they can exploit them to gain unauthorized access to systems, data, or services.

12345678910
# Example of a hardcoded API key (INSECURE) import requests API_KEY = "sk_live_1234567890abcdef" # Hardcoded secret def get_user_data(user_id): url = f"https://api.example.com/users/{user_id}" headers = {"Authorization": f"Bearer {API_KEY}"} response = requests.get(url, headers=headers) return response.json()
copy

In the code above, the API key is directly written into the script. If this file is ever committed to a version control system like Git, anyone with access to the repository could retrieve the API key. Even private repositories are not immune—secrets can still leak through accidental sharing, backups, or misconfigured permissions. Hardcoded secrets are also difficult to rotate or revoke, since you must update every copy of the code and ensure all deployments are synchronized. This makes your systems more vulnerable to attacks and complicates incident response.

123456789101112
# Secure pattern: using environment variables (simulated here with a variable assignment) import requests import os # Simulate retrieving the API key from an environment variable API_KEY = os.getenv("API_KEY", "REPLACE_ME") # In practice, set API_KEY in your environment def get_user_data(user_id): url = f"https://api.example.com/users/{user_id}" headers = {"Authorization": f"Bearer {API_KEY}"} response = requests.get(url, headers=headers) return response.json()
copy

By retrieving the API key from an environment variable rather than hardcoding it, you keep sensitive information out of your source code. This approach makes it much less likely that secrets will be accidentally exposed through code sharing or version control. It also allows you to manage and rotate secrets independently of your codebase, reducing operational risk and improving your security posture.

Note
Definition

In software security, "secrets" are pieces of sensitive information—such as passwords, API keys, cryptographic keys, or tokens—that grant access to protected resources or systems. Keeping secrets secure is critical to preventing unauthorized access and data breaches.

1. Why should secrets never be hardcoded in source code?

2. What is a safer alternative to hardcoding secrets?

question mark

Why should secrets never be hardcoded in source code?

Select the correct answer

question mark

What is a safer alternative to hardcoding secrets?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4
some-alt