Use of Hard-coded Credentials
The product contains hard-coded credentials, such as a password or cryptographic key, which it uses for its own inbound authentication, outbound communication to external components, or encryption of internal data.
Hard-coded credentials typically create a significant hole that allows an attacker to bypass the authentication that has been configured by the product administrator. This hole might be difficult for the system administrator to detect.
So behebst du diese Schwachstelle
Präventionsstrategien für Hardcoded Credentials basierend auf 11 Shoulder-Erkennungsregeln.
Load SECRET_KEY from environment variables, never commit it to source control
# settings.py - SECRET_KEY = 'django-insecure-abc123def456' + import os + + SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
Store all credentials in environment variables or a secrets manager, never in code
- password = "super_secret_password" - api_key = "sk-abc123456789xyz" - db_password = "db_p@ssw0rd_2024" + import os + + password = os.environ['APP_PASSWORD'] + api_key = os.environ['API_KEY'] + db_password = os.environ['DB_PASSWORD']
Load all secrets from environment variables or a secrets manager
- SECRET_KEY = 'django-insecure-abc123def456' - API_KEY = 'sk-proj-abc123456789' - DATABASE_PASSWORD = 'super_secret_123' + import os + + SECRET_KEY = os.environ['SECRET_KEY'] + API_KEY = os.environ['API_KEY'] + DATABASE_PASSWORD = os.environ['DB_PASSWORD']
Use BuildKit secrets or runtime environment variables instead of hardcoded credentials
- FROM node:24-alpine - ENV DATABASE_PASSWORD=supersecret123 - ARG API_KEY=sk_live_abc123 - WORKDIR /app + # syntax=docker/dockerfile:1 + FROM node:24-alpine + WORKDIR /app + RUN --mount=type=secret,id=db_pass \ + cat /run/secrets/db_pass > /dev/null COPY . .
Load secrets from environment variables or a secrets manager instead of hardcoding
package main - const ( - APIKey = "sk-1234567890abcdefghijklmnop" - DBPassword = "superSecretPassword123" - ) - - func connectDB() (*sql.DB, error) { - connStr := "postgres://admin:superSecretPassword123@localhost:5432/db" + import "os" + + func connectDB() (*sql.DB, error) { + apiKey := os.Getenv("API_KEY") + if apiKey == "" { + log.Fatal("API_KEY not set") + } + dbPass := os.Getenv("DB_PASSWORD") + connStr := fmt.Sprintf("postgres://admin:%s@localhost:5432/db", dbPass) return sql.Open("postgres", connStr) }
Never use hardcoded fallbacks for secrets; fail fast if environment variables are missing
- const JWT_SECRET = process.env.JWT_SECRET || 'my-insecure-secret-key'; + function getRequiredEnv(name) { + const value = process.env[name]; + if (!value) throw new Error(`Required env var ${name} is not set`); + return value; + } + const JWT_SECRET = getRequiredEnv('JWT_SECRET');
Load credentials from environment variables instead of hardcoding in source code
- const connection = mysql.createConnection({ - host: 'localhost', - user: 'root', - password: 'admin123', - database: 'myapp' + require('dotenv').config(); + const connection = mysql.createConnection({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME });
Move secrets to environment variables using dotenv or a secret manager
- const apiKey = 'sk_live_abc123def456ghi789'; + require('dotenv').config(); + const apiKey = process.env.STRIPE_API_KEY;
Use Kubernetes Secrets with secretKeyRef instead of hardcoding credentials in manifests
apiVersion: v1 kind: Pod spec: containers: - name: app env: - name: DB_PASSWORD - value: "super-secret-password" + valueFrom: + secretKeyRef: + name: db-secret + key: password
Wichtige Praktiken
- loaded from environment variables or secure secret management systems
- stored in environment variables or secure vaults
- stored in environment variables or secure vaults, never committed to version control
Finden Sie Schwachstellen in Ihrem Code
Verwenden Sie Shoulder, um Ihren Code nach Use of Hard-coded Credentials-Mustern zu scannen. 11 Regeln.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=798 # Or scan entire project npx @shoulderdev/cli trust .
Erkennungsregeln (11)
Worauf bei Code-Reviews zu achten ist
Diese Muster weisen auf potenzielle Use of Hard-coded Credentials-Schwachstellen hin. Achten Sie bei Code-Reviews und Sicherheitsaudits darauf.
Scanne deine Codebasis nach Use of Hard-coded Credentials
Shoulder CLI findet anfällige Muster in deiner gesamten Codebasis.