@@ -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.
4353func 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 ,
0 commit comments