fix(17): WR-06 apply the admin save rules in menu:import-winter

The importer copied URLs, targets and cross-menu parents as exported, so
a javascript: URL or a _parent target broke the first admin save of the
row, and an entry under a parent in another menu vanished from the
public tree. Kept entries now get a trimmed or cleared URL, a cleared
target and a top-level place in their own menu, each with a report
line.
This commit is contained in:
2026-10-12 21:39:57 +02:00
parent 64215f2bf3
commit 9c0f833878
3 changed files with 73 additions and 2 deletions

View File

@@ -291,4 +291,35 @@ func TestMenuImport(t *testing.T) {
if err := e.gdb.Model(&models.MenuEntry{}).Count(&count).Error; err != nil || count != 2 {
t.Fatalf("refused imports changed the entries: %d %v", count, err)
}
// WR-06: the copies pass the admin save rules, with a report line each.
twoMenus := writeFile(t, dir, "two-menus.jsonl", `{"id": 1, "name": "Main", "code": "main", "is_active": 1}`+"\n"+`{"id": 2, "name": "Footer", "code": "footer", "is_active": 1}`+"\n")
ruled := writeFile(t, dir, "ruled.jsonl",
`{"id": 1, "menu_id": 1, "parent_id": null, "sort_order": 1, "type": "link", "title": "Evil", "url": "javascript:alert(1)", "target": "_parent", "is_published": 1}`+"\n"+
`{"id": 2, "menu_id": 1, "parent_id": null, "sort_order": 2, "type": "link", "title": "Spaced", "url": " /spaced ", "target": "_blank", "is_published": 1}`+"\n"+
`{"id": 3, "menu_id": 2, "parent_id": 2, "sort_order": 1, "type": "link", "title": "Elsewhere", "url": "/e", "is_published": 1}`+"\n"+
`{"id": 4, "menu_id": 2, "parent_id": 3, "sort_order": 2, "type": "link", "title": "Under elsewhere", "url": "/u", "is_published": 1}`+"\n")
out, buf = testOutput()
report, err = console.ImportWinterFiles(ctx, e.gdb, reg, console.WinterOptions{MenusFile: twoMenus, EntriesFile: ruled, Force: true}, out)
if err != nil || report.Entries != 4 || report.Dropped != 0 {
t.Fatalf("ruled import: %+v %v\n%s", report, err, buf.String())
}
for _, want := range []string{
`entry 1 'Evil' (menu 1, type 'link'): url "javascript:alert(1)" cleared (only http, https, mailto, tel, / and # links are served)`,
"entry 1 'Evil' (menu 1, type 'link'): target '_parent' cleared (only _self and _blank are allowed)",
"entry 2 'Spaced' (menu 1, type 'link'): url ' /spaced ' trimmed to '/spaced'",
"entry 3 'Elsewhere' (menu 2, type 'link'): copied as a top-level entry (its parent 2 is in menu 1)",
} {
if !strings.Contains(buf.String(), want) {
t.Errorf("report lacks %q:\n%s", want, buf.String())
}
}
kept = nil
if err := e.gdb.Order("id").Find(&kept).Error; err != nil || len(kept) != 4 {
t.Fatalf("ruled entries = %+v %v", kept, err)
}
if kept[0].URL != nil || kept[0].Target != nil || models.Text(kept[1].URL) != "/spaced" || models.Text(kept[1].Target) != "_blank" ||
kept[2].ParentID != nil || kept[2].MenuID != 2 || kept[3].ParentID == nil || *kept[3].ParentID != 3 {
t.Fatalf("ruled entries = %+v", kept)
}
}