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-JWT-SEC-001 --project .About This Rule
Understanding the vulnerability and how it is detected
This rule detects jwt.encode() calls in your codebase where the signing secret might be hardcoded. A hardcoded JWT secret means anyone who reads your source code -- a contractor, an ex-employee, anyone with access to your Git history -- can forge valid tokens for any user. It also means you can't rotate the secret without redeploying every service that uses it.
The rule works by matching all jwt.encode() calls through type-aware resolution of the PyJWT library. It currently operates as an audit-level rule, meaning it flags every jwt.encode() call for manual review. This is because the engine cannot yet distinguish between a string literal argument (hardcoded) and a variable reference (potentially safe). Future versions will add an is_literal() qualifier to filter only calls where the secret argument is a string literal.
In practice, the fix is simple: move the secret to an environment variable, a secrets manager, or a configuration file that isn't checked into version control. For distributed systems, consider asymmetric signing (RS256/ES256) where the private key never leaves the signing service.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Token Forgery
Anyone with the secret can create valid JWTs. An attacker who finds the secret in your Git history, a Docker image layer, or a leaked backup can issue tokens with any claims -- admin roles, any user ID, unlimited expiration.
No Secret Rotation
Hardcoded secrets can't be rotated without a code change and redeployment. If the secret is compromised, you need to deploy new code to every service. With an environment variable or secrets manager, rotation is an ops task.
Credential Leakage via Source Code
Source code gets shared -- open source, contractor handoffs, CI logs, error messages. A secret in code is a secret waiting to leak. Environment variables and secrets managers keep credentials out of the codebase entirely.
Compliance Violations
PCI DSS, SOC 2, and ISO 27001 all prohibit hardcoded credentials. Auditors specifically look for secrets in source code. This finding will fail a compliance review.
How to Fix
Recommended remediation steps
- 1Move JWT signing secrets to environment variables or a secrets manager like AWS Secrets Manager, HashiCorp Vault, or GCP Secret Manager
- 2Use asymmetric algorithms (RS256, ES256) for distributed systems so the signing key never leaves the auth service
- 3Rotate secrets periodically and ensure your architecture supports rotation without downtime
- 4Add pre-commit hooks or CI checks to prevent secrets from being committed to version control
- 5Never log or include JWT secrets in error messages, stack traces, or debug output
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule matches all jwt.encode() calls resolved through the PyJWT library using QueryType-based type inference. It identifies the jwt module by its fully qualified name and matches the encode method. Currently operates as an audit rule that flags all jwt.encode() calls for review.
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
JWT None Algorithm
Detects jwt.encode() calls using algorithm='none', which creates unsigned tokens that anyone can forge.
JWT Exposed Credentials
Detects jwt.encode() calls where passwords or secrets may be included in the token payload, exposing them to anyone who reads the token.
Flask SQL Injection via Tainted String
Finds user input reaching raw SQL queries in Flask apps where parameterized queries should be used instead.
Frequently Asked Questions
Common questions about JWT Hardcoded Secret
New feature
Get these findings posted directly on your GitHub pull requests
The JWT Hardcoded Secret rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.