Skip to content

Commit

Permalink
fix(pgs): infinite redirect with 404
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 10, 2025
1 parent 3c2cdad commit a942011
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pgs/calc_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func correlatePlaceholder(orig, pattern string) (string, string) {
nextList = append(nextList, strings.ReplaceAll(item, "*", "(.*)"))
} else if item == origList[idx] {
nextList = append(nextList, origList[idx])
} else {
nextList = append(nextList, item)
// if we are on the last pattern item then we need to ensure
// it matches the end of string so partial matches are not counted
if idx == len(patternList)-1 {
// regex end of string matcher
nextList = append(nextList, "$")
}
}
}

Expand Down Expand Up @@ -266,7 +274,7 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe
// we might have a directory so add a trailing slash with a 301
// we can't check for file extention because route could have a dot
// and ext parsing gets confused
if fp != "" && !strings.HasSuffix(fp, "/") {
if !strings.HasSuffix(fp, "/") {
redirectRoute := shared.GetAssetFileName(&utils.FileEntry{
Filepath: fp + "/",
})
Expand Down
20 changes: 20 additions & 0 deletions pgs/calc_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,26 @@ func TestCalcRoutes(t *testing.T) {
{Filepath: "public/404.html", Status: 404},
},
},
{
Name: "non-match-dont-redirect",
Actual: calcRoutes(
"public",
"/scripts/asd",
[]*RedirectRule{
{
From: "/concat/*",
To: "/scripts/:splat",
Status: 301,
},
},
),
Expected: []*HttpReply{
{Filepath: "public/scripts/asd", Status: 200},
{Filepath: "public/scripts/asd.html", Status: 200},
{Filepath: "/scripts/asd/", Status: 301},
{Filepath: "public/404.html", Status: 404},
},
},
}

for _, fixture := range fixtures {
Expand Down

0 comments on commit a942011

Please sign in to comment.