Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(export): escape tag names in the netscape bookmark export

Mohamed Bassemf0381b4277f3
diff --git a/packages/shared/import-export/exporters.test.ts b/packages/shared/import-export/exporters.test.ts
index 46dd9a71..52fde158 100644
--- a/packages/shared/import-export/exporters.test.ts
+++ b/packages/shared/import-export/exporters.test.ts
@@ -3,7 +3,11 @@ import { describe, expect, it } from "vitest";
import { BookmarkTypes, ZBookmark } from "../types/bookmarks";
import { toNetscapeFormat } from "./exporters";
-function linkBookmark(url: string, title = "title"): ZBookmark {
+function linkBookmark(
+ url: string,
+ title = "title",
+ tagNames: string[] = [],
+): ZBookmark {
return {
id: "id1",
createdAt: new Date(1700000000000),
@@ -18,7 +22,11 @@ function linkBookmark(url: string, title = "title"): ZBookmark {
summary: null,
source: "api",
userId: "user1",
- tags: [],
+ tags: tagNames.map((name, i) => ({
+ id: `tag${i}`,
+ name,
+ attachedBy: "ai" as const,
+ })),
assets: [],
content: {
type: BookmarkTypes.LINK,
@@ -56,4 +64,15 @@ describe("toNetscapeFormat", () => {
]);
expect(out).toContain('HREF="https://example.com/?a=1&b=2"');
});
+
+ it("escapes HTML metacharacters in the TAGS attribute", () => {
+ const out = toNetscapeFormat([
+ linkBookmark("https://example.com/page", "innocent title", [
+ 'pwn"><script>alert(document.domain)</script><a x="',
+ ]),
+ ]);
+ expect(out).not.toContain("<script>");
+ expect(out).toContain("&lt;script&gt;");
+ expect(out).toContain('TAGS="pwn&quot;&gt;&lt;script&gt;');
+ });
});
diff --git a/packages/shared/import-export/exporters.ts b/packages/shared/import-export/exporters.ts
index f121157a..bb6c318c 100644
--- a/packages/shared/import-export/exporters.ts
+++ b/packages/shared/import-export/exporters.ts
@@ -120,8 +120,11 @@ export function toNetscapeFormat(bookmarks: ZBookmark[]): string {
? `ADD_DATE="${Math.floor(bookmark.createdAt.getTime() / 1000)}"`
: "";
+ // Tag names are attacker-influenced (the AI tagger writes them straight
+ // from model output), so they have to be escaped like any other value
+ // interpolated into an attribute.
const tagNames = bookmark.tags.map((t) => t.name).join(",");
- const tags = tagNames.length > 0 ? `TAGS="${tagNames}"` : "";
+ const tags = tagNames.length > 0 ? `TAGS="${escapeHtml(tagNames)}"` : "";
const encodedUrl = escapeHtml(encodeURI(bookmark.content.url));
const displayTitle = bookmark.title ?? bookmark.content.url;