fix: an unknown redirect status, match or query is refused, not defaulted (WR-17)

The API, MCP, batches and the CSV import copied any value and Normalize
replaced it with the default, answering 201 for a 307 saved as a 301.
RedirectFields.Problems refuses values outside {301, 302, 410}, {exact,
pattern, regex} and {ignore, pass} with value_invalid; the import marks
such rows refused.
This commit is contained in:
2026-10-27 15:36:21 +01:00
parent c8e0961740
commit 24e6f384ca
6 changed files with 68 additions and 0 deletions

View File

@@ -49,6 +49,36 @@ type RedirectFields struct {
Note *string `json:"note,omitempty"`
}
// Problems refuses a status, match or query option outside the allowed
// values (WR-17), so a caller learns its request was not taken rather than
// getting a default in its place; an empty match or query is the default.
func (f RedirectFields) Problems() map[string][]models.Problem {
out := map[string][]models.Problem{}
invalid := []models.Problem{{Key: "golem15.seo::lang.api.value_invalid"}}
if f.Status != nil {
switch models.RedirectStatus(*f.Status) {
case models.Status301, models.Status302, models.Status410:
default:
out["status"] = invalid
}
}
if f.Match != nil {
switch strings.TrimSpace(*f.Match) {
case "", models.RedirectExact, models.RedirectPattern, models.RedirectRegex:
default:
out["match"] = invalid
}
}
if f.Query != nil {
switch strings.TrimSpace(*f.Query) {
case "", models.QueryIgnore, models.QueryPass:
default:
out["query"] = invalid
}
}
return out
}
// Apply writes the fields that are set onto r.
func (f RedirectFields) Apply(r *models.Redirect) {
if f.Source != nil {
@@ -380,6 +410,9 @@ func (s *Service) applyChange(ctx context.Context, tx *gorm.DB, index int, ch Ba
}
}
if ch.Redirect != nil {
if problems := ch.Redirect.Problems(); len(problems) > 0 {
return &ProblemsError{Problems: problems}
}
ch.Redirect.Apply(&r)
}
check, err := s.SaveRedirect(ctx, tx, &r)

View File

@@ -422,6 +422,16 @@ func (s *Service) planRedirect(ctx context.Context, tx *gorm.DB, row CSVRow) (pl
f.TrailingSlash = &b
}
}
if problems := f.Problems(); len(problems) > 0 {
p.status = "refused"
for _, column := range []string{"status", "match", "query"} {
if _, bad := problems[column]; bad {
v, _ := cell(row, column)
p.notes = append(p.notes, s.PhraseCtx(ctx, "golem15.seo::lang.transfer.row_invalid", map[string]string{"field": column, "message": v}))
}
}
return p, nil, nil
}
var rows []models.Redirect
if err := tx.Where("source = ? AND match = ?", strings.TrimSpace(source), strings.TrimSpace(match)).Limit(1).Find(&rows).Error; err != nil {
return p, nil, err