Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 01:30 UTC
map: Fix 95c03506 (avoid repeated expansions)
Matthew Holt committed
Oct 19, 2021 at 12:25 UTC
a2119c09e95eeb871c2b30046f596d61257b108c
2 files changed
+33
-22
modules/caddyhttp/map/map.go
+11
-17
@@ -131,27 +131,21 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
131
// find the first mapping matching the input and return
132
// the requested destination/output value
133
for _, m := range h.Mappings {
134
- if m.re != nil {
135
- if m.re.MatchString(input) {
136
- if output := m.Outputs[destIdx]; output == nil {
137
- continue
138
- } else {
139
- var result []byte
140
- for _, submatches := range m.re.FindAllStringSubmatchIndex(input, -1) {
141
- result = m.re.ExpandString(result, m.Outputs[destIdx].(string), input, submatches)
142
- }
143
- output = string(result)
144
- return output, true
145
- }
146
- }
134
+ output := m.Outputs[destIdx]
135
+ if output == nil {
136
continue
137
}
149
- if input == m.Input {
150
- if output := m.Outputs[destIdx]; output == nil {
138
+ if m.re != nil {
139
+ var result []byte
140
+ matches := m.re.FindStringSubmatchIndex(input)
141
+ if matches == nil {
142
continue
152
- } else {
153
- return output, true
143
}
144
+ result = m.re.ExpandString(result, output.(string), input, matches)
145
+ return string(result), true
146
+ }
147
+ if input == m.Input {
148
+ return output, true
149
}
150
}
151
modules/caddyhttp/map/map_test.go
+22
-5
@@ -14,11 +14,11 @@ import (
14
func TestHandler(t *testing.T) {
15
for i, tc := range []struct {
16
handler Handler
17
- reqPath string
17
+ reqURI string
18
expect map[string]interface{}
19
}{
20
{
21
- reqPath: "/foo",
21
+ reqURI: "/foo",
22
handler: Handler{
23
Source: "{http.request.uri.path}",
24
Destinations: []string{"{output}"},
@@ -34,7 +34,7 @@ func TestHandler(t *testing.T) {
34
},
35
},
36
{
37
- reqPath: "/abcdef",
37
+ reqURI: "/abcdef",
38
handler: Handler{
39
Source: "{http.request.uri.path}",
40
Destinations: []string{"{output}"},
@@ -50,7 +50,7 @@ func TestHandler(t *testing.T) {
50
},
51
},
52
{
53
- reqPath: "/ABCxyzDEF",
53
+ reqURI: "/ABCxyzDEF",
54
handler: Handler{
55
Source: "{http.request.uri.path}",
56
Destinations: []string{"{output}"},
@@ -65,12 +65,29 @@ func TestHandler(t *testing.T) {
65
"output": "...xyz...",
66
},
67
},
68
+ {
69
+ // Test case from https://caddy.community/t/map-directive-and-regular-expressions/13866/14?u=matt
70
+ reqURI: "/?s=0%27+AND+%28SELECT+0+FROM+%28SELECT+count%28%2A%29%2C+CONCAT%28%28SELECT+%40%40version%29%2C+0x23%2C+FLOOR%28RAND%280%29%2A2%29%29+AS+x+FROM+information_schema.columns+GROUP+BY+x%29+y%29+-+-+%27",
71
+ handler: Handler{
72
+ Source: "{http.request.uri}",
73
+ Destinations: []string{"{output}"},
74
+ Mappings: []Mapping{
75
+ {
76
+ InputRegexp: "(?i)(\\^|`|<|>|%|\\\\|\\{|\\}|\\|)",
77
+ Outputs: []interface{}{"3"},
78
+ },
79
+ },
80
+ },
81
+ expect: map[string]interface{}{
82
+ "output": "3",
83
+ },
84
+ },
85
} {
86
if err := tc.handler.Provision(caddy.Context{}); err != nil {
87
t.Fatalf("Test %d: Provisioning handler: %v", i, err)
88
}
89
73
- req, err := http.NewRequest(http.MethodGet, tc.reqPath, nil)
90
+ req, err := http.NewRequest(http.MethodGet, tc.reqURI, nil)
91
if err != nil {
92
t.Fatalf("Test %d: Creating request: %v", i, err)
93
}