Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-102 --project .About This Rule
Understanding the vulnerability and how it is detected
Python functions can define default values for parameters. When a parameter name suggests a credential — such as password, passwd, pwd, secret, api_key, token, auth_key, or passphrase — and the default value is a non-empty string literal, the credential is hardcoded into the source code.
Hardcoded credentials are a severe security risk because the source code may be stored in version control, distributed as part of a package, included in error messages, or accessed by developers who should not have the credential. Even if the repository is private, any developer with read access to the code gains the credential, and the credential cannot be rotated without a code change and deployment cycle.
Default function arguments with hardcoded credentials are especially dangerous because callers may rely on the default without realizing it is a real credential, and the credential persists in git history even after it is removed from the current codebase.
Credentials should always be loaded from environment variables, secret management systems, or configuration files outside the source tree. Default values for credential parameters should be None, forcing the caller to explicitly provide a value.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Credential Exposure in Version Control
Hardcoded credentials committed to version control are visible to all developers with repository access. Once committed, the credential remains in git history permanently even if the line is later deleted. Public repository exposure makes the credential available to the entire internet within minutes of the first push.
Shared Credentials Across All Deployments
A hardcoded default credential is identical across every deployment — development, staging, and production. Compromise of any single environment or developer machine gives the attacker the production credential without needing to target production specifically.
Silent Use of Default Credentials
Callers who do not explicitly pass the credential parameter silently use the hardcoded default. This can result in production systems connecting to databases or external services with a well-known default password that was intended only for local development, with no visible indication of the misconfiguration.
Inability to Rotate Credentials
Hardcoded credentials cannot be rotated without a code change and full deployment cycle. During a security incident requiring immediate credential rotation, this dependency on code deployment introduces dangerous delay. Dynamic credential loading from environment variables or secret managers allows rotation without any code change.
How to Fix
Recommended remediation steps
- 1Never use string literals as default values for parameters named password, passwd, pwd, secret, api_key, token, auth, or similar credential names.
- 2Use None as the default value for credential parameters and load the real value from os.environ or a secret manager when None is received.
- 3Require credential parameters explicitly by using keyword-only arguments without defaults, forcing all callers to provide the credential from their own environment.
- 4Audit git history for previously hardcoded credentials and rotate any that were exposed, even if they have since been removed from the codebase.
- 5Use environment-specific configuration files (not committed to version control) or a dedicated secrets manager such as HashiCorp Vault, AWS Secrets Manager, or Azure Key Vault.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects Python function definitions where a parameter whose name matches credential patterns (password, passwd, pwd, secret, api_key, apikey, token, auth_key, passphrase, private_key) has a non-empty string literal as its default value. Both regular function definitions and class method definitions are checked. The rule does not flag parameters whose default is None, an empty string, or a non-string literal.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Insecure File Permissions via os.chmod
os.chmod() or os.fchmod() sets overly permissive file permissions that allow unauthorized read, write, or execute access.
Logger Credential Leak Risk
Detects logging calls (info, debug, warning, error, critical) that should be audited for accidental credential or secret leakage in log output.
Django Command Injection via os.system()
User input flows to os.system(), enabling arbitrary OS command execution with the privileges of the Django process.
Frequently Asked Questions
Common questions about Hardcoded Password in Default Function Argument
New feature
Get these findings posted directly on your GitHub pull requests
The Hardcoded Password in Default Function Argument rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.