mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
map: Fix 95c03506
(avoid repeated expansions)
This commit is contained in:
parent
062657d0d8
commit
a2119c09e9
2 changed files with 33 additions and 22 deletions
|
@ -131,27 +131,21 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt
|
||||||
// find the first mapping matching the input and return
|
// find the first mapping matching the input and return
|
||||||
// the requested destination/output value
|
// the requested destination/output value
|
||||||
for _, m := range h.Mappings {
|
for _, m := range h.Mappings {
|
||||||
if m.re != nil {
|
output := m.Outputs[destIdx]
|
||||||
if m.re.MatchString(input) {
|
if output == nil {
|
||||||
if output := m.Outputs[destIdx]; output == nil {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
var result []byte
|
|
||||||
for _, submatches := range m.re.FindAllStringSubmatchIndex(input, -1) {
|
|
||||||
result = m.re.ExpandString(result, m.Outputs[destIdx].(string), input, submatches)
|
|
||||||
}
|
|
||||||
output = string(result)
|
|
||||||
return output, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if input == m.Input {
|
if m.re != nil {
|
||||||
if output := m.Outputs[destIdx]; output == nil {
|
var result []byte
|
||||||
|
matches := m.re.FindStringSubmatchIndex(input)
|
||||||
|
if matches == nil {
|
||||||
continue
|
continue
|
||||||
} else {
|
|
||||||
return output, true
|
|
||||||
}
|
}
|
||||||
|
result = m.re.ExpandString(result, output.(string), input, matches)
|
||||||
|
return string(result), true
|
||||||
|
}
|
||||||
|
if input == m.Input {
|
||||||
|
return output, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,11 @@ import (
|
||||||
func TestHandler(t *testing.T) {
|
func TestHandler(t *testing.T) {
|
||||||
for i, tc := range []struct {
|
for i, tc := range []struct {
|
||||||
handler Handler
|
handler Handler
|
||||||
reqPath string
|
reqURI string
|
||||||
expect map[string]interface{}
|
expect map[string]interface{}
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
reqPath: "/foo",
|
reqURI: "/foo",
|
||||||
handler: Handler{
|
handler: Handler{
|
||||||
Source: "{http.request.uri.path}",
|
Source: "{http.request.uri.path}",
|
||||||
Destinations: []string{"{output}"},
|
Destinations: []string{"{output}"},
|
||||||
|
@ -34,7 +34,7 @@ func TestHandler(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
reqPath: "/abcdef",
|
reqURI: "/abcdef",
|
||||||
handler: Handler{
|
handler: Handler{
|
||||||
Source: "{http.request.uri.path}",
|
Source: "{http.request.uri.path}",
|
||||||
Destinations: []string{"{output}"},
|
Destinations: []string{"{output}"},
|
||||||
|
@ -50,7 +50,7 @@ func TestHandler(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
reqPath: "/ABCxyzDEF",
|
reqURI: "/ABCxyzDEF",
|
||||||
handler: Handler{
|
handler: Handler{
|
||||||
Source: "{http.request.uri.path}",
|
Source: "{http.request.uri.path}",
|
||||||
Destinations: []string{"{output}"},
|
Destinations: []string{"{output}"},
|
||||||
|
@ -65,12 +65,29 @@ func TestHandler(t *testing.T) {
|
||||||
"output": "...xyz...",
|
"output": "...xyz...",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
// Test case from https://caddy.community/t/map-directive-and-regular-expressions/13866/14?u=matt
|
||||||
|
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",
|
||||||
|
handler: Handler{
|
||||||
|
Source: "{http.request.uri}",
|
||||||
|
Destinations: []string{"{output}"},
|
||||||
|
Mappings: []Mapping{
|
||||||
|
{
|
||||||
|
InputRegexp: "(?i)(\\^|`|<|>|%|\\\\|\\{|\\}|\\|)",
|
||||||
|
Outputs: []interface{}{"3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: map[string]interface{}{
|
||||||
|
"output": "3",
|
||||||
|
},
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
if err := tc.handler.Provision(caddy.Context{}); err != nil {
|
if err := tc.handler.Provision(caddy.Context{}); err != nil {
|
||||||
t.Fatalf("Test %d: Provisioning handler: %v", i, err)
|
t.Fatalf("Test %d: Provisioning handler: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, tc.reqPath, nil)
|
req, err := http.NewRequest(http.MethodGet, tc.reqURI, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Test %d: Creating request: %v", i, err)
|
t.Fatalf("Test %d: Creating request: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue