A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix: sanitize input url and list names
Mohamed Bassemffd105898044
diff --git a/packages/api/utils/rss.ts b/packages/api/utils/rss.tsindex 079b3f5a..75be0179 100644--- a/packages/api/utils/rss.ts+++ b/packages/api/utils/rss.ts@@ -6,6 +6,7 @@ import {ZPublicBookmark,} from "@karakeep/shared/types/bookmarks";import { getAssetUrl } from "@karakeep/shared/utils/assetUtils";+import { isAllowedBookmarkUrl } from "@karakeep/shared/utils/url";export function toRSS(params: {@@ -27,7 +28,10 @@ export function toRSS(bookmarks.filter((b) =>- b.content.type === BookmarkTypes.LINK ||+ // Drop links with unsafe schemes (javascript:, data:, ...) that may+ // predate URL validation, so feed readers can't follow them.+ (b.content.type === BookmarkTypes.LINK &&+ isAllowedBookmarkUrl(b.content.url)) ||b.content.type === BookmarkTypes.ASSET,).forEach((bookmark) => {diff --git a/packages/shared/import-export/exporters.test.ts b/packages/shared/import-export/exporters.test.tsnew file mode 100644index 00000000..46dd9a71--- /dev/null+++ b/packages/shared/import-export/exporters.test.ts@@ -0,0 +1,59 @@+import { describe, expect, it } from "vitest";++import { BookmarkTypes, ZBookmark } from "../types/bookmarks";+import { toNetscapeFormat } from "./exporters";++function linkBookmark(url: string, title = "title"): ZBookmark {+ return {+ id: "id1",+ createdAt: new Date(1700000000000),+ modifiedAt: null,+ title,+ archived: false,+ favourited: false,+ taggingStatus: "success",+ summarizationStatus: null,+ embeddingStatus: null,+ note: null,+ summary: null,+ source: "api",+ userId: "user1",+ tags: [],+ assets: [],+ content: {+ type: BookmarkTypes.LINK,+ url,+ },+ };+}++describe("toNetscapeFormat", () => {+ it("exports http and https links", () => {+ const out = toNetscapeFormat([+ linkBookmark("https://example.com/page"),+ linkBookmark("http://example.com/other"),+ ]);+ expect(out).toContain('HREF="https://example.com/page"');+ expect(out).toContain('HREF="http://example.com/other"');+ });++ it("drops links with unsafe schemes", () => {+ const out = toNetscapeFormat([+ linkBookmark("javascript:alert(document.cookie)"),+ linkBookmark("data:text/html,<script>alert(1)</script>"),+ linkBookmark("vbscript:MsgBox(1)"),+ linkBookmark("https://example.com/safe"),+ ]);+ expect(out).not.toContain("javascript:");+ expect(out).not.toContain("data:");+ expect(out).not.toContain("vbscript:");+ expect(out).toContain('HREF="https://example.com/safe"');+ });++ it("escapes HTML metacharacters in the URL attribute", () => {+ const out = toNetscapeFormat([+ linkBookmark("https://example.com/?a=1&b=2"),+ ]);+ expect(out).toContain('HREF="https://example.com/?a=1&b=2"');+ });+});diff --git a/packages/shared/import-export/exporters.ts b/packages/shared/import-export/exporters.tsindex f7a2581a..3962c1fd 100644--- a/packages/shared/import-export/exporters.ts+++ b/packages/shared/import-export/exporters.ts@@ -2,6 +2,8 @@ import { z } from "zod";import { BookmarkTypes, ZBookmark } from "../types/bookmarks";import { ZBookmarkList } from "../types/lists";+import { escapeHtml } from "../utils/htmlUtils";+import { isAllowedBookmarkUrl } from "../utils/url";export const zExportListSchema = z.object({id: z.string(),@@ -107,6 +109,11 @@ export function toNetscapeFormat(bookmarks: ZBookmark[]): string {if (bookmark.content?.type !== BookmarkTypes.LINK) {return "";}+ // Drop unsafe schemes (javascript:, data:, ...) that may predate+ // URL validation, so they can't execute when the export is opened.+ if (!isAllowedBookmarkUrl(bookmark.content.url)) {+ return "";+ }const addDate = bookmark.createdAt? `ADD_DATE="${Math.floor(bookmark.createdAt.getTime() / 1000)}"`: "";@@ -114,7 +121,7 @@ export function toNetscapeFormat(bookmarks: ZBookmark[]): string {const tagNames = bookmark.tags.map((t) => t.name).join(",");const tags = tagNames.length > 0 ? `TAGS="${tagNames}"` : "";- const encodedUrl = encodeURI(bookmark.content.url);+ const encodedUrl = escapeHtml(encodeURI(bookmark.content.url));const displayTitle = bookmark.title ?? bookmark.content.url;const encodedTitle = escapeHtml(displayTitle);@@ -125,16 +132,3 @@ export function toNetscapeFormat(bookmarks: ZBookmark[]): string {return `${header}\n${bookmarkEntries}\n${footer}`;}--function escapeHtml(input: string): string {- const escapeMap: Record<string, string> = {- "&": "&",- "'": "'",- "`": "`",- '"': """,- "<": "<",- ">": ">",- };-- return input.replace(/[&'`"<>]/g, (match) => escapeMap[match] || "");-}diff --git a/packages/shared/types/bookmarks.ts b/packages/shared/types/bookmarks.tsindex b5f495ed..3d0b47f8 100644--- a/packages/shared/types/bookmarks.ts+++ b/packages/shared/types/bookmarks.ts@@ -1,10 +1,20 @@import { z } from "zod";+import { isAllowedBookmarkUrl } from "../utils/url";import { zCursorV2 } from "./pagination";import { zAttachedByEnumSchema, zBookmarkTagSchema } from "./tags";export const MAX_BOOKMARK_TITLE_LENGTH = 1000;+// Zod's url() accepts any scheme (javascript:, data:, ...), so restrict+// bookmark links to schemes that are safe to reflect in exports and feeds.+export const zBookmarkUrlSchema = z+ .string()+ .url()+ .refine(isAllowedBookmarkUrl, {+ message: "Only http and https URLs are allowed",+ });+export const enum BookmarkTypes {LINK = "link",TEXT = "text",@@ -175,7 +185,7 @@ export const zNewBookmarkRequestSchema = z.intersection(z.discriminatedUnion("type", [z.object({type: z.literal(BookmarkTypes.LINK),- url: z.string().url(),+ url: zBookmarkUrlSchema,precrawledArchiveId: z.string().optional(),}),z.object({@@ -236,7 +246,7 @@ export const zUpdateBookmarksRequestSchema = z.object({.optional().meta({ type: "string", format: "date-time" }),// Link specific fields (optional)- url: z.string().url().optional(),+ url: zBookmarkUrlSchema.optional(),description: z.string().nullish(),author: z.string().nullish(),publisher: z.string().nullish(),diff --git a/packages/shared/utils/htmlUtils.ts b/packages/shared/utils/htmlUtils.tsindex 60272899..a8b124c3 100644--- a/packages/shared/utils/htmlUtils.ts+++ b/packages/shared/utils/htmlUtils.ts@@ -15,3 +15,20 @@ export function htmlToPlainText(htmlContent: string): string {// TODO, we probably should also remove singlefile inline images from the contentreturn compiledConvert(htmlContent);}++const HTML_ESCAPE_MAP: Record<string, string> = {+ "&": "&",+ "'": "'",+ "`": "`",+ '"': """,+ "<": "<",+ ">": ">",+};++/**+ * Escapes HTML special characters so that untrusted input can be safely+ * interpolated into an HTML context (e.g. email bodies, exported files).+ */+export function escapeHtml(input: string): string {+ return input.replace(/[&'`"<>]/g, (match) => HTML_ESCAPE_MAP[match] || "");+}diff --git a/packages/shared/utils/url.test.ts b/packages/shared/utils/url.test.tsindex c360b3a8..5bad5721 100644--- a/packages/shared/utils/url.test.ts+++ b/packages/shared/utils/url.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { setUrlHostnameFromResolvedAddress } from "./url";+import { isAllowedBookmarkUrl, setUrlHostnameFromResolvedAddress } from "./url";describe("setUrlHostnameFromResolvedAddress", () => {it("sets IPv4 addresses as URL hostnames", () => {@@ -31,3 +31,39 @@ describe("setUrlHostnameFromResolvedAddress", () => {);});});++describe("isAllowedBookmarkUrl", () => {+ it("accepts http and https URLs", () => {+ expect(isAllowedBookmarkUrl("http://example.com")).toBe(true);+ expect(isAllowedBookmarkUrl("https://example.com/path?q=1#frag")).toBe(+ true,+ );+ });++ it("rejects script-executing schemes", () => {+ expect(isAllowedBookmarkUrl("javascript:alert(document.cookie)")).toBe(+ false,+ );+ expect(+ isAllowedBookmarkUrl("data:text/html,<script>alert(1)</script>"),+ ).toBe(false);+ expect(isAllowedBookmarkUrl("vbscript:MsgBox(1)")).toBe(false);+ });++ it("rejects scheme casing and whitespace tricks", () => {+ expect(isAllowedBookmarkUrl("JaVaScRiPt:alert(1)")).toBe(false);+ expect(isAllowedBookmarkUrl(" javascript:alert(1)")).toBe(false);+ expect(isAllowedBookmarkUrl("java\tscript:alert(1)")).toBe(false);+ });++ it("rejects other non-web schemes", () => {+ expect(isAllowedBookmarkUrl("file:///etc/passwd")).toBe(false);+ expect(isAllowedBookmarkUrl("ftp://example.com/file")).toBe(false);+ expect(isAllowedBookmarkUrl("chrome://settings")).toBe(false);+ });++ it("rejects strings that are not URLs", () => {+ expect(isAllowedBookmarkUrl("not a url")).toBe(false);+ expect(isAllowedBookmarkUrl("")).toBe(false);+ });+});diff --git a/packages/shared/utils/url.ts b/packages/shared/utils/url.tsindex 5c455b70..641e8a21 100644--- a/packages/shared/utils/url.ts+++ b/packages/shared/utils/url.ts@@ -1,3 +1,17 @@export function setUrlHostnameFromResolvedAddress(url: URL, address: string) {url.hostname = address.includes(":") ? `[${address}]` : address;}++const ALLOWED_BOOKMARK_URL_PROTOCOLS: readonly string[] = ["http:", "https:"];++/**+ * Bookmark link URLs are reflected in HTML exports, RSS feeds and anchor tags,+ * so schemes like javascript:, data: and vbscript: must never be accepted.+ */+export function isAllowedBookmarkUrl(url: string): boolean {+ try {+ return ALLOWED_BOOKMARK_URL_PROTOCOLS.includes(new URL(url).protocol);+ } catch {+ return false;+ }+}diff --git a/packages/trpc/email.ts b/packages/trpc/email.tsindex 15e1ef74..a8cf4f29 100644--- a/packages/trpc/email.ts+++ b/packages/trpc/email.ts@@ -2,6 +2,7 @@ import { createTransport } from "nodemailer";import { getTracer, withSpan } from "@karakeep/shared-server";import serverConfig from "@karakeep/shared/config";+import { escapeHtml } from "@karakeep/shared/utils/htmlUtils";const tracer = getTracer("@karakeep/trpc");@@ -68,7 +69,7 @@ export const sendVerificationEmail = withTracing(subject: "Verify your email address",html: `<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">- <h2>Welcome to Karakeep, ${name}!</h2>+ <h2>Welcome to Karakeep, ${escapeHtml(name)}!</h2><p>Please verify your email address by clicking the link below:</p><p><a href="${verificationUrl}" style="background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;">@@ -114,7 +115,7 @@ export const sendInviteEmail = withTracing(html: `<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"><h2>You've been invited to join Karakeep!</h2>- <p>${inviterName} has invited you to join Karakeep, the bookmark everything app.</p>+ <p>${escapeHtml(inviterName)} has invited you to join Karakeep, the bookmark everything app.</p><p>Click the link below to accept your invitation and create your account:</p><p><a href="${inviteUrl}" style="background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;">@@ -162,7 +163,7 @@ export const sendPasswordResetEmail = withTracing(html: `<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"><h2>Password Reset Request</h2>- <p>Hi ${name},</p>+ <p>Hi ${escapeHtml(name)},</p><p>You requested to reset your password for your Karakeep account. Click the link below to reset your password:</p><p><a href="${resetUrl}" style="background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;">@@ -209,7 +210,7 @@ export const sendListInvitationEmail = withTracing(html: `<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"><h2>You've been invited to collaborate on a list!</h2>- <p>${inviterName} has invited you to collaborate on the list <strong>"${listName}"</strong> in Karakeep.</p>+ <p>${escapeHtml(inviterName)} has invited you to collaborate on the list <strong>"${escapeHtml(listName)}"</strong> in Karakeep.</p><p>Click the link below to view and accept or decline the invitation:</p><p><a href="${inviteUrl}" style="background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;">