Skip to content

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

:bug: Preserve map geometry precision and explain loading failures https://github.com/siyuan-note/siyuan/issues/20129

Daniel447d641101cc
diff --git a/app/electron/mapHostDiagnostics.js b/app/electron/mapHostDiagnostics.js
index 47e575f7d6..b56c963c3f 100644
--- a/app/electron/mapHostDiagnostics.js
+++ b/app/electron/mapHostDiagnostics.js
@@ -1,24 +1,30 @@
-// 仅返回固定分类,不转发 SDK 日志、请求地址、脚本片段或凭据。
+// 仅返回固定分类或受限公共主机名,不转发 SDK 日志、完整地址、脚本片段或凭据。
+const MAX_MAP_DIAGNOSTICS = 64;
const mapDiagnosticCodes = Object.freeze([
"hostSetupFailed", "assetUnavailable", "documentLoadFailed", "bootstrapTimeout", "sdkTimeout",
"providerRequestDenied", "providerInsecureRequest", "providerHTTPFailure", "providerNetworkFailure",
"cspScript", "cspWorker", "cspConnect", "cspImage", "cspStyle", "cspEval", "cspWasm",
"storageUnavailable", "webglUnavailable", "amapInvalidKey", "amapInvalidSecurityCode",
- "amapDomainMismatch", "amapPlatformMismatch",
+ "amapDomainMismatch", "amapPlatformMismatch", "geometryInvalid", "geometryLogicalBounds",
+ "geometryCropBounds", "geometryWindowBounds", "geometryRoundedEmpty",
]);
const mapCSPDiagnosticCodes = Object.freeze(["cspScript", "cspWorker", "cspConnect", "cspImage", "cspStyle", "cspEval", "cspWasm"]);
const mapCSPResourceCodes = Object.freeze([
"blob", "data", "inline", "eval", "wasm", "other",
- "https:webapi.amap.com", "https:restapi.amap.com", "https:vdata.amap.com", "https:a.amap.com",
- "https:g.alicdn.com", "https:fourier.taobao.com", "https:autonavi-tile", "https:other",
- "http:webapi.amap.com", "http:restapi.amap.com", "http:vdata.amap.com", "http:a.amap.com",
- "http:g.alicdn.com", "http:fourier.taobao.com", "http:autonavi-tile", "http:other",
+ "owner-origin", "local-address", "redacted",
]);
-const resourceHosts = Object.freeze(["webapi.amap.com", "restapi.amap.com", "vdata.amap.com", "a.amap.com",
- "g.alicdn.com", "fourier.taobao.com"]);
+const isMapCSPHostname = value => typeof value === "string" && value.length <= 96 &&
+ /(?:^|\.)(?:amap\.com|autonavi\.com|alicdn\.com|taobao\.com)$/.test(value) &&
+ value.split(".").every(label => /^[a-z](?:[a-z0-9-]{0,22}[a-z0-9])?$/.test(label));
+const isMapCSPResource = value => {
+ if (typeof value !== "string" || value.length > 102) return false;
+ if (mapCSPResourceCodes.includes(value)) return true;
+ const hostname = /^https?:([a-z0-9.-]+)$/.exec(value)?.[1];
+ return isMapCSPHostname(hostname);
+};
-const classifyMapCSPResources = message => {
- if (typeof message !== "string" || !/content security policy/i.test(message)) return [];
+const classifyMapCSPResources = (message, ownerOrigin) => {
+ if (typeof message !== "string" || message.length > 32768 || !/content security policy/i.test(message)) return [];
// Chromium 的 eval 文案在 directive 和实际策略之间还有原因,不能按资源 URL 解析。
if (/^(?:Evaluating a string as JavaScript|Refused to evaluate a string as JavaScript)/i.test(message)) {
return [{code: "cspEval", resource: "eval"}];
@@ -41,14 +47,17 @@ const classifyMapCSPResources = message => {
try {
const url = new URL(target);
if (["https:", "http:"].includes(url.protocol)) {
- const host = url.port ? "other" : resourceHosts.includes(url.hostname) ? url.hostname :
- /^(?:webst|webrd)\d+\.is\.autonavi\.com$/.test(url.hostname) ? "autonavi-tile" : "other";
- resource = url.protocol + host;
+ const hostname = url.hostname;
+ const local = !hostname.includes(".") || hostname.startsWith("[") ||
+ /^(?:\d{1,3}\.){3}\d{1,3}$/.test(hostname) ||
+ /(?:^|\.)(?:localhost|local|internal|lan|home)$/.test(hostname);
+ resource = url.username || url.password ? "redacted" : url.origin === ownerOrigin ? "owner-origin" :
+ local ? "local-address" : isMapCSPHostname(hostname) ? url.protocol + hostname : "redacted";
}
} catch (_error) { /* 未识别资源只保留固定 other 类别。 */ }
}
}
- return mapCSPResourceCodes.includes(resource) ? [{code, resource}] : [];
+ return isMapCSPResource(resource) ? [{code, resource}] : [];
};
const classifyMapConsoleMessage = message => {
@@ -70,4 +79,5 @@ const classifyMapConsoleMessage = message => {
return ret;
};
-module.exports = {mapDiagnosticCodes, mapCSPDiagnosticCodes, mapCSPResourceCodes, classifyMapConsoleMessage, classifyMapCSPResources};
+module.exports = {MAX_MAP_DIAGNOSTICS, mapDiagnosticCodes, mapCSPDiagnosticCodes, mapCSPResourceCodes,
+ isMapCSPResource, classifyMapConsoleMessage, classifyMapCSPResources};
diff --git a/app/electron/mapHostDiagnostics.test.js b/app/electron/mapHostDiagnostics.test.js
index b819c63f39..abe0b8daeb 100644
--- a/app/electron/mapHostDiagnostics.test.js
+++ b/app/electron/mapHostDiagnostics.test.js
@@ -1,6 +1,6 @@
const assert = require("node:assert/strict");
const {test} = require("node:test");
-const {mapDiagnosticCodes, mapCSPDiagnosticCodes, mapCSPResourceCodes, classifyMapConsoleMessage,
+const {MAX_MAP_DIAGNOSTICS, mapDiagnosticCodes, mapCSPDiagnosticCodes, mapCSPResourceCodes, isMapCSPResource, classifyMapConsoleMessage,
classifyMapCSPResources} = require("./mapHostDiagnostics");
test("provider console diagnostics return fixed categories without SDK text, URL or credentials", () => {
@@ -20,17 +20,19 @@ test("provider console diagnostics return fixed categories without SDK text, URL
}
});
-test("Chromium CSP messages identify fixed resource categories without retaining their URLs", () => {
+test("Chromium CSP messages identify bounded resources and public hostnames without retaining their URLs", () => {
// Chromium 152 的三类资源拒绝文案,以及旧式 Refused to 文案。
const cases = [
["Loading the script 'https://g.alicdn.com/private.js?key=secret' violates the following Content Security Policy directive: \"script-src https://webapi.amap.com\". The action has been blocked.", "cspScript", "https:g.alicdn.com"],
["Connecting to 'https://fourier.taobao.com/rp?secret=private' violates the following Content Security Policy directive: \"connect-src https://webapi.amap.com\".", "cspConnect", "https:fourier.taobao.com"],
["Creating a worker from 'blob:null/private-token' violates the following Content Security Policy directive: \"worker-src 'none'\".", "cspWorker", "blob"],
["Refused to create a worker from 'data:text/javascript,private' because it violates the following Content Security Policy directive: \"worker-src 'none'\".", "cspWorker", "data"],
- ["Refused to load the script 'https://[email protected]/log?key=secret' because it violates the following Content Security Policy directive: \"script-src https://webapi.amap.com\".", "cspScript", "https:restapi.amap.com"],
+ ["Refused to load the script 'https://[email protected]/log?key=secret' because it violates the following Content Security Policy directive: \"script-src https://webapi.amap.com\".", "cspScript", "redacted"],
["Refused to connect to 'http://vdata.amap.com/private' because it violates the following Content Security Policy directive: \"connect-src https://vdata.amap.com\".", "cspConnect", "http:vdata.amap.com"],
- ["Connecting to 'https://webst02.is.autonavi.com/private' violates the following Content Security Policy directive: \"connect-src https://vdata.amap.com\".", "cspConnect", "https:autonavi-tile"],
- ["Loading the image 'http://webrd01.is.autonavi.com/private' violates the following Content Security Policy directive: \"img-src https://webapi.amap.com\".", "cspImage", "http:autonavi-tile"],
+ ["Connecting to 'https://webst02.is.autonavi.com/private' violates the following Content Security Policy directive: \"connect-src https://vdata.amap.com\".", "cspConnect", "https:webst02.is.autonavi.com"],
+ ["Loading the image 'http://webrd01.is.autonavi.com/private' violates the following Content Security Policy directive: \"img-src https://webapi.amap.com\".", "cspImage", "http:webrd01.is.autonavi.com"],
+ ["Connecting to 'https://wprd01.is.autonavi.com/private?key=secret' violates the following Content Security Policy directive: \"connect-src https://vdata.amap.com\".", "cspConnect", "https:wprd01.is.autonavi.com"],
+ ["Loading the script 'https://jsapi0.amap.com/private?key=secret' violates the following Content Security Policy directive: \"script-src https://webapi.amap.com\".", "cspScript", "https:jsapi0.amap.com"],
["Executing inline script violates the following Content Security Policy directive 'script-src https://webapi.amap.com'. Either the 'unsafe-inline' keyword, a hash, or a nonce is required.", "cspScript", "inline"],
["Evaluating a string as JavaScript violates the following Content Security Policy directive because 'unsafe-eval' is not an allowed source of script: \"script-src https://webapi.amap.com\".", "cspEval", "eval"],
["Compiling or instantiating a WebAssembly module violates the following Content Security policy directive because 'unsafe-eval' is not an allowed source of script: \"script-src https://webapi.amap.com\".", "cspWasm", "wasm"],
@@ -39,21 +41,68 @@ test("Chromium CSP messages identify fixed resource categories without retaining
for (const [message, code, resource] of cases) {
const result = classifyMapCSPResources(message);
assert.deepEqual(result, [{code, resource}]);
- assert.ok(result.every(item => mapCSPDiagnosticCodes.includes(item.code) && mapCSPResourceCodes.includes(item.resource)));
+ assert.ok(result.every(item => mapCSPDiagnosticCodes.includes(item.code) && isMapCSPResource(item.resource)));
assert.doesNotMatch(JSON.stringify(result), /secret|private|token|\/\/|\?/);
}
});
test("unknown or malformed CSP resources stay bounded and URL contents cannot become diagnostic data", () => {
- for (const [url, resource] of [["https://webapi.amap.com.evil.invalid/path?key=secret", "https:other"],
- ["https://webapi.amap.com:8443/private", "https:other"],
- ["http://unknown.invalid/unsafe-eval?blob:secret", "http:other"], ["file:///secret", "other"],
+ for (const [url, resource] of [["https://webapi.amap.com.evil.invalid/path?key=secret", "redacted"],
+ ["https://webapi.amap.com:8443/private", "https:webapi.amap.com"],
+ ["http://unknown.invalid/unsafe-eval?blob:secret", "redacted"], ["file:///secret", "other"],
["not a URL private", "other"], ["blob:https://private.invalid/secret", "blob"]]) {
const result = classifyMapCSPResources(`Loading the script '${url}' violates the following Content Security Policy directive: "script-src https://webapi.amap.com".`);
assert.deepEqual(result, [{code: "cspScript", resource}]);
}
for (const message of [undefined, {}, "provider secret text", "Content Security Policy unknown directive private",
- "Loading the script 'https://private.invalid' violates a made-up directive"]) {
+ "Loading the script 'https://private.invalid' violates a made-up directive",
+ "Loading the script 'https://webapi.amap.com/" + "private".repeat(5000) + "' violates Content Security Policy directive: \"script-src 'none'\"."]) {
assert.deepEqual(classifyMapCSPResources(message), []);
}
});
+
+test("owner and other local addresses become fixed labels without disclosing names, ports, or credentials", () => {
+ const origin = "https://127.0.0.1:6806";
+ for (const [target, resource] of [
+ [origin + "/private?key=secret", "owner-origin"],
+ ["https://127.0.0.1:6807/private", "local-address"],
+ ["http://127.0.0.1:6806/private", "local-address"],
+ ["https://localhost:6806/private", "local-address"],
+ ["https://machine.local/private", "local-address"],
+ ["https://machine.internal/private", "local-address"],
+ ["https://intranet/private", "local-address"],
+ ["https://[::1]:6806/private", "local-address"],
+ ["https://192.0.2.1/private", "local-address"],
+ ["https://name:[email protected]:6806/private", "redacted"],
+ ]) {
+ const result = classifyMapCSPResources(`Connecting to '${target}' violates the following Content Security Policy directive: "connect-src 'none'".`, origin);
+ assert.deepEqual(result, [{code: "cspConnect", resource}]);
+ assert.doesNotMatch(JSON.stringify(result), /127|6806|6807|machine|private|secret|localhost|intranet|192/);
+ }
+ const result = classifyMapCSPResources("Connecting to 'https://notes.example:7443/private' violates the following Content Security Policy directive: \"connect-src 'none'\".", "https://notes.example:7443");
+ assert.deepEqual(result, [{code: "cspConnect", resource: "owner-origin"}]);
+});
+
+test("resource validator accepts only fixed codes or short canonical hostnames in bounded public domain families", () => {
+ for (const resource of [...mapCSPResourceCodes, "https:webapi.amap.com", "https:wprd01.is.autonavi.com",
+ "http:g.alicdn.com", "https:fourier.taobao.com", "https:jsapi0.amap.com"]) {
+ assert.equal(isMapCSPResource(resource), true, resource);
+ }
+ for (const resource of [undefined, null, {}, ["blob"], "https://webapi.amap.com", "https:webapi.amap.com:8443",
+ "https:webapi.amap.com/private", "https:webapi.amap.com?key=secret", "https:webapi.amap.com#secret",
+ "https:[email protected]", "https:webapi.amap.com.evil.invalid", "https:notamap.com",
+ "https:unknown.example", "https:127.0.0.1", "https:[::1]", "https:machine.local", "https:localhost",
+ "HTTPS:webapi.amap.com", "https:WEBAPI.amap.com", "https:1234567890.amap.com", "https:bad-.amap.com",
+ "https:" + "a".repeat(25) + ".amap.com", "https:" + ("a".repeat(24) + ".").repeat(4) + "amap.com"]) {
+ assert.equal(isMapCSPResource(resource), false, String(resource));
+ }
+ assert.equal(MAX_MAP_DIAGNOSTICS, 64);
+});
+
+test("suspicious public host labels are redacted while the effective blocked directive survives", () => {
+ for (const host of ["unknown.example", "amap.com.evil.example", "a".repeat(25) + ".amap.com",
+ ("a".repeat(24) + ".").repeat(4) + "amap.com", "1234567890.amap.com", "bad-.amap.com"]) {
+ const message = `Loading the script 'https://${host}/private?key=secret' violates the following Content Security Policy directive: "script-src https://webapi.amap.com".`;
+ assert.deepEqual(classifyMapCSPResources(message), [{code: "cspScript", resource: "redacted"}]);
+ }
+});
diff --git a/app/electron/mapHostManager.js b/app/electron/mapHostManager.js
index bc0bc55e42..1686699675 100644
--- a/app/electron/mapHostManager.js
+++ b/app/electron/mapHostManager.js
@@ -5,7 +5,7 @@
const path = require("node:path");
const fs = require("node:fs/promises");
const {randomBytes} = require("node:crypto");
-const {mapDiagnosticCodes, mapCSPDiagnosticCodes, mapCSPResourceCodes, classifyMapConsoleMessage,
+const {mapDiagnosticCodes, mapCSPDiagnosticCodes, isMapCSPResource, MAX_MAP_DIAGNOSTICS, classifyMapConsoleMessage,
classifyMapCSPResources} = require("./mapHostDiagnostics");
const {
hasUnsafeMapSwitches, normalizeMapOrigin, parseMapCreate, parseMapCommand, parseMapReply, parseMapGeometry,
@@ -311,10 +311,10 @@ const createMapHostManager = ({app, ipcMain, session, BrowserWindow, WebContents
let creationFailure = "hostSetupFailed";
const report = (code, resource) => {
if (!host || host.destroyed || !mapDiagnosticCodes.includes(code) ||
- resource !== undefined && (!mapCSPDiagnosticCodes.includes(code) || !mapCSPResourceCodes.includes(resource))) return;
- // 两个有限白名单的组合去重,同一指令拒绝不同资源时也保留证据。
+ resource !== undefined && (!mapCSPDiagnosticCodes.includes(code) || !isMapCSPResource(resource))) return;
+ // 按实例去重并限制总量,同一指令拒绝不同受限来源时也保留证据。
const key = code + ":" + (resource || "");
- if (host.diagnostics.has(key)) return;
+ if (host.diagnostics.has(key) || host.diagnostics.size >= MAX_MAP_DIAGNOSTICS) return;
host.diagnostics.add(key);
sendOwner(host, {version: 1, instanceID: init.instanceID, type: "diagnostic", code,
...(resource === undefined ? {} : {resource})});
@@ -328,7 +328,7 @@ const createMapHostManager = ({app, ipcMain, session, BrowserWindow, WebContents
navigateOnDragDrop: false, safeDialogs: true, disableDialogs: true, devTools: false,
spellcheck: false, backgroundThrottling: false, preload: path.join(__dirname, "mapHostPreload.js")}});
const contents = view.webContents;
- host = {key, init, owner: event.sender, frame: event.senderFrame, win: owner.win, origin: owner.origin, diagnostics: new Set(),
+ host = {key, init, owner: event.sender, frame: event.senderFrame, win: owner.win, origin: owner.origin, diagnostics: new Set(), report,
view, router, listeners: [], ids: new Set(), revision: -1, loaded: false, ready: false, bootstrapped: false, destroyed: false};
hosts.set(key, host);
// 全局 web-contents-created 会安装外部打开链接处理器,必须在加载前替换为拒绝。
@@ -343,7 +343,7 @@ const createMapHostManager = ({app, ipcMain, session, BrowserWindow, WebContents
listen(host, contents, "certificate-error", (_event, _url, _error, _certificate, callback) => callback(false));
listen(host, contents, "console-message", (event, _level, legacyMessage) => {
const message = event?.message ?? legacyMessage;
- const resources = classifyMapCSPResources(message);
+ const resources = classifyMapCSPResources(message, host.origin);
for (const code of classifyMapConsoleMessage(message)) {
if (!resources.some(item => item.code === code)) report(code);
}
@@ -451,7 +451,7 @@ const createMapHostManager = ({app, ipcMain, session, BrowserWindow, WebContents
const host = getHost(event, value);
if (!host) return;
try {
- const geometry = parseMapGeometry(value, host.owner.getZoomFactor(), host.win.getContentBounds());
+ const geometry = parseMapGeometry(value, host.owner.getZoomFactor(), host.win.getContentBounds(), host.report);
if (!geometry?.visible) { hide(host); return; }
host.geometry = geometry;
applyGeometry(host);
diff --git a/app/electron/mapHostManager.test.js b/app/electron/mapHostManager.test.js
index 6b5b308d74..64cd0da29e 100644
--- a/app/electron/mapHostManager.test.js
+++ b/app/electron/mapHostManager.test.js
@@ -292,6 +292,10 @@ test("geometry is invisible before ready, uses owner zoom, and hides on invalid
s.handlers["siyuan-map-geometry"](s.event(), geometry); assert.equal(s.views[0].visible, true);
s.handlers["siyuan-map-geometry"](s.event(), {...geometry, bounds: {...geometry.bounds, width: 10000}});
assert.equal(s.views[0].visible, false);
+ assert.deepEqual(s.owner.sent.at(-1)[1], {...envelope, type: "diagnostic", code: "geometryLogicalBounds"});
+ const count = s.owner.sent.length;
+ s.handlers["siyuan-map-geometry"](s.event(), {...geometry, bounds: {...geometry.bounds, width: 10000}});
+ assert.equal(s.owner.sent.length, count);
s.manager.destroyAll();
});
@@ -359,7 +363,7 @@ test("CSP resources are captured before load, deduplicated by category and isola
emit("https://g.alicdn.com/another?key=another-secret");
emit("blob:null/private");
for (let index = 0; index < 100; index++) emit(`https://unknown-${index}.invalid/secret`);
- assert.deepEqual(s.owner.sent.map(entry => entry[1].resource), ["blob", "https:g.alicdn.com", "blob", "https:other"]);
+ assert.deepEqual(s.owner.sent.map(entry => entry[1].resource), ["blob", "https:g.alicdn.com", "blob", "redacted"]);
assert.equal(JSON.stringify(s.owner.sent).includes("secret"), false);
const count = s.owner.sent.length;
s.load();
@@ -371,6 +375,18 @@ test("CSP resources are captured before load, deduplicated by category and isola
assert.equal(s.owner.sent.length, count);
});
+test("dynamic public CSP source diagnostics remain bounded per host", () => {
+ const s = setup(); s.create();
+ const contents = s.views[0].webContents;
+ for (let index = 0; index < 100; index++) {
+ contents.emit("console-message", {message: `Connecting to 'https://map${index}.amap.com/private?key=secret' violates the following Content Security Policy directive: "connect-src https://webapi.amap.com".`});
+ }
+ assert.equal(s.owner.sent.length, 64);
+ assert.ok(s.owner.sent.every(entry => entry[1].instanceID === envelope.instanceID));
+ assert.equal(JSON.stringify(s.owner.sent).includes("secret"), false);
+ s.manager.destroyAll();
+});
+
test("provider routing reports fixed transport failures without logging requests", async () => {
const diagnostics = [];
const {ses, router} = setupRouter({report: code => diagnostics.push(code)});
diff --git a/app/electron/mapHostPolicy.js b/app/electron/mapHostPolicy.js
index 5613b90685..682de434c8 100644
--- a/app/electron/mapHostPolicy.js
+++ b/app/electron/mapHostPolicy.js
@@ -168,23 +168,26 @@ const createMapContentSecurityPolicy = (origin, provider) => {
};
// 渲染器传入 CSS 像素,原生视图使用设备无关像素,不能再乘屏幕像素密度。
-const parseMapGeometry = (value, zoom, contentBounds) => {
- if (!isRecord(value) || typeof value.visible !== "boolean") return;
+const parseMapGeometry = (value, zoom, contentBounds, report = () => {}) => {
+ const reject = code => { report(code); return undefined; };
+ if (!isRecord(value) || typeof value.visible !== "boolean") return reject("geometryInvalid");
if (!value.visible) return {visible: false};
if (!Number.isFinite(zoom) || zoom < 0.25 || zoom > 5 || !isRecord(value.bounds) ||
- !isRecord(value.logicalSize) || !isRecord(value.crop)) return;
+ !isRecord(value.logicalSize) || !isRecord(value.crop)) return reject("geometryInvalid");
const {x, y, width, height} = value.bounds;
const full = value.logicalSize;
const crop = value.crop;
const numbers = [x, y, width, height, full.width, full.height, crop.x, crop.y];
- if (!numbers.every(Number.isFinite) || numbers.some(number => number < 0 || number > 32768) ||
- width < 1 || height < 1 || full.width < width || full.height < height ||
- crop.x + width > full.width + 1 || crop.y + height > full.height + 1 ||
- (x + width) * zoom > contentBounds.width + 1 || (y + height) * zoom > contentBounds.height + 1) return;
+ if (!numbers.every(Number.isFinite) || numbers.some(number => number < 0 || number > 32768)) return reject("geometryInvalid");
+ if (width < 1 || height < 1 || full.width < width || full.height < height) return reject("geometryLogicalBounds");
+ if (crop.x + width > full.width + 1 || crop.y + height > full.height + 1) return reject("geometryCropBounds");
+ if ((x + width) * zoom > contentBounds.width + 1 || (y + height) * zoom > contentBounds.height + 1) {
+ return reject("geometryWindowBounds");
+ }
// 向内取整,避免原生视图绘制到可见 DOM 矩形之外。
const left = Math.ceil(x * zoom), top = Math.ceil(y * zoom);
const right = Math.floor((x + width) * zoom), bottom = Math.floor((y + height) * zoom);
- if (right <= left || bottom <= top) return {visible: false};
+ if (right <= left || bottom <= top) { report("geometryRoundedEmpty"); return {visible: false}; }
return {visible: true, zoom, bounds: {x: left, y: top, width: right - left, height: bottom - top},
logicalSize: {width: full.width, height: full.height},
crop: {x: crop.x + left / zoom - x, y: crop.y + top / zoom - y}};
diff --git a/app/electron/mapHostPolicy.test.js b/app/electron/mapHostPolicy.test.js
index 39ca65d773..a77816b601 100644
--- a/app/electron/mapHostPolicy.test.js
+++ b/app/electron/mapHostPolicy.test.js
@@ -148,3 +148,26 @@ test("native geometry converts CSS to DIP, crops inward, and denies off-window o
assert.equal(parseMapGeometry({...value, bounds: {...value.bounds, x: NaN}}, 1, {width: 800, height: 600}), undefined);
assert.deepEqual(parseMapGeometry({visible: false}, 1, {}), {visible: false});
});
+
+test("geometry rejection reports only fixed reasons without weakening validation", () => {
+ const value = {visible: true, bounds: {x: 10, y: 20, width: 200, height: 100},
+ logicalSize: {width: 400, height: 200}, crop: {x: 0, y: 0}};
+ const viewport = {width: 800, height: 600};
+ for (const [input, zoom, bounds, reason] of [
+ [null, 1, viewport, "geometryInvalid"],
+ [value, 0, viewport, "geometryInvalid"],
+ [{...value, bounds: {...value.bounds, x: NaN}}, 1, viewport, "geometryInvalid"],
+ [{...value, logicalSize: {width: 199.99999999999997, height: 200}}, 1, viewport, "geometryLogicalBounds"],
+ [{...value, crop: {x: 300, y: 0}}, 1, viewport, "geometryCropBounds"],
+ [value, 1, {width: 100, height: 100}, "geometryWindowBounds"],
+ [{...value, bounds: {x: 0.1, y: 0.1, width: 1, height: 1}}, 0.25, viewport, "geometryRoundedEmpty"],
+ ]) {
+ const reasons = [];
+ const result = parseMapGeometry(input, zoom, bounds, code => reasons.push(code));
+ assert.ok(!result?.visible);
+ assert.deepEqual(reasons, [reason]);
+ }
+ const reasons = [];
+ assert.deepEqual(parseMapGeometry({visible: false}, 1, viewport, code => reasons.push(code)), {visible: false});
+ assert.deepEqual(reasons, []);
+});
diff --git a/app/src/protyle/render/av/map/desktopTransport.test.ts b/app/src/protyle/render/av/map/desktopTransport.test.ts
index d0c55c5100..d3cc084716 100644
--- a/app/src/protyle/render/av/map/desktopTransport.test.ts
+++ b/app/src/protyle/render/av/map/desktopTransport.test.ts
@@ -171,6 +171,18 @@ describe("desktop map transport", () => {
assert.equal(warnings.length, mapCSPResourceCodes.length);
assert.equal(JSON.stringify(warnings).includes("secret"), false);
});
+ it("bounds public CSP diagnostics per instance and rejects raw addresses", () => {
+ const warnings: unknown[][] = [];
+ const f = fixture(warnings);
+ for (let i = 0; i < 100; i++) {
+ const value = {type: "diagnostic", code: "cspConnect", resource: `https:map${i}.amap.com`};
+ f.reply(value);
+ f.reply(value);
+ }
+ assert.equal(warnings.length, 64);
+ assert.deepEqual(warnings[0], ["Database map diagnostic:", "cspConnect", "https:map0.amap.com"]);
+ f.host.destroy();
+ });
it("logs only approved capability reason codes and never exception or response details", async () => {
for (const reason of ["ownerUnavailable", "notMainFrame", "notInitialized", "unregisteredOwner", "invalidKernelOrigin",
"originMismatch", "unsupportedDocument", "invalidDocument", "unsafeProcessSwitches"]) {
@@ -209,6 +221,140 @@ describe("desktop map transport", () => {
assert.equal(await load({invoke: async () => ({version: 2, supported: true})}).isDesktopAVMapHostSupported(), false);
assert.equal(await load({invoke: async () => { throw new Error("old main"); }}).isDesktopAVMapHostSupported(), false);
});
+ it("preserves fractional geometry through native validation at every clipped edge and zoom", () => {
+ const compute = load({}).computeDesktopAVMapGeometry;
+ const {parseMapGeometry} = require("../../../../../electron/mapHostPolicy");
+ const rect = {x: 155.55555555555554, y: 555.5555555555555, width: 1433.3333333333333, height: 480};
+ assert.ok(rect.y + rect.height - rect.y > rect.height, "the fixture must reproduce floating-point expansion");
+ const cases = [
+ {clips: [], width: rect.width, height: rect.height, cropX: 0, cropY: 0},
+ {clips: [{x: rect.x + 40, y: 0, width: 2000, height: 1400}],
+ width: rect.width - 40, height: rect.height, cropX: 40, cropY: 0},
+ {clips: [{x: 0, y: 0, width: rect.x + rect.width - 40, height: 1400}],
+ width: rect.width - 40, height: rect.height, cropX: 0, cropY: 0},
+ {clips: [{x: 0, y: rect.y + 40, width: 2000, height: 1400}],
+ width: rect.width, height: rect.height - 40, cropX: 0, cropY: 40},
+ {clips: [{x: 0, y: 0, width: 2000, height: rect.y + rect.height - 40}],
+ width: rect.width, height: rect.height - 40, cropX: 0, cropY: 0},
+ {clips: [{x: rect.x + 10, y: rect.y + 30, width: rect.width - 30, height: rect.height - 70}],
+ width: rect.width - 30, height: rect.height - 70, cropX: 10, cropY: 30},
+ ];
+ for (const entry of cases) {
+ const geometry = compute(rect, entry.clips);
+ assert.equal(geometry.visible, true);
+ assert.ok(geometry.bounds.width <= rect.width);
+ assert.ok(geometry.bounds.height <= rect.height);
+ assert.ok(Math.abs(geometry.bounds.width - entry.width) < 1e-9);
+ assert.ok(Math.abs(geometry.bounds.height - entry.height) < 1e-9);
+ assert.ok(Math.abs(geometry.crop.x - entry.cropX) < 1e-9);
+ assert.ok(Math.abs(geometry.crop.y - entry.cropY) < 1e-9);
+ for (const zoom of [0.8, 0.9, 1, 1.1, 1.25, 1.5, 2]) {
+ const native = parseMapGeometry(geometry, zoom, {width: 4000, height: 3000});
+ assert.equal(native?.visible, true, `native validation must retain the map at zoom ${zoom}`);
+ assert.ok(native.bounds.x >= geometry.bounds.x * zoom);
+ assert.ok(native.bounds.y >= geometry.bounds.y * zoom);
+ assert.ok(native.bounds.x + native.bounds.width <= (geometry.bounds.x + geometry.bounds.width) * zoom);
+ assert.ok(native.bounds.y + native.bounds.height <= (geometry.bounds.y + geometry.bounds.height) * zoom);
+ }
+ }
+ const widthRect = {...rect, x: rect.y, width: rect.height};
+ assert.ok(widthRect.x + widthRect.width - widthRect.x > widthRect.width);
+ const geometry = compute(widthRect, []);
+ assert.equal(geometry.bounds.width, widthRect.width);
+ assert.equal(parseMapGeometry(geometry, 1.25, {width: 4000, height: 3000})?.visible, true);
+ });
+ it("reports only fixed visibility transitions and distinguishes clipped bottom hit tests", async () => {
+ const oldMutation = globalThis.MutationObserver, oldResize = globalThis.ResizeObserver;
+ class Observer { observe() {} disconnect() {} }
+ globalThis.MutationObserver = Observer as any;
+ globalThis.ResizeObserver = Observer as any;
+ const warnings: unknown[][] = [];
+ const f = fixture(warnings);
+ const getStyle = f.doc.defaultView.getComputedStyle;
+ const visibility = () => warnings.filter(([prefix]) => prefix === "Database map visibility:");
+ const lastGeometry = () => f.calls.filter(([name]) => name === "siyuan-map-geometry").at(-1)[1];
+ f.container.id = "private-block-id";
+ f.container.textContent = "private-document-content";
+ try {
+ await f.created();
+ assert.deepEqual(visibility(), [["Database map visibility:", "openfreemap", "stabilizing"]]);
+ f.advance();
+ assert.deepEqual(visibility().at(-1), ["Database map visibility:", "openfreemap", "visible"]);
+ const scroll = f.domListeners.get("scroll");
+ const expectHidden = (reason: string) => {
+ scroll();
+ assert.equal(lastGeometry().visible, false);
+ assert.deepEqual(visibility().at(-1), ["Database map visibility:", "openfreemap", reason]);
+ const count = warnings.length;
+ for (let i = 0; i < 10; i++) f.advance(30);
+ assert.equal(warnings.length, count, "unchanged reasons must not log on every animation frame");
+ };
+ f.doc.hidden = true;
+ expectHidden("documentHidden");
+ f.doc.hidden = false;
+ f.container.isConnected = false;
+ expectHidden("disconnected");
+ f.container.isConnected = true;
+ f.container.getClientRects = (): DOMRect[] => [];
+ expectHidden("noClientRects");
+ f.container.getClientRects = () => [f.rect];
+ f.doc.defaultView.getComputedStyle = () => ({...getStyle(), visibility: "hidden"});
+ expectHidden("hiddenStyle");
+ f.doc.defaultView.getComputedStyle = getStyle;
+ f.rect.width = 0;
+ expectHidden("zeroRect");
+ f.rect.width = 400;
+ f.rect.x = NaN;
+ expectHidden("invalidGeometry");
+ f.rect.x = 10;
+ f.rect.y = 900;
+ expectHidden("outsideViewport");
+ f.rect.y = 20;
+ const parent = {parentElement: null as HTMLElement | null, getBoundingClientRect: () => ({x: 0, y: 0, width: 1000, height: 800}),
+ offsetWidth: 1000, offsetHeight: 800, clientWidth: 1000, clientHeight: 0, clientLeft: 0, clientTop: 0};
+ f.container.parentElement = parent;
+ f.doc.defaultView.getComputedStyle = (element: unknown) => ({...getStyle(),
+ overflowY: element === parent ? "hidden" : "visible"});
+ expectHidden("clipped");
+ f.container.parentElement = null;
+ f.doc.defaultView.getComputedStyle = getStyle;
+ f.doc.querySelectorAll = () => [{contains: () => false, getClientRects: () => [f.rect],
+ getBoundingClientRect: () => f.rect, textContent: "private-overlay-content"}];
+ expectHidden("overlay");
+ f.doc.querySelectorAll = (): HTMLElement[] => [];
+ for (const [y, reason] of [[f.rect.y + 0.5, "hitTestTop"],
+ [f.rect.y + f.rect.height / 2, "hitTestMiddle"], [f.rect.y + f.rect.height - 0.5, "hitTestBottom"]] as const) {
+ f.doc.elementFromPoint = (_x: number, pointY: number) => pointY === y ?
+ {textContent: "private-hit-content", id: "private-overlay-id"} : f.container;
+ expectHidden(reason);
+ }
+ f.doc.elementFromPoint = () => f.container;
+ scroll();
+ assert.equal(lastGeometry().visible, true);
+ assert.deepEqual(visibility().at(-1), ["Database map visibility:", "openfreemap", "visible"]);
+ const fixedReasons = new Set(["stabilizing", "visible", "documentHidden", "disconnected", "noClientRects",
+ "hiddenStyle", "zeroRect", "invalidGeometry", "outsideViewport", "clipped", "overlay", "hitTestTop",
+ "hitTestMiddle", "hitTestBottom"]);
+ assert.ok(warnings.every(values => values.length === 3 && values[0] === "Database map visibility:" &&
+ values[1] === "openfreemap" && fixedReasons.has(values[2] as string)));
+ assert.doesNotMatch(JSON.stringify(warnings), /private|do-not-send|https:|instanceID/);
+ for (let i = 0; i < 100; i++) {
+ f.doc.hidden = i % 2 === 0;
+ scroll();
+ assert.equal(lastGeometry().visible, !f.doc.hidden, "the log limit must not interrupt geometry updates");
+ }
+ assert.equal(visibility().length, 64);
+ const count = warnings.length;
+ f.host.destroy();
+ scroll();
+ f.advance();
+ assert.equal(warnings.length, count);
+ } finally {
+ f.host.destroy();
+ globalThis.MutationObserver = oldMutation;
+ globalThis.ResizeObserver = oldResize;
+ }
+ });
it("caches ready commands, validates replies and recovers from scroll, occlusion and visibility changes", async () => {
const oldMutation = globalThis.MutationObserver, oldResize = globalThis.ResizeObserver;
let disconnects = 0;
diff --git a/app/src/protyle/render/av/map/desktopTransport.ts b/app/src/protyle/render/av/map/desktopTransport.ts
index 0d27837d69..f775f5aa29 100644
--- a/app/src/protyle/render/av/map/desktopTransport.ts
+++ b/app/src/protyle/render/av/map/desktopTransport.ts
@@ -11,14 +11,23 @@ interface MapRect { x: number; y: number; width: number; height: number; }
type MapGeometry = {visible: false} | {
visible: true; bounds: MapRect; logicalSize: {width: number; height: number}; crop: {x: number; y: number};
};
+type MapVisibilityReason = "visible" | "stabilizing" | "ownerUnavailable" | "documentHidden" | "disconnected" |
+ "noClientRects" | "hiddenStyle" | "invalidGeometry" | "zeroRect" | "outsideViewport" | "clipped" | "overlay" |
+ "hitTestTop" | "hitTestMiddle" | "hitTestBottom";
const intersects = (a: MapRect, b: MapRect) => a.x < b.x + b.width && b.x < a.x + a.width &&
a.y < b.y + b.height && b.y < a.y + a.height;
// All coordinates stay in owner CSS pixels. Only the main process applies the owner zoom factor.
-export const computeDesktopAVMapGeometry = (rect: MapRect, clips: MapRect[], occluders: MapRect[] = []): MapGeometry => {
+export const computeDesktopAVMapGeometry = (rect: MapRect, clips: MapRect[], occluders: MapRect[] = [],
+ report?: (reason: MapVisibilityReason) => void): MapGeometry => {
if (![rect, ...clips, ...occluders].every((item) => [item.x, item.y, item.width, item.height].every(Number.isFinite) &&
- item.width >= 0 && item.height >= 0) || rect.width < 1 || rect.height < 1) {
+ item.width >= 0 && item.height >= 0)) {
+ report?.("invalidGeometry");
+ return {visible: false};
+ }
+ if (rect.width < 1 || rect.height < 1) {
+ report?.("zeroRect");
return {visible: false};
}
let left = rect.x, top = rect.y, right = rect.x + rect.width, bottom = rect.y + rect.height;
@@ -28,11 +37,17 @@ export const computeDesktopAVMapGeometry = (rect: MapRect, clips: MapRect[], occ
right = Math.min(right, clip.x + clip.width);
bottom = Math.min(bottom, clip.y + clip.height);
});
- const bounds = {x: left, y: top, width: right - left, height: bottom - top};
- if (left < 0 || top < 0 || bounds.width < 1 || bounds.height < 1 ||
- occluders.some((item) => item.width > 0 && item.height > 0 && intersects(bounds, item))) {
+ // 裁剪差值的浮点舍入不能使可见尺寸超过原始尺寸。
+ const bounds = {x: left, y: top, width: Math.min(rect.width, right - left), height: Math.min(rect.height, bottom - top)};
+ if (left < 0 || top < 0 || bounds.width < 1 || bounds.height < 1) {
+ report?.("clipped");
+ return {visible: false};
+ }
+ if (occluders.some((item) => item.width > 0 && item.height > 0 && intersects(bounds, item))) {
+ report?.("overlay");
return {visible: false};
}
+ report?.("visible");
return {visible: true, bounds, logicalSize: {width: rect.width, height: rect.height},
crop: {x: left - rect.x, y: top - rect.y}};
};
@@ -55,21 +70,26 @@ const mapDiagnosticCodes = new Set([
"providerRequestDenied", "providerInsecureRequest", "providerHTTPFailure", "providerNetworkFailure",
"cspScript", "cspWorker", "cspConnect", "cspImage", "cspStyle", "cspEval", "cspWasm",
"storageUnavailable", "webglUnavailable", "amapInvalidKey", "amapInvalidSecurityCode",
- "amapDomainMismatch", "amapPlatformMismatch",
+ "amapDomainMismatch", "amapPlatformMismatch", "geometryInvalid", "geometryLogicalBounds",
+ "geometryCropBounds", "geometryWindowBounds", "geometryRoundedEmpty",
]);
const mapCSPDiagnosticCodes = new Set(["cspScript", "cspWorker", "cspConnect", "cspImage", "cspStyle", "cspEval", "cspWasm"]);
const mapCSPResourceCodes = new Set([
"blob", "data", "inline", "eval", "wasm", "other",
- "https:webapi.amap.com", "https:restapi.amap.com", "https:vdata.amap.com", "https:a.amap.com",
- "https:g.alicdn.com", "https:fourier.taobao.com", "https:autonavi-tile", "https:other",
- "http:webapi.amap.com", "http:restapi.amap.com", "http:vdata.amap.com", "http:a.amap.com",
- "http:g.alicdn.com", "http:fourier.taobao.com", "http:autonavi-tile", "http:other",
+ "owner-origin", "local-address", "redacted",
]);
+const isMapCSPResource = (value: unknown): value is string => {
+ if (typeof value !== "string" || value.length > 102) return false;
+ if (mapCSPResourceCodes.has(value)) return true;
+ const hostname = /^https?:([a-z0-9.-]+)$/.exec(value)?.[1];
+ return !!hostname && hostname.length <= 96 && /(?:^|\.)(?:amap\.com|autonavi\.com|alicdn\.com|taobao\.com)$/.test(hostname) &&
+ hostname.split(".").every(label => /^[a-z](?:[a-z0-9-]{0,22}[a-z0-9])?$/.test(label));
+};
const logMapDiagnostic = (code: unknown, resource?: unknown) => {
if (typeof code === "string" && mapDiagnosticCodes.has(code)) {
if (resource === undefined) {
console.warn("Database map diagnostic:", code);
- } else if (mapCSPDiagnosticCodes.has(code) && typeof resource === "string" && mapCSPResourceCodes.has(resource)) {
+ } else if (mapCSPDiagnosticCodes.has(code) && isMapCSPResource(resource)) {
console.warn("Database map diagnostic:", code, resource);
}
}
@@ -95,11 +115,24 @@ export const isDesktopAVMapHostSupported = async (): Promise<boolean> => {
return false;
};
-const readGeometry = (container: HTMLElement): MapGeometry => {
+const readGeometry = (container: HTMLElement, report: (reason: MapVisibilityReason) => void): MapGeometry => {
const doc = container.ownerDocument;
const scope = doc.defaultView;
- if (!scope || doc.hidden || !container.isConnected || !container.getClientRects().length) {
+ const hidden = (reason: MapVisibilityReason): MapGeometry => {
+ report(reason);
return {visible: false};
+ };
+ if (!scope) {
+ return hidden("ownerUnavailable");
+ }
+ if (doc.hidden) {
+ return hidden("documentHidden");
+ }
+ if (!container.isConnected) {
+ return hidden("disconnected");
+ }
+ if (!container.getClientRects().length) {
+ return hidden("noClientRects");
}
const rect = container.getBoundingClientRect();
const clips: MapRect[] = [{x: 0, y: 0, width: scope.innerWidth, height: scope.innerHeight}];
@@ -107,7 +140,7 @@ const readGeometry = (container: HTMLElement): MapGeometry => {
const style = scope.getComputedStyle(element);
if (style.display === "none" || style.visibility !== "visible" || Number(style.opacity) === 0 ||
style.getPropertyValue("content-visibility") === "hidden") {
- return {visible: false};
+ return hidden("hiddenStyle");
}
if (element !== container && /hidden|clip|scroll|auto/.test(style.overflowX + style.overflowY)) {
const bounds = element.getBoundingClientRect();
@@ -145,15 +178,19 @@ const readGeometry = (container: HTMLElement): MapGeometry => {
occluders.push(element.getBoundingClientRect());
}
});
- const geometry = computeDesktopAVMapGeometry(rect, clips, occluders);
+ const geometry = computeDesktopAVMapGeometry(rect, clips, occluders, reason => {
+ report(reason === "clipped" && !intersects(rect, clips[0]) ? "outsideViewport" : reason);
+ });
if (geometry.visible) {
const bounds = geometry.bounds;
// Hit-test the owner's DOM (native child views are not in this tree), including sticky toolbars.
for (const x of [bounds.x + 0.5, bounds.x + bounds.width / 2, bounds.x + bounds.width - 0.5]) {
- for (const y of [bounds.y + 0.5, bounds.y + bounds.height / 2, bounds.y + bounds.height - 0.5]) {
+ const samples: Array<[number, MapVisibilityReason]> = [[bounds.y + 0.5, "hitTestTop"],
+ [bounds.y + bounds.height / 2, "hitTestMiddle"], [bounds.y + bounds.height - 0.5, "hitTestBottom"]];
+ for (const [y, reason] of samples) {
const top = doc.elementFromPoint(x, y);
if (!top || !container.contains(top)) {
- return {visible: false};
+ return hidden(reason);
}
}
}
@@ -170,6 +207,10 @@ export const createDesktopAVMapHost = (container: HTMLElement, options: AVMapHos
let ids = new Set<string>();
let frame = 0, timeout = 0, stableAfter = 0, scrollingUntil = 0;
let lastGeometry = "", sentGeometry = "";
+ let visibilityReason: MapVisibilityReason = "stabilizing", lastVisibilityReason: MapVisibilityReason;
+ let visibilityDiagnosticsRemaining = 64;
+ const diagnostics = new Set<string>();
+ const provider = isAVMapProvider(options.provider) ? options.provider : undefined;
let observer: MutationObserver;
let resizeObserver: ResizeObserver;
const envelope = () => ({version: AV_MAP_PROTOCOL_VERSION, instanceID} as const);
@@ -178,8 +219,16 @@ export const createDesktopAVMapHost = (container: HTMLElement, options: AVMapHos
ipc.send("siyuan-map-command", command);
}
};
- const geometry = (value: MapGeometry) => {
+ const geometry = (value: MapGeometry, reason = visibilityReason) => {
const key = JSON.stringify(value);
+ if (created && provider && reason !== lastVisibilityReason) {
+ // 仅记录可见状态切换和固定原因,不输出 DOM、尺寸、位置或实例标识。
+ if (visibilityDiagnosticsRemaining > 0) {
+ console.warn("Database map visibility:", provider, reason);
+ visibilityDiagnosticsRemaining--;
+ }
+ lastVisibilityReason = reason;
+ }
if (created && key !== sentGeometry) {
ipc.send("siyuan-map-geometry", {...envelope(), ...value});
sentGeometry = key;
@@ -188,11 +237,11 @@ export const createDesktopAVMapHost = (container: HTMLElement, options: AVMapHos
const invalidate = () => {
if (!destroyed && scope) {
stableAfter = scope.performance.now() + 120;
- geometry({visible: false});
+ geometry({visible: false}, "stabilizing");
}
};
const checkGeometry = () => {
- const next = readGeometry(container);
+ const next = readGeometry(container, reason => { visibilityReason = reason; });
const key = JSON.stringify(next);
if (key !== lastGeometry) {
lastGeometry = key;
@@ -200,9 +249,13 @@ export const createDesktopAVMapHost = (container: HTMLElement, options: AVMapHos
stableAfter = 0;
geometry(next);
} else {
- invalidate();
+ stableAfter = scope.performance.now() + 120;
+ geometry({visible: false}, next.visible ? "stabilizing" : visibilityReason);
}
}
+ if (!next.visible) {
+ geometry(next);
+ }
return next;
};
const onScroll = () => {
@@ -263,6 +316,12 @@ export const createDesktopAVMapHost = (container: HTMLElement, options: AVMapHos
}
const diagnostic = value as {version?: unknown; instanceID?: unknown; type?: unknown; code?: unknown; resource?: unknown};
if (diagnostic?.version === AV_MAP_PROTOCOL_VERSION && diagnostic.instanceID === instanceID && diagnostic.type === "diagnostic") {
+ if (typeof diagnostic.code !== "string" || !mapDiagnosticCodes.has(diagnostic.code) ||
+ diagnostic.resource !== undefined && (!mapCSPDiagnosticCodes.has(diagnostic.code) ||
+ !isMapCSPResource(diagnostic.resource))) return;
+ const key = diagnostic.code + ":" + (diagnostic.resource || "");
+ if (diagnostics.has(key) || diagnostics.size >= 64) return;
+ diagnostics.add(key);
logMapDiagnostic(diagnostic.code, diagnostic.resource);
return;
}