An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Avoid treating pasted webpage components as local files https://github.com/siyuan-note/siyuan/issues/19155
Danielfafdd9e2f0d3
diff --git a/app/src/protyle/upload/htmlLocalAssets.test.ts b/app/src/protyle/upload/htmlLocalAssets.test.tsindex dda7d292a0..93c6d86061 100644--- a/app/src/protyle/upload/htmlLocalAssets.test.ts+++ b/app/src/protyle/upload/htmlLocalAssets.test.ts@@ -2,12 +2,62 @@ import {describe, it} from "node:test";import * as assert from "node:assert/strict";import {applyHTMLLocalAssetPaths,+ collectHTMLLocalAssets,+ getHTMLAssetSourceURL,type IHTMLLocalAsset,isHTMLLocalAssetPath,removeHTMLLocalAssetPaths,+ resolveHTMLAssetURL,+ resolveHTMLAssetURLs,} from "./htmlLocalAssets";describe("HTML local assets", () => {+ const createElement = (tagName: string, attributes: Record<string, string>) => ({+ tagName,+ getAttribute: (name: string) => attributes[name] ?? null,+ setAttribute: (name: string, value: string) => attributes[name] = value,+ }) as unknown as Element;++ it("ignores webpage components and collects only supported resource attributes", () => {+ const elements = [+ createElement("INCLUDE-FRAGMENT", {src: "/in-product-messaging/code-scanning-ai-findings-preview-banner?page_path=test"}),+ createElement("SCRIPT", {src: "/scripts/site.js"}),+ createElement("LINK", {href: "/styles/site.css"}),+ createElement("IMG", {src: "/tmp/image.png", href: "/not-an-image"}),+ createElement("A", {href: "C:\\files\\attachment.pdf", src: "/not-an-attachment"}),+ createElement("VIDEO", {src: "file:///tmp/movie.mp4"}),+ createElement("AUDIO", {src: "/tmp/sound.mp3"}),+ createElement("SOURCE", {src: "/tmp/media.webm"}),+ ];+ const root = {querySelectorAll: () => elements} as unknown as ParentNode;+ assert.deepEqual(collectHTMLLocalAssets(root).map(item => item.path), [+ "/tmp/image.png", "C:\\files\\attachment.pdf", "file:///tmp/movie.mp4", "/tmp/sound.mp3", "/tmp/media.webm",+ ]);+ });++ it("resolves web resource paths before local asset collection", () => {+ const image = createElement("IMG", {src: "/images/a.png"});+ const link = createElement("A", {href: "../attachment.pdf"});+ const local = createElement("IMG", {src: "file:///tmp/a.png"});+ const root = {querySelectorAll: () => [image, link, local]} as unknown as ParentNode;+ resolveHTMLAssetURLs(root, "https://example.com/docs/page");+ assert.equal(image.getAttribute("src"), "https://example.com/images/a.png");+ assert.equal(link.getAttribute("href"), "https://example.com/attachment.pdf");+ assert.deepEqual(collectHTMLLocalAssets(root).map(item => item.path), ["file:///tmp/a.png"]);+ });++ it("uses only explicit web provenance and preserves local and internal paths", () => {+ assert.equal(getHTMLAssetSourceURL("Version:0.9\r\nSourceURL:https://example.com/docs/page\r\n<html>"), "https://example.com/docs/page");+ assert.equal(getHTMLAssetSourceURL("", "https://example.com/base/"), "https://example.com/base/");+ assert.equal(getHTMLAssetSourceURL("SourceURL:https://example.com/docs/page", "../base/"), "https://example.com/base/");+ assert.equal(getHTMLAssetSourceURL(""), undefined);+ assert.equal(getHTMLAssetSourceURL("SourceURL:file:///tmp/document.html"), undefined);+ assert.equal(resolveHTMLAssetURL("/tmp/a.png"), "/tmp/a.png");+ for (const value of ["file:///tmp/a.png", "C:\\Images\\a.png", "\\\\server\\a.png", "assets/a.png", "#section", "data:image/png;base64,test"]) {+ assert.equal(resolveHTMLAssetURL(value, "https://example.com/"), value);+ }+ assert.equal(resolveHTMLAssetURL("//cdn.example.com/a.png", "https://example.com/"), "https://cdn.example.com/a.png");+ });it("recognizes absolute paths without treating web and asset URLs as local", () => {assert.equal(isHTMLLocalAssetPath("file:///tmp/a.png"), true);assert.equal(isHTMLLocalAssetPath("C:\\Images\\a.png"), true);diff --git a/app/src/protyle/upload/htmlLocalAssets.ts b/app/src/protyle/upload/htmlLocalAssets.tsindex 35544cdcfb..85d0a350c7 100644--- a/app/src/protyle/upload/htmlLocalAssets.ts+++ b/app/src/protyle/upload/htmlLocalAssets.ts@@ -4,6 +4,55 @@ export interface IHTMLLocalAsset {path: string;}+const getAssetAttribute = (element: Element): "href" | "src" | undefined => {+ switch (element.tagName.toLowerCase()) {+ case "a":+ return "href";+ case "img":+ case "audio":+ case "video":+ case "source":+ return "src";+ }+};++export const getHTMLAssetSourceURL = (html: string, baseHref?: string) => {+ const source = html.match(/^SourceURL:\s*(\S+)/mi)?.[1];+ try {+ const url = baseHref ? new URL(baseHref, source) : new URL(source);+ return /^https?:$/.test(url.protocol) ? url.href : undefined;+ } catch (_error) {+ return undefined;+ }+};++export const resolveHTMLAssetURL = (value: string, sourceURL?: string) => {+ const path = value.trim();+ // 明确的本地路径、思源资源和页内锚点不按网页来源展开。+ if (!sourceURL || !path || /^(?:[a-z][a-z\d+.-]*:|\\\\|assets\/|#)/i.test(path)) {+ return value;+ }+ try {+ const source = new URL(sourceURL);+ return /^https?:$/.test(source.protocol) ? new URL(path, source).href : value;+ } catch (_error) {+ return value;+ }+};++export const resolveHTMLAssetURLs = (root: ParentNode, sourceURL?: string) => {+ if (!sourceURL) {+ return;+ }+ root.querySelectorAll("[href], [src]").forEach(element => {+ const attribute = getAssetAttribute(element);+ const value = attribute && element.getAttribute(attribute);+ if (value) {+ element.setAttribute(attribute, resolveHTMLAssetURL(value, sourceURL));+ }+ });+};+export const isHTMLLocalAssetPath = (value: string) => {const path = value.trim();if (!path || path.toLowerCase().startsWith("assets/")) {@@ -18,12 +67,11 @@ export const isHTMLLocalAssetPath = (value: string) => {export const collectHTMLLocalAssets = (root: ParentNode) => {const assets: IHTMLLocalAsset[] = [];root.querySelectorAll("[href], [src]").forEach(element => {- (["href", "src"] as const).forEach(attribute => {- const path = element.getAttribute(attribute);- if (path && isHTMLLocalAssetPath(path)) {- assets.push({element, attribute, path});- }- });+ const attribute = getAssetAttribute(element);+ const path = attribute && element.getAttribute(attribute);+ if (path && isHTMLLocalAssetPath(path)) {+ assets.push({element, attribute, path});+ }});return assets;};diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.tsindex 67799515f9..b11f4b52fa 100644--- a/app/src/protyle/util/paste.ts+++ b/app/src/protyle/util/paste.ts@@ -26,7 +26,7 @@ import {remapTabsDOMIDs, wrapPastedTabItems} from "./tabsCopy";import {getTabItems, getTabTitle} from "../render/tabsRender";import {removeZWJ} from "./normalizeText";import {base64ToURL, showBase64ImageSizeLimit} from "../upload/base64";-import {applyHTMLLocalAssetPaths, collectHTMLLocalAssets, removeHTMLLocalAssetPaths} from "../upload/htmlLocalAssets";+import {applyHTMLLocalAssetPaths, collectHTMLLocalAssets, getHTMLAssetSourceURL, removeHTMLLocalAssetPaths, resolveHTMLAssetURLs} from "../upload/htmlLocalAssets";import {applyHTMLEmbeddedAssetPaths,collectHTMLEmbeddedAssets,@@ -751,6 +751,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvenif (!siyuanHTML) {// process wordconst doc = new DOMParser().parseFromString(textHTML, "text/html");+ // 在移除文档头之前解析来源,避免将网页根路径当成本地绝对路径。+ resolveHTMLAssetURLs(doc, getHTMLAssetSourceURL(textHTML, doc.querySelector("base[href]")?.getAttribute("href")));if (doc.body && doc.body.innerHTML) {textHTML = doc.body.innerHTML;}