Skip to content

Commit 2f681f5

Browse files
committed
Require geofeeds to be valid UTF-8
1 parent 633a920 commit 2f681f5

11 files changed

Lines changed: 68 additions & 26 deletions

go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ module github.com/maxmind/mm-geofeed-verifier/v3
33
go 1.24.0
44

55
require (
6-
github.com/TomOnTime/utfutil v1.0.0
7-
github.com/oschwald/geoip2-golang v1.13.0
6+
github.com/oschwald/maxminddb-golang/v2 v2.1.0
87
github.com/stretchr/testify v1.11.1
98
)
109

1110
require (
1211
github.com/davecgh/go-spew v1.1.1 // indirect
13-
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
14-
github.com/oschwald/maxminddb-golang/v2 v2.1.0 // indirect
1512
github.com/pmezard/go-difflib v1.0.0 // indirect
1613
golang.org/x/sys v0.37.0 // indirect
17-
golang.org/x/text v0.23.0 // indirect
1814
gopkg.in/yaml.v3 v3.0.1 // indirect
1915
)

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
github.com/TomOnTime/utfutil v1.0.0 h1:/0Ivgo2OjXJxo8i7zgvs7ewSFZMLwCRGm3P5Umowb90=
2-
github.com/TomOnTime/utfutil v1.0.0/go.mod h1:l9lZmOniizVSuIliSkEf87qivMRlSNzbdBFKjuLRg1c=
31
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
42
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-
github.com/oschwald/geoip2-golang v1.13.0 h1:Q44/Ldc703pasJeP5V9+aFSZFmBN7DKHbNsSFzQATJI=
6-
github.com/oschwald/geoip2-golang v1.13.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
7-
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
8-
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
93
github.com/oschwald/maxminddb-golang/v2 v2.1.0 h1:2Iv7lmG9XtxuZA/jFAsd7LnZaC1E59pFsj5O/nU15pw=
104
github.com/oschwald/maxminddb-golang/v2 v2.1.0/go.mod h1:gG4V88LsawPEqtbL1Veh1WRh+nVSYwXzJ1P5Fcn77g0=
115
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
126
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
137
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
148
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
15-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
16-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
179
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
1810
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
19-
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
20-
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
2111
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2212
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2313
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

verify/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package verify
33
import "errors"
44

55
var (
6+
// ErrNotUTF8 indicates a file encoding that is not valid UTF-8 (with
7+
// optional BOM). RFC 8805 says that "feeds MUST use UTF-8 character
8+
// encoding". This is a separate error from ErrInvalidGeofeed, because we
9+
// can't confidently read anything from the file if it's not UTF-8.
10+
ErrNotUTF8 = errors.New("geofeed is not valid UTF-8")
611
// ErrInvalidGeofeed represents error that is returned in case of incomplete
712
// compliance with RFC 8805 standards and the mode in which the program is
813
// run.
-200 Bytes
Binary file not shown.
-189 Bytes
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2a02:ecc0::/29 ,US,US-NJ, Parsippany ,
2+
202.196.224.5/32,AT,AT-9,Vienna,1060,/22
3+
# Verifier should compare most specific subdivision, i.e. GB-WBK instead of GB-ENG
4+
2.125.160.216/29,GB,GB-WBK,“Œ‹ž,
406 Bytes
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2a02:ecc0::/29 ,US,US-NJ, Parsippany ,
2+
202.196.224.5/32,AT,AT-9,Vienna,1060,/22
3+
# Verifier should compare most specific subdivision, i.e. GB-WBK instead of GB-ENG
4+
2.125.160.216/29,GB,GB-WBK,Boxford,

verify/test_data/geofeed-valid.csv

-204 Bytes
Binary file not shown.

verify/verify.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
package verify
44

55
import (
6+
"bytes"
67
"encoding/csv"
78
"errors"
89
"fmt"
910
"io"
10-
"log"
1111
"net/netip"
12+
"os"
1213
"path/filepath"
1314
"strings"
15+
"unicode/utf8"
1416

15-
"github.com/TomOnTime/utfutil"
1617
"github.com/oschwald/maxminddb-golang/v2"
1718
)
1819

@@ -62,20 +63,20 @@ func ProcessGeofeed(
6263
c := NewCheckResult()
6364
var diffLines []string
6465

65-
// Use utfutil to remove a BOM, if present (common on files from Windows).
66-
// See https://github.com/golang/go/issues/33887.
67-
geofeedFH, err := utfutil.OpenFile(filepath.Clean(geofeedFilename), utfutil.UTF8)
66+
geofeedData, err := os.ReadFile(filepath.Clean(geofeedFilename))
6867
if err != nil {
6968
if opts.HideFilePathsInErrorMessages {
7069
return c, diffLines, nil, fmt.Errorf("unable to open file: %w", err)
7170
}
7271
return c, diffLines, nil, fmt.Errorf("unable to open %s: %w", geofeedFilename, err)
7372
}
74-
defer func() {
75-
if err := geofeedFH.Close(); err != nil {
76-
log.Println(err)
77-
}
78-
}()
73+
74+
// Strip UTF-8 BOM if present (common on files from Windows).
75+
geofeedData = bytes.TrimPrefix(geofeedData, []byte{0xEF, 0xBB, 0xBF})
76+
77+
if !utf8.Valid(geofeedData) {
78+
return c, diffLines, nil, ErrNotUTF8
79+
}
7980

8081
db, err := maxminddb.Open(filepath.Clean(mmdbFilename))
8182
if err != nil {
@@ -99,7 +100,7 @@ func ProcessGeofeed(
99100
}
100101
asnCounts := map[uint]int{}
101102

102-
csvReader := csv.NewReader(geofeedFH)
103+
csvReader := csv.NewReader(bytes.NewReader(geofeedData))
103104
csvReader.ReuseRecord = true
104105
csvReader.Comment = '#'
105106
csvReader.FieldsPerRecord = -1

0 commit comments

Comments
 (0)