[kind, pattern]. Kind is what LIVE-005's severity follows: payment, cloud, ai or other. */ private const PATTERNS = [ 'Stripe secret key' => [ 'payment', '/\b[rs]k_live_[0-9a-zA-Z]{20,247}\b/' ], 'Square access token' => [ 'payment', '/\b(?:sq0csp-[0-9A-Za-z_-]{43}|EAAA[A-Za-z0-9_-]{60})\b/' ], 'PayPal access token' => [ 'payment', '/\baccess_token\$production\$[0-9a-z]{16}\$[0-9a-f]{32}\b/' ], 'AWS access key' => [ 'cloud', '/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/' ], 'Private key' => [ 'cloud', '/-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY(?: BLOCK)?-----/' ], 'OpenAI key' => [ 'ai', '/\bsk-(?:proj-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}T3BlbkFJ[A-Za-z0-9_-]{20,}/' ], 'Anthropic key' => [ 'ai', '/\bsk-ant-(?:api|admin)\d{2}-[A-Za-z0-9_-]{80,}/' ], 'GitHub token' => [ 'other', '/\b(?:gh[pousr]_[A-Za-z0-9]{36,255}|github_pat_[A-Za-z0-9_]{60,255})\b/' ], 'GitLab token' => [ 'other', '/\bglpat-[A-Za-z0-9_-]{20,}/' ], 'Slack token' => [ 'other', '/\bxox[abposr]-[A-Za-z0-9-]{10,200}/' ], 'Slack webhook' => [ 'other', '#https://hooks\.slack\.com/services/T[A-Z0-9]{6,}/B[A-Z0-9]{6,}/[A-Za-z0-9]{20,}#' ], 'SendGrid key' => [ 'other', '/\bSG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b/' ], ]; /** * The secrets in a text, each with its line and masked form, never the secret itself. * * @return array */ public static function find( string $text ): array { $hits = []; foreach ( self::PATTERNS as $name => $rule ) { if ( ! preg_match_all( $rule[1], $text, $matches, PREG_OFFSET_CAPTURE ) ) { continue; } foreach ( $matches[0] as $match ) { if ( ! self::real( (string) $match[0] ) ) { continue; } if ( 0 === strpos( (string) $match[0], '-----BEGIN' ) && ! self::key_body_follows( $text, (int) $match[1] + strlen( (string) $match[0] ) ) ) { continue; } $hits[] = [ 'name' => $name, 'kind' => $rule[0], 'line' => 1 + substr_count( $text, "\n", 0, (int) $match[1] ), 'masked' => self::mask( (string) $match[0] ), ]; if ( count( $hits ) >= self::MAX_HITS ) { return $hits; } } } usort( $hits, static function ( array $a, array $b ): int { return $a['line'] <=> $b['line']; } ); return $hits; } /** * A private key's whole block, body included, up to its END line or the end of the text. PATTERNS * matches only the header, which is enough to report a key but would leave its body in evidence. */ private const KEY_BLOCK = '/-----BEGIN [A-Z ]*PRIVATE KEY(?: BLOCK)?-----.*?(?:-----END [A-Z ]*PRIVATE KEY(?: BLOCK)?-----|$)/s'; /** * Values given to settings named like secrets, masked in evidence though never reported on their own: * DB_PASSWORD or AUTH_KEY in wp-config.php, a .env line, a JSON or array key. The first group is the * name and what leads to the value, the second the value. */ private const NAMED = [ '/\b((?:(?:[A-Za-z0-9]+_)*(?:PASSWORD|PASSWD|SECRET|TOKEN)|(?:[A-Za-z0-9]+_)+(?:KEY|SALT)|APIKEY)[\'"]?\s*(?:,|=>|=|:)\s*[\'"])([^\'"]{5,})/i', '/\b((?:(?:[A-Za-z0-9]+_)*(?:PASSWORD|PASSWD|SECRET|TOKEN)|(?:[A-Za-z0-9]+_)+(?:KEY|SALT)|APIKEY)=)([^\s\'"]{5,})/i', ]; /** * The text with every secret in it masked, for evidence (R6). */ public static function mask_all( string $text ): string { $mask = static function ( array $m ): string { return self::mask( $m[0] ); }; $text = (string) preg_replace_callback( self::KEY_BLOCK, $mask, $text ); foreach ( self::PATTERNS as $rule ) { $text = (string) preg_replace_callback( $rule[1], $mask, $text ); } foreach ( self::NAMED as $pattern ) { $text = (string) preg_replace_callback( $pattern, static function ( array $m ): string { return $m[1] . self::mask( $m[2] ); }, $text ); } return $text; } /** * The first 4 characters and an ellipsis. */ public static function mask( string $secret ): string { return substr( $secret, 0, 4 ) . '…'; } /** * Whether a private key's body follows its header: at least 200 characters of base64, over lines that * may be quoted, written with "\n" escapes or broken with
tags, after any armour headers * ("Proc-Type: …", "Version: …"). A header alone is how code that parses keys, and tutorials, name a * key; it isn't one. */ private static function key_body_follows( string $text, int $offset ): bool { $after = str_replace( [ '\\/', '\\r', '\\n' ], [ '/', "\n", "\n" ], substr( $text, $offset, 4000 ) ); $after = (string) preg_replace( '##i', "\n", $after ); $lines = preg_split( '/\R/', $after ); $body = ''; foreach ( false === $lines ? [] : $lines as $line ) { $line = trim( $line, " \t'\",.;+" ); if ( '' === $line || false !== strpos( $line, ':' ) ) { continue; } if ( 1 !== preg_match( '#^[A-Za-z0-9+/=]+$#', $line ) ) { return false; } $body .= $line; if ( strlen( $body ) >= 200 ) { return true; } } return false; } /** * Leaves out documentation examples and placeholders: AWS's AKIAIOSFODNN7EXAMPLE, and runs like * sk_live_xxxxxxxxxxxxxxxxxxxxxxxx. */ private static function real( string $found ): bool { if ( false !== stripos( $found, 'EXAMPLE' ) ) { return false; } if ( 0 === strpos( $found, '-----BEGIN' ) ) { return true; } $tail = (string) preg_replace( '/^[A-Za-z]+[_-](?:[a-z]+[_-])?/', '', $found ); return count( array_unique( str_split( $tail ) ) ) >= 8; } }