Skip to content

Commit 0fce9c8

Browse files
authored
Merge pull request #14 from justincjohnson/justincjohnson/validate-status
fix: validate status codes
2 parents 4946b03 + 7216fa7 commit 0fce9c8

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

redirects.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,25 @@ func ParseString(s string) ([]Rule, error) {
154154
func parseStatus(s string) (code int, err error) {
155155
if strings.HasSuffix(s, "!") {
156156
// See https://docs.netlify.com/routing/redirects/rewrites-proxies/#shadowing
157-
return -1, fmt.Errorf("forced redirects (or \"shadowing\") are not supported by IPFS gateways")
157+
return 0, fmt.Errorf("forced redirects (or \"shadowing\") are not supported by IPFS gateways")
158158
}
159159

160160
code, err = strconv.Atoi(s)
161-
return code, err
161+
if err != nil {
162+
return 0, err
163+
}
164+
165+
if !isValidStatusCode(code) {
166+
return 0, fmt.Errorf("status code %d is not supported", code)
167+
}
168+
169+
return code, nil
170+
}
171+
172+
func isValidStatusCode(status int) bool {
173+
switch status {
174+
case 200, 301, 302, 303, 307, 308, 404, 410, 451:
175+
return true
176+
}
177+
return false
162178
}

redirects_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ func TestRule_IsRewrite(t *testing.T) {
5858
})
5959
}
6060

61-
func TestParse(t *testing.T) {
61+
func Test_Parse(t *testing.T) {
6262
t.Run("with illegal force", func(t *testing.T) {
6363
_, err := Parse(strings.NewReader(`
6464
/home / 301!
6565
`))
6666

67-
assert.Error(t, err, "forced redirects should return an error")
67+
assert.Error(t, err)
68+
assert.Contains(t, err.Error(), "forced redirects")
69+
})
70+
71+
t.Run("with illegal code", func(t *testing.T) {
72+
_, err := Parse(strings.NewReader(`
73+
/home / 42
74+
`))
75+
76+
assert.Error(t, err)
77+
assert.Contains(t, err.Error(), "status code 42 is not supported")
6878
})
6979
}

0 commit comments

Comments
 (0)