repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
7bf1673
parent
6503633
author
Mac Chaffee
date
2024-10-13 03:45:15 +0000 UTC
Fix silent failure on invalid redirect syntax (#148)

2 files changed,  +29, -5
M pgs/redirect.go
+4, -4
 1@@ -98,7 +98,7 @@ func parseRedirectText(text string) ([]*RedirectRule, error) {
 2 		}
 3 
 4 		from := parts[0]
 5-		rest := parts[0:]
 6+		rest := parts[1:]
 7 		status, forced := hasStatusCode(rest[0])
 8 		if status != 0 {
 9 			rules = append(rules, &RedirectRule{
10@@ -118,9 +118,9 @@ func parseRedirectText(text string) ([]*RedirectRule, error) {
11 				return rules, fmt.Errorf("the destination path/URL must start with '/', 'http:' or 'https:'")
12 			}
13 
14-			queryParts := parts[:toIndex]
15-			to := parts[toIndex]
16-			lastParts := parts[toIndex+1:]
17+			queryParts := rest[:toIndex]
18+			to := rest[toIndex]
19+			lastParts := rest[toIndex+1:]
20 			conditions := map[string]string{}
21 			sts := http.StatusOK
22 			frcd := false
M pgs/redirect_test.go
+25, -1
 1@@ -10,6 +10,7 @@ type RedirectFixture struct {
 2 	name   string
 3 	input  string
 4 	expect []*RedirectRule
 5+	shouldError bool
 6 }
 7 
 8 func TestParseRedirectText(t *testing.T) {
 9@@ -56,16 +57,39 @@ func TestParseRedirectText(t *testing.T) {
10 		},
11 	}
12 
13+	absoluteUriNoProto := RedirectFixture{
14+		name:  "absolute-uri-no-proto",
15+		input: "/*  www.example.com  301",
16+		expect: []*RedirectRule{},
17+		shouldError: true,
18+	}
19+
20+	absoluteUriWithProto := RedirectFixture{
21+		name:  "absolute-uri-no-proto",
22+		input: "/*  https://www.example.com  301",
23+		expect: []*RedirectRule{
24+			{
25+				From:       "/*",
26+				To:         "https://www.example.com",
27+				Status:     301,
28+				Query:      empty,
29+				Conditions: empty,
30+			},
31+		},
32+	}
33+
34 	fixtures := []RedirectFixture{
35 		spa,
36 		withStatus,
37 		noStatus,
38+		absoluteUriNoProto,
39+		absoluteUriWithProto,
40 	}
41 
42 	for _, fixture := range fixtures {
43 		t.Run(fixture.name, func(t *testing.T) {
44 			results, err := parseRedirectText(fixture.input)
45-			if err != nil {
46+			if err != nil && !fixture.shouldError {
47 				t.Error(err)
48 			}
49 			if cmp.Equal(results, fixture.expect) == false {