Skip to content

Commit

Permalink
fix(pgs): redirect slice out-of-bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 25, 2024
1 parent e3ce3b9 commit c728ab4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pgs/calc_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pgs/calc_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c728ab4

Please sign in to comment.