File
Blob: waf/rules.go
| 1 | package waf |
| 2 | |
| 3 | import ( |
| 4 | "archive/zip" |
| 5 | "bytes" |
| 6 | _ "embed" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "io/fs" |
| 10 | "strings" |
| 11 | |
| 12 | openpgp "github.com/ProtonMail/go-crypto/openpgp/v2" |
| 13 | lts "github.com/corazawaf/coraza-coreruleset/lts/v4" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | maxArchiveBytes = 8 << 20 |
| 18 | maxSignatureBytes = 64 << 10 |
| 19 | maxExpandedBytes = 32 << 20 |
| 20 | maxRuleFileBytes = 4 << 20 |
| 21 | maxArchiveFiles = 512 |
| 22 | crsFingerprint = "36006F0E0BA167832158821138EEACA1AB8A6E72" |
| 23 | ) |
| 24 | |
| 25 | // The release key is authenticated by the fingerprint published in upstream's |
| 26 | // SECURITY.md. New trust roots ship with Specter, never through release discovery. |
| 27 | // |
| 28 | //go:embed crs-security.asc |
| 29 | var crsPublicKey string |
| 30 | |
| 31 | var bundledVersion = strings.TrimPrefix(lts.Version, "v") |
| 32 | |
| 33 | type ruleRelease struct { |
| 34 | version string |
| 35 | rules fs.FS |
| 36 | archive []byte |
| 37 | signature []byte |
| 38 | } |
| 39 | |
| 40 | func trustedCRSKeys() (openpgp.EntityList, error) { |
| 41 | keys, err := openpgp.ReadArmoredKeyRing(strings.NewReader(crsPublicKey)) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | if len(keys) != 1 || fmt.Sprintf("%X", keys[0].PrimaryKey.Fingerprint) != crsFingerprint { |
| 46 | return nil, fmt.Errorf("embedded CRS signing key has an unexpected fingerprint") |
| 47 | } |
| 48 | return keys, nil |
| 49 | } |
| 50 | |
| 51 | func verifyRelease(version string, archive, signature []byte, keys openpgp.EntityList) (*ruleRelease, error) { |
| 52 | if !validReleaseVersion(version) || len(archive) > maxArchiveBytes || len(signature) > maxSignatureBytes { |
| 53 | return nil, fmt.Errorf("invalid CRS release or archive size") |
| 54 | } |
| 55 | if _, _, err := openpgp.VerifyArmoredDetachedSignature(keys, bytes.NewReader(archive), bytes.NewReader(signature), nil); err != nil { |
| 56 | return nil, fmt.Errorf("verify CRS %s signature: %w", version, err) |
| 57 | } |
| 58 | rules, err := openRulesArchive(version, archive) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | return &ruleRelease{version: version, rules: rules, archive: archive, signature: signature}, nil |
| 63 | } |
| 64 | |
| 65 | // A ZIP reader owns the archive bytes and implements fs.FS. Engines therefore |
| 66 | // remain usable after their on-disk cache files have been retired. |
| 67 | func openRulesArchive(version string, archive []byte) (fs.FS, error) { |
| 68 | r, err := zip.NewReader(bytes.NewReader(archive), int64(len(archive))) |
| 69 | if err != nil { |
| 70 | return nil, fmt.Errorf("open CRS archive: %w", err) |
| 71 | } |
| 72 | if len(r.File) > maxArchiveFiles { |
| 73 | return nil, fmt.Errorf("CRS archive contains too many files") |
| 74 | } |
| 75 | root := "coreruleset-" + version |
| 76 | seen := make(map[string]bool, len(r.File)) |
| 77 | var expanded uint64 |
| 78 | for _, f := range r.File { |
| 79 | name := strings.TrimSuffix(f.Name, "/") |
| 80 | if !fs.ValidPath(name) || strings.Contains(name, "\\") || (name != root && !strings.HasPrefix(name, root+"/")) || seen[name] { |
| 81 | return nil, fmt.Errorf("invalid or duplicate CRS archive path %q", f.Name) |
| 82 | } |
| 83 | seen[name] = true |
| 84 | if f.FileInfo().IsDir() { |
| 85 | continue |
| 86 | } |
| 87 | if !f.Mode().IsRegular() || f.UncompressedSize64 > maxRuleFileBytes { |
| 88 | return nil, fmt.Errorf("invalid CRS archive file %q", f.Name) |
| 89 | } |
| 90 | expanded += f.UncompressedSize64 |
| 91 | if expanded > maxExpandedBytes { |
| 92 | return nil, fmt.Errorf("expanded CRS archive is too large") |
| 93 | } |
| 94 | body, err := f.Open() |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | n, readErr := io.Copy(io.Discard, io.LimitReader(body, maxRuleFileBytes+1)) |
| 99 | closeErr := body.Close() |
| 100 | if readErr != nil || closeErr != nil || n != int64(f.UncompressedSize64) { |
| 101 | return nil, fmt.Errorf("invalid CRS archive content %q", f.Name) |
| 102 | } |
| 103 | } |
| 104 | for _, required := range []string{"crs-setup.conf.example", "rules/REQUEST-901-INITIALIZATION.conf", "rules/REQUEST-949-BLOCKING-EVALUATION.conf"} { |
| 105 | if !seen[root+"/"+required] { |
| 106 | return nil, fmt.Errorf("CRS archive is missing %s", required) |
| 107 | } |
| 108 | } |
| 109 | return fs.Sub(r, root) |
| 110 | } |
| 111 | |
| 112 | // Adapt the upstream embedded aliases once to the same layout as a downloaded |
| 113 | // release. The embedded module is authenticated with the Specter binary. |
| 114 | func bundledRules() (fs.FS, error) { |
| 115 | var data bytes.Buffer |
| 116 | w := zip.NewWriter(&data) |
| 117 | root := "coreruleset-" + bundledVersion + "/" |
| 118 | copyFile := func(source, target string) error { |
| 119 | contents, err := fs.ReadFile(lts.FS, source) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | file, err := w.CreateHeader(&zip.FileHeader{Name: root + target, Method: zip.Store}) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | _, err = file.Write(contents) |
| 128 | return err |
| 129 | } |
| 130 | if err := copyFile("@crs-setup-lts.conf.example", "crs-setup.conf.example"); err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | if err := fs.WalkDir(lts.FS, "@owasp_crs_lts", func(name string, entry fs.DirEntry, err error) error { |
| 134 | if err != nil || entry.IsDir() { |
| 135 | return err |
| 136 | } |
| 137 | return copyFile(name, "rules/"+strings.TrimPrefix(name, "@owasp_crs_lts/")) |
| 138 | }); err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | if err := w.Close(); err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | return openRulesArchive(bundledVersion, data.Bytes()) |
| 145 | } |