Transactional Email or SMS API Key Exposed
Identifies hardcoded SendGrid API keys (SG.xxx.yyy) and Twilio API Key SIDs (SK + 32 hex characters) exposed in backend services, serverless functions, or scripts.
- Rule Slug
- sendgrid-mailgun-twilio-key
- Affected Files
- *
- Detection Method
- Regex + Entropy (>= 3.4)
Vulnerable Pattern vs. Hardened Fix
// mail/mailer.ts
import sgMail from '@sendgrid/mail';
sgMail.setApiKey('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');// mail/mailer.ts
import sgMail from '@sendgrid/mail';
const apiKey = process.env.SENDGRID_API_KEY;
if (!apiKey) {
throw new Error('SENDGRID_API_KEY is not configured');
}
sgMail.setApiKey(apiKey);Why This Fails in Production
Transactional messaging providers such as SendGrid and Twilio sit at the core of account recovery, two-factor authentication (2FA), billing receipts, and domain reputation. A leaked SendGrid API key (matching the SG.<22-char>.<43-char> structure) allows external attackers to send authenticated emails using your verified SPF, DKIM, and DMARC domain alignment. Because those spoofed messages pass domain authentication checks, phishing emails sent through your SendGrid account land straight in recipients’ primary inboxes while permanently damaging your domain’s sender reputation.
Similarly, exposed Twilio API keys (SK...) paired with their secret or account SID enable attackers to perform SMS pumping (toll fraud) against premium-rate international numbers, draining thousands of dollars in prepaid balances overnight. If the compromised messaging account also exposes message logs or inbound webhook settings, attackers can intercept OTP verification codes sent to your users and hijack customer accounts directly.
Email and SMS credentials are frequently leaked inside quick notification scripts, forgotten cron jobs, or `.env` files accidentally committed during rapid MVP launches. When messaging providers detect spam spikes or public credential leaks, they often suspend the entire sending account immediately, halting password resets, login magic links, and transactional order confirmations for legitimate customers.
Always scope SendGrid keys to strictly the Mail Send permission (never Full Access), restrict Twilio geo-permissions to countries where your users actually reside, and inject all keys exclusively via encrypted environment variables.
Remediation Steps
- 1Delete and regenerate the exposed key immediately in SendGrid (Settings -> API Keys) or the Twilio Console (Account -> API keys & tokens).
- 2Audit SendGrid Email Activity and Twilio Programmable Messaging logs for unauthorized sends, bounced phishing campaigns, or international SMS spikes.
- 3Replace hardcoded credentials with process.env.SENDGRID_API_KEY or process.env.TWILIO_API_KEY injected at runtime.
- 4Enable SendGrid IP Access Management and Twilio SMS Geographic Permissions to limit blast radius.
Frequently Asked Questions
- Why does a leaked SendGrid key hurt my domain even after I revoke it?
- If spammers send thousands of phishing emails using your verified DKIM domain before you revoke the key, mailbox providers like Gmail and Outlook can blacklist your domain and IP pool, causing legitimate transactional emails to land in spam for weeks.
- Should I use a Full Access SendGrid key for my web app?
- No. Always create a Restricted Access key limited exclusively to Mail Send so a leaked key cannot export contact lists, alter DNS settings, or create subusers.
- How can I protect against Twilio SMS toll fraud?
- Keep Twilio API keys strictly in server environment variables, disable unused international destinations under Twilio Geo Permissions, and enforce rate limits plus CAPTCHA on OTP endpoints.