REGEXVAULTv2.0
Security/API Keys & Tokens
Verified Safe

AWS Secret Access Key Regex for JavaScript

/(?:aws_secret_access_key|AWS_SECRET_ACCESS_KEY|secret.?access.?key)(?:[\s=:"']+)([A-Za-z0-9/+=]{40})(?:["'\s]|$)/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching aws secret access key, ported and verified for JavaScript. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.

Javascript Implementation

Javascript
// AWS Secret Access Key
// ReDoS-safe | RegexVault — Security > API Keys & Tokens

const awsSecretAccessKeyRegex = /(?:aws_secret_access_key|AWS_SECRET_ACCESS_KEY|secret.?access.?key)(?:[\s=:"']+)([A-Za-z0-9\/+=]{40})(?:["'\s]|$)/i;

function validateAwsSecretAccessKey(input: string): boolean {
  return awsSecretAccessKeyRegex.test(input);
}

// Example
console.log(validateAwsSecretAccessKey("aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")); // true

Test Cases

Matches (Valid)
Rejects (Invalid)
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYsecret_key = short
AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"aws_access_key_id = AKIAIOSFODNN7EXAMPLE

When to use this pattern

This pattern is drawn from the Security > API Keys & Tokens category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

AWS Secret Access Keys in .env files committed to Git are the single most common cloud security breach vector. Use AWS Secrets Manager or Parameter Store instead of .env files. GitGuardian, TruffleHog, and git-secrets scan for these.

Technical Notes

Context-aware pattern — requires the key name label to be present (common in config files, environment variables, and .env files). The secret itself is 40 base64 characters. Bare 40-char base64 strings without the label context are too broad to match reliably.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern