PC-006CRITICALSecrets & API Keys

PEM / RSA / EC Private Key Header in Source

Detects embedded PEM-encoded cryptographic private keys (RSA, ECDSA, DSA, OpenSSH, and PKCS#8) pasted inside source files, environment configs, or test fixtures.

Rule Slug
private-cryptographic-key-block
Affected Files
*
Detection Method
Deterministic Regex
Live Rule Tester100% Browser-Local
CRITICAL: Detected 1 violation
Line 2:-----BEGIN RSA PRIVATE KEY-----(entropy: 3.382)

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
// auth/jwtSigner.ts
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA0Z3VS5JJcds3xfn/ygWyF8PbnGy0AHB7Mhg...
-----END RSA PRIVATE KEY-----`;
Recommended Fix
// auth/jwtSigner.ts
const encodedKey = process.env.JWT_PRIVATE_KEY_BASE64;
if (!encodedKey) {
  throw new Error('JWT_PRIVATE_KEY_BASE64 is required');
}

export const PRIVATE_KEY = Buffer.from(encodedKey, 'base64').toString('utf8');

Why This Fails in Production

Cryptographic private keys form the mathematical foundation of TLS transport encryption, SSH server authentication, JWT RS256/ES256 token signing, GitHub App authentication, and mutual TLS (mTLS) service-to-service identity. Embedding a PEM private key block (-----BEGIN PRIVATE KEY-----) inside application source code, JSON configuration files, or unignored environment variables completely breaks the secrecy guarantee of asymmetric cryptography.

The consequences depend on what the private key signs or protects. A leaked TLS certificate private key enables man-in-the-middle decryption and domain impersonation until the certificate authority revokes the certificate. A leaked OpenSSH private key grants direct interactive shell access to production Linux servers, bastion hosts, or deployment targets. A leaked JWT signing key permits any attacker to forge cryptographically valid administrator session tokens with arbitrary user IDs and roles, bypassing authentication without ever touching your database.

Developers often paste multi-line PEM keys into source code or JSON service-account blobs when troubleshooting newline escaping issues ( ) in cloud environment variables. Even if the key is labeled for staging or internal microservices, shared certificate authorities or reused SSH keys across environments frequently allow lateral movement into production systems.

Private key material must live exclusively inside dedicated hardware security modules (KMS/CloudHSM), mounted read-only secret volumes, or encrypted runtime secret stores, never inside a Git working tree.

Remediation Steps

  1. 1Immediately revoke the associated TLS certificate, remove the corresponding public key from ~/.ssh/authorized_keys on all hosts, or rotate the JWKS/signing key pair.
  2. 2Verify server auth logs (/var/log/auth.log) and JWT token issuance logs for unauthorized logins or forged claims.
  3. 3Load private keys at runtime from a secret manager (AWS KMS, GCP Secret Manager, Vault) or base64-encoded environment variables outside version control.
  4. 4Ensure *.pem, *.key, id_rsa, and id_ed25519 patterns are explicitly listed in your root .gitignore.

Frequently Asked Questions

Are public keys (-----BEGIN PUBLIC KEY-----) flagged by this rule?
No. Public keys and X.509 certificates (-----BEGIN CERTIFICATE-----) are safe to distribute and are intentionally ignored by this rule, which only matches PRIVATE KEY headers.
What if the private key is passphrase-protected (ENCRYPTED PRIVATE KEY)?
Committing encrypted private keys is still a critical risk because offline brute-force tools can crack weak passphrases rapidly once the ciphertext is in an attacker’s possession.
How should I store multi-line PEM keys in environment variables?
Base64-encode the PEM file when storing it in your hosting provider’s secret store, then decode it into UTF-8 memory at application startup.

Related Secrets & API Keys Rules

Scan local project folder →