Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan

The conversation on API security has moved from classic OWASP Top 10 items like injection and broken auth to a very specific, hot problem: API tokens leaking through build pipelines, environment variables, and third‑party SDKs. In the last six months GitHub, GitLab, and even major cloud providers have published breach reports showing that a single exposed token can grant an attacker unfettered access to internal services, data lakes, and even production databases.
Developers love the convenience of process.env.API_KEY or AWS_SECRET_ACCESS_KEY baked into their serverless functions, but that convenience is a double‑edged sword. When a repo is forked, a CI job fails, or a log file is inadvertently shipped to a public bucket, the token becomes public faster than any traditional vulnerability scan can flag it.
Hot take: Treat API secrets like code, not configuration. If you would run a static analysis tool on your source, you should run the same on your secrets.
Most security audits still focus on how an API validates a request (OAuth scopes, JWT signatures, rate limiting). Those checks are fine if the attacker can’t simply become a legitimate client. When a token is leaked, the attacker bypasses every layer because the request looks exactly like a trusted internal call.
Think of it like a badge system in a corporate office. If the badge printer is compromised, the security guard at the door is powerless – the badge is valid. The same logic applies to API keys: once the key is out, the API sees a perfectly valid credential.
.env containing PAYMENT_API_KEY to a public GitHub repo.The post‑mortem revealed that the team relied on "security through obscurity" – they assumed the key would never be accessed because it lived only in the CI environment. Reality proved otherwise.
Environment variables are not a vault. They are simple key/value pairs that any process with the right permissions can read. In serverless platforms like AWS Lambda, the entire environment is visible to anyone who can invoke the GetFunctionConfiguration API – a privilege many internal services already have.
My opinion: The moment you write process.env.SECRET in code, you have introduced a hard‑coded secret risk. Replace that with a call to a secret manager at runtime.
AWS example:
javascriptconst { SecretsManagerClient, GetSecretValueCommand } = require("@aws-sdk/client-secrets-manager");
const client = new SecretsManagerClient({ region: "us-east-1" });
async function getApiKey() {
const command = new GetSecretValueCommand({ SecretId: "prod/paymentApiKey" });
const response = await client.send(command);
return JSON.parse(response.SecretString).key;
}
// Use the key at request time, never store it in memory longer than needed
Google Cloud example (Node.js):
javascriptconst { SecretManagerServiceClient } = require("@google-cloud/secret-manager");
const client = new SecretManagerServiceClient();
async function fetchKey() {
const [version] = await client.accessSecretVersion({ name: "projects/123/secrets/payment-key/versions/latest" });
return version.payload.data.toString();
}
Both snippets fetch the secret just before making the API call, keeping the key out of logs and environment dumps.
Give the Lambda execution role only secretsmanager:GetSecretValue for the specific secret. Do NOT grant * or broad secretsmanager:* permissions. This limits the blast radius if the function is compromised.
Add a step to your pipeline that runs a secret‑detection tool. Example with git-secrets:
bash#!/bin/bash
# .gitlab-ci.yml snippet
secret_scan:
stage: test
image: alpine:latest
script:
- apk add --no-cache git curl
- curl -sSL https://github.com/awslabs/git-secrets/archive/refs/heads/master.zip -o /tmp/git-secrets.zip
- unzip /tmp/git-secrets.zip -d /tmp && chmod +x /tmp/git-secrets-master/git-secrets
- /tmp/git-secrets-master/git-secrets --scan -r .
Automate rotation using the secret manager's built‑in versioning. For example, AWS Secrets Manager can rotate a secret on a 30‑day schedule, automatically updating the Lambda's reference without code changes.
These tools are converging on a single goal: make secret leakage detectable before it becomes exploitable.
API security is no longer just about validating the request; it’s about protecting the credentials that validate the request. The industry is finally waking up to the fact that a single leaked token can be more devastating than a classic injection flaw.
Takeaway checklist:
Implement these, and you’ll turn the most talked‑about API vulnerability of 2024 into a non‑issue. The next wave of breaches will be judged by how quickly you can revoke a compromised key, not by whether you have a firewall.
Feel free to argue – I’ll gladly debate any claim that “environment variables are safe enough”. The data says otherwise, and the community should stop treating secrets like an afterthought.