rules:
  - id: auth.rust.crypto.bcrypt-low-cost
    languages:
      - rust
    severity: WARNING
    message: |
      `bcrypt::hash` (or `bcrypt::hash_with_result`) is called with a cost
      factor below 10. A low work factor makes each hash cheap to compute,
      which lets an attacker brute-force stolen password hashes far too
      quickly. OWASP recommends a bcrypt cost of at least 10, and ≥ 12 for new
      applications, tuned so a single hash takes roughly 250ms on your
      hardware.

      Common LLM-generated mistake: `bcrypt::hash(password, 8)` because the
      literal "looks fast enough". Use `bcrypt::DEFAULT_COST` (12) or raise the
      cost factor to 12 or higher.
    # Matches only a numeric literal cost < 10 passed to `bcrypt::hash` /
    # `bcrypt::hash_with_result`. `metavariable-comparison` constrains $N to
    # numeric literals only, so `bcrypt::DEFAULT_COST`, a cost ≥ 10, or a
    # variable (`bcrypt::hash(password, cost)`) are NOT flagged.
    patterns:
      - pattern-either:
          - pattern: bcrypt::hash($PW, $N)
          - pattern: bcrypt::hash_with_result($PW, $N)
      - metavariable-comparison:
          metavariable: $N
          comparison: $N < 10
    metadata:
      oauthlint-rule-id: AUTH-RUST-CRYPTO-002
      oauthlint-doc-url: https://oauthlint.dev/rules/rust-crypto-bcrypt-low-cost
      category: security
      cwe: CWE-916
      owasp: A02:2021
      llm-prevalence: MEDIUM
      technology:
        - bcrypt
      references:
        - https://docs.rs/bcrypt/latest/bcrypt/fn.hash.html
        - https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
        - https://cwe.mitre.org/data/definitions/916.html
