rules:
  - id: csharp-tls-version-too-old
    patterns:
      - pattern-either:
          - pattern: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
          - pattern: ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
          - pattern: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
          - pattern: ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
          - pattern: ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11
          - pattern: new SslStream(..., SslProtocols.Tls, ...)
          - pattern: new SslStream(..., SslProtocols.Tls11, ...)
          - pattern: new SslStream(..., SslProtocols.Ssl3, ...)
          - pattern: $HANDLER.SslProtocols = SslProtocols.Tls
          - pattern: $HANDLER.SslProtocols = SslProtocols.Tls11
    message: >-
      TLS 1.0/1.1 use broken cipher suites (RC4, 3DES, EXPORT) and are prohibited by RFC 8996. Upgrade to
      TLS 1.3.
    severity: ERROR
    languages: [csharp]
    metadata:
      category: security
      technology: [csharp, dotnet, tls, post-quantum]
      cwe:
        - "CWE-326: Inadequate Encryption Strength"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Blocks PQC migration"
      quantum_risk_score: 8

  - id: csharp-tls-cert-validation-disabled
    patterns:
      - pattern-either:
          - pattern: ServicePointManager.ServerCertificateValidationCallback = ($S, $C, $CH, $E) => true
          - pattern: ServicePointManager.ServerCertificateValidationCallback += ($S, $C, $CH, $E) => true
          - pattern: $HANDLER.ServerCertificateCustomValidationCallback = ($M, $C, $CH, $E) => true
          - pattern: $HANDLER.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
    message: >-
      SECURITY VULNERABILITY: TLS certificate validation is disabled. This allows man-in-the-middle
      attacks and completely defeats TLS security.
    severity: ERROR
    languages: [csharp]
    metadata:
      category: security
      technology: [csharp, dotnet, tls]
      cwe:
        - "CWE-295: Improper Certificate Validation"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: HIGH
      impact: CRITICAL
      quantum_vulnerable: false
      quantum_risk_score: 9

  - id: csharp-tls-weak-cipher-suite
    patterns:
      - pattern-either:
          - pattern: new CipherSuitesPolicy(new[] { TlsCipherSuite.TLS_RSA_WITH_RC4_128_SHA, ... })
          - pattern: new CipherSuitesPolicy(new[] { TlsCipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA, ... })
    message: >-
      Explicit weak cipher suite configuration bypasses secure TLS defaults. Use TLS 1.3 which negotiates
      cipher suites automatically.
    severity: ERROR
    languages: [csharp]
    metadata:
      category: security
      technology: [csharp, dotnet, tls, post-quantum]
      cwe:
        - "CWE-327: Use of a Broken or Risky Cryptographic Algorithm"
      owasp:
        - A02:2021 - Cryptographic Failures
      confidence: HIGH
      likelihood: MEDIUM
      impact: HIGH
      quantum_vulnerable: true
      quantum_attack: "Blocks PQC migration"
      quantum_risk_score: 8
