From c728ab4bd733c43f198ea0818f8140fa65efeb6b Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Wed, 25 Dec 2024 15:55:23 -0500 Subject: [PATCH] fix(pgs): redirect slice out-of-bounds --- pgs/calc_route.go | 10 +++++----- pgs/calc_route_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/pgs/calc_route.go b/pgs/calc_route.go index d5fc710..506eedc 100644 --- a/pgs/calc_route.go +++ b/pgs/calc_route.go @@ -97,6 +97,9 @@ func correlatePlaceholder(orig, pattern string) (string, string) { _type = "match" } else if strings.Contains(pattern, "*") { _type = "wildcard" + if pattern == "/*" { + nextList = append(nextList, ".*") + } } else if strings.Contains(pattern, ":") { _type = "variable" } @@ -148,7 +151,7 @@ func genRedirectRoute(actual string, fromStr string, to string) string { ls := actualList[idx:] // if the * is part of other text in the segment (e.g. `/files*`) // then we don't want to include "files" in the destination - if len(item) > 1 { + if len(item) > 1 && len(actualList) > idx+1 { ls = actualList[idx+1:] } // standalone splat @@ -164,11 +167,8 @@ func genRedirectRoute(actual string, fromStr string, to string) string { fin := []string{"/"} - fmt.Println(toList) for _, item := range toList { - fmt.Println(item) if strings.HasSuffix(item, ":splat") { - fmt.Println(mapper[item]) fin = append(fin, mapper[item]) } else if mapper[item] != "" { fin = append(fin, mapper[item]) @@ -221,7 +221,7 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe break } - if len(match) > 0 { + if len(match) > 0 && match[0] != "" { isRedirect := checkIsRedirect(redirect.Status) if !isRedirect && !hasProtocol(redirect.To) { route := genRedirectRoute(fp, from, redirect.To) diff --git a/pgs/calc_route_test.go b/pgs/calc_route_test.go index cf2af1e..71df140 100644 --- a/pgs/calc_route_test.go +++ b/pgs/calc_route_test.go @@ -550,6 +550,24 @@ func TestCalcRoutes(t *testing.T) { {Filepath: "https://some.dev/.well-known/webfinger?query=nice", Status: 301}, }, }, + { + Name: "well-known-splat-suffix-error", + Actual: calcRoutes( + "public", + "/software/", + []*RedirectRule{ + { + From: "/.well-known/nodeinfo*", + To: "https://some.dev/.well-known/nodeinfo:splat", + Status: 301, + }, + }, + ), + Expected: []*HttpReply{ + {Filepath: "public/software/index.html", Status: 200}, + {Filepath: "public/404.html", Status: 404}, + }, + }, } for _, fixture := range fixtures {