Skip to content

Commit 8b96dda

Browse files
committed
Harden timezone mapping updates
1 parent 97ff164 commit 8b96dda

16 files changed

Lines changed: 264 additions & 342 deletions

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ It should print the go runtime timezone.
115115

116116
The generator downloads the latest IANA legacy names and Unicode CLDR Windows
117117
zone mappings, then rewrites `tzlocal/tzmapping.go`. It therefore requires
118-
network access to `ftp.iana.org` and `raw.githubusercontent.com`.
118+
network access to `data.iana.org` and `raw.githubusercontent.com`.
119119

120120
From the repository root, run:
121121

‎generate.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
//go:generate go run tzlocal/cmd/update_tzmapping.go
22

3-
package main
3+
package timezone_local

‎tzdata/download_oldtz.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package tzdata
22

33
import (
4-
"bufio"
54
"bytes"
65
"strings"
76
)
87

9-
const tzdataURL = `ftp://ftp.iana.org/tz/tzdata-latest.tar.gz`
8+
const tzdataURL = `https://data.iana.org/time-zones/tzdata-latest.tar.gz`
109

1110
// DownloadOldNames fetches the list of old tz names and returns a mapping
1211
func DownloadOldNames() (map[string]string, error) {
1312
var err error
1413

15-
buf, err := FTPDownload(tzdataURL)
14+
buf, err := downloadHTTP(tzdataURL)
1615
if err != nil {
1716
return nil, err
1817
}
1918

2019
var b bytes.Buffer
21-
w := bufio.NewWriter(&b)
22-
err = ExtractTarGz(&buf, w, "backward")
20+
err = ExtractTarGz(&buf, &b, "backward")
2321
if err != nil {
2422
return nil, err
2523
}

‎tzdata/download_oldtz_test.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package tzdata
22

3-
import "testing"
3+
import (
4+
"os"
5+
"testing"
6+
)
47

58
func TestDownloadOldNames(t *testing.T) {
9+
if os.Getenv("TZLOCAL_LIVE_TESTS") == "" {
10+
t.Skip("set TZLOCAL_LIVE_TESTS=1 to run live download tests")
11+
}
612
_, err := DownloadOldNames()
713
if err != nil {
814
t.Errorf("error: %v", err)

‎tzdata/download_wintz.go‎

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package tzdata
22

33
import (
4+
"bytes"
45
"encoding/xml"
56
"fmt"
67
"io"
78
"net/http"
9+
"time"
810
)
911

1012
const winZonesURL = `https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/windowsZones.xml`
1113

14+
const maxDownloadSize = 10 << 20
15+
1216
type SupplementalData struct {
1317
WindowsZones WindowsZones `xml:"windowsZones"`
1418
}
@@ -30,24 +34,60 @@ type MapZone struct {
3034

3135
// DownloadWindowsZones fetches Windows mapping info from unicode.org
3236
func DownloadWindowsZones() (SupplementalData, error) {
37+
return downloadWindowsZones(winZonesURL)
38+
}
39+
40+
func downloadWindowsZones(target string) (SupplementalData, error) {
41+
buf, err := downloadHTTP(target)
42+
if err != nil {
43+
return SupplementalData{}, err
44+
}
45+
return parseWindowsZones(buf.Bytes(), target)
46+
}
47+
48+
func parseWindowsZones(contents []byte, source string) (SupplementalData, error) {
3349
var data SupplementalData
3450

35-
resp, err := http.Get(winZonesURL)
51+
if err := xml.Unmarshal(contents, &data); err != nil {
52+
return data, fmt.Errorf("failed to parse %q: %w", source, err)
53+
}
54+
foundMappings := false
55+
for _, mappings := range data.WindowsZones.MapTimezones {
56+
if len(mappings.MapZone) > 0 {
57+
foundMappings = true
58+
break
59+
}
60+
}
61+
if !foundMappings {
62+
return data, fmt.Errorf("failed to parse %q: no time zone mappings found", source)
63+
}
64+
65+
return data, nil
66+
}
67+
68+
func downloadHTTP(target string) (bytes.Buffer, error) {
69+
client := http.Client{Timeout: 30 * time.Second}
70+
return downloadHTTPWithClient(&client, target)
71+
}
72+
73+
func downloadHTTPWithClient(client *http.Client, target string) (bytes.Buffer, error) {
74+
var buf bytes.Buffer
75+
resp, err := client.Get(target)
3676
if err != nil {
37-
return data, err
77+
return buf, err
3878
}
3979
defer resp.Body.Close()
4080

4181
if resp.StatusCode != http.StatusOK {
42-
return data, fmt.Errorf("failed to download \"%v\", http error: %v", winZonesURL, resp.StatusCode)
82+
return buf, fmt.Errorf("failed to download %q, HTTP status: %s", target, resp.Status)
4383
}
4484

45-
bytes, err := io.ReadAll(resp.Body)
46-
if err != nil {
47-
return data, err
85+
if _, err := io.Copy(&buf, io.LimitReader(resp.Body, maxDownloadSize+1)); err != nil {
86+
return buf, err
87+
}
88+
if buf.Len() > maxDownloadSize {
89+
return bytes.Buffer{}, fmt.Errorf("failed to download %q: response exceeds %d bytes", target, maxDownloadSize)
4890
}
4991

50-
xml.Unmarshal(bytes, &data)
51-
52-
return data, nil
92+
return buf, nil
5393
}

‎tzdata/download_wintz_test.go‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,73 @@
11
package tzdata
22

3-
import "testing"
3+
import (
4+
"io"
5+
"net/http"
6+
"os"
7+
"strings"
8+
"testing"
9+
)
410

511
func TestDownloadWindowsZones(t *testing.T) {
12+
if os.Getenv("TZLOCAL_LIVE_TESTS") == "" {
13+
t.Skip("set TZLOCAL_LIVE_TESTS=1 to run live download tests")
14+
}
615
_, err := DownloadWindowsZones()
716
if err != nil {
817
t.Errorf("error: %v", err)
918
}
1019
}
20+
21+
func TestDownloadWindowsZonesRejectsInvalidData(t *testing.T) {
22+
for _, body := range []string{
23+
"not XML",
24+
"<supplementalData/>",
25+
"<supplementalData><windowsZones><mapTimezones/></windowsZones></supplementalData>",
26+
} {
27+
t.Run(body, func(t *testing.T) {
28+
if _, err := parseWindowsZones([]byte(body), "test data"); err == nil {
29+
t.Fatal("parseWindowsZones() error = nil, want an error")
30+
}
31+
})
32+
}
33+
}
34+
35+
func TestDownloadHTTP(t *testing.T) {
36+
client := &http.Client{Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
37+
status := http.StatusOK
38+
body := "contents"
39+
switch r.URL.Path {
40+
case "/ok":
41+
case "/large":
42+
body = strings.Repeat("x", maxDownloadSize+1)
43+
default:
44+
status = http.StatusNotFound
45+
}
46+
return &http.Response{
47+
StatusCode: status,
48+
Status: http.StatusText(status),
49+
Body: io.NopCloser(strings.NewReader(body)),
50+
Header: make(http.Header),
51+
}, nil
52+
})}
53+
54+
buf, err := downloadHTTPWithClient(client, "http://example.test/ok")
55+
if err != nil {
56+
t.Fatalf("downloadHTTP() failed: %v", err)
57+
}
58+
if got, want := buf.String(), "contents"; got != want {
59+
t.Fatalf("downloadHTTP() = %q, want %q", got, want)
60+
}
61+
if _, err := downloadHTTPWithClient(client, "http://example.test/missing"); err == nil {
62+
t.Fatal("downloadHTTP() accepted a non-success HTTP status")
63+
}
64+
if _, err := downloadHTTPWithClient(client, "http://example.test/large"); err == nil {
65+
t.Fatal("downloadHTTP() accepted an oversized response")
66+
}
67+
}
68+
69+
type roundTripperFunc func(*http.Request) (*http.Response, error)
70+
71+
func (f roundTripperFunc) RoundTrip(request *http.Request) (*http.Response, error) {
72+
return f(request)
73+
}

0 commit comments

Comments
 (0)