Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve HTML paste content when local assets fail https://github.com/siyuan-note/siyuan/issues/20057

Daniel6ec4f448ade3
diff --git a/app/src/protyle/upload/htmlLocalAssets.test.ts b/app/src/protyle/upload/htmlLocalAssets.test.ts
index 93c6d86061..62be52469d 100644
--- a/app/src/protyle/upload/htmlLocalAssets.test.ts
+++ b/app/src/protyle/upload/htmlLocalAssets.test.ts
@@ -67,21 +67,24 @@ describe("HTML local assets", () => {
assert.equal(isHTMLLocalAssetPath("assets/a.png"), false);
});
- it("rewrites only successful paths by their input indexes", () => {
+ it("rewrites successful paths and removes failed local references by their input indexes", () => {
const values = ["file:///tmp/a.png", "/tmp/b.pdf"];
const assets = values.map((path, index) => ({
element: {
setAttribute(_attribute: string, value: string) {
values[index] = value;
},
- } as Element,
+ removeAttribute() {
+ values[index] = undefined;
+ },
+ } as unknown as Element,
attribute: index === 0 ? "src" : "href",
path,
})) as IHTMLLocalAsset[];
applyHTMLLocalAssetPaths(assets, [undefined, "assets/b.pdf"]);
- assert.deepEqual(values, ["file:///tmp/a.png", "assets/b.pdf"]);
+ assert.deepEqual(values, [undefined, "assets/b.pdf"]);
});
it("removes local paths before remote HTML conversion", () => {
diff --git a/app/src/protyle/upload/htmlLocalAssets.ts b/app/src/protyle/upload/htmlLocalAssets.ts
index 85d0a350c7..9ea150e681 100644
--- a/app/src/protyle/upload/htmlLocalAssets.ts
+++ b/app/src/protyle/upload/htmlLocalAssets.ts
@@ -77,10 +77,11 @@ export const collectHTMLLocalAssets = (root: ParentNode) => {
};
export const applyHTMLLocalAssetPaths = (assets: IHTMLLocalAsset[], paths: Array<string | undefined>) => {
- paths.forEach((path, index) => {
- const asset = assets[index];
- if (asset && path) {
- asset.element.setAttribute(asset.attribute, path);
+ assets.forEach((asset, index) => {
+ if (paths[index]) {
+ asset.element.setAttribute(asset.attribute, paths[index]);
+ } else {
+ asset.element.removeAttribute(asset.attribute);
}
});
};
diff --git a/app/src/protyle/upload/index.test.ts b/app/src/protyle/upload/index.test.ts
index b0252d1c79..e9f5480d30 100644
--- a/app/src/protyle/upload/index.test.ts
+++ b/app/src/protyle/upload/index.test.ts
@@ -8,8 +8,9 @@ import * as insertPosition from "./insertPosition";
import * as uploadResult from "./uploadResult";
// 执行完整上传模块,替换网络、界面和插件等待,以控制文档切换发生的时刻。
-const createHarness = () => {
+const createHarness = (failLocalUpload = false) => {
const results: Array<{status: string}> = [];
+ const completions: Array<Omit<IAssetUploadResult, "requestId" | "input"> | undefined> = [];
const requests: Array<{id: string; respond(): void}> = [];
let resolvePlugin: (prepared: unknown) => void;
let onComplete = () => {};
@@ -26,6 +27,9 @@ const createHarness = () => {
};
const response = {code: 0, data: {succMap: {"image.png": "assets/image.png"},
succFiles: [{index: 0, name: "image.png", path: "assets/image.png"}]}};
+ const localResponse: {code: number; msg?: string; data: Pick<IAssetUploadResult, "succMap" | "succFiles" | "failedFiles">} =
+ failLocalUpload ? {code: -1, msg: "local asset path is not allowed",
+ data: {succMap: {}, succFiles: [], failedFiles: [{index: 0, name: "image.png", error: "denied"}]}} : response;
class FormDataStub {
values = new Map<string, unknown>();
append(key: string, value: unknown) { this.values.set(key, value); }
@@ -61,7 +65,7 @@ const createHarness = () => {
}
}},
"../../util/fetch": {fetchSyncPost: (_url: string, data: {id: string}) =>
- new Promise(resolve => requests.push({id: data.id, respond: () => resolve(response)}))},
+ new Promise(resolve => requests.push({id: data.id, respond: () => resolve(localResponse)}))},
"./pluginEvent": {prepareAssetUpload: ({input}: {input: IAssetUploadInput}) => {
const task = {input, startUpload() {}, complete(result: {status: string}) {
results.push(result);
@@ -94,10 +98,10 @@ const createHarness = () => {
() => { consumed++; }, undefined, options);
} else {
api.uploadLocalFiles([{path: "/image.png", size: 1}], protyle as unknown as IProtyle,
- true, options, () => { consumed++; });
+ true, options, () => { consumed++; }, (_succeeded, result) => completions.push(result));
}
};
- return {protyle, requests, results, start, resume: () => resolvePlugin(undefined),
+ return {protyle, requests, results, completions, start, resume: () => resolvePlugin(undefined),
delayConfirmation: () => { delayConfirmation = true; }, confirm: () => confirm(),
consumed: () => consumed, onComplete: (callback: () => void) => { onComplete = callback; }};
};
@@ -105,6 +109,18 @@ const createHarness = () => {
const settle = () => new Promise(resolve => setImmediate(resolve));
describe("upload document binding", () => {
+ it("delivers failed local resource results to the completion callback", async () => {
+ const harness = createHarness(true);
+ harness.start("local-files");
+ harness.resume();
+ await settle();
+ harness.requests[0].respond();
+ await settle();
+ assert.equal(harness.consumed(), 0);
+ assert.equal(harness.completions[0].status, "failed");
+ assert.equal(harness.completions[0].failedFiles[0].index, 0);
+ });
+
it("uploads lightweight document fragments to their owning document", async () => {
const harness = createHarness();
harness.protyle.lite = true;
diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts
index e8665f3f62..38453f4078 100644
--- a/app/src/protyle/upload/index.ts
+++ b/app/src/protyle/upload/index.ts
@@ -217,7 +217,7 @@ interface IAssetUploadCallbacks {
input: IAssetUploadInput,
result: Omit<IAssetUploadResult, "requestId" | "input">): void | PromiseLike<void>;
formatResponse?: (responseText: string, input: IAssetUploadInput) => string;
- complete?: (succeeded: boolean) => void;
+ complete?: (succeeded: boolean, result?: Omit<IAssetUploadResult, "requestId" | "input">) => void;
reset?: () => void;
}
@@ -257,9 +257,10 @@ const getErrorMessage = (error: unknown) => {
return typeof error === "string" ? error : "";
};
-const finishCallbacks = (callbacks: IAssetUploadCallbacks, succeeded: boolean) => {
+const finishCallbacks = (callbacks: IAssetUploadCallbacks, succeeded: boolean,
+ result?: Omit<IAssetUploadResult, "requestId" | "input">) => {
try {
- callbacks.complete?.(succeeded);
+ callbacks.complete?.(succeeded, result);
} catch (error) {
console.error(error);
}
@@ -275,7 +276,7 @@ const finishUpload = (task: IAssetUploadTask | undefined, callbacks: IAssetUploa
if (task && !task.complete(result)) {
return;
}
- finishCallbacks(callbacks, result.status === "success");
+ finishCallbacks(callbacks, result.status === "success", result);
};
const finishSuccessfulUpload = (task: IAssetUploadTask | undefined, callbacks: IAssetUploadCallbacks,
@@ -310,7 +311,7 @@ const finishSuccessfulUpload = (task: IAssetUploadTask | undefined, callbacks: I
console.error(messageError);
}
} finally {
- finishCallbacks(callbacks, result.status === "success");
+ finishCallbacks(callbacks, result.status === "success", result);
}
};
@@ -678,7 +679,7 @@ export const uploadStandaloneAssetFiles = async (files: File[], options: {
export const uploadLocalFiles = (files: ILocalFiles[], protyle: IProtyle, isUpload: boolean,
options?: IUploadInsertOptions, successCB?: (response: IWebSocketData,
result: Omit<IAssetUploadResult, "requestId" | "input">) => void,
- completeCB?: (succeeded: boolean) => void) => {
+ completeCB?: IAssetUploadCallbacks["complete"]) => {
if (isProtyleUploadDisabled(protyle)) {
completeCB?.(false);
return;
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index a192cc4b92..58dd25c516 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -10,6 +10,7 @@ import {
captureUploadDocument,
createUploadInsertPosition,
getAvailableUploadInsertRange,
+ isUploadDocumentAvailable,
isUploadInsertPositionAvailable,
} from "../upload/insertPosition";
import {processPasteCode, processRender} from "./processCode";
@@ -44,7 +45,7 @@ import {
type IHTMLEmbeddedAsset,
validateHTMLEmbeddedAssetSizes,
} from "../upload/htmlEmbeddedAssets";
-import {getCompleteAssetUploadPathsByInput} from "../upload/uploadResult";
+import {getAssetUploadPathsByInput, getCompleteAssetUploadPathsByInput} from "../upload/uploadResult";
import {resolveLinkDest} from "../toolbar/util";
import {updateTransaction} from "../wysiwyg/transaction";
import * as dayjs from "dayjs";
@@ -1439,24 +1440,31 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
preparedHTML = true;
}
if (localAssets.length > 0) {
- let localAssetPaths: string[] | undefined;
+ let localAssetResult: Omit<IAssetUploadResult, "requestId" | "input"> | undefined;
await new Promise<void>(resolve => {
uploadLocalFiles(localAssets.map(item => ({path: item.path, size: null})), protyle, true, {
...assetUploadOptions,
requiredFileCount: localAssets.length,
fromHTMLPaste: true,
}, (_response, result) => {
- localAssetPaths = getCompleteAssetUploadPathsByInput(localAssets.length, result);
- }, () => resolve());
+ localAssetResult = result;
+ }, (_succeeded, result) => {
+ localAssetResult = result || localAssetResult;
+ resolve();
+ });
});
- if (!localAssetPaths) {
+ if (!localAssetResult || localAssetResult.status === "canceled" ||
+ !isUploadDocumentAvailable(protyle, assetUploadOptions.document)) {
return;
}
range = restorePasteInsertRange();
if (!range) {
return;
}
- applyHTMLLocalAssetPaths(localAssets, localAssetPaths);
+ applyHTMLLocalAssetPaths(localAssets, getAssetUploadPathsByInput(localAssets.length, localAssetResult));
+ if (localAssetResult.status !== "success") {
+ showMessage(window.siyuan.languages.uploadError);
+ }
}
let embeddedAssets: IHTMLEmbeddedAsset[];
try {
diff --git a/app/tests/markdownClipboard.test.js b/app/tests/markdownClipboard.test.js
index f49ad066b2..3585ef8728 100644
--- a/app/tests/markdownClipboard.test.js
+++ b/app/tests/markdownClipboard.test.js
@@ -11,6 +11,8 @@ const sources = () => {
return {
clipboard: compile("protyle/util/markdownClipboard.ts"),
paste: compile("protyle/util/paste.ts"),
+ localAssets: compile("protyle/upload/htmlLocalAssets.ts"),
+ uploadResult: compile("protyle/upload/uploadResult.ts"),
fixture: JSON.parse(readFileSync(path.join(__dirname, "fixtures/doubao-clipboard.json"), "utf8")),
};
};
@@ -90,9 +92,13 @@ const runCases = async source => {
};
// 执行共享入口,覆盖桌面剪贴板事件、移动端读取结果以及保留来源格式选项。
- const paste = async (mobile, preserveSourceFormat = false, restricted = false) => {
+ const paste = async (mobile, preserveSourceFormat = false, restricted = false, localAssetStatus, switched = false) => {
let inserted;
let htmlConversions = 0;
+ let convertedSource;
+ const pasteHTML = localAssetStatus ? '<p>Before<img src="file:///tmp/missing.png"></p>' +
+ '<table><tr><td>Table cell</td></tr></table><p><img src="file:///tmp/good.png"></p><p>After</p>' : html;
+ const pasteText = localAssetStatus ? "" : text;
let checkedBlockDOM;
const validated = new Error("validated restricted content");
const block = document.createElement("div");
@@ -106,7 +112,7 @@ const runCases = async source => {
"../runtimeCapabilities": {
getProtyleBlockDOMSanitizer: () => restricted ? value => value : undefined,
isProtyleRichHTMLPasteEnabled: () => restricted,
- isProtyleUploadDisabled: () => true,
+ isProtyleUploadDisabled: () => !localAssetStatus,
areProtylePluginExtensionsEnabled: () => false,
getProtyleUnsupportedPasteBlocks: () => value => {
checkedBlockDOM = value;
@@ -116,6 +122,7 @@ const runCases = async source => {
"../upload/insertPosition": {
createUploadInsertPosition: () => ({range}), captureUploadDocument: () => ({}),
isUploadInsertPositionAvailable: () => true, getAvailableUploadInsertRange: () => range,
+ isUploadDocumentAvailable: () => !switched,
},
"./selection": {getEditorRange: () => range},
"./hasClosest": {hasClosestBlock: () => block},
@@ -127,12 +134,28 @@ const runCases = async source => {
"./pasteSource": {extractCrossBlockPasteContext: value => ({html: value})},
"./processCode": {processPasteCode: () => false, processRender: noop},
"./inlineElementMarker": {stripSemanticMarkersFromRangeText: () => ""},
- "../upload/htmlLocalAssets": {resolveHTMLAssetURLs: noop, getHTMLAssetSourceURL: () => undefined,
- collectHTMLLocalAssets: () => [], removeHTMLLocalAssetPaths: noop},
- "../upload/htmlEmbeddedAssets": {hasHTMLEmbeddedAssets: () => false, collectHTMLEmbeddedAssets: () => []},
- "../../util/hostCapabilities": {getHostCapabilities: () => ({localFileSystem: false})},
+ "../upload/htmlLocalAssets": load(source.localAssets),
+ "../upload/uploadResult": load(source.uploadResult),
+ "../upload/htmlEmbeddedAssets": {hasHTMLEmbeddedAssets: () => false, collectHTMLEmbeddedAssets: () => [],
+ validateHTMLEmbeddedAssetSizes: noop},
+ "../../util/hostCapabilities": {getHostCapabilities: () => ({localFileSystem: !mobile && Boolean(localAssetStatus)})},
+ "../../dialog/message": {showMessage: noop},
+ "../upload": {uploadLocalFiles: (files, _protyle, _upload, options, success, complete) => {
+ check.equal(files.length, 2);
+ check.equal(options.fromHTMLPaste, true);
+ const result = {status: localAssetStatus, succFiles: localAssetStatus === "partial" ?
+ [{index: 1, name: "good.png", path: "assets/good.png"}] : []};
+ if (localAssetStatus === "partial") {
+ success({}, result);
+ }
+ complete(false, result);
+ }},
"../../util/fetch": {fetchSyncPost: async (_url, request) => {
+ if (request.preflight) {
+ return {code: 0, data: {converted: true, useHTML: true, normalizedHTML: request.dom}};
+ }
htmlConversions++;
+ convertedSource = request.dom;
return {code: 0, data: lute.HTML2BlockDOM(request.dom)};
}},
"./insertHTML": {insertHTML: value => { inserted = value; }},
@@ -143,12 +166,12 @@ const runCases = async source => {
"../../util/highlightById": {scrollCenter: noop},
"electron": {ipcRenderer: {invoke: async () => ""}},
};
- window.siyuan = {config: {editor: {pasteURLAutoConvert: false}}};
+ window.siyuan = {config: {editor: {pasteURLAutoConvert: false}}, languages: {uploadError: "upload failed"}};
const protyle = {lute, hint: {}, options: {upload: {max: 1024 * 1024}},
wysiwyg: {element: block}, toolbar: {getCurrentType: () => []}};
- const event = mobile ? {textHTML: html, textPlain: text, siyuanHTML: "", target: block, preserveSourceFormat} : {
+ const event = mobile ? {textHTML: pasteHTML, textPlain: pasteText, siyuanHTML: "", target: block, preserveSourceFormat} : {
target: block, stopPropagation: noop, preventDefault: noop, preserveSourceFormat,
- clipboardData: {files: [], types: [], getData: type => ({"text/html": html, "text/plain": text}[type] || "")},
+ clipboardData: {files: [], types: [], getData: type => ({"text/html": pasteHTML, "text/plain": pasteText}[type] || "")},
};
try {
await load(source.paste, mocks).paste(protyle, event);
@@ -158,7 +181,7 @@ const runCases = async source => {
throw error;
}
}
- return {inserted, htmlConversions, checkedBlockDOM};
+ return {inserted, htmlConversions, checkedBlockDOM, convertedSource};
};
for (const mobile of [false, true]) {
const result = await paste(mobile);
@@ -171,6 +194,28 @@ const runCases = async source => {
const restrictedPreserved = await paste(mobile, true, true);
check.doesNotMatch(restrictedPreserved.checkedBlockDOM, /data-type="inline-math"/);
}
+ for (const status of ["partial", "failed", "canceled"]) {
+ const result = await paste(false, true, false, status);
+ if (status === "canceled") {
+ check.equal(result.inserted, undefined);
+ check.equal(result.htmlConversions, 0);
+ continue;
+ }
+ check.match(result.inserted, /Before/);
+ check.match(result.inserted, /After/);
+ check.match(result.inserted, /Table cell/);
+ check.match(result.inserted, /data-type="NodeTable"/);
+ check.doesNotMatch(result.convertedSource, /file:\/\//);
+ if (status === "partial") {
+ check.match(result.convertedSource, /src="assets\/good.png"/);
+ }
+ }
+ const mobileLocal = await paste(true, true, false, "failed");
+ check.match(mobileLocal.inserted, /Table cell/);
+ check.doesNotMatch(mobileLocal.convertedSource, /file:\/\//);
+ const switched = await paste(false, true, false, "partial", true);
+ check.equal(switched.inserted, undefined);
+ check.equal(switched.htmlConversions, 0);
return "Markdown clipboard cases passed";
};
diff --git a/kernel/api/asset_html_paste_test.go b/kernel/api/asset_html_paste_test.go
new file mode 100644
index 0000000000..6d912cdfc4
--- /dev/null
+++ b/kernel/api/asset_html_paste_test.go
@@ -0,0 +1,98 @@
+package api
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
+ "github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestAPIContractHTMLLocalAssets(t *testing.T) {
+ previousConf, previousWorkspace, previousData, previousHome := model.Conf, util.WorkspaceDir, util.DataDir, util.HomeDir
+ util.WorkspaceDir, util.HomeDir = t.TempDir(), t.TempDir()
+ util.DataDir = filepath.Join(util.WorkspaceDir, "data")
+ model.Conf = model.NewAppConf()
+ model.Conf.Sync = conf.NewSync()
+ boxID := "20261002120000-htmlenc"
+ t.Cleanup(func() {
+ model.Conf, util.WorkspaceDir, util.DataDir, util.HomeDir = previousConf, previousWorkspace, previousData, previousHome
+ })
+ write := func(name string, data []byte) {
+ t.Helper()
+ if err := os.MkdirAll(filepath.Dir(name), 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(name, data, 0600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ boxConf := conf.NewBoxConf()
+ boxConf.Encrypted = true
+ boxData, _ := json.Marshal(boxConf)
+ write(filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json"), boxData)
+ source := filepath.Join(t.TempDir(), "image space.png")
+ content := []byte("HTML paste temporary image")
+ write(source, content)
+ write(filepath.Join(util.DataDir, "assets", "image space.png"), content)
+ hash, err := util.GetEtagByHandle(bytes.NewReader(content), int64(len(content)))
+ if err != nil {
+ t.Fatal(err)
+ }
+ cache.SetAssetHash(hash, "assets/image space.png")
+ t.Cleanup(func() { cache.RemoveAssetHash(hash) })
+ fileURL := (&url.URL{Scheme: "file", Path: "/" + strings.TrimPrefix(filepath.ToSlash(source), "/")}).String()
+ paths := []string{fileURL, filepath.Join(util.HomeDir, ".ssh", "private.png"),
+ filepath.Join(util.WorkspaceDir, "conf", "private.png"), filepath.Join(util.WorkspaceDir, "temp", "decrypted.png"),
+ filepath.Join(util.DataDir, boxID, "assets", "encrypted.png"), filepath.Join(t.TempDir(), "missing.png"), fileURL}
+ for _, p := range paths[1:5] {
+ write(p, []byte("protected original"))
+ }
+ body, _ := json.Marshal(apicontract.InsertLocalAssetsRequest{AssetPaths: paths, FromHTMLPaste: true})
+ recorder := httptest.NewRecorder()
+ context, _ := gin.CreateTestContext(recorder)
+ context.Request = httptest.NewRequest(http.MethodPost, "/api/asset/insertLocalAssets", bytes.NewReader(body))
+ context.Request.Header.Set("Content-Type", "application/json")
+ insertLocalAssets(context)
+ requireAPIContract(t, http.MethodPost, "/api/asset/insertLocalAssets", recorder)
+ var response struct {
+ Code int `json:"code"`
+ Data apicontract.AssetUploadData `json:"data"`
+ }
+ if err = json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatal(err)
+ }
+ if response.Code != -1 || len(response.Data.SuccFiles) != 2 || len(response.Data.FailedFiles) != 5 {
+ t.Fatalf("unexpected partial response: %s", recorder.Body.String())
+ }
+ for i, index := range []int{0, 6} {
+ if response.Data.SuccFiles[i].Index != index || response.Data.SuccFiles[i].Path != "assets/image space.png" {
+ t.Fatalf("successful input mapping changed: %+v", response.Data.SuccFiles)
+ }
+ }
+ for i, failure := range response.Data.FailedFiles {
+ if failure.Index != i+1 {
+ t.Fatalf("failed input mapping changed: %+v", response.Data.FailedFiles)
+ }
+ if i < 4 && failure.Error != "local asset path is not allowed" {
+ t.Errorf("protected file was not rejected: %+v", failure)
+ }
+ }
+ for _, p := range paths[1:5] {
+ data, err := os.ReadFile(p)
+ if err != nil || string(data) != "protected original" {
+ t.Errorf("protected source changed: %s (%v)", p, err)
+ }
+ }
+}
diff --git a/kernel/apicontract/asset.go b/kernel/apicontract/asset.go
index 3750efd7d1..ad658492df 100644
--- a/kernel/apicontract/asset.go
+++ b/kernel/apicontract/asset.go
@@ -50,6 +50,9 @@ type AssetPathsCloudUploadRequest struct {
IgnorePushMsg bool `json:"ignorePushMsg" api:"optional,nullable"`
}
+// InsertLocalAssetsRequest 在 fromHTMLPaste 为 true 时上传剪贴板引用的本地资源。
+// 允许系统临时目录及用户文档中的普通文件,仍拒绝凭据、工作空间私有路径及加密笔记本原始文件。
+// 单个文件失败通过 failedFiles 返回,其他文件的成功结果保留在 succFiles 中。
type InsertLocalAssetsRequest struct {
AssetPaths []string `json:"assetPaths"`
IsUpload *bool `json:"isUpload" api:"optional"`
diff --git a/kernel/model/upload.go b/kernel/model/upload.go
index a94faf5837..cea98187dd 100644
--- a/kernel/model/upload.go
+++ b/kernel/model/upload.go
@@ -199,7 +199,8 @@ func insertLocalAssets(id string, assetAbsPaths []string, isUpload, validateHTML
assetAbsPath = util.FileURLToLocalPath(assetAbsPath)
}
baseName := filepath.Base(assetAbsPath)
- if validateHTMLPath && (util.IsSensitivePath(assetAbsPath) || EncryptedRawPathBoxID(assetAbsPath) != "") {
+ if validateHTMLPath && (util.IsSensitiveHTMLAssetPath(assetAbsPath) || EncryptedRawPathBoxID(assetAbsPath) != "") {
+ logging.LogWarnf("HTML paste local asset path is not allowed [%s]", assetAbsPath)
recordAssetUploadFailure(&failedFiles, index, baseName, errors.New("local asset path is not allowed"))
continue
}
diff --git a/kernel/util/path.go b/kernel/util/path.go
index 94c538ef3c..352b81ee63 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -451,7 +451,7 @@ func IsSensitivePath(p string) bool {
if p == "" {
return false
}
- if isSensitivePath(p) {
+ if isSensitivePath(p, false) {
return true
}
// 仅对工作空间外的路径解析符号链接,防止用符号链接绕过黑名单指向敏感目标。
@@ -460,7 +460,27 @@ func IsSensitivePath(p string) bool {
}
resolved := ResolveLongestExistingParent(p)
if resolved != p {
- if isSensitivePath(resolved) {
+ if isSensitivePath(resolved, false) {
+ return true
+ }
+ }
+ return false
+}
+
+// IsSensitiveHTMLAssetPath 允许剪贴板引用用户文档及系统临时文件,仍检查凭据、工作空间私有目录和符号链接目标。
+func IsSensitiveHTMLAssetPath(p string) bool {
+ if p == "" {
+ return false
+ }
+ return isSensitivePath(p, true) || isSensitivePath(ResolveLongestExistingParent(p), true)
+}
+
+func isHTMLAssetUserPath(p string) bool {
+ for _, root := range []string{os.TempDir(), HomeDir, systemHomeDir} {
+ if root == "" {
+ continue
+ }
+ if gulu.File.IsSubPath(root, p) || gulu.File.IsSubPath(ResolveLongestExistingParent(root), p) {
return true
}
}
@@ -468,7 +488,7 @@ func IsSensitivePath(p string) bool {
}
// isSensitivePath 执行敏感性黑名单匹配,必要时解析工作空间路径,但不解析目标路径。
-func isSensitivePath(p string) bool {
+func isSensitivePath(p string, htmlAsset bool) bool {
toCheckPathLower := filepath.Clean(strings.ToLower(p))
toCheckNameLower := filepath.Base(toCheckPathLower)
workspaceDir := WorkspaceDir
@@ -482,9 +502,9 @@ func isSensitivePath(p string) bool {
}
}
- // 系统目录前缀检查仅对工作空间外的路径执行,工作空间内仍需检查配置、临时文件和凭据。
- // iOS 沙箱及 Linux /var/home 下的合法工作空间可能位于 /var,需按工作空间边界判断。
- if !inWorkspace {
+ // 工作空间和剪贴板引用的用户目录可位于系统目录中,但仍需检查配置、私有临时文件和凭据。
+ // iOS 沙箱、macOS 临时目录及 Linux /var/home 均按各自目录边界判断。
+ if !inWorkspace && !(htmlAsset && isHTMLAssetUserPath(p)) {
// 敏感目录前缀(UNIX 风格)
prefixes := []string{
"/.",
diff --git a/kernel/util/path_html_asset_test.go b/kernel/util/path_html_asset_test.go
new file mode 100644
index 0000000000..523984f3fd
--- /dev/null
+++ b/kernel/util/path_html_asset_test.go
@@ -0,0 +1,72 @@
+package util
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+)
+
+func TestIsSensitivePathHTMLAssets(t *testing.T) {
+ previousHome, previousWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = t.TempDir(), t.TempDir()
+ t.Cleanup(func() { HomeDir, WorkspaceDir = previousHome, previousWorkspace })
+ for _, item := range []struct {
+ path string
+ want bool
+ }{
+ {filepath.Join(t.TempDir(), "lu123.tmp", "image.png"), false},
+ {filepath.Join(HomeDir, "Documents", "image.png"), false},
+ {filepath.Join(HomeDir, ".ssh", "key.png"), true},
+ {filepath.Join(HomeDir, ".config", "token"), true},
+ {filepath.Join(HomeDir, ".netrc"), true},
+ {filepath.Join(os.TempDir(), "credentials.json"), true},
+ {filepath.Join(os.TempDir(), "id_rsa"), true},
+ {filepath.Join(WorkspaceDir, "conf", "conf.json"), true},
+ {filepath.Join(WorkspaceDir, "temp", "decrypted.png"), true},
+ {filepath.Join(WorkspaceDir, "temp", "export", "image.png"), false},
+ } {
+ if got := IsSensitiveHTMLAssetPath(item.path); got != item.want {
+ t.Errorf("IsSensitiveHTMLAssetPath(%q) = %v, want %v", item.path, got, item.want)
+ }
+ }
+}
+
+func TestIsSensitivePathHTMLAssetsUnixRoots(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Unix system directory prefixes")
+ }
+ previousHome, previousWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = "/var/home/siyuan-html-paste", "/var/home/siyuan-html-paste/workspace"
+ t.Cleanup(func() { HomeDir, WorkspaceDir = previousHome, previousWorkspace })
+ t.Setenv("TMPDIR", "/var/folders/siyuan-html-paste/T")
+ for _, p := range []string{HomeDir + "/Documents/image.png", os.TempDir() + "/lu123.tmp/image.png"} {
+ if IsSensitiveHTMLAssetPath(p) || !IsSensitivePath(p) {
+ t.Errorf("clipboard exception must not change generic path protection: %s", p)
+ }
+ }
+ for _, p := range []string{"/var/home/siyuan-html-paste-other/image.png", "/var/lib/private.png", "/etc/passwd", "/proc/self/environ"} {
+ if !IsSensitiveHTMLAssetPath(p) {
+ t.Errorf("system path was allowed: %s", p)
+ }
+ }
+}
+
+func TestIsSensitivePathHTMLAssetsSymlinks(t *testing.T) {
+ previousHome, previousWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = t.TempDir(), t.TempDir()
+ t.Cleanup(func() { HomeDir, WorkspaceDir = previousHome, previousWorkspace })
+ for _, relative := range []string{"Documents", ".ssh"} {
+ target := filepath.Join(HomeDir, relative)
+ if err := os.MkdirAll(target, 0700); err != nil {
+ t.Fatal(err)
+ }
+ link := filepath.Join(t.TempDir(), "image-dir")
+ if err := os.Symlink(target, link); err != nil {
+ t.Skipf("symlink unavailable: %v", err)
+ }
+ if got := IsSensitiveHTMLAssetPath(filepath.Join(link, "image.png")); got != (relative == ".ssh") {
+ t.Errorf("incorrect protection for symlink to %s", relative)
+ }
+ }
+}
diff --git a/kernel/util/path_test.go b/kernel/util/path_test.go
index 78f10f801a..7649aaf518 100644
--- a/kernel/util/path_test.go
+++ b/kernel/util/path_test.go
@@ -254,10 +254,10 @@ func TestIsSensitivePathWorkspacePrefixBoundary(t *testing.T) {
t.Cleanup(func() { WorkspaceDir = originalWorkspace })
for _, parent := range parents {
WorkspaceDir = filepath.Join(parent, "siyuan-test-workspace")
- if isSensitivePath(filepath.Join(WorkspaceDir, "data", "public.txt")) {
+ if isSensitivePath(filepath.Join(WorkspaceDir, "data", "public.txt"), false) {
t.Fatalf("workspace data should be allowed: %s", WorkspaceDir)
}
- if !isSensitivePath(filepath.Join(WorkspaceDir+"-outside", "public.txt")) {
+ if !isSensitivePath(filepath.Join(WorkspaceDir+"-outside", "public.txt"), false) {
t.Fatalf("workspace prefix sibling should remain sensitive: %s", WorkspaceDir)
}
}