AWS Secret Access Key Regex for PHP
/(?:aws_secret_access_key|AWS_SECRET_ACCESS_KEY|secret.?access.?key)(?:[\s=:"']+)([A-Za-z0-9/+=]{40})(?:["'\s]|$)/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching aws secret access key, ported and verified for PHP. 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 PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// AWS Secret Access Key
// ReDoS-safe | RegexVault — Security > API Keys & Tokens
define('AWS_SECRET_ACCESS_KEY_PATTERN', '/(?:aws_secret_access_key|AWS_SECRET_ACCESS_KEY|secret.?access.?key)(?:[\s=:"\']+)([A-Za-z0-9\/+=]{40})(?:["\'\s]|$)/');
function validate_aws_secret_access_key(string $input): bool {
return (bool) preg_match(AWS_SECRET_ACCESS_KEY_PATTERN, $input);
}
// Example
var_dump(validate_aws_secret_access_key("aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY | secret_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 PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. 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