Skip to content

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

:bug: Resolve Windows junctions in appearance resource checks https://github.com/siyuan-note/siyuan/issues/19456

Daniel054f8deb057d
diff --git a/app/src/assets/scss/util/_reset.scss b/app/src/assets/scss/util/_reset.scss
index 1f602d08e0..e8a6c75022 100644
--- a/app/src/assets/scss/util/_reset.scss
+++ b/app/src/assets/scss/util/_reset.scss
@@ -18,6 +18,21 @@ html {
color-scheme: light;
}
+// 语言包或主题不可用时,仍使用系统配色显示启动错误。
+.language-load-error {
+ position: fixed;
+ inset: 0;
+ z-index: 2147483647;
+ box-sizing: border-box;
+ margin: 0;
+ padding: 24px;
+ overflow: auto;
+ overflow-wrap: anywhere;
+ white-space: pre-wrap;
+ color: CanvasText;
+ background-color: Canvas;
+}
+
body {
text-rendering: optimizeLegibility;
margin: 0;
diff --git a/app/src/boot/loadLanguages.test.ts b/app/src/boot/loadLanguages.test.ts
index 633b519a31..269debc935 100644
--- a/app/src/boot/loadLanguages.test.ts
+++ b/app/src/boot/loadLanguages.test.ts
@@ -8,6 +8,7 @@ it("stops startup and shows the failed language URL when the server returns 404"
const originalConsoleError = console.error;
let displayed = "";
let role = "";
+ let className = "";
let booted = false;
globalThis.fetch = async () => new Response("", {status: 404});
console.error = () => {};
@@ -15,6 +16,7 @@ it("stops startup and shows the failed language URL when the server returns 404"
createElement: (tag: string) => {
assert.equal(tag, "pre");
return {
+ className: "",
textContent: "",
setAttribute: (name: string, value: string) => {
if (name === "role") {
@@ -24,13 +26,18 @@ it("stops startup and shows the failed language URL when the server returns 404"
};
},
body: {
- replaceChildren: (element: {textContent: string}) => displayed = element.textContent,
+ appendChild: (element: {className: string; textContent: string}) => {
+ displayed = element.textContent;
+ className = element.className;
+ },
+ replaceChildren: () => assert.fail("startup markup must remain available"),
},
} as unknown as Document;
try {
await loadLanguages("zh-CN", "3.8.6", () => booted = true);
assert.equal(booted, false);
assert.equal(role, "alert");
+ assert.equal(className, "language-load-error");
assert.match(displayed, /\/appearance\/langs\/zh-CN\.json\?v=3\.8\.6\nError: HTTP 404/);
} finally {
globalThis.fetch = originalFetch;
diff --git a/app/src/boot/loadLanguages.ts b/app/src/boot/loadLanguages.ts
index 2851ac0045..161966c47b 100644
--- a/app/src/boot/loadLanguages.ts
+++ b/app/src/boot/loadLanguages.ts
@@ -15,9 +15,10 @@ export const loadLanguages = async (lang: string, version: string, onLoaded: (la
} catch (error) {
console.error(url, error);
const element = document.createElement("pre");
+ element.className = "language-load-error";
element.setAttribute("role", "alert");
element.textContent = `${url}\n${String(error)}`;
- document.body.replaceChildren(element);
+ document.body.appendChild(element);
return;
}
onLoaded(languages);
diff --git a/kernel/server/appearance_path.go b/kernel/server/appearance_path.go
index 4a7b62d522..7c3513beec 100644
--- a/kernel/server/appearance_path.go
+++ b/kernel/server/appearance_path.go
@@ -84,7 +84,8 @@ func resolveAppearanceFilePath(root, requestPath string) (string, int) {
logging.LogWarnf("stat appearance resource [%s] failed: %s", target, statErr)
return "", http.StatusForbidden
}
- if info.Mode()&os.ModeSymlink != 0 {
+ // Windows 目录联接可能标记为 ModeIrregular,同样需要检查真实路径。
+ if info.Mode()&(os.ModeSymlink|os.ModeIrregular) != 0 {
resolvedTarget, resolveErr := evalAppearanceSymlinks(target)
if resolveErr != nil {
logging.LogWarnf("resolve appearance link [%s] failed: %s", target, resolveErr)
diff --git a/kernel/server/appearance_symlink_other.go b/kernel/server/appearance_symlink_other.go
index f8eaa99f3e..1f8445510c 100644
--- a/kernel/server/appearance_symlink_other.go
+++ b/kernel/server/appearance_symlink_other.go
@@ -1,4 +1,4 @@
-//go:build !linux
+//go:build !linux && !windows
// SiYuan - From thought to insight, with agents
// Copyright (c) 2020-present, b3log.org
diff --git a/kernel/server/appearance_symlink_windows.go b/kernel/server/appearance_symlink_windows.go
new file mode 100644
index 0000000000..46a67e044c
--- /dev/null
+++ b/kernel/server/appearance_symlink_windows.go
@@ -0,0 +1,26 @@
+//go:build windows
+
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package server
+
+import "github.com/siyuan-note/siyuan/kernel/model"
+
+func evalAppearanceSymlinks(name string) (string, error) {
+ // 通过句柄解析完整链路,兼容可打开但无法逐级解析的目录联接。
+ return model.ResolveRealPath(name)
+}
diff --git a/kernel/server/appearance_symlink_windows_test.go b/kernel/server/appearance_symlink_windows_test.go
new file mode 100644
index 0000000000..78817118d9
--- /dev/null
+++ b/kernel/server/appearance_symlink_windows_test.go
@@ -0,0 +1,81 @@
+//go:build windows
+
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package server
+
+import (
+ "net/http"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "testing"
+)
+
+func TestAppearanceDirectoryJunction(t *testing.T) {
+ physical := filepath.Join(t.TempDir(), "physical target")
+ appearance := filepath.Join(physical, "appearance")
+ if err := os.MkdirAll(filepath.Join(appearance, "langs"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(appearance, "langs", "en.json"), []byte(`{"name":"English"}`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ linked := filepath.Join(t.TempDir(), "linked path")
+ createAppearanceTestJunction(t, linked, physical)
+ linkedAppearance := filepath.Join(linked, "appearance")
+
+ resolved, err := evalAppearanceSymlinks(linkedAppearance)
+ if err != nil {
+ t.Fatal(err)
+ }
+ resolvedInfo, err := os.Stat(resolved)
+ if err != nil {
+ t.Fatal(err)
+ }
+ physicalInfo, err := os.Stat(appearance)
+ if err != nil || !os.SameFile(resolvedInfo, physicalInfo) {
+ t.Fatalf("resolved path %q does not match appearance directory: %v", resolved, err)
+ }
+
+ resource, status := resolveAppearanceFilePath(linkedAppearance, "langs/en.json")
+ if status != 0 {
+ t.Fatalf("junction resource status = %d", status)
+ }
+ if data, readErr := os.ReadFile(resource); readErr != nil || string(data) != `{"name":"English"}` {
+ t.Fatalf("junction resource = %q, %v", data, readErr)
+ }
+
+ outside := t.TempDir()
+ if err = os.WriteFile(filepath.Join(outside, "secret.json"), []byte("secret"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ createAppearanceTestJunction(t, filepath.Join(appearance, "langs", "escape"), outside)
+ if _, status = resolveAppearanceFilePath(linkedAppearance, "langs/escape/secret.json"); status != http.StatusForbidden {
+ t.Fatalf("junction escape status = %d, want %d", status, http.StatusForbidden)
+ }
+ if _, status = resolveAppearanceFilePath(filepath.Join(linked, "missing"), "langs/en.json"); status != http.StatusNotFound {
+ t.Fatalf("missing root status = %d, want %d", status, http.StatusNotFound)
+ }
+}
+
+func createAppearanceTestJunction(t *testing.T, link, target string) {
+ t.Helper()
+ if output, err := exec.Command("cmd", "/c", "mklink", "/J", link, target).CombinedOutput(); err != nil {
+ t.Fatalf("create junction %q -> %q: %v, %s", link, target, err, output)
+ }
+}