| 1 |
/* |
| 2 |
* ZLint Copyright 2024 Regents of the University of Michigan |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 |
* use this file except in compliance with the License. You may obtain a copy |
| 6 |
* of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 11 |
* implied. See the License for the specific language governing |
| 12 |
* permissions and limitations under the License. |
| 13 |
*/ |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"encoding/json" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"go/format" |
| 24 |
"html/template" |
| 25 |
"io" |
| 26 |
"net" |
| 27 |
"net/http" |
| 28 |
"os" |
| 29 |
"strings" |
| 30 |
"time" |
| 31 |
|
| 32 |
log "github.com/sirupsen/logrus" |
| 33 |
"github.com/zmap/zlint/v3/util" |
| 34 |
) |
| 35 |
|
| 36 |
//nolint:revive |
| 37 |
const ( |
| 38 |
// ICANN_GTLD_JSON is the URL for the ICANN gTLD JSON registry (version 2). |
| 39 |
// This registry does not contain ccTLDs but does carry full gTLD information |
| 40 |
// needed to determine validity periods. |
| 41 |
// See https://www.icann.org/resources/pages/registries/registries-en for more |
| 42 |
// information. |
| 43 |
ICANN_GTLD_JSON = "https://www.icann.org/resources/registries/gtlds/v2/gtlds.json" |
| 44 |
// ICANN_TLDS is the URL for the ICANN list of valid top-level domains |
| 45 |
// maintained by the IANA. It contains both ccTLDs and gTLDs but does not |
| 46 |
// carry sufficient granularity to determine validity periods. |
| 47 |
// See https://www.icann.org/resources/pages/tlds-2012-02-25-en for more |
| 48 |
// information. |
| 49 |
ICANN_TLDS = "https://data.iana.org/TLD/tlds-alpha-by-domain.txt" |
| 50 |
) |
| 51 |
|
| 52 |
var ( |
| 53 |
// version is replaced by GoReleaser or `make` using an LDFlags option at |
| 54 |
// build time. Here we supply a default value for folks that `go install` or |
| 55 |
// `go build` directly from src. |
| 56 |
version = "dev-unknown" |
| 57 |
|
| 58 |
// httpClient is a http.Client instance configured with timeouts. |
| 59 |
httpClient = &http.Client{ |
| 60 |
Transport: &http.Transport{ |
| 61 |
Dial: (&net.Dialer{ |
| 62 |
Timeout: 15 * time.Second, |
| 63 |
KeepAlive: 15 * time.Second, |
| 64 |
}).Dial, |
| 65 |
TLSHandshakeTimeout: 5 * time.Second, |
| 66 |
ResponseHeaderTimeout: 5 * time.Second, |
| 67 |
ExpectContinueTimeout: 1 * time.Second, |
| 68 |
}, |
| 69 |
} |
| 70 |
// gTLDMapTemplate is a template that produces a Golang source code file in |
| 71 |
// the "util" package containing a single member variable, a map of strings to |
| 72 |
// `util.GTLDPeriod` objects called `tldMap`. |
| 73 |
gTLDMapTemplate = template.Must(template.New("gTLDMapTemplate").Parse( |
| 74 |
`// Code generated by go generate; DO NOT EDIT. |
| 75 |
// This file was generated by zlint-gtld-update. |
| 76 |
|
| 77 |
/* |
| 78 |
* ZLint Copyright 2024 Regents of the University of Michigan |
| 79 |
* |
| 80 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 81 |
* use this file except in compliance with the License. You may obtain a copy |
| 82 |
* of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 83 |
* |
| 84 |
* Unless required by applicable law or agreed to in writing, software |
| 85 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 86 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 87 |
* implied. See the License for the specific language governing |
| 88 |
* permissions and limitations under the License. |
| 89 |
*/ |
| 90 |
|
| 91 |
package util |
| 92 |
|
| 93 |
var tldMap = map[string]GTLDPeriod{ |
| 94 |
{{- range .GTLDs }} |
| 95 |
"{{ .GTLD }}": { |
| 96 |
GTLD: "{{ .GTLD }}", |
| 97 |
DelegationDate: "{{ .DelegationDate }}", |
| 98 |
RemovalDate: "{{ .RemovalDate }}", |
| 99 |
}, |
| 100 |
{{- end }} |
| 101 |
// .onion is a special case and not a general gTLD. However, it is allowed in |
| 102 |
// some circumstances in the web PKI so the Zlint gtldMap includes it with |
| 103 |
// a delegationDate based on the CABF ballot to allow EV issuance for .onion |
| 104 |
// domains: https://cabforum.org/2015/02/18/ballot-144-validation-rules-dot-onion-names/ |
| 105 |
"onion": { |
| 106 |
GTLD: "onion", |
| 107 |
DelegationDate: "2015-02-18", |
| 108 |
RemovalDate: "", |
| 109 |
}, |
| 110 |
} |
| 111 |
`)) |
| 112 |
|
| 113 |
printVersion = false |
| 114 |
) |
| 115 |
|
| 116 |
// getData fetches the response body bytes from an HTTP get to the provider url, |
| 117 |
// or returns an error. |
| 118 |
func getData(url string) ([]byte, error) { |
| 119 |
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 120 |
defer cancel() |
| 121 |
|
| 122 |
// Change NewRequest to NewRequestWithContext and pass context it |
| 123 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 124 |
if err != nil { |
| 125 |
return nil, err |
| 126 |
} |
| 127 |
|
| 128 |
resp, err := httpClient.Do(req) |
| 129 |
if err != nil { |
| 130 |
return nil, fmt.Errorf("unable to fetch data from %q : %s", |
| 131 |
url, err) |
| 132 |
} |
| 133 |
defer resp.Body.Close() |
| 134 |
|
| 135 |
if resp.StatusCode != http.StatusOK { |
| 136 |
return nil, fmt.Errorf("unexpected status code fetching data "+ |
| 137 |
"from %q : expected status %d got %d", |
| 138 |
url, http.StatusOK, resp.StatusCode) |
| 139 |
} |
| 140 |
|
| 141 |
respBody, err := io.ReadAll(resp.Body) |
| 142 |
if err != nil { |
| 143 |
return nil, fmt.Errorf("unexpected error reading response "+ |
| 144 |
"body from %q : %s", |
| 145 |
url, err) |
| 146 |
} |
| 147 |
return respBody, nil |
| 148 |
} |
| 149 |
|
| 150 |
// getTLDData fetches the ICANN_TLDS list and uses the information to build |
| 151 |
// and return a list of util.GTLDPeriod objects (or an error if anything fails). |
| 152 |
// Since this data source only contains TLD names and not any information |
| 153 |
// about delegation/removal all of the returned `util.GTLDPeriod` objects will |
| 154 |
// have the DelegationDate "1985-01-01" (matching the `.com` delegation date) |
| 155 |
// and no RemovalDate. |
| 156 |
func getTLDData() ([]util.GTLDPeriod, error) { |
| 157 |
respBody, err := getData(ICANN_TLDS) |
| 158 |
if err != nil { |
| 159 |
return nil, fmt.Errorf("error getting ICANN TLD list : %s", err) |
| 160 |
} |
| 161 |
tlds := strings.Split(string(respBody), "\n") |
| 162 |
|
| 163 |
var results []util.GTLDPeriod |
| 164 |
for _, tld := range tlds { |
| 165 |
// Skip empty lines and the header comment line |
| 166 |
if strings.TrimSpace(tld) == "" || strings.HasPrefix(tld, "#") { |
| 167 |
continue |
| 168 |
} |
| 169 |
results = append(results, util.GTLDPeriod{ |
| 170 |
GTLD: strings.ToLower(tld), |
| 171 |
// The TLD list doesn't indicate when any of the TLDs were delegated so |
| 172 |
// assume these TLDs were all delegated at the same time as "com". |
| 173 |
DelegationDate: "1985-01-01", |
| 174 |
}) |
| 175 |
} |
| 176 |
return results, nil |
| 177 |
} |
| 178 |
|
| 179 |
// getGTLDData fetches the ICANN_GTLD_JSON and parses it into a list of |
| 180 |
// util.GTLDPeriod objects, or returns an error. The gTLDEntries are returned |
| 181 |
// as-is and may contain entries that were never delegated from the root DNS. |
| 182 |
func getGTLDData() ([]util.GTLDPeriod, error) { |
| 183 |
respBody, err := getData(ICANN_GTLD_JSON) |
| 184 |
if err != nil { |
| 185 |
return nil, fmt.Errorf("error getting ICANN gTLD JSON : %s", err) |
| 186 |
} |
| 187 |
|
| 188 |
//nolint:musttag |
| 189 |
var results struct { |
| 190 |
GTLDs []util.GTLDPeriod |
| 191 |
} |
| 192 |
if err := json.Unmarshal(respBody, &results); err != nil { |
| 193 |
return nil, fmt.Errorf("unexpected error unmarshaling ICANN gTLD JSON response "+ |
| 194 |
"body from %q : %s", |
| 195 |
ICANN_GTLD_JSON, err) |
| 196 |
} |
| 197 |
return results.GTLDs, nil |
| 198 |
} |
| 199 |
|
| 200 |
// delegatedGTLDs filters the provided list of GTLDPeriods removing any entries |
| 201 |
// that were never delegated from the root DNS. |
| 202 |
func delegatedGTLDs(entries []util.GTLDPeriod) []util.GTLDPeriod { |
| 203 |
var results []util.GTLDPeriod |
| 204 |
for _, gTLD := range entries { |
| 205 |
if gTLD.DelegationDate == "" { |
| 206 |
continue |
| 207 |
} |
| 208 |
results = append(results, gTLD) |
| 209 |
} |
| 210 |
return results |
| 211 |
} |
| 212 |
|
| 213 |
// validateGTLDs checks that all entries have a valid parseable DelegationDate |
| 214 |
// string, and if not-empty, a valid parseable RemovalDate string. This function |
| 215 |
// assumes an entry with an empty DelegationDate is an error. Use |
| 216 |
// `delegatedGTLDs` to filter out entries that were never delegated before |
| 217 |
// validating. |
| 218 |
func validateGTLDs(entries []util.GTLDPeriod) error { |
| 219 |
for _, gTLD := range entries { |
| 220 |
// All entries should have a valid delegation date |
| 221 |
if _, err := time.Parse(util.GTLDPeriodDateFormat, gTLD.DelegationDate); err != nil { |
| 222 |
return err |
| 223 |
} |
| 224 |
// a gTLD that has not been removed has an empty RemovalDate and that's OK |
| 225 |
if _, err := time.Parse(util.GTLDPeriodDateFormat, gTLD.RemovalDate); gTLD.RemovalDate != "" && err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
} |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
// renderGTLDMap fetches the ICANN gTLD data, filters out undelegated entries, |
| 233 |
// validates the remaining entries have parseable dates, and renders the |
| 234 |
// gTLDMapTemplate to the provided writer using the validated entries (or |
| 235 |
// returns an error if any of the aforementioned steps fail). It then fetches |
| 236 |
// the ICANN TLD data, and uses it to populate any missing entries for ccTLDs. |
| 237 |
// These entries will have a default delegationDate because the data source is |
| 238 |
// not specific enough to provide one. The produced output text is a Golang |
| 239 |
// source code file in the `util` package that contains a single map variable |
| 240 |
// containing GTLDPeriod objects created with the ICANN data. |
| 241 |
func renderGTLDMap(writer io.Writer) error { |
| 242 |
// Get all of ICANN's gTLDs including ones that haven't been delegated. |
| 243 |
allGTLDs, err := getGTLDData() |
| 244 |
if err != nil { |
| 245 |
return err |
| 246 |
} |
| 247 |
|
| 248 |
// Filter out the non-delegated gTLD entries |
| 249 |
delegatedGTLDs := delegatedGTLDs(allGTLDs) |
| 250 |
|
| 251 |
// Validate that all of the delegated gTLDs have correct dates |
| 252 |
if err := validateGTLDs(delegatedGTLDs); err != nil { |
| 253 |
return err |
| 254 |
} |
| 255 |
|
| 256 |
// Get all of the TLDs. This data source doesn't provide delegationDates and |
| 257 |
// so we only want to use it to populate missing entries in `delegatedGTLDs`, |
| 258 |
// not to replace any existing entries that have more specific information |
| 259 |
// about the validity period for the TLD. |
| 260 |
allTLDs, err := getTLDData() |
| 261 |
if err != nil { |
| 262 |
return err |
| 263 |
} |
| 264 |
|
| 265 |
tldMap := make(map[string]util.GTLDPeriod) |
| 266 |
|
| 267 |
// Deduplicate delegatedGTLDs into the tldMap first |
| 268 |
for _, tld := range delegatedGTLDs { |
| 269 |
tldMap[tld.GTLD] = tld |
| 270 |
} |
| 271 |
|
| 272 |
// Then populate any missing entries from the allTLDs list |
| 273 |
for _, tld := range allTLDs { |
| 274 |
if _, found := tldMap[tld.GTLD]; !found { |
| 275 |
tldMap[tld.GTLD] = tld |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
templateData := struct { |
| 280 |
GTLDs map[string]util.GTLDPeriod |
| 281 |
}{ |
| 282 |
GTLDs: tldMap, |
| 283 |
} |
| 284 |
|
| 285 |
// Render the gTLD map to a buffer with the delegated gTLD data |
| 286 |
var buf bytes.Buffer |
| 287 |
if err := gTLDMapTemplate.Execute(&buf, templateData); err != nil { |
| 288 |
return err |
| 289 |
} |
| 290 |
|
| 291 |
// format the buffer so it won't trip up the `gofmt_test.go` checks |
| 292 |
formatted, err := format.Source(buf.Bytes()) |
| 293 |
if err != nil { |
| 294 |
return err |
| 295 |
} |
| 296 |
|
| 297 |
// Write the formatted buffer to the writer |
| 298 |
_, err = writer.Write(formatted) |
| 299 |
if err != nil { |
| 300 |
return err |
| 301 |
} |
| 302 |
return nil |
| 303 |
} |
| 304 |
|
| 305 |
// init sets up command line flags |
| 306 |
func init() { |
| 307 |
flag.Usage = func() { |
| 308 |
fmt.Fprintf(os.Stderr, "ZLint version %s\n\n", version) |
| 309 |
fmt.Fprintf(os.Stderr, "Usage: %s [flags]\n", os.Args[0]) |
| 310 |
flag.PrintDefaults() |
| 311 |
} |
| 312 |
flag.BoolVar(&printVersion, "version", false, "Print ZLint version and exit") |
| 313 |
flag.Parse() |
| 314 |
log.SetLevel(log.InfoLevel) |
| 315 |
} |
| 316 |
|
| 317 |
// main handles rendering a gTLD map to either standard out (when no argument is |
| 318 |
// provided) or to the provided filename. If an error occurs it is printed to |
| 319 |
// standard err and the program terminates with a non-zero exit status. |
| 320 |
func main() { |
| 321 |
errQuit := func(err error) { |
| 322 |
fmt.Fprintf(os.Stderr, "error updating gTLD map: %s\n", err) |
| 323 |
os.Exit(1) |
| 324 |
} |
| 325 |
|
| 326 |
if printVersion { |
| 327 |
fmt.Printf("ZLint version %s\n", version) |
| 328 |
return |
| 329 |
} |
| 330 |
|
| 331 |
// Default to writing to standard out |
| 332 |
writer := os.Stdout |
| 333 |
if flag.NArg() > 0 { |
| 334 |
// If a filename is specified as a command line flag then open it (creating |
| 335 |
// if needed), truncate the existing contents, and use the file as the |
| 336 |
// writer instead of standard out |
| 337 |
filename := flag.Args()[0] |
| 338 |
f, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664) |
| 339 |
if err != nil { |
| 340 |
errQuit(err) |
| 341 |
} |
| 342 |
defer f.Close() |
| 343 |
writer = f |
| 344 |
} |
| 345 |
|
| 346 |
if err := renderGTLDMap(writer); err != nil { |
| 347 |
errQuit(err) |
| 348 |
} |
| 349 |
} |