An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Validate the check-auth redirect target against the kernel origin https://github.com/siyuan-note/siyuan/security/advisories/GHSA-fmj3-xc2f-6w4w
Daniel2f246d5f3d10
diff --git a/app/stage/auth.html b/app/stage/auth.htmlindex 8868df6998..9623e97fa2 100644--- a/app/stage/auth.html+++ b/app/stage/auth.html@@ -470,7 +470,20 @@}const url = new URL(window.location)- toPath = url.searchParams.get("to") || "/"+ // 仅允许跳转到同源地址,避免 javascript: 等伪协议在授权页执行+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-fmj3-xc2f-6w4w+ const safeRedirectTarget = (target) => {+ try {+ const resolved = new URL(target, window.location.origin);+ if (resolved.origin !== window.location.origin) {+ return "/";+ }+ return resolved.pathname + resolved.search + resolved.hash;+ } catch (e) {+ return "/";+ }+ };+ toPath = safeRedirectTarget(url.searchParams.get("to") || "/");const exitSiYuan = () => {if (toPath.indexOf("/stage/build/app/window.html") > -1) {diff --git a/app/tests/authRedirectTarget.test.js b/app/tests/authRedirectTarget.test.jsnew file mode 100644index 0000000000..ca5524bb84--- /dev/null+++ b/app/tests/authRedirectTarget.test.js@@ -0,0 +1,63 @@+const assert = require("node:assert/strict");+const {readFileSync} = require("node:fs");+const path = require("node:path");+const {test} = require("node:test");+const vm = require("node:vm");++const authPageSource = readFileSync(path.join(__dirname, "../stage/auth.html"), "utf8");++// 提取授权页实际发布的跳转校验函数,保证测试与线上代码一致+const extractSafeRedirectTarget = () => {+ const matched = /const safeRedirectTarget = \(target\) => \{[\s\S]*?\n {4}\};/.exec(authPageSource);+ assert.ok(matched, "auth.html 必须定义 safeRedirectTarget 跳转校验函数");+ return matched[0];+};++// 复现授权页的 toPath 求值:to 参数缺失或为空时回退首页,否则必须通过同源校验+const createToPathResolver = (origin) => {+ const context = vm.createContext({URL, window: {location: {origin}}});+ vm.runInContext(`${extractSafeRedirectTarget()}+this.resolveToPath = (to) => safeRedirectTarget(to || "/");`, context);+ return (to) => context.resolveToPath(to);+};++test("auth page rejects redirect targets outside the kernel origin", () => {+ const resolveToPath = createToPathResolver("http://127.0.0.1:6806");+ for (const target of [+ 'javascript:document.title="poc";void 0',+ "javascript:alert(1)",+ "JavaScript:alert(1)",+ "data:text/html,<script>alert(1)</script>",+ "vbscript:msgbox(1)",+ "//evil.example/stage/build/desktop/",+ "https://evil.example/stage/build/desktop/",+ "http://evil.example/stage/build/desktop/",+ "/\\evil.example",+ "\\\\evil.example",+ "https://127.0.0.1:6806/stage/build/desktop/",+ ]) {+ assert.equal(resolveToPath(target), "/", `跳转目标必须回退到首页:${target}`);+ }+});++test("auth page keeps same-origin redirect targets", () => {+ const origin = "http://127.0.0.1:6806";+ const resolveToPath = createToPathResolver(origin);+ assert.equal(resolveToPath("/stage/build/desktop/"), "/stage/build/desktop/");+ // redirectToCheckAuth 传入的是绝对同源地址,必须保留原页面而不是回退首页+ assert.equal(resolveToPath(`${origin}/stage/build/app/window.html?w=1`), "/stage/build/app/window.html?w=1");+ assert.equal(resolveToPath(`${origin}/stage/build/desktop/?id=x#y`), "/stage/build/desktop/?id=x#y");+});++test("auth page falls back to home when the to parameter is absent", () => {+ const resolveToPath = createToPathResolver("http://127.0.0.1:6806");+ assert.equal(resolveToPath(null), "/");+ assert.equal(resolveToPath(""), "/");+});++test("auth page routes the to parameter through the origin guard", () => {+ assert.ok(authPageSource.includes('toPath = safeRedirectTarget(url.searchParams.get("to") || "/")'),+ "to 参数必须经过 safeRedirectTarget 校验");+ assert.ok(!authPageSource.includes('toPath = url.searchParams.get("to")'),+ "授权页不得直接使用未校验的 to 参数");+});