rules:
  - id: javascript-use-of-md5-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createHash('md5')
          - pattern: crypto.createHash("md5")
    message: >-
      MD5 is cryptographically broken and quantum-weakened by Grover's algorithm -- effective security
      drops from 128 to 64 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9
      post_quantum_alternatives:
        - "SHA-256"
        - "SHA-3"
        - "SPHINCS+"

  - id: javascript-use-of-sha1-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createHash('sha1')
          - pattern: crypto.createHash("sha1")
          - pattern: crypto.createHash('SHA1')
          - pattern: crypto.createHash("SHA1")
    message: >-
      SHA-1 is deprecated and quantum-weakened by Grover's algorithm -- effective security drops from 160
      to 80 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 9

  - id: javascript-use-of-md4-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createHash('md4')
          - pattern: crypto.createHash("md4")
    message: >-
      MD4 is completely broken classically and quantum-weakened by Grover's algorithm. Use SHA-256,
      SHA-384, or SHA-3.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_risk_score: 10

  - id: javascript-use-of-des-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createCipheriv('des', ...)
          - pattern: crypto.createCipheriv("des", ...)
          - pattern: crypto.createCipheriv('des-ecb', ...)
          - pattern: crypto.createCipheriv("des-ecb", ...)
          - pattern: crypto.createCipheriv('des-cbc', ...)
          - pattern: crypto.createCipheriv("des-cbc", ...)
          - pattern: crypto.createDecipheriv('des', ...)
          - pattern: crypto.createDecipheriv("des", ...)
          - pattern: crypto.createDecipheriv('des-ecb', ...)
          - pattern: crypto.createDecipheriv("des-ecb", ...)
    message: >-
      DES has a 56-bit key -- completely broken classically and trivially quantum-vulnerable. Migrate to
      AES-256-GCM.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 10

  - id: javascript-use-of-3des-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createCipheriv('des-ede3', ...)
          - pattern: crypto.createCipheriv("des-ede3", ...)
          - pattern: crypto.createCipheriv('des-ede3-cbc', ...)
          - pattern: crypto.createCipheriv("des-ede3-cbc", ...)
          - pattern: crypto.createDecipheriv('des-ede3', ...)
          - pattern: crypto.createDecipheriv("des-ede3", ...)
    message: >-
      3DES has 112-bit effective key strength -- broken classically and quantum-vulnerable. NIST deprecated
      3DES in 2023. Migrate to AES-256-GCM.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "AES-256"
        - "AES-256-GCM"

  - id: javascript-use-of-rc4-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createCipheriv('rc4', ...)
          - pattern: crypto.createCipheriv("rc4", ...)
          - pattern: crypto.createDecipheriv('rc4', ...)
          - pattern: crypto.createDecipheriv("rc4", ...)
    message: >-
      RC4 is completely broken and banned by RFC 7465. Migrate to AES-256-GCM or ChaCha20-Poly1305.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_risk_score: 10

  - id: javascript-weak-rsa-key-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.generateKeyPairSync('rsa', { modulusLength:512, ... })
          - pattern: crypto.generateKeyPairSync('rsa', { modulusLength:1024, ... })
          - pattern: crypto.generateKeyPairSync('rsa', { modulusLength:2048, ... })
          - pattern: crypto.generateKeyPair('rsa', { modulusLength:512, ... }, ...)
          - pattern: crypto.generateKeyPair('rsa', { modulusLength:1024, ... }, ...)
          - pattern: crypto.generateKeyPair('rsa', { modulusLength:2048, ... }, ...)
    message: >-
      RSA is broken by Shor's algorithm regardless of key size. Migrate to ML-KEM (FIPS 203) for key
      encapsulation or ML-DSA (FIPS 204) for signatures.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10
      post_quantum_alternatives:
        - "CRYSTALS-Dilithium"
        - "CRYSTALS-Kyber"
        - "FALCON"

  - id: javascript-weak-dsa-key-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.generateKeyPairSync('dsa', { modulusLength:1024, ... })
          - pattern: crypto.generateKeyPair('dsa', { modulusLength:1024, ... }, ...)
    message: >-
      QUANTUM VULNERABILITY: DSA with 1024-bit key is vulnerable to quantum attacks using Shor's
      algorithm.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10

  - id: javascript-weak-dh-quantum
    patterns:
      - pattern-either:
          - pattern: crypto.createDiffieHellman(512)
          - pattern: crypto.createDiffieHellman(1024)
          - pattern: crypto.createDiffieHellman(2048)
    message: >-
      DH/DSA is broken by Shor's algorithm. Migrate to ML-KEM (FIPS 203) for key exchange or ML-DSA (FIPS
      204) for signatures.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: true
      quantum_attack: "Shor's Algorithm"
      quantum_risk_score: 10

  - id: javascript-ecb-mode
    patterns:
      - pattern-either:
          - pattern: crypto.createCipheriv('aes-128-ecb', ...)
          - pattern: crypto.createCipheriv('aes-192-ecb', ...)
          - pattern: crypto.createCipheriv('aes-256-ecb', ...)
          - pattern: crypto.createCipheriv("aes-128-ecb", ...)
          - pattern: crypto.createCipheriv("aes-192-ecb", ...)
          - pattern: crypto.createCipheriv("aes-256-ecb", ...)
    message: >-
      AES in ECB mode leaks data patterns -- identical plaintext blocks produce identical ciphertext. Use
      AES-256-GCM for authenticated encryption.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 6

  - id: javascript-weak-random-mathrandom
    patterns:
      - pattern-either:
          - pattern: Math.random()
    message: >-
      Non-cryptographic PRNG used for security-sensitive operations -- output is predictable. Use a
      cryptographically secure random number generator.
    severity: WARNING
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: javascript-weak-ec-curve
    patterns:
      - pattern-either:
          - pattern: crypto.generateKeyPairSync('ec', { namedCurve:'secp112r1', ... })
          - pattern: crypto.generateKeyPairSync('ec', { namedCurve:"secp112r1", ... })
          - pattern: crypto.generateKeyPair('ec', { namedCurve:'secp112r1', ... }, ...)
          - pattern: crypto.generateKeyPair('ec', { namedCurve:"secp112r1", ... }, ...)
    message: >-
      QUANTUM VULNERABILITY: Elliptic curve secp112r1 is too weak and vulnerable to quantum attacks.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
        - post-quantum
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Modified Shor's Algorithm"
      quantum_risk_score: 9

  - id: javascript-weak-pbkdf2-iterations
    patterns:
      - pattern-either:
          - pattern: crypto.pbkdf2Sync($PASSWORD, $SALT, $ITERATIONS, ...)
          - pattern: crypto.pbkdf2($PASSWORD, $SALT, $ITERATIONS, ...)
      - metavariable-comparison:
          metavariable: $ITERATIONS
          comparison: $ITERATIONS < 100000
    message: >-
      Insufficient PBKDF2 iterations make the derived key brute-forceable. Use at least 310,000 iterations
      (NIST SP 800-132) or switch to Argon2.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-916: Use of Password Hash With Insufficient Computational Effort"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm (accelerates brute-force)"
      quantum_risk_score: 8
      post_quantum_alternatives:
        - "Argon2"
        - "scrypt"
        - "bcrypt"
      references:
        - https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html

  - id: javascript-static-zero-iv
    patterns:
      - pattern-either:
          - pattern: |
              const $IV = Buffer.alloc($SIZE, 0)
              ...
              crypto.createCipheriv(..., ..., $IV)
          - pattern: |
              const $IV = Buffer.alloc($SIZE)
              ...
              crypto.createCipheriv(..., ..., $IV)
          - pattern: |
              let $IV = Buffer.from($HEXSTRING)
              ...
              crypto.createCipheriv(..., ..., $IV)
          - pattern: crypto.createCipheriv(..., ..., Buffer.alloc($SIZE, 0))
          - pattern: crypto.createCipheriv(..., ..., Buffer.alloc($SIZE))
    message: >-
      Static or hardcoded IV breaks IND-CPA security -- identical plaintexts produce identical ciphertext.
      Generate a fresh random IV for every encryption operation.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-329: Not Using a Random IV with CBC Mode"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: false
      quantum_risk_score: 7

  - id: javascript-hardcoded-crypto-key
    patterns:
      - pattern-either:
          - pattern: |
              const $KEY = '...'
              ...
              crypto.createCipheriv(..., $KEY, ...)
          - pattern: |
              const $KEY = "..."
              ...
              crypto.createCipheriv(..., $KEY, ...)
          - pattern: |
              const $KEY = Buffer.from('...')
              ...
              crypto.createCipheriv(..., $KEY, ...)
          - pattern: crypto.createCipheriv(..., '...', ...)
          - pattern: crypto.createCipheriv(..., "...", ...)
    message: >-
      Hardcoded cryptographic key in source code exposes key material to anyone with code access. Use
      environment variables or a dedicated secrets manager.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-798: Use of Hard-coded Credentials"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: MEDIUM
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 8

  - id: javascript-deprecated-crypto-cipher
    patterns:
      - pattern-either:
          - pattern: crypto.createCipher(...)
          - pattern: crypto.createDecipher(...)
    message: >-
      MD5 is cryptographically broken and quantum-weakened by Grover's algorithm -- effective security
      drops from 128 to 64 bits. Use SHA-256, SHA-384, or SHA-3.
    severity: ERROR
    languages: [javascript, typescript]
    metadata:
      category: security
      technology:
        - nodejs
        - crypto
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
        - "CWE-916: Use of Password Hash With Insufficient Computational Effort"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Grover's Algorithm (weak MD5 key derivation)"
      quantum_risk_score: 9
      references:
        - https://nodejs.org/api/crypto.html#cryptocreatecipheralgorithm-password-options
