repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
c728ab4
parent
e3ce3b9
author
Eric Bower
date
2024-12-25 15:55:23 -0500 EST
fix(pgs): redirect slice out-of-bounds
2 files changed,  +23, -5
M pgs/calc_route.go
+5, -5
 1@@ -97,6 +97,9 @@ func correlatePlaceholder(orig, pattern string) (string, string) {
 2 		_type = "match"
 3 	} else if strings.Contains(pattern, "*") {
 4 		_type = "wildcard"
 5+		if pattern == "/*" {
 6+			nextList = append(nextList, ".*")
 7+		}
 8 	} else if strings.Contains(pattern, ":") {
 9 		_type = "variable"
10 	}
11@@ -148,7 +151,7 @@ func genRedirectRoute(actual string, fromStr string, to string) string {
12 			ls := actualList[idx:]
13 			// if the * is part of other text in the segment (e.g. `/files*`)
14 			// then we don't want to include "files" in the destination
15-			if len(item) > 1 {
16+			if len(item) > 1 && len(actualList) > idx+1 {
17 				ls = actualList[idx+1:]
18 			}
19 			// standalone splat
20@@ -164,11 +167,8 @@ func genRedirectRoute(actual string, fromStr string, to string) string {
21 
22 	fin := []string{"/"}
23 
24-	fmt.Println(toList)
25 	for _, item := range toList {
26-		fmt.Println(item)
27 		if strings.HasSuffix(item, ":splat") {
28-			fmt.Println(mapper[item])
29 			fin = append(fin, mapper[item])
30 		} else if mapper[item] != "" {
31 			fin = append(fin, mapper[item])
32@@ -221,7 +221,7 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe
33 			break
34 		}
35 
36-		if len(match) > 0 {
37+		if len(match) > 0 && match[0] != "" {
38 			isRedirect := checkIsRedirect(redirect.Status)
39 			if !isRedirect && !hasProtocol(redirect.To) {
40 				route := genRedirectRoute(fp, from, redirect.To)
M pgs/calc_route_test.go
+18, -0
 1@@ -550,6 +550,24 @@ func TestCalcRoutes(t *testing.T) {
 2 				{Filepath: "https://some.dev/.well-known/webfinger?query=nice", Status: 301},
 3 			},
 4 		},
 5+		{
 6+			Name: "well-known-splat-suffix-error",
 7+			Actual: calcRoutes(
 8+				"public",
 9+				"/software/",
10+				[]*RedirectRule{
11+					{
12+						From:   "/.well-known/nodeinfo*",
13+						To:     "https://some.dev/.well-known/nodeinfo:splat",
14+						Status: 301,
15+					},
16+				},
17+			),
18+			Expected: []*HttpReply{
19+				{Filepath: "public/software/index.html", Status: 200},
20+				{Filepath: "public/404.html", Status: 404},
21+			},
22+		},
23 	}
24 
25 	for _, fixture := range fixtures {