Skip to content

Commit 4bd8bd1

Browse files
authored
Fix bug overwriting defaults (#32)
1 parent d8e9a1a commit 4bd8bd1

File tree

2 files changed

+41
-45
lines changed

2 files changed

+41
-45
lines changed

main.go

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,24 @@ type captchaResponse struct {
7474

7575
func CreateConfig() *Config {
7676
return &Config{
77-
RateLimit: 20,
78-
Window: 86400,
79-
IPv4SubnetMask: 16,
80-
IPv6SubnetMask: 64,
81-
IPForwardedHeader: "",
82-
ProtectParameters: "false",
83-
ProtectRoutes: []string{},
84-
ExcludeRoutes: []string{},
85-
ProtectHttpMethods: []string{},
86-
ProtectFileExtensions: []string{
87-
"html",
88-
},
89-
GoodBots: []string{},
90-
ExemptIPs: []string{
91-
"127.0.0.0/8",
92-
"10.0.0.0/8",
93-
"172.16.0.0/12",
94-
"192.168.0.0/16",
95-
"fc00::/8",
96-
},
97-
ChallengeURL: "/challenge",
98-
ChallengeTmpl: "challenge.tmpl.html",
99-
EnableStatsPage: "false",
100-
LogLevel: "INFO",
101-
IPDepth: 0,
77+
RateLimit: 20,
78+
Window: 86400,
79+
IPv4SubnetMask: 16,
80+
IPv6SubnetMask: 64,
81+
IPForwardedHeader: "",
82+
ProtectParameters: "false",
83+
ProtectRoutes: []string{},
84+
ExcludeRoutes: []string{},
85+
ProtectHttpMethods: []string{},
86+
ProtectFileExtensions: []string{},
87+
GoodBots: []string{},
88+
ExemptIPs: []string{},
89+
ChallengeURL: "/challenge",
90+
ChallengeTmpl: "challenge.tmpl.html",
91+
EnableStatsPage: "false",
92+
LogLevel: "INFO",
93+
IPDepth: 0,
94+
CaptchaProvider: "turnstile",
10295
}
10396
}
10497

@@ -156,9 +149,21 @@ func NewCaptchaProtect(ctx context.Context, next http.Handler, config *Config, n
156149
}
157150
}
158151

152+
if !strInSlice("html", config.ProtectFileExtensions) {
153+
config.ProtectFileExtensions = append(config.ProtectFileExtensions, "html")
154+
}
155+
159156
// transform exempt IP strings into what go can easily parse (net.IPNet)
160157
var ips []*net.IPNet
161-
for _, ip := range config.ExemptIPs {
158+
exemptIps := []string{
159+
"127.0.0.0/8",
160+
"10.0.0.0/8",
161+
"172.16.0.0/12",
162+
"192.168.0.0/16",
163+
"fc00::/8",
164+
}
165+
exemptIps = append(exemptIps, config.ExemptIPs...)
166+
for _, ip := range exemptIps {
162167
parsedIp, err := ParseCIDR(ip)
163168
if err != nil {
164169
return nil, fmt.Errorf("error parsing cidr %s: %v", ip, err)

main_test.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,9 @@ func TestRouteIsProtected(t *testing.T) {
395395
c.ProtectRoutes = append(c.ProtectRoutes, tt.config.ProtectRoutes...)
396396
c.ExcludeRoutes = append(c.ExcludeRoutes, tt.config.ExcludeRoutes...)
397397
c.ProtectFileExtensions = append(c.ProtectFileExtensions, tt.config.ProtectFileExtensions...)
398-
bc := &CaptchaProtect{
399-
config: c,
398+
bc, err := NewCaptchaProtect(context.Background(), nil, c, "captcha-protect")
399+
if err != nil {
400+
t.Errorf("unexpected error %v", err)
400401
}
401402

402403
result := bc.RouteIsProtected(tt.path)
@@ -411,7 +412,6 @@ func TestGetClientIP(t *testing.T) {
411412
tests := []struct {
412413
name string
413414
config Config
414-
exemptIps []*net.IPNet
415415
headerValue string
416416
remoteAddr string
417417
expectedIP string
@@ -422,7 +422,6 @@ func TestGetClientIP(t *testing.T) {
422422
IPForwardedHeader: "X-Forwarded-For",
423423
IPDepth: 0,
424424
},
425-
exemptIps: []*net.IPNet{},
426425
headerValue: "1.1.1.1, 2.2.2.2",
427426
remoteAddr: "3.3.3.3:1234",
428427
expectedIP: "2.2.2.2",
@@ -432,8 +431,8 @@ func TestGetClientIP(t *testing.T) {
432431
config: Config{
433432
IPForwardedHeader: "X-Forwarded-For",
434433
IPDepth: 0,
434+
ExemptIPs: []string{"2.2.2.2/32"},
435435
},
436-
exemptIps: []*net.IPNet{parseCIDR("2.2.2.2/32", t)},
437436
headerValue: "1.1.1.1, 3.3.3.3, 2.2.2.2",
438437
remoteAddr: "3.3.3.3:1234",
439438
expectedIP: "3.3.3.3",
@@ -444,7 +443,6 @@ func TestGetClientIP(t *testing.T) {
444443
IPForwardedHeader: "X-Forwarded-For",
445444
IPDepth: 1,
446445
},
447-
exemptIps: []*net.IPNet{},
448446
headerValue: "1.1.1.1, 2.2.2.2, 3.3.3.3, 127.0.0.1, 192.168.0.1",
449447
remoteAddr: "3.3.3.3:1234",
450448
expectedIP: "2.2.2.2",
@@ -455,7 +453,6 @@ func TestGetClientIP(t *testing.T) {
455453
IPForwardedHeader: "X-Forwarded-For",
456454
IPDepth: 1,
457455
},
458-
exemptIps: []*net.IPNet{},
459456
headerValue: "1.1.1.1, 2.2.2.2, 3.3.3.3",
460457
remoteAddr: "3.3.3.3:1234",
461458
expectedIP: "2.2.2.2",
@@ -465,8 +462,8 @@ func TestGetClientIP(t *testing.T) {
465462
config: Config{
466463
IPForwardedHeader: "X-Forwarded-For",
467464
IPDepth: 0,
465+
ExemptIPs: []string{"2.2.0.0/16"},
468466
},
469-
exemptIps: []*net.IPNet{parseCIDR("2.2.0.0/16", t)},
470467
headerValue: "127.0.0.1, 192.168.1.1, 172.16.1.2, 2.2.3.4",
471468
remoteAddr: "4.4.4.4:5678",
472469
expectedIP: "4.4.4.4",
@@ -477,7 +474,6 @@ func TestGetClientIP(t *testing.T) {
477474
IPForwardedHeader: "X-Forwarded-For",
478475
IPDepth: 0,
479476
},
480-
exemptIps: []*net.IPNet{},
481477
headerValue: "",
482478
remoteAddr: "4.4.4.4:5678",
483479
expectedIP: "4.4.4.4",
@@ -488,7 +484,6 @@ func TestGetClientIP(t *testing.T) {
488484
IPDepth: 0,
489485
IPForwardedHeader: "",
490486
},
491-
exemptIps: []*net.IPNet{},
492487
headerValue: "shouldBeIgnored",
493488
remoteAddr: "5.5.5.5:4321",
494489
expectedIP: "5.5.5.5",
@@ -506,16 +501,12 @@ func TestGetClientIP(t *testing.T) {
506501
c := CreateConfig()
507502
c.IPForwardedHeader = tc.config.IPForwardedHeader
508503
c.IPDepth = tc.config.IPDepth
509-
exemptIps := tc.exemptIps
510-
bc := &CaptchaProtect{
511-
config: c,
512-
ipv4Mask: net.CIDRMask(16, 32),
513-
ipv6Mask: net.CIDRMask(64, 128),
514-
}
515-
for _, ip := range c.ExemptIPs {
516-
exemptIps = append(exemptIps, parseCIDR(ip, t))
504+
c.ProtectRoutes = []string{"/"}
505+
c.ExemptIPs = tc.config.ExemptIPs
506+
bc, err := NewCaptchaProtect(context.Background(), nil, c, "captcha-protect")
507+
if err != nil {
508+
t.Errorf("unexpected error %v", err)
517509
}
518-
bc.exemptIps = exemptIps
519510

520511
ip, _ := bc.getClientIP(req)
521512
if ip != tc.expectedIP {

0 commit comments

Comments
 (0)