fix: address changes run the loop simulation and validate their addresses (WR-09)

AddressChanged wrote its 301 and re-pointed rules without Problems or the
save-time simulation, and removed only an exact rule whose source equals
the new address literally. It now removes every exact rule that matches
the new address with its options, simulates the 301 and the re-pointing
with the stored rules and writes nothing when that loops, stores an
absolute old address on the site as its path, and skips an address that
is not a path on the site.
This commit is contained in:
2026-10-27 15:27:21 +01:00
parent 58b2504fe3
commit cb614cb1fb
3 changed files with 184 additions and 67 deletions

View File

@@ -374,6 +374,60 @@ func TestExactRedirectKeys(t *testing.T) {
}
}
// TestAddressChangeGuards checks that an address change never stores a
// redirect loop or an unusable source (WR-09): a pattern rule that sends
// the new address back to the old one stops the 301, an exact rule that
// matches the new address with its case or trailing-slash options is
// removed, an absolute old address on the site is stored as its path, and
// a new address that is not a path on the site creates nothing.
func TestAddressChangeGuards(t *testing.T) {
env := bootEnv(t)
svc := env.seoPlugin(t).Service()
change := func(old, new string) classes.AddressResult {
t.Helper()
var res classes.AddressResult
if err := env.db.Transaction(func(tx *gorm.DB) error {
var err error
res, err = svc.AddressChanged(t.Context(), tx, classes.AddressChange{Old: old, New: new})
return err
}); err != nil {
t.Fatal(err)
}
return res
}
env.mustRedirect(t, "/a/*", "pattern", "/b/*", 301)
if res := change("/b/x", "/a/x"); res.Redirect != nil {
t.Fatalf("/b/x -> /a/x under /a/* -> /b/*: redirect = %+v, want none (a loop)", res.Redirect)
}
if got := redirectTo(env.redirects(t), "/b/x"); got != "" {
t.Fatalf("/b/x has a redirect to %q, want none", got)
}
env.mustRedirect(t, "/Moved/", "exact", "/elsewhere", 301, func(r *models.Redirect) { r.IgnoreCase = true })
if res := change("/before", "/moved"); res.Redirect == nil {
t.Fatal("/before -> /moved: no redirect")
}
rows := env.redirects(t)
if got := redirectTo(rows, "/Moved/"); got != "" {
t.Fatalf("/Moved/ still redirects to %q; it shadows the record's new address", got)
}
env.expectRedirect(t, "/before", 301, siteURL+"/moved")
if res := change(siteURL+"/abs-old", "/abs-new"); res.Redirect == nil || res.Redirect.Source != "/abs-old" {
t.Fatalf("absolute old address: result = %+v, want a redirect from /abs-old", res)
}
env.expectRedirect(t, "/abs-old", 301, siteURL+"/abs-new")
before := env.redirectCount(t)
if res := change("/plain", "//evil.example/x"); res.Redirect != nil {
t.Fatalf("protocol-relative new address: redirect = %+v, want none", res.Redirect)
}
if got := env.redirectCount(t); got != before {
t.Fatalf("redirects = %d after a refused change, want %d", got, before)
}
}
// redirectCount is the number of stored redirects.
func (e *testEnv) redirectCount(t *testing.T) int64 {
t.Helper()