Skip to content

Commit 1e68c42

Browse files
Merge pull request #74 from maxmind/mm_david/hide-errors
Add option to toggle file paths in error messages
2 parents 2cababd + ddb42af commit 1e68c42

5 files changed

Lines changed: 52 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## CHANGELOG
22

3+
## 3.0.0 (2024-08-14)
4+
5+
* Update interface of ProcessGeofeed in the verifier package, adding a new
6+
struct to hold verification options. This will make it easier to add/remove
7+
options in the future.
8+
* Add an option to ProcessGeofeed to reduce the verbosity of error messages,
9+
toggling whether file paths are included.
10+
311
## 2.4.0 (2023-07-13)
412

513
* Update files to comply with major release version 2

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/maxmind/mm-geofeed-verifier/v2
1+
module github.com/maxmind/mm-geofeed-verifier/v3
22

33
go 1.21
44

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"sort" //nolint:depguard // preexisting
1919
"strings"
2020

21-
"github.com/maxmind/mm-geofeed-verifier/v2/verify"
21+
"github.com/maxmind/mm-geofeed-verifier/v3/verify"
2222
)
2323

2424
// This value is set by build scripts. Changing the name of
@@ -46,7 +46,12 @@ func run() error {
4646
return err
4747
}
4848

49-
c, diffLines, asnCounts, err := verify.ProcessGeofeed(conf.gf, conf.db, conf.isp, conf.laxMode)
49+
c, diffLines, asnCounts, err := verify.ProcessGeofeed(
50+
conf.gf,
51+
conf.db,
52+
conf.isp,
53+
verify.Options{LaxMode: conf.laxMode},
54+
)
5055
if err != nil {
5156
if errors.Is(err, verify.ErrInvalidGeofeed) {
5257
log.Printf("Found %d invalid rows out of %d rows in total, examples by type:", c.Invalid, c.Total)

verify/verify.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@ func NewCheckResult() CheckResult {
3737
}
3838
}
3939

40+
// Options contains configuration options for geofeed verification.
41+
type Options struct {
42+
// // LaxMode controls validation for region codes. If LaxMode is false
43+
// (default), ISO-3166-2 region codes format is required. Otherwise region
44+
// code is accepted both with or without country code.
45+
LaxMode bool
46+
// HideFilePathsInErrorMessages, if set to true, will prevent file paths
47+
// from appearing in error messages. This reduces information leakage in
48+
// contexts where the error messages might be shared.
49+
HideFilePathsInErrorMessages bool
50+
}
51+
4052
// ProcessGeofeed attempts to validate a given geofeedFilename.
41-
// If laxMode is false (default), ISO-3166-2 region codes format is required.
42-
// Otherwise region code is accepted both with or without country code.
4353
func ProcessGeofeed(
4454
geofeedFilename,
4555
mmdbFilename,
4656
ispFilename string,
47-
laxMode bool,
57+
opts Options,
4858
) (CheckResult, []string, map[uint]int, error) { //nolint:unparam // false positive on map[uint]int
4959
c := NewCheckResult()
5060
var diffLines []string
@@ -53,6 +63,9 @@ func ProcessGeofeed(
5363
// See https://github.com/golang/go/issues/33887.
5464
geofeedFH, err := utfutil.OpenFile(filepath.Clean(geofeedFilename), utfutil.UTF8)
5565
if err != nil {
66+
if opts.HideFilePathsInErrorMessages {
67+
return c, diffLines, nil, fmt.Errorf("unable to open file: %w", err)
68+
}
5669
return c, diffLines, nil, fmt.Errorf("unable to open %s: %w", geofeedFilename, err)
5770
}
5871
defer func() {
@@ -63,6 +76,9 @@ func ProcessGeofeed(
6376

6477
db, err := geoip2.Open(filepath.Clean(mmdbFilename))
6578
if err != nil {
79+
if opts.HideFilePathsInErrorMessages {
80+
return c, diffLines, nil, fmt.Errorf("unable to open MMDB: %w", err)
81+
}
6682
return c, diffLines, nil, fmt.Errorf("unable to open MMDB %s: %w", mmdbFilename, err)
6783
}
6884
defer db.Close()
@@ -71,7 +87,10 @@ func ProcessGeofeed(
7187
if ispFilename != "" {
7288
ispdb, err = geoip2.Open(filepath.Clean(ispFilename))
7389
if err != nil {
74-
return c, diffLines, nil, fmt.Errorf("unable to open MMDB %s: %w", ispFilename, err)
90+
if opts.HideFilePathsInErrorMessages {
91+
return c, diffLines, nil, fmt.Errorf("unable to open ISP MMDB: %w", err)
92+
}
93+
return c, diffLines, nil, fmt.Errorf("unable to open ISP MMDB %s: %w", ispFilename, err)
7594
}
7695
defer ispdb.Close()
7796
}
@@ -91,8 +110,10 @@ func ProcessGeofeed(
91110
break
92111
}
93112
if err != nil {
94-
return c, diffLines, asnCounts,
95-
fmt.Errorf("unable to read next row in %s: %w", geofeedFilename, err)
113+
if opts.HideFilePathsInErrorMessages {
114+
return c, diffLines, asnCounts, fmt.Errorf("unable to read next row: %w", err)
115+
}
116+
return c, diffLines, asnCounts, fmt.Errorf("unable to read next row in %s: %w", geofeedFilename, err)
96117
}
97118

98119
c.Total++
@@ -111,7 +132,7 @@ func ProcessGeofeed(
111132
continue
112133
}
113134

114-
diffLine, result := verifyCorrection(row[:expectedFieldsPerRecord], db, ispdb, asnCounts, laxMode)
135+
diffLine, result := verifyCorrection(row[:expectedFieldsPerRecord], db, ispdb, asnCounts, opts)
115136
if !result.valid {
116137
if _, ok := c.SampleInvalidRows[result.invalidityType]; !ok {
117138
c.SampleInvalidRows[result.invalidityType] = fmt.Sprintf(
@@ -130,8 +151,10 @@ func ProcessGeofeed(
130151
}
131152
}
132153
if err != nil && !errors.Is(err, io.EOF) {
133-
return c, diffLines, asnCounts,
134-
fmt.Errorf("error while reading %s: %w", geofeedFilename, err)
154+
if opts.HideFilePathsInErrorMessages {
155+
return c, diffLines, asnCounts, fmt.Errorf("error reading file: %w", err)
156+
}
157+
return c, diffLines, asnCounts, fmt.Errorf("error while reading %s: %w", geofeedFilename, err)
135158
}
136159

137160
if c.Invalid > 0 || len(c.SampleInvalidRows) > 0 {
@@ -151,7 +174,7 @@ func verifyCorrection(
151174
correction []string,
152175
db, ispdb *geoip2.Reader,
153176
asnCounts map[uint]int,
154-
laxMode bool,
177+
opts Options,
155178
) (string, verificationResult) {
156179
/*
157180
0: network (CIDR or single IP)
@@ -207,7 +230,7 @@ func verifyCorrection(
207230
// In "--lax" mode both region code formats (with or without country code) are accepted.
208231
if strings.Contains(correction[2], "-") {
209232
mostSpecificSubdivision = mmdbRecord.Country.IsoCode + "-" + mostSpecificSubdivision
210-
} else if correction[2] != "" && !laxMode {
233+
} else if correction[2] != "" && !opts.LaxMode {
211234
return "", verificationResult{
212235
valid: false,
213236
invalidityType: InvalidRegionCode,

verify/verify_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestProcessGeofeed_Valid(t *testing.T) {
8181
for _, test := range goodTests {
8282
t.Run(
8383
test.gf+" "+test.db, func(t *testing.T) {
84-
c, dl, _, err := ProcessGeofeed(test.gf, test.db, "", test.laxMode)
84+
c, dl, _, err := ProcessGeofeed(test.gf, test.db, "", Options{LaxMode: test.laxMode})
8585
require.NoError(t, err, "processGeofeed ran without error")
8686
for i, s := range test.dl {
8787
assert.Contains(
@@ -167,7 +167,7 @@ func TestProcessGeofeed_Invalid(t *testing.T) {
167167
for _, test := range badTests {
168168
t.Run(
169169
test.gf+" "+test.db, func(t *testing.T) {
170-
c, _, _, err := ProcessGeofeed(test.gf, test.db, "", test.laxMode)
170+
c, _, _, err := ProcessGeofeed(test.gf, test.db, "", Options{LaxMode: test.laxMode})
171171
require.ErrorIs(
172172
t,
173173
err,

0 commit comments

Comments
 (0)