Commit e0ed7093 authored by Toby Allen's avatar Toby Allen Committed by Matt Holt

rewrite: Raise error if rewrite path does not begin with / #1610 (#1629)

* Raise syntax error if no '/' prefix to rewrite. Added Tests

* fix case where to keyword is used.

* Fixed spelling issue

* Changes to use Errf rather than new Err function

* Remove new RewritePathErr Function
parent b3dd6049
...@@ -76,6 +76,10 @@ func rewriteParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) { ...@@ -76,6 +76,10 @@ func rewriteParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) {
return nil, c.ArgErr() return nil, c.ArgErr()
} }
to = strings.Join(args1, " ") to = strings.Join(args1, " ")
// ensure rewrite path begins with /
if !strings.HasPrefix(to, "/") {
return nil, c.Errf("%s:%d - Syntax error: Rewrite path must begin with '/'. Provided: '%s'", c.File(), c.Line(), c.Val())
}
case "ext": case "ext":
args1 := c.RemainingArgs() args1 := c.RemainingArgs()
if len(args1) == 0 { if len(args1) == 0 {
...@@ -90,14 +94,20 @@ func rewriteParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) { ...@@ -90,14 +94,20 @@ func rewriteParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) {
if to == "" { if to == "" {
return nil, c.ArgErr() return nil, c.ArgErr()
} }
if rule, err = NewComplexRule(base, pattern, to, ext, matcher); err != nil { if rule, err = NewComplexRule(base, pattern, to, ext, matcher); err != nil {
return nil, err return nil, err
} }
rules = append(rules, rule) rules = append(rules, rule)
// the only unhandled case is 2 and above // handle case of 2 arguments: "from to"
default: default:
rule = NewSimpleRule(args[0], strings.Join(args[1:], " ")) // ensure rewrite path begins with /
topath := strings.Join(args[1:], " ")
if !strings.HasPrefix(topath, "/") {
return nil, c.Errf("%s:%d - Syntax error: Rewrite path must begin with '/'. Provided: '%s'", c.File(), c.Line(), c.Val())
}
rule = NewSimpleRule(args[0], topath)
rules = append(rules, rule) rules = append(rules, rule)
} }
......
...@@ -45,15 +45,19 @@ func TestRewriteParse(t *testing.T) { ...@@ -45,15 +45,19 @@ func TestRewriteParse(t *testing.T) {
SimpleRule{From: "/from", To: "/to"}, SimpleRule{From: "/from", To: "/to"},
}}, }},
{`rewrite /from /to {`rewrite /from /to
rewrite a b`, false, []Rule{ rewrite a /b`, false, []Rule{
SimpleRule{From: "/from", To: "/to"}, SimpleRule{From: "/from", To: "/to"},
SimpleRule{From: "a", To: "b"}, SimpleRule{From: "a", To: "/b"},
}}, }},
{`rewrite a b`, true, []Rule{}},
{`rewrite a`, true, []Rule{}}, {`rewrite a`, true, []Rule{}},
{`rewrite`, true, []Rule{}}, {`rewrite`, true, []Rule{}},
{`rewrite a b c`, false, []Rule{ {`rewrite a b c`, true, []Rule{
SimpleRule{From: "a", To: "b c"}, SimpleRule{From: "a", To: "b c"},
}}, }},
{`rewrite a /b c`, false, []Rule{
SimpleRule{From: "a", To: "/b c"},
}},
} }
for i, test := range simpleTests { for i, test := range simpleTests {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment