security/api-keys Correctness ​
What it does ​
Disallows hard-coded API keys and other credentials.
Why is this bad? ​
Hard-coding API keys and committing them to source control is a serious security risk.
- If your code is leaked, attackers can use your API keys to access your services and data.
- Accidental bundling of API keys can lead them to be exposed publicly in your website, compriming your services.
- Any developer or contractor you hire will have access to your services, even after they lose access to your codebase.
- Even after being deleted, they will be visible in your git repo's commit history.
- Key rotation requires a code change and redeployment, and can therefore not be handled by security teams or by automated systems.
- Many, many more reasons.
ts
const API_KEY = "abcdef123456";
const data = await fetch("/api/some/endpoint", {
headers: {
Authorization: `Bearer ${API_KEY}`,
},
});
What To Do Instead ​
WARNING
The Oxc team are not security experts. We do not endorse any particular key management service or strategy. Do your research and choose the best solution/architecture for your use case.
One possible alternative is to store secrets in a secure secrets manager (such as AWS KMS, HashiCorp Vault, Pangea, etc.) and request them when your application starts (e.g. a Docker container, an EC2).
Examples ​
Examples of incorrect code for this rule:
js
const AWS_ACCESS_KEY_ID = "AKIA1234X678C123B567";
const OPENAI_API_KEY = "sk_test_1234567890";
Examples of correct code for this rule:
js
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const OPENAI_API_KEY = await getSecret("open-ai-api-key");