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: Preserve custom Chromium feature switches
Danielc612302fb674
diff --git a/app/electron/chromiumSwitches.js b/app/electron/chromiumSwitches.jsnew file mode 100644index 0000000000..414333c5b3--- /dev/null+++ b/app/electron/chromiumSwitches.js@@ -0,0 +1,21 @@+// 功能列表与内置选项合并,其他参数只在第一个等号处分隔名称和值。+const appendChromiumSwitch = (commandLine, arg) => {+ if (!arg.startsWith("--") || arg === "--") {+ return false;+ }+ const separator = arg.indexOf("=");+ const name = arg.slice(2, separator < 0 ? undefined : separator);+ let value = separator < 0 ? undefined : arg.slice(separator + 1);+ if (name === "enable-features" || name === "disable-features") {+ value = [...new Set([commandLine.getSwitchValue(name), value || ""]+ .flatMap(features => features.split(",").map(feature => feature.trim()).filter(Boolean)))].join(",");+ }+ if (value === undefined) {+ commandLine.appendSwitch(name);+ } else {+ commandLine.appendSwitch(name, value);+ }+ return true;+};++module.exports = {appendChromiumSwitch};diff --git a/app/electron/chromiumSwitches.test.js b/app/electron/chromiumSwitches.test.jsnew file mode 100644index 0000000000..9cfb0751f3--- /dev/null+++ b/app/electron/chromiumSwitches.test.js@@ -0,0 +1,129 @@+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 {appendChromiumSwitch} = require("./chromiumSwitches");+const {getUnsafeRemoteChromiumSwitchName, unsafeRemoteChromiumSwitchNames} = require("./remoteKernel");++const createCommandLine = (args = []) => {+ const values = new Map();+ const appendedSwitches = [];+ const commandLine = {+ appendedSwitches,+ appendSwitch: (name, value = "") => {+ appendedSwitches.push(name);+ values.set(name, value);+ },+ getSwitchValue: name => values.get(name) || "",+ hasSwitch: name => values.has(name),+ removeSwitch: name => values.delete(name),+ };+ for (const arg of args) {+ if (!arg.startsWith("--")) continue;+ const separator = arg.indexOf("=");+ commandLine.appendSwitch(arg.slice(2, separator < 0 ? undefined : separator),+ separator < 0 ? "" : arg.slice(separator + 1));+ }+ appendedSwitches.length = 0;+ return commandLine;+};++const source = readFileSync(path.join(__dirname, "main.js"), "utf8");+const start = source.indexOf('app.commandLine.appendSwitch("auto-detect"');+const end = source.indexOf("// 桌面端辅助功能配置", start);+const securityStart = source.indexOf("if (remoteKernelTarget) {");+const securityEnd = source.indexOf("const initialSiYuanOpenURL", securityStart);+assert.ok(start >= 0 && end > start && securityStart >= 0 && securityEnd > securityStart);++const startup = (args = [], {packaged = true, remote = false} = {}) => {+ const commandLine = createCommandLine(args);+ const context = vm.createContext({+ app: {commandLine, isPackaged: packaged},+ process: {argv: packaged ? ["SiYuan.exe", ...args] : ["electron.exe", "main.js", ...args]},+ appendChromiumSwitch,+ remoteKernelTarget: remote ? {mode: "remote"} : undefined,+ remoteKernelArgError: undefined,+ getUnsafeRemoteChromiumSwitchName,+ unsafeRemoteChromiumSwitchNames,+ openAsHidden: false,+ writeLog: () => {},+ });+ vm.runInContext(source.slice(securityStart, securityEnd), context);+ vm.runInContext(source.slice(start, end), context);+ return {commandLine, context};+};++test("default startup retains HTTP/2 and certificate verification caching", () => {+ const {commandLine} = startup();+ assert.equal(commandLine.hasSwitch("disable-http2"), false);+ assert.equal(commandLine.getSwitchValue("disable-features"), "AutoupgradeMixedContent");+ assert.equal(commandLine.getSwitchValue("enable-features"),+ "PlatformHEVCDecoderSupport,OriginKeyedProcessesByDefault");+});++test("explicit compatibility flags survive packaged and development startup", () => {+ for (const packaged of [true, false]) {+ const {commandLine} = startup(["--disable-http2", "--disable-features=CacheCertVerification"], {packaged});+ assert.equal(commandLine.hasSwitch("disable-http2"), true);+ assert.deepEqual(new Set(commandLine.getSwitchValue("disable-features").split(",")),+ new Set(["AutoupgradeMixedContent", "CacheCertVerification"]));+ assert.equal(commandLine.hasSwitch("ignore-certificate-errors"), false);+ assert.equal(commandLine.hasSwitch("allow-insecure-localhost"), false);+ }+});++test("feature lists merge multiple arguments and retain trial parameters without duplicates", () => {+ const {commandLine} = startup([+ "--disable-features=CacheCertVerification, AutoupgradeMixedContent,",+ "--disable-features=CalculateNativeWinOcclusion,CacheCertVerification",+ "--enable-features=ExampleFeature<ExampleTrial:param/value,OriginKeyedProcessesByDefault",+ "--enable-features=AnotherFeature",+ ]);+ const disabled = commandLine.getSwitchValue("disable-features").split(",");+ assert.equal(disabled.length, 3);+ assert.deepEqual(new Set(disabled),+ new Set(["AutoupgradeMixedContent", "CacheCertVerification", "CalculateNativeWinOcclusion"]));+ const enabled = commandLine.getSwitchValue("enable-features").split(",");+ assert.equal(enabled.length, 4);+ assert.ok(enabled.includes("ExampleFeature<ExampleTrial:param/value"));+ assert.ok(enabled.includes("PlatformHEVCDecoderSupport"));+});++test("switch values retain spaces and additional equals signs", () => {+ const commandLine = createCommandLine();+ appendChromiumSwitch(commandLine, "--proxy-server=https=127.0.0.1:8080;http=127.0.0.1:8081");+ appendChromiumSwitch(commandLine, "--log-file=C:/logs with spaces/network.log");+ appendChromiumSwitch(commandLine, "--empty-value=");+ appendChromiumSwitch(commandLine, "--disable-http2");+ assert.equal(commandLine.getSwitchValue("proxy-server"), "https=127.0.0.1:8080;http=127.0.0.1:8081");+ assert.equal(commandLine.getSwitchValue("log-file"), "C:/logs with spaces/network.log");+ assert.equal(commandLine.hasSwitch("empty-value"), true);+ assert.equal(commandLine.hasSwitch("disable-http2"), true);+ assert.equal(appendChromiumSwitch(commandLine, "--"), false);+ assert.equal(appendChromiumSwitch(commandLine, "C:/notes/file"), false);+});++test("application flags are not forwarded as Chromium switches", () => {+ const {commandLine, context} = startup(["--workspace=C:/notes", "--port=6806", "--openAsHidden"]);+ assert.equal(commandLine.appendedSwitches.includes("workspace"), false);+ assert.equal(commandLine.appendedSwitches.includes("port"), false);+ assert.equal(commandLine.appendedSwitches.includes("openAsHidden"), false);+ assert.equal(context.openAsHidden, true);+});++test("remote startup keeps unsafe switches blocked before feature lists are merged", () => {+ for (const unsafe of ["--ignore-certificate-errors", "--disable-web-security",+ "--disable-features=IsolateOrigins,CacheCertVerification",+ "--disable-features=IsolateOrigins<ExampleTrial,CacheCertVerification"]) {+ const {commandLine, context} = startup([unsafe, "--disable-http2"], {remote: true});+ assert.equal(context.remoteKernelArgError.code, "ERR_REMOTE_UNSAFE_CHROMIUM_SWITCH");+ assert.equal(commandLine.hasSwitch("ignore-certificate-errors"), false);+ assert.equal(commandLine.hasSwitch("disable-web-security"), false);+ assert.equal(commandLine.getSwitchValue("disable-features"), "AutoupgradeMixedContent");+ }+ const {commandLine, context} = startup(["--disable-http2", "--disable-features=CacheCertVerification"], {remote: true});+ assert.equal(context.remoteKernelArgError, undefined);+ assert.equal(commandLine.hasSwitch("ignore-certificate-errors"), false);+ assert.equal(commandLine.hasSwitch("no-proxy-server"), false);+});diff --git a/app/electron/main.js b/app/electron/main.jsindex 62b36a2b89..1b631d761f 100644--- a/app/electron/main.js+++ b/app/electron/main.js@@ -37,6 +37,7 @@ const {powerMonitor} = require("electron");const {updateGlobalShortcutBindings} = require("./globalShortcutBindings");+const {appendChromiumSwitch} = require("./chromiumSwitches");const path = require("path");const fs = require("fs");const {pathToFileURL} = require("url");@@ -758,10 +759,10 @@ if (!remoteKernelTarget) {}// 同源窗口共享渲染进程,按来源隔离以保留不同端口工作空间之间的进程隔离。app.commandLine.appendSwitch("process-per-site");-app.commandLine.appendSwitch("enable-features", "PlatformHEVCDecoderSupport,OriginKeyedProcessesByDefault");+appendChromiumSwitch(app.commandLine, "--enable-features=PlatformHEVCDecoderSupport,OriginKeyedProcessesByDefault");app.commandLine.appendSwitch("xdg-portal-required-version", "4");// 本地 HTTPS 页面加载 HTTP 外链图时,禁止自动升级为 HTTPS-app.commandLine.appendSwitch("disable-features", "AutoupgradeMixedContent");+appendChromiumSwitch(app.commandLine, "--disable-features=AutoupgradeMixedContent");// Support set Chromium command line arguments on the desktop https://github.com/siyuan-note/siyuan/issues/9696writeLog("app is packaged [" + app.isPackaged + "], command line args [" + process.argv.join(", ") + "]");@@ -789,8 +790,9 @@ for (let i = argStart; i < process.argv.length; i++) {continue;}- app.commandLine.appendSwitch(arg);- writeLog("command line switch [" + arg + "]");+ if (appendChromiumSwitch(app.commandLine, arg)) {+ writeLog("command line switch [" + arg + "]");+ }}// 桌面端辅助功能配置在创建窗口前应用,对本机所有工作空间生效。diff --git a/app/electron/remoteKernel.js b/app/electron/remoteKernel.jsindex c750e84523..4429620d7e 100644--- a/app/electron/remoteKernel.js+++ b/app/electron/remoteKernel.js@@ -82,8 +82,9 @@ const getUnsafeRemoteChromiumSwitchName = (arg) => {const name = arg.slice(2).split("=", 1)[0].toLowerCase();if (name === "disable-features") {const disabledFeatures = arg.slice(arg.indexOf("=") + 1).toLowerCase().split(",");+ // 去除 Chromium 功能名后的试验名称,防止绕过远程模式的安全限制。if (disabledFeatures.some((feature) => ["isolateorigins", "site-per-process", "siteperprocess"]- .includes(feature.trim()))) {+ .includes(feature.split("<", 1)[0].trim()))) {return name;}}diff --git a/app/electron/remoteKernel.test.js b/app/electron/remoteKernel.test.jsindex 4828489316..78420448c0 100644--- a/app/electron/remoteKernel.test.js+++ b/app/electron/remoteKernel.test.js@@ -392,6 +392,14 @@ test("remote mode recognizes Chromium switches that weaken renderer security", (assert.equal(getUnsafeRemoteChromiumSwitchName("--disable-gpu"), undefined);});+test("remote mode recognizes isolation features with field trial suffixes", () => {+ for (const feature of ["IsolateOrigins", "site-per-process", "SitePerProcess"]) {+ assert.equal(getUnsafeRemoteChromiumSwitchName(+ `--disable-features=CacheCertVerification, ${feature}<ExampleTrial`), "disable-features");+ }+ assert.equal(getUnsafeRemoteChromiumSwitchName("--disable-features=CacheCertVerification<ExampleTrial"), undefined);+});+test("remote mode only opens web URLs through the operating system", () => {assert.equal(isAllowedRemoteExternalURL("https://example.com/path"), true);assert.equal(isAllowedRemoteExternalURL("http://example.com/path"), true);diff --git a/app/electron/rendererProcess.test.js b/app/electron/rendererProcess.test.jsindex e882f18c81..1e4ab36fcc 100644--- a/app/electron/rendererProcess.test.js+++ b/app/electron/rendererProcess.test.js@@ -29,7 +29,9 @@ if (!process.versions.electron) {const start = source.indexOf('app.commandLine.appendSwitch("auto-detect"');const end = source.indexOf("// Support set Chromium command line arguments", start);assert.ok(start >= 0 && end > start);- vm.runInNewContext(source.slice(start, end), {app, remoteKernelTarget: false});+ vm.runInNewContext(source.slice(start, end), {+ app, remoteKernelTarget: false, ...require("./chromiumSwitches"),+ });app.setPath("userData", process.argv[2]);app.disableHardwareAcceleration();