diff --git a/backend/downloads.go b/backend/downloads.go index 1c9eb0c..1d1a9ff 100644 --- a/backend/downloads.go +++ b/backend/downloads.go @@ -3,6 +3,7 @@ package main import ( "crypto/sha256" "fmt" + "html" "strings" ) @@ -29,6 +30,38 @@ func normalizeDownloads(input []Download) []Download { output = append(output, item) } } + for index, item := range output { + if len(item.Files) == 0 || len(item.Files) > 100 { + continue + } + valid := true + for _, file := range item.Files { + if strings.TrimSpace(file.Label) == "" || len(file.Label) > 500 || !navigationURL(file.URL) { + valid = false + break + } + } + if !valid { + continue // Keep invalid legacy fields so writes are rejected rather than silently dropping them. + } + links := make([]string, 0, len(item.Files)) + for _, file := range item.Files { + label := strings.NewReplacer("\\", "\\\\", "[", "\\[", "]", "\\]", "\r", " ", "\n", " ").Replace(html.EscapeString(file.Label)) + url := strings.NewReplacer("<", "%3C", ">", "%3E", "\\", "%5C", " ", "%20").Replace(file.URL) + if strings.Contains(item.Description, "]("+file.URL+")") || strings.Contains(item.Description, "](<"+file.URL+">)") || strings.Contains(item.Description, "]("+url+")") || strings.Contains(item.Description, "](<"+url+">)") { + continue + } + links = append(links, fmt.Sprintf("- [%s](<%s>)", label, url)) + } + if len(links) > 0 { + if item.Description != "" { + item.Description += "\n\n" + } + item.Description += strings.Join(links, "\n") + } + item.Files = nil + output[index] = item + } return output } @@ -38,18 +71,13 @@ func validDownloads(items []Download) bool { } ids := map[string]bool{} for _, item := range items { - if !validName(item.ID) || ids[item.ID] || strings.TrimSpace(item.Name) == "" || len(item.Name) > 300 || len(item.Description) > 100000 || len(item.Files) < 1 || len(item.Files) > 100 || item.Version != "" || item.URL != "" { + if !validName(item.ID) || ids[item.ID] || strings.TrimSpace(item.Name) == "" || len(item.Name) > 300 || strings.TrimSpace(item.Description) == "" || len(item.Description) > 100000 || len(item.Files) != 0 || item.Version != "" || item.URL != "" { return false } if item.Category != "os" && item.Category != "software" && item.Category != "fonts" { return false } ids[item.ID] = true - for _, file := range item.Files { - if strings.TrimSpace(file.Label) == "" || len(file.Label) > 500 || !navigationURL(file.URL) { - return false - } - } } return true } diff --git a/backend/downloads_test.go b/backend/downloads_test.go index 61948c8..5b207a6 100644 --- a/backend/downloads_test.go +++ b/backend/downloads_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "path/filepath" "reflect" + "strings" "testing" ) @@ -31,7 +32,7 @@ func TestDownloadCatalogLegacyCompatibilityAndPersistence(t *testing.T) { t.Fatal(err) } items := public.Site.Downloads - if len(items) != 2 || items[0].Name != "Ubuntu" || len(items[0].Files) != 2 || items[0].Files[1].Label != "24.04 (arm64)" || items[1].Files[0].Label != "Tool" || items[0].Category != "software" || !validName(items[0].ID) || items[0].URL != "" { + if len(items) != 2 || items[0].Name != "Ubuntu" || len(items[0].Files) != 0 || !strings.Contains(items[0].Description, "[24.04 (arm64)]()") || !strings.Contains(items[0].Description, "[24.04 (amd64)]()") || !strings.Contains(items[1].Description, "[Tool]()") || items[0].Category != "software" || !validName(items[0].ID) || items[0].URL != "" { t.Fatalf("legacy projects or version links lost: %+v", items) } c.app.db.QueryRow("SELECT data FROM records WHERE kind='settings' AND id='site'").Scan(&rawAfter) @@ -49,9 +50,8 @@ func TestDownloadCatalogLegacyCompatibilityAndPersistence(t *testing.T) { t.Fatal("older form lost downloads or changed stable project IDs") } settings.Downloads[0].Category = "os" - settings.Downloads[0].Description = "## Ubuntu\n\n选择适合设备架构的版本。" - settings.Downloads[0].Files[0].URL = "/mirrors/ubuntu-releases/24.04/ubuntu.iso" - settings.Downloads = append(settings.Downloads, Download{ID: "fonts", Name: "字体", Category: "fonts", Files: []DownloadFile{{Label: "字体压缩包", URL: "https://example.invalid/font.zip"}}}) + settings.Downloads[0].Description = "## Ubuntu\n\n[24.04 (amd64)](/mirrors/ubuntu-releases/24.04/ubuntu.iso)\n\nSHA256: `0123456789abcdef`" + settings.Downloads = append(settings.Downloads, Download{ID: "fonts", Name: "字体", Category: "fonts", Description: "字体的安装方法。"}) w = c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": saved.Revision}, true) status(t, w, 200) status(t, c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": saved.Revision}, true), 409) @@ -87,19 +87,19 @@ func TestDownloadCatalogValidationAndPermissions(t *testing.T) { c := fixture(t) c.login("admin") record, _ := c.app.record("settings", "site") - base := Download{ID: "ubuntu", Name: "Ubuntu", Category: "os", Files: []DownloadFile{{Label: "24.04 LTS (amd64)", URL: "/mirrors/ubuntu-releases/24.04/ubuntu.iso"}}} + base := Download{ID: "ubuntu", Name: "Ubuntu", Category: "os", Description: "## Ubuntu\n\n[24.04 LTS (amd64)](/mirrors/ubuntu-releases/24.04/ubuntu.iso)"} mutations := []func(*Download){ func(d *Download) { d.ID = "" }, func(d *Download) { d.Category = "unknown" }, func(d *Download) { d.Name = " " }, - func(d *Download) { d.Files = nil }, - func(d *Download) { d.Files[0].Label = " " }, - func(d *Download) { d.Files[0].URL = "javascript:alert(1)" }, - func(d *Download) { d.Files[0].URL = "//example.invalid/a.iso" }, - func(d *Download) { d.Files[0].URL = "/\\example.invalid/a.iso" }, - func(d *Download) { d.Files[0].URL = "https://user:password@example.invalid/a.iso" }, + func(d *Download) { d.Description = " \n\t" }, + func(d *Download) { d.Description = strings.Repeat("a", 100001) }, + func(d *Download) { d.Files = []DownloadFile{{Label: " ", URL: "/mirrors/a.iso"}} }, func(d *Download) { d.URL = "https://example.invalid/ambiguous-old-field" }, } + for _, url := range []string{"javascript:alert(1)", "//example.invalid/a.iso", "/\\example.invalid/a.iso", "https://user:password@example.invalid/a.iso"} { + mutations = append(mutations, func(d *Download) { d.Files = []DownloadFile{{Label: "ISO", URL: url}} }) + } for index, mutate := range mutations { var item Download decodeData(base, &item) @@ -115,6 +115,46 @@ func TestDownloadCatalogValidationAndPermissions(t *testing.T) { settings.Downloads = []Download{base, base} status(t, c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": record.Revision}, true), 400) settings.Downloads = []Download{base} + status(t, c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": record.Revision}, true), 200) c.login("editor") status(t, c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": record.Revision}, true), 403) } + +func TestDownloadArticlesPreserveExistingMarkdownAndLinksOnce(t *testing.T) { + body := "## Debian 13\n\n版本、签名与 SHA256:`abc123`。\n\n[签名](/mirrors/debian-cd/SHA256SUMS.sign)" + input := []Download{{ID: "debian", Name: "Debian", Category: "os", Description: body, Files: []DownloadFile{ + {Label: "ISO [amd64] ", URL: "/mirrors/debian-cd/image (amd64).iso?source="}, + {Label: "SHA256SUMS", URL: "https://example.invalid/SHA256SUMS"}, + }}} + articles := normalizeDownloads(input) + if len(articles[0].Files) != 0 || !validDownloads(articles) || !strings.HasPrefix(articles[0].Description, body+"\n\n") { + t.Fatal("existing article or legacy links were lost") + } + for _, link := range []string{ + "- [ISO \\[amd64\\] <mirror>]()", + "- [SHA256SUMS]()", + } { + if !strings.Contains(articles[0].Description, link) { + t.Fatalf("legacy link not safely preserved: %s", articles[0].Description) + } + } + if !reflect.DeepEqual(normalizeDownloads(articles), articles) || len(input[0].Files) != 2 || input[0].Description != body { + t.Fatal("normalization duplicated links or mutated the input") + } + raw, _ := json.Marshal(articles) + if strings.Contains(string(raw), `"files"`) { + t.Fatal("article response still exposes a separate download-link module") + } +} + +func TestDownloadArticlesReuseLinksAlreadyInMarkdown(t *testing.T) { + body := "## 下载\n\n[ISO](/mirrors/debian/image.iso)\n\n[签名]()" + input := []Download{{ID: "debian", Name: "Debian", Category: "os", Description: body, Files: []DownloadFile{ + {Label: "安装镜像", URL: "/mirrors/debian/image.iso"}, + {Label: "签名", URL: "https://example.invalid/image.iso.sig"}, + }}} + output := normalizeDownloads(input) + if !validDownloads(output) || output[0].Description != body || len(output[0].Files) != 0 { + t.Fatal("migration duplicated links already maintained in the article") + } +} diff --git a/backend/models.go b/backend/models.go index b382dd9..7d0db18 100644 --- a/backend/models.go +++ b/backend/models.go @@ -13,14 +13,14 @@ type RelatedLink struct { URL string `json:"url"` } type Download struct { - ID string `json:"id"` - Name string `json:"name"` - Category string `json:"category"` - Description string `json:"description"` - Files []DownloadFile `json:"files"` + ID string `json:"id"` + Name string `json:"name"` + Category string `json:"category"` + Description string `json:"description"` // Read old settings and submissions without losing existing download links. - Version string `json:"version,omitempty"` - URL string `json:"url,omitempty"` + Files []DownloadFile `json:"files,omitempty"` + Version string `json:"version,omitempty"` + URL string `json:"url,omitempty"` } type DownloadFile struct { Label string `json:"label"` diff --git a/src/api.ts b/src/api.ts index 0344cda..cf88680 100644 --- a/src/api.ts +++ b/src/api.ts @@ -2,8 +2,7 @@ export type Localized = Record export interface SiteLink { title: Localized; url: string } export interface RelatedLink extends SiteLink { kind: 'url' | 'help' } export type DownloadCategory = 'os' | 'software' | 'fonts' -export interface DownloadFile { label: string; url: string } -export interface SiteDownload { id: string; name: string; category: DownloadCategory; description: string; files: DownloadFile[] } +export interface SiteDownload { id: string; name: string; category: DownloadCategory; description: string } export interface SiteSettings { name: Localized; description: Localized; logoLight: string; logoDark: string; favicon: string; avatar: string email: string; address: Localized; links: SiteLink[]; relatedLinks: RelatedLink[]; downloads: SiteDownload[] diff --git a/src/components/DownloadCatalogEditor.vue b/src/components/DownloadCatalogEditor.vue index 6d7a3eb..cb52320 100644 --- a/src/components/DownloadCatalogEditor.vue +++ b/src/components/DownloadCatalogEditor.vue @@ -15,7 +15,7 @@ watch(() => items.value.map(item => item.id), ids => { }, { immediate: true }) function addProject() { const id = Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('') - items.value.push({ id, name: '', category: 'software', description: '', files: [{ label: '', url: '' }] }) + items.value.push({ id, name: '', category: 'software', description: '' }) selectedId.value = id } function moveProject(offset: number) { @@ -30,13 +30,7 @@ function removeProject() { items.value.splice(from, 1) selectedId.value = items.value[Math.min(from, items.value.length - 1)]?.id || '' } -function moveFile(index: number, offset: number) { - const files = selected.value?.files, to = index + offset - if (!files || to < 0 || to >= files.length) return - const [file] = files.splice(index, 1) - if (file) files.splice(to, 0, file) -} -const invalidProject = computed(() => items.value.find(item => !item.name.trim() || !item.files.length || item.files.some(file => !file.label.trim() || !file.url.trim()))) +const invalidProject = computed(() => items.value.find(item => !item.name.trim() || !item.description.trim())) function validate() { if (invalidProject.value) selectedId.value = invalidProject.value.id return !invalidProject.value @@ -52,16 +46,8 @@ defineExpose({ validate })
-