Skip to content

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

:bug: Harden Live Photo upload and preview handling https://github.com/siyuan-note/siyuan/issues/20675

Danielf6fb171cfdb4
diff --git a/app/src/protyle/preview/image.livePhoto.test.ts b/app/src/protyle/preview/image.livePhoto.test.ts
index 1d6b52d1e0..101eb36287 100644
--- a/app/src/protyle/preview/image.livePhoto.test.ts
+++ b/app/src/protyle/preview/image.livePhoto.test.ts
@@ -8,7 +8,7 @@ import {test} from "node:test";
import {promisify} from "node:util";
import {transpileModule, ModuleKind, ScriptTarget} from "typescript";
-test("image gallery selects each Live Photo association and releases playback on navigation", async () => {
+test("image gallery releases playing and converting Live Photos on navigation and slideshow", async () => {
const root = realpathSync(tmpdir());
const temporary = mkdtempSync(path.join(root, "siyuan-live-photo-gallery-"));
const server = createServer((request, response) => {
@@ -30,6 +30,9 @@ test("image gallery selects each Live Photo association and releases playback on
const browserCase = `(async () => {
const starts = [];
let stopped = 0;
+ let phase = "playing";
+ const closed = [];
+ const active = {playing:false,converting:false};
const image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=";
const entries = [{src:image,video:"assets/first.mov"},{src:image,video:"assets/second.mov"},{src:image}];
window.siyuan = {languages:{livePhoto:"Live Photo",previous:"Previous",next:"Next"}};
@@ -40,7 +43,12 @@ test("image gallery selects each Live Photo association and releases playback on
const script=document.createElement("script");script.src=src;script.onload=resolve;script.onerror=reject;document.head.append(script);
}),
fetchPost: (_url,_data,callback) => callback({data:entries}),
- createLivePhotoPlayer: (_host,video) => {starts.push(video);return () => {stopped++;};},
+ createLivePhotoPlayer: (_host,video) => {
+ const current=phase;
+ active[current]=true;
+ starts.push(video);
+ return () => {stopped++;closed.push(current);active[current]=false;};
+ },
isEncryptedBox: () => false,
});
${source}
@@ -57,11 +65,20 @@ test("image gallery selects each Live Photo association and releases playback on
viewer.view(0);
await wait(()=>viewer.index===0 && viewer.viewed);
button.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",bubbles:true}));
+ viewer.options.autoplay=false;
+ viewer.play();
+ if(active.playing || stopped!==2){throw new Error("slideshow retained previous playback");}
+ viewer.stop();
+ phase="converting";
+ button.click();
+ viewer.play();
+ if(active.converting || stopped!==3){throw new Error("slideshow retained pending conversion");}
+ viewer.stop();
viewer.view(2);
await wait(()=>viewer.index===2 && viewer.viewed);
const hidden=button.classList.contains("fn__none");
viewer.destroy();
- return {starts,stopped,hidden};
+ return {starts,stopped,hidden,closed};
})()`;
const script = path.join(temporary, "main.cjs");
writeFileSync(script, `
@@ -82,7 +99,8 @@ test("image gallery selects each Live Photo association and releases playback on
env, windowsHide: true, timeout: 20000,
});
assert.deepEqual(JSON.parse(result.stdout.trim().split("\n").at(-1)), {
- starts: ["assets/second.mov", "assets/first.mov"], stopped: 2, hidden: true,
+ starts: ["assets/second.mov", "assets/first.mov", "assets/first.mov"], stopped: 3, hidden: true,
+ closed: ["playing", "playing", "converting"],
});
} finally {
server.close();
diff --git a/app/src/protyle/preview/image.ts b/app/src/protyle/preview/image.ts
index bab2f67e20..2ffe633d9a 100644
--- a/app/src/protyle/preview/image.ts
+++ b/app/src/protyle/preview/image.ts
@@ -47,6 +47,8 @@ export const previewImages = (srcList: Array<string | {src: string, video?: stri
stopLivePhoto?.();
stopLivePhoto = undefined;
};
+ imagesElement.addEventListener("play", stopLive);
+ imagesElement.addEventListener("playing", stopLive);
const close = () => {
stopLive();
viewer.destroy();
@@ -174,6 +176,8 @@ export const previewImages = (srcList: Array<string | {src: string, video?: stri
const destroyViewer = viewer.destroy.bind(viewer);
viewer.destroy = () => {
stopLive();
+ imagesElement.removeEventListener("play", stopLive);
+ imagesElement.removeEventListener("playing", stopLive);
destroyViewer();
};
window.siyuan.viewer = viewer;
diff --git a/app/src/protyle/upload/index.test.ts b/app/src/protyle/upload/index.test.ts
index d80ea80750..6c8e5929b8 100644
--- a/app/src/protyle/upload/index.test.ts
+++ b/app/src/protyle/upload/index.test.ts
@@ -8,6 +8,9 @@ import * as insertPosition from "./insertPosition";
import * as uploadResult from "./uploadResult";
import * as fetchAppId from "../../util/fetchAppId";
+Object.assign(globalThis, {SIYUAN_VERSION: "test", NODE_ENV: "test"});
+const livePhotoPairs = require("./livePhotoPairs") as typeof import("./livePhotoPairs");
+
// 执行完整上传模块,替换网络、界面和插件等待,以控制文档切换发生的时刻。
const createHarness = (failLocalUpload = false) => {
const results: Array<{status: string}> = [];
@@ -18,6 +21,8 @@ const createHarness = (failLocalUpload = false) => {
let onComplete = () => {};
let confirm: (() => void) | undefined;
let delayConfirmation = false;
+ let renderRange: Range | undefined;
+ let insertedHTML = "";
const protyle = {
lite: false,
element: {isConnected: true},
@@ -53,12 +58,21 @@ const createHarness = (failLocalUpload = false) => {
}
}
const mocks: Record<string, unknown> = {
- "./insertPosition": insertPosition,
+ "./insertPosition": {...insertPosition, getAvailableUploadInsertRange:
+ (...args: Parameters<typeof insertPosition.getAvailableUploadInsertRange>) =>
+ renderRange || insertPosition.getAvailableUploadInsertRange(...args)},
"../../util/fetchAppId": fetchAppId,
"./uploadResult": uploadResult,
+ "./livePhotoPairs": livePhotoPairs,
+ "../util/insertHTML": {insertHTML: (html: string) => { insertedHTML = html; }},
+ "../util/hasClosest": {hasClosestBlock: (): undefined => undefined},
+ "../../util/pathName": {pathPosix: () => ({extname: (name: string) => name.slice(name.lastIndexOf("."))})},
+ "../../asset/renderAssets": {genAssetHTML: (type: string, path: string) =>
+ type === ".heic" ? `<img src="${path}">` : `<video src="${path}"></video>`},
"../runtimeCapabilities": {isProtyleUploadDisabled: () => false},
"../../util/hostCapabilities": {getHostCapabilities: () => ({localFileSystem: true})},
- "../../constants": {Constants: {SIZE_UPLOAD_TIP_SIZE: 1024, SIYUAN_APPID: "upload-window"}},
+ "../../constants": {Constants: {SIZE_UPLOAD_TIP_SIZE: 1024, SIYUAN_APPID: "upload-window",
+ SIYUAN_ASSETS_IMAGE: [".heic"], SIYUAN_ASSETS_AUDIO: [], SIYUAN_ASSETS_VIDEO: [".mov"]}},
"../../util/escape": {escapeHtml: (value: string) => value},
"../../dialog/message": {showMessage: () => "message", hideMessage: () => {}},
"../../dialog/confirmDialog": {confirmDialog: (_title: string, _msg: string, yes: () => void) => {
@@ -86,14 +100,17 @@ const createHarness = (failLocalUpload = false) => {
const compiled = ts.transpileModule(source, {compilerOptions: {
module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022,
}}).outputText;
- runInNewContext(compiled, {
+ runInNewContext(compiled + "\nexports.renderUploadedLabel = genUploadedLabel;", {
module, exports: module.exports, require: (name: string) => mocks[name] || {}, Promise,
FormData: FormDataStub, XMLHttpRequest: XHRStub, DataTransferItem: class {},
location: {origin: "https://siyuan.test"},
document: {baseURI: "https://siyuan.test/stage/build/app/", body: {contains: () => protyle.element.isConnected}},
- window: {siyuan: {languages: {uploading: "uploading"}}}, console,
+ window: {siyuan: {languages: {uploading: "uploading"}}}, console, setTimeout: () => 0,
});
- const api = module.exports as typeof import("./index");
+ const api = module.exports as typeof import("./index") & {
+ renderUploadedLabel(responseText: string, protyle: IProtyle,
+ options: import("./index").IUploadInsertOptions, input: IAssetUploadInput): Promise<void>;
+ };
let consumed = 0;
const start = (kind: "files" | "local-files", document?: insertPosition.IUploadDocument) => {
const options = {target: "background" as const, document};
@@ -108,12 +125,30 @@ const createHarness = (failLocalUpload = false) => {
};
return {protyle, requests, results, completions, start, headers, resume: () => resolvePlugin(undefined),
delayConfirmation: () => { delayConfirmation = true; }, confirm: () => confirm(),
- consumed: () => consumed, onComplete: (callback: () => void) => { onComplete = callback; }};
+ consumed: () => consumed, onComplete: (callback: () => void) => { onComplete = callback; },
+ renderLivePhotos: async (files: File[], pairs: import("./livePhotoPairs").ILivePhotoPair[],
+ succMap: Record<string, string>) => {
+ renderRange = {toString: () => "", startContainer: {nodeType: 1}} as unknown as Range;
+ await api.renderUploadedLabel(JSON.stringify({code: 0, data: {succMap}}), protyle as unknown as IProtyle,
+ {livePhotoPairs: pairs}, {kind: "files", files});
+ return insertedHTML;
+ }};
};
const settle = () => new Promise(resolve => setImmediate(resolve));
describe("upload document binding", () => {
+ it("inserts both successful stills and only their matching legacy Live Photo video after a partial upload", async () => {
+ const harness = createHarness();
+ const files = ["a.heic", "a.mov", "b.heic", "b.mov"].map(name => new File([name], name));
+ const html = await harness.renderLivePhotos(files,
+ [{photo: files[0], video: files[1]}, {photo: files[2], video: files[3]}],
+ {"a.heic": "assets/a.heic", "b.heic": "assets/b.heic", "b.mov": "assets/b.mov"});
+ assert.match(html, /<img src="assets\/a.heic">/);
+ assert.match(html, /<img custom-sy-live-photo="1" custom-sy-live-photo-video="assets\/b.mov" src="assets\/b.heic">/);
+ assert.doesNotMatch(html, /<video/);
+ });
+
it("delivers failed local resource results to the completion callback", async () => {
const harness = createHarness(true);
harness.start("local-files");
diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts
index 75217042b6..1caa07dc80 100644
--- a/app/src/protyle/upload/index.ts
+++ b/app/src/protyle/upload/index.ts
@@ -160,7 +160,7 @@ const genUploadedLabel = async (responseText: string, protyle: IProtyle, options
range.setEndAfter(range.startContainer.parentElement);
range.collapse(false);
}
- let successes = getAssetUploadSuccesses(response.data);
+ let successes = getAssetUploadSuccesses(response.data, input);
const livePhotos = options?.livePhotoPairs && input?.kind === "files" ?
getLivePhotoUploadPairs(options.livePhotoPairs, input.files, successes) : undefined;
if (livePhotos) {
diff --git a/app/src/protyle/upload/livePhotoPairs.test.ts b/app/src/protyle/upload/livePhotoPairs.test.ts
index 1156925271..e51951f771 100644
--- a/app/src/protyle/upload/livePhotoPairs.test.ts
+++ b/app/src/protyle/upload/livePhotoPairs.test.ts
@@ -4,6 +4,7 @@ import {resolve} from "node:path";
Object.assign(globalThis, {SIYUAN_VERSION: "test", NODE_ENV: "test"});
const {attachLivePhotoVideo, getLivePhotoUploadPairs} = require("./livePhotoPairs") as typeof import("./livePhotoPairs");
+const {getAssetUploadSuccesses} = require("./uploadResult") as typeof import("./uploadResult");
test("Live Photo pairs follow uploaded file indices through reordering and deduplication", () => {
const photoA = new File(["photo-a"], "same.heic");
@@ -34,6 +35,40 @@ test("partial uploads never create a standalone video or associate a different f
assert.equal(replaced.videos.size, 0);
});
+test("legacy partial and reordered successes retain the matching photo and video", () => {
+ const files = ["a.heic", "a.mov", "b.heic", "b.mov"].map(name => new File([name], name));
+ const pairs = [{photo: files[0], video: files[1]}, {photo: files[2], video: files[3]}];
+ for (const succMap of [
+ {"a.heic": "assets/a.heic", "b.heic": "assets/b.heic", "b.mov": "assets/b.mov"},
+ {"b.mov": "assets/b.mov", "b.heic": "assets/b.heic", "a.heic": "assets/a.heic"},
+ ]) {
+ const successes = getAssetUploadSuccesses({succMap}, {kind: "files", files});
+ const result = getLivePhotoUploadPairs(pairs, files, successes);
+ assert.deepEqual([...result.videos], [[2, "assets/b.mov"]]);
+ assert.deepEqual(successes.filter(item => !result.hidden.has(item.index)).map(item => item.name).sort(),
+ ["a.heic", "b.heic"]);
+ assert.equal(result.incomplete, true);
+ }
+ const successes = getAssetUploadSuccesses({succMap: {
+ "b.mov": "assets/b.mov", "a.mov": "assets/a.mov", "b.heic": "assets/b.heic", "a.heic": "assets/a.heic",
+ }}, {kind: "files", files});
+ const result = getLivePhotoUploadPairs(pairs, files, successes);
+ assert.deepEqual([...result.videos], [[0, "assets/a.mov"], [2, "assets/b.mov"]]);
+ assert.equal(result.incomplete, false);
+});
+
+test("ambiguous legacy filenames keep the still image without pairing or inserting a standalone MOV", () => {
+ const files = ["same.heic", "same.mov", "same.heic", "same.mov"].map(name => new File([name], name));
+ const successes = getAssetUploadSuccesses({succMap: {
+ "same.heic": "assets/same.heic", "same.mov": "assets/same.mov",
+ }}, {kind: "files", files});
+ const result = getLivePhotoUploadPairs([{photo: files[0], video: files[1]}, {photo: files[2], video: files[3]}],
+ files, successes);
+ assert.equal(result.videos.size, 0);
+ assert.equal(result.incomplete, true);
+ assert.deepEqual(successes.filter(item => !result.hidden.has(item.index)).map(item => item.path), ["assets/same.heic"]);
+});
+
test("uploaded image markup retains the paired path through editor normalization", () => {
require(resolve("stage/protyle/js/lute/lute.min.js"));
const lute = Lute.New();
diff --git a/app/src/protyle/upload/livePhotoPairs.ts b/app/src/protyle/upload/livePhotoPairs.ts
index 2c3f4cef66..9045669136 100644
--- a/app/src/protyle/upload/livePhotoPairs.ts
+++ b/app/src/protyle/upload/livePhotoPairs.ts
@@ -10,7 +10,7 @@ export const attachLivePhotoVideo = (html: string, video: string) =>
html.replace(/<img\b/, `<img ${Constants.CUSTOM_SY_LIVE_PHOTO}="1" ${Constants.CUSTOM_SY_LIVE_PHOTO_VIDEO}="${escapeHtmlTextAndAttr(video)}"`);
export const getLivePhotoUploadPairs = (pairs: ILivePhotoPair[], files: File[],
- successes: Array<{index: number, path: string}>) => {
+ successes: Array<{index: number, path: string, name?: string}>) => {
const videos = new Map<number, string>();
const hidden = new Set<number>();
let incomplete = false;
@@ -20,6 +20,11 @@ export const getLivePhotoUploadPairs = (pairs: ILivePhotoPair[], files: File[],
const video = files.indexOf(pair.video);
if (video >= 0) {
hidden.add(video);
+ successes.forEach(item => {
+ if (item.index < 0 && item.name === pair.video.name) {
+ hidden.add(item.index);
+ }
+ });
}
if (photo >= 0 && video >= 0 && paths.has(photo) && paths.has(video)) {
videos.set(photo, paths.get(video));
diff --git a/app/src/protyle/upload/uploadResult.test.ts b/app/src/protyle/upload/uploadResult.test.ts
index 7e85e07277..1ac10fadd3 100644
--- a/app/src/protyle/upload/uploadResult.test.ts
+++ b/app/src/protyle/upload/uploadResult.test.ts
@@ -125,6 +125,24 @@ describe("asset upload result", () => {
};
assert.deepEqual(getAssetUploadPathsByInput(2, result), [undefined, "assets/b.png"]);
+ assert.deepEqual(getAssetUploadSuccesses(result, acceptedInput), [
+ {index: 1, name: "b.png", path: "assets/b.png"},
+ ]);
+ });
+
+ it("preserves legacy successes with unknown or ambiguous input positions without assigning a file index", () => {
+ const input: IAssetUploadInput = {kind: "local-files", files: [
+ {path: "C:\\first\\same.png", size: 1}, {path: "/second/same.png", size: 1},
+ {path: "/third/unique.png", size: 1},
+ ]};
+ const result = getAssetUploadSuccesses({succMap: {
+ "unique.png": "assets/unique.png", "same.png": "assets/same.png", "extra.png": "assets/extra.png",
+ }}, input);
+ assert.deepEqual(result, [
+ {index: 2, name: "unique.png", path: "assets/unique.png"},
+ {index: -2, name: "same.png", path: "assets/same.png"},
+ {index: -3, name: "extra.png", path: "assets/extra.png"},
+ ]);
});
it("does not guess legacy result positions for duplicate input names", () => {
diff --git a/app/src/protyle/upload/uploadResult.ts b/app/src/protyle/upload/uploadResult.ts
index a13bcbc5be..18740d60e4 100644
--- a/app/src/protyle/upload/uploadResult.ts
+++ b/app/src/protyle/upload/uploadResult.ts
@@ -1,11 +1,21 @@
export const getAssetUploadSuccesses = (data?: {
succFiles?: IAssetUploadSuccess[],
succMap?: Record<string, string>
-}): IAssetUploadSuccess[] => {
+}, input?: IAssetUploadInput): IAssetUploadSuccess[] => {
if (Array.isArray(data?.succFiles)) {
return data.succFiles;
}
- return Object.entries(data?.succMap || {}).map(([name, path], index) => ({index, name, path}));
+ const inputIndexes = new Map<string, number>();
+ if (input) {
+ const names = input.kind === "files" ? input.files.map(file => file.name) :
+ input.files.map(file => file.path.split(/[\\/]/).pop() || "");
+ names.forEach((name, index) => inputIndexes.set(name, inputIndexes.has(name) ? -1 : index));
+ }
+ return Object.entries(data?.succMap || {}).map(([name, path], index) => {
+ const inputIndex = inputIndexes.get(name);
+ // 无法确定原始位置的成功项仍可插入,但不参与依赖上传索引的配对。
+ return {index: input ? (inputIndex >= 0 ? inputIndex : -index - 1) : index, name, path};
+ });
};
export const getAssetUploadPathsByInput = (inputCount: number,
@@ -16,14 +26,9 @@ export const getAssetUploadPathsByInput = (inputCount: number,
if (Array.isArray(result.succFiles)) {
result.succFiles.forEach(item => succeeded.set(item.index, item.path));
} else if (result.succMap && result.acceptedInput) {
- const names = result.acceptedInput.kind === "files" ?
- result.acceptedInput.files.map(file => file.name) :
- result.acceptedInput.files.map(file => file.path.split(/[\\/]/).pop() || "");
- const nameCounts = new Map<string, number>();
- names.forEach(name => nameCounts.set(name, (nameCounts.get(name) || 0) + 1));
- names.forEach((name, index) => {
- if (nameCounts.get(name) === 1 && Object.prototype.hasOwnProperty.call(result.succMap, name)) {
- succeeded.set(index, result.succMap[name]);
+ getAssetUploadSuccesses({succMap: result.succMap}, result.acceptedInput).forEach(item => {
+ if (item.index >= 0) {
+ succeeded.set(item.index, item.path);
}
});
}
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index b4e40b2202..b1843465d8 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -3499,6 +3499,8 @@ export interface APIPOSTRoutes {
};
/**
* 按需生成 H.264/AAC 兼容预览,不修改原件,不同步缓存;输入限制为 64 MiB、十秒、600 帧和 4096×2160 像素。
+ * path 为相对资源 URL,路径中的百分号编码只解码一次,box 等查询参数保持原有资源解析语义。
+ * 普通资源的真实路径必须位于对应 assets 目录内;加密资源沿用 box 边界校验与解锁租约。
* 支持 SDR HEVC 4:2:0(8/10 位)或 AVC,以及 AAC 或 44.1/48 kHz 单声道、双声道整数 PCM。
* clientEncode 为 true 且 encode 为 true 时,调用方顺序读取帧并提交 Annex B 编码;否则直接读取预览。
* 客户端编码只对管理员开放,其他角色由内核生成预览,不能提交共享缓存内容。
diff --git a/kernel/api/contract_live_photo_test.go b/kernel/api/contract_live_photo_test.go
index e440711081..7f716872dc 100644
--- a/kernel/api/contract_live_photo_test.go
+++ b/kernel/api/contract_live_photo_test.go
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"net/http/httptest"
+ "net/url"
"os"
"path/filepath"
"testing"
@@ -14,8 +15,58 @@ import (
"github.com/siyuan-note/siyuan/kernel/internal/testutil"
"github.com/siyuan-note/siyuan/kernel/livephoto"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
+func TestAPIContractLivePhotoPreviewSymlinkBoundary(t *testing.T) {
+ assets := setupAssetContractWorkspace(t)
+ model.Conf.FileTree.Store(conf.NewFileTree())
+ const boxID = "20261019000000-box0002"
+ box := &model.Box{ID: boxID}
+ if err := box.SaveConf(conf.NewBoxConf()); err != nil {
+ t.Fatal(err)
+ }
+ outside := filepath.Join(t.TempDir(), "source.mov")
+ if err := os.WriteFile(outside, testutil.LivePhotoMOV(t), 0600); err != nil {
+ t.Fatal(err)
+ }
+ documentAssets := filepath.Join(util.DataDir, boxID, "20261019000001-doc0002", "assets")
+ if err := os.MkdirAll(documentAssets, 0755); err != nil {
+ t.Fatal(err)
+ }
+ for _, dir := range []string{assets, documentAssets} {
+ if err := os.Symlink(outside, filepath.Join(dir, "outside.mov")); err != nil {
+ t.Skipf("symlink unavailable: %v", err)
+ }
+ }
+ engine := gin.New()
+ engine.Use(boxLeaseMiddleware, func(c *gin.Context) { c.Set(model.RoleContextKey, model.RoleAdministrator) })
+ engine.POST("/api/asset/startLivePhotoPreview", startLivePhotoPreview)
+ for _, location := range []string{"global", "document"} {
+ t.Run(location, func(t *testing.T) {
+ if location == "document" {
+ if err := os.Remove(filepath.Join(assets, "outside.mov")); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := model.GetAssetAbsPathInBox("assets/outside.mov", ""); err != nil {
+ t.Fatalf("fixture is not discoverable: %v", err)
+ }
+ }
+ data, _ := json.Marshal(apicontract.StartLivePhotoPreviewRequest{Path: "assets/outside.mov"})
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", "/api/asset/startLivePhotoPreview", bytes.NewReader(data)))
+ requireAPIContract(t, "POST", "/api/asset/startLivePhotoPreview", recorder)
+ var response struct {
+ Code int
+ Data apicontract.LivePhotoPreviewSession
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Code >= 0 || response.Data.ID != "" {
+ t.Fatalf("outside asset admitted: %s, %v", recorder.Body, err)
+ }
+ })
+ }
+}
+
func TestAPIContractLivePhotoPreview(t *testing.T) {
assets := setupAssetContractWorkspace(t)
model.Conf.FileTree.Store(conf.NewFileTree())
@@ -70,7 +121,57 @@ func TestAPIContractLivePhotoPreview(t *testing.T) {
if err != nil || !bytes.Equal(after, source) {
t.Fatal("source movie changed")
}
- for _, invalid := range []string{"../live.mov", "https://example.com/live.mov", "assets/missing.mov"} {
+ for _, filename := range []string{"夏天.mov", "Summer day.mov", "plus+clip.mov", "literal%20.mov", "hash#clip.mov"} {
+ for _, boxID := range []string{"", "20261019000000-box0001"} {
+ t.Run(filename+"/"+boxID, func(t *testing.T) {
+ assetDir := assets
+ if boxID != "" {
+ assetDir = filepath.Join(util.DataDir, boxID, "assets")
+ box := &model.Box{ID: boxID}
+ if err := box.SaveConf(conf.NewBoxConf()); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := os.MkdirAll(assetDir, 0700); err != nil {
+ t.Fatal(err)
+ }
+ assetPath := filepath.Join(assetDir, filename)
+ if err := os.WriteFile(assetPath, source, 0600); err != nil {
+ t.Fatal(err)
+ }
+ requestPath := "assets/" + url.PathEscape(filename)
+ if boxID != "" {
+ requestPath += "?box=" + boxID + "&note=Summer+day%26box%3Dother"
+ }
+ started := call("startLivePhotoPreview", apicontract.StartLivePhotoPreviewRequest{Path: requestPath})
+ var response struct {
+ Code int
+ Data apicontract.LivePhotoPreviewSession
+ }
+ if err := json.Unmarshal(started.Body.Bytes(), &response); err != nil || response.Code != 0 || response.Data.ID == "" {
+ t.Fatalf("encoded asset: %s, %v", started.Body, err)
+ }
+ origin, err := model.LivePhotoPreviews.Origin(response.Data.ID)
+ if err != nil || origin != assetPath {
+ t.Fatalf("resolved asset: %s, want %s, %v", origin, assetPath, err)
+ }
+ preview := call("getLivePhotoPreview", apicontract.LivePhotoPreviewRequest{ID: response.Data.ID})
+ if preview.Code != 200 {
+ t.Fatalf("encoded asset preview: %s", preview.Body)
+ }
+ if _, err := livephoto.Open(preview.Body.Bytes()); err != nil {
+ t.Fatal(err)
+ }
+ call("cancelLivePhotoPreview", apicontract.LivePhotoPreviewRequest{ID: response.Data.ID})
+ after, err := os.ReadFile(assetPath)
+ if err != nil || !bytes.Equal(after, source) {
+ t.Fatal("renamed original movie changed")
+ }
+ })
+ }
+ }
+ for _, invalid := range []string{"../live.mov", "https://example.com/live.mov", "assets/missing.mov",
+ "assets/%GG.mov", "assets/%2e%2e/private.mov", "assets/live.mov?box=%GG", "assets/live.mov?box=invalid"} {
result := call("startLivePhotoPreview", apicontract.StartLivePhotoPreviewRequest{Path: invalid})
var failure struct{ Code int }
if err := json.Unmarshal(result.Body.Bytes(), &failure); err != nil || failure.Code == 0 {
diff --git a/kernel/api/live_photo.go b/kernel/api/live_photo.go
index b2f435ffe9..184a09a203 100644
--- a/kernel/api/live_photo.go
+++ b/kernel/api/live_photo.go
@@ -8,6 +8,8 @@ import (
"errors"
"io"
"net/http"
+ "net/url"
+ "strings"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/apicontract"
@@ -33,7 +35,20 @@ func checkLivePhotoSession(c *gin.Context, id string) error {
}
var startLivePhotoPreview = contractHandler(apicontract.StartLivePhotoPreview, func(c *gin.Context, request apicontract.StartLivePhotoPreviewRequest) apicontract.Response[apicontract.LivePhotoPreviewSession] {
- absPath, err := model.GetAssetAbsPathInBox(request.Path, "")
+ assetPath, rawQuery, hasQuery := strings.Cut(request.Path, "?")
+ assetPath, err := url.PathUnescape(assetPath)
+ if err != nil || strings.Contains(assetPath, "?") {
+ return apicontract.Failure[apicontract.LivePhotoPreviewSession](-1, "invalid Live Photo asset path")
+ }
+ if _, err = url.ParseQuery(rawQuery); err != nil {
+ return apicontract.Failure[apicontract.LivePhotoPreviewSession](-1, "invalid Live Photo asset query")
+ }
+ // 路径只按 URL 解码一次,查询参数交给资源解析器,权限校验和读取使用同一个地址。
+ request.Path = assetPath
+ if hasQuery {
+ request.Path += "?" + rawQuery
+ }
+ absPath, err := model.ResolveLivePhotoAssetPath(request.Path)
if err != nil {
return apicontract.Failure[apicontract.LivePhotoPreviewSession](-1, "Live Photo asset not found")
}
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index 23c061f150..2f2d64ded6 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -1084,6 +1084,8 @@ var GetDocImageAssets = define[AssetDocumentRequest, []string]("getDocImageAsset
var GetDocPreviewImages = define[AssetDocumentRequest, []DocumentPreviewImage]("getDocPreviewImages", "/api/asset/getDocPreviewImages", AuthenticatedAccess, JSONBody, ResponseOptions{}, "POST")
// 按需生成 H.264/AAC 兼容预览,不修改原件,不同步缓存;输入限制为 64 MiB、十秒、600 帧和 4096×2160 像素。
+// path 为相对资源 URL,路径中的百分号编码只解码一次,box 等查询参数保持原有资源解析语义。
+// 普通资源的真实路径必须位于对应 assets 目录内;加密资源沿用 box 边界校验与解锁租约。
// 支持 SDR HEVC 4:2:0(8/10 位)或 AVC,以及 AAC 或 44.1/48 kHz 单声道、双声道整数 PCM。
// clientEncode 为 true 且 encode 为 true 时,调用方顺序读取帧并提交 Annex B 编码;否则直接读取预览。
// 客户端编码只对管理员开放,其他角色由内核生成预览,不能提交共享缓存内容。
diff --git a/kernel/model/live_photo.go b/kernel/model/live_photo.go
index 6de4a08b15..c5971abf23 100644
--- a/kernel/model/live_photo.go
+++ b/kernel/model/live_photo.go
@@ -19,9 +19,28 @@ var LivePhotoPreviews = livephoto.NewManager(func() string {
return filepath.Join(util.TempDir, "assets-cache", "livephoto")
})
+// ResolveLivePhotoAssetPath 沿用普通资源的真实路径校验,加密资源由带 box 的解析器验证边界。
+func ResolveLivePhotoAssetPath(assetPath string) (string, error) {
+ absPath, err := GetAssetAbsPathInBox(assetPath, "")
+ if err != nil {
+ return "", err
+ }
+ if !IsEncryptedAssetPath(absPath) {
+ relativePath, relErr := filepath.Rel(util.DataDir, absPath)
+ if relErr != nil {
+ return "", relErr
+ }
+ _, validatedPath, resolveErr := ResolveDataAssetPath(filepath.ToSlash(relativePath))
+ if resolveErr != nil || filepath.Clean(validatedPath) != filepath.Clean(absPath) {
+ return "", errors.New("Live Photo asset resolves outside assets directory")
+ }
+ }
+ return absPath, nil
+}
+
// StartLivePhotoPreview 为在途任务取得独立租约,笔记本锁定先取消任务再排空租约。
func StartLivePhotoPreview(assetPath string, clientEncode bool) (livephoto.Session, error) {
- absPath, err := GetAssetAbsPathInBox(assetPath, "")
+ absPath, err := ResolveLivePhotoAssetPath(assetPath)
if err != nil {
return livephoto.Session{}, errors.New("Live Photo asset not found")
}
diff --git a/kernel/model/live_photo_preview_test.go b/kernel/model/live_photo_preview_test.go
index 04a5a729f4..ad1befab5e 100644
--- a/kernel/model/live_photo_preview_test.go
+++ b/kernel/model/live_photo_preview_test.go
@@ -13,6 +13,54 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func TestLivePhotoAssetPathBoundaries(t *testing.T) {
+ for _, location := range []string{"global", "notebook", "document"} {
+ t.Run(location, func(t *testing.T) {
+ boxID, _ := setupUnusedAssetWorkspace(t)
+ assets := filepath.Join(util.DataDir, "assets")
+ if location == "notebook" {
+ assets = filepath.Join(util.DataDir, boxID, "assets")
+ } else if location == "document" {
+ assets = filepath.Join(util.DataDir, boxID, "20261019000001-doc0001", "assets")
+ }
+ if err := os.MkdirAll(assets, 0755); err != nil {
+ t.Fatal(err)
+ }
+ regular := filepath.Join(assets, "live.mov")
+ if err := os.WriteFile(regular, testutil.LivePhotoMOV(t), 0600); err != nil {
+ t.Fatal(err)
+ }
+ got, err := ResolveLivePhotoAssetPath("assets/live.mov")
+ if err != nil || filepath.Clean(got) != regular {
+ t.Fatalf("regular asset: %s, %v", got, err)
+ }
+ t.Run("symlink", func(t *testing.T) {
+ inside := filepath.Join(assets, "inside.mov")
+ if err := os.Symlink(regular, inside); err != nil {
+ t.Skipf("symlink unavailable: %v", err)
+ }
+ if got, err := ResolveLivePhotoAssetPath("assets/inside.mov"); err != nil || filepath.Clean(got) != inside {
+ t.Fatalf("inside symlink: %s, %v", got, err)
+ }
+ outside := filepath.Join(t.TempDir(), "outside.mov")
+ if err := os.WriteFile(outside, testutil.LivePhotoMOV(t), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink(outside, filepath.Join(assets, "outside.mov")); err != nil {
+ t.Fatal(err)
+ }
+ if got, err := ResolveLivePhotoAssetPath("assets/outside.mov"); err == nil {
+ t.Fatalf("outside symlink resolved: %s", got)
+ }
+ if session, err := StartLivePhotoPreview("assets/outside.mov", false); err == nil {
+ LivePhotoPreviews.Cancel(session.ID)
+ t.Fatal("outside symlink admitted preview")
+ }
+ })
+ })
+ }
+}
+
func TestEncryptedLivePhotoPreviewCompatibilityAndLock(t *testing.T) {
for _, legacy := range []bool{false, true} {
t.Run(map[bool]string{false: "current", true: "legacy"}[legacy], func(t *testing.T) {