File
Blob: waf/update.go
| 1 | package waf |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | "go.miragespace.co/specter/spec/protocol" |
| 14 | |
| 15 | openpgp "github.com/ProtonMail/go-crypto/openpgp/v2" |
| 16 | ) |
| 17 | |
| 18 | type ruleSource struct { |
| 19 | client *http.Client |
| 20 | releasesURL string |
| 21 | downloadURL string |
| 22 | keys openpgp.EntityList |
| 23 | } |
| 24 | |
| 25 | func officialRuleSource() (*ruleSource, error) { |
| 26 | keys, err := trustedCRSKeys() |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | return &ruleSource{ |
| 31 | client: &http.Client{ |
| 32 | Timeout: 30 * time.Second, |
| 33 | CheckRedirect: func(r *http.Request, via []*http.Request) error { |
| 34 | if len(via) >= 5 || r.URL.Scheme != "https" { |
| 35 | return fmt.Errorf("invalid CRS download redirect") |
| 36 | } |
| 37 | switch r.URL.Hostname() { |
| 38 | case "github.com", "api.github.com", "release-assets.githubusercontent.com", "objects.githubusercontent.com": |
| 39 | return nil |
| 40 | default: |
| 41 | return fmt.Errorf("unexpected CRS download host %q", r.URL.Hostname()) |
| 42 | } |
| 43 | }, |
| 44 | }, |
| 45 | releasesURL: "https://api.github.com/repos/coreruleset/coreruleset/releases", |
| 46 | downloadURL: "https://github.com/coreruleset/coreruleset/releases/download", |
| 47 | keys: keys, |
| 48 | }, nil |
| 49 | } |
| 50 | |
| 51 | func (s *ruleSource) get(ctx context.Context, url string, limit int64) ([]byte, error) { |
| 52 | r, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | r.Header.Set("User-Agent", "Specter-WAF") |
| 57 | r.Header.Set("Accept", "application/vnd.github+json") |
| 58 | response, err := s.client.Do(r) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | defer response.Body.Close() |
| 63 | if response.StatusCode != http.StatusOK { |
| 64 | return nil, fmt.Errorf("CRS source returned %s", response.Status) |
| 65 | } |
| 66 | data, err := io.ReadAll(io.LimitReader(response.Body, limit+1)) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | if int64(len(data)) > limit { |
| 71 | return nil, fmt.Errorf("CRS source response exceeds size limit") |
| 72 | } |
| 73 | return data, nil |
| 74 | } |
| 75 | |
| 76 | func (s *ruleSource) latest(ctx context.Context, channel protocol.WAFSettings_Channel) (string, error) { |
| 77 | latest := "" |
| 78 | // A bounded release listing covers the supported LTS window without following |
| 79 | // arbitrary links from unsigned discovery metadata. |
| 80 | for page := 1; page <= 3; page++ { |
| 81 | data, err := s.get(ctx, fmt.Sprintf("%s?per_page=100&page=%d", s.releasesURL, page), 4<<20) |
| 82 | if err != nil { |
| 83 | return "", err |
| 84 | } |
| 85 | var releases []struct { |
| 86 | Tag string `json:"tag_name"` |
| 87 | Prerelease bool `json:"prerelease"` |
| 88 | Draft bool `json:"draft"` |
| 89 | } |
| 90 | if err := json.Unmarshal(data, &releases); err != nil { |
| 91 | return "", fmt.Errorf("read CRS release listing: %w", err) |
| 92 | } |
| 93 | for _, release := range releases { |
| 94 | if !strings.HasPrefix(release.Tag, "v") || release.Prerelease || release.Draft { |
| 95 | continue |
| 96 | } |
| 97 | version := strings.TrimPrefix(release.Tag, "v") |
| 98 | if releaseInChannel(version, channel) && (latest == "" || compareReleases(version, latest) > 0) { |
| 99 | latest = version |
| 100 | } |
| 101 | } |
| 102 | if len(releases) < 100 { |
| 103 | break |
| 104 | } |
| 105 | } |
| 106 | if latest == "" { |
| 107 | return "", fmt.Errorf("no published CRS release found for %s", channel) |
| 108 | } |
| 109 | return latest, nil |
| 110 | } |
| 111 | |
| 112 | func (s *ruleSource) download(ctx context.Context, version string) (*ruleRelease, error) { |
| 113 | if !validReleaseVersion(version) { |
| 114 | return nil, fmt.Errorf("invalid CRS release version") |
| 115 | } |
| 116 | url := fmt.Sprintf("%s/v%s/coreruleset-%s-minimal.zip", s.downloadURL, version, version) |
| 117 | archive, err := s.get(ctx, url, maxArchiveBytes) |
| 118 | if err != nil { |
| 119 | return nil, err |
| 120 | } |
| 121 | signature, err := s.get(ctx, url+".asc", maxSignatureBytes) |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | return verifyRelease(version, archive, signature, s.keys) |
| 126 | } |
| 127 | |
| 128 | func validReleaseVersion(version string) bool { |
| 129 | if len(version) > 32 || !releaseVersion.MatchString(version) { |
| 130 | return false |
| 131 | } |
| 132 | for _, part := range strings.Split(version, ".") { |
| 133 | if _, err := strconv.ParseUint(part, 10, 32); err != nil || (len(part) > 1 && part[0] == '0') { |
| 134 | return false |
| 135 | } |
| 136 | } |
| 137 | return true |
| 138 | } |
| 139 | |
| 140 | func releaseInChannel(version string, channel protocol.WAFSettings_Channel) bool { |
| 141 | return validReleaseVersion(version) && (channel == protocol.WAFSettings_STABLE || (channel == protocol.WAFSettings_LTS && strings.HasPrefix(version, "4.25."))) |
| 142 | } |
| 143 | |
| 144 | func compareReleases(a, b string) int { |
| 145 | ap, bp := strings.Split(a, "."), strings.Split(b, ".") |
| 146 | for i := range ap { |
| 147 | av, _ := strconv.ParseUint(ap[i], 10, 32) |
| 148 | bv, _ := strconv.ParseUint(bp[i], 10, 32) |
| 149 | if av < bv { |
| 150 | return -1 |
| 151 | } |
| 152 | if av > bv { |
| 153 | return 1 |
| 154 | } |
| 155 | } |
| 156 | return 0 |
| 157 | } |