Skip to content

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

:bug: Allow approved AMap resource origins with bounded redirects https://github.com/siyuan-note/siyuan/issues/20129

Daniel3e5ea95e14cf
diff --git a/app/electron/mapHostManager.js b/app/electron/mapHostManager.js
index 1686699675..1afb708c66 100644
--- a/app/electron/mapHostManager.js
+++ b/app/electron/mapHostManager.js
@@ -16,6 +16,7 @@ const deniedResponse = () => new Response("Forbidden", {status: 403,
headers: {"Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store"}});
const withoutCredentials = (headers) => Object.fromEntries(Object.entries(headers || {})
.filter(([name]) => !["cookie", "authorization", "proxy-authorization"].includes(name.toLowerCase())));
+const amapSingleHostRedirects = ["jsapi.amap.com", "jsapi-service.amap.com"];
const clearMapSession = ses => {
for (const method of ["closeAllConnections", "clearStorageData", "clearAuthCache", "clearCache", "clearHostResolverCache"]) {
try { void Promise.resolve(ses[method]()).catch(() => {}); } catch (_error) { /* 继续清理其他会话状态。 */ }
@@ -98,12 +99,20 @@ const createMapSessionRouter = ({ses, origin, provider, appDir, readFile = fs.re
abort.signal.addEventListener("abort", finish, {once: true});
try {
let url = request.url;
+ const initialHostname = new URL(url).hostname;
for (let redirects = 0; redirects <= 5; redirects++) {
if (destroyed || abort.signal.aborted) break;
if (!isAllowedMapProviderURL(url, provider)) {
report(url.startsWith("http:") ? "providerInsecureRequest" : "providerRequestDenied");
break;
}
+ const hostname = new URL(url).hostname;
+ // 代理会隐藏重定向终点;新增来源不得借跨主机跳转越过各自的 CSP 用途限制。
+ if (provider === "amap" && hostname !== initialHostname &&
+ [initialHostname, hostname].some(host => amapSingleHostRedirects.includes(host))) {
+ report("providerRequestDenied");
+ break;
+ }
if (activeURL) trackFetch(activeURL, -1);
activeURL = url;
trackFetch(activeURL, 1);
diff --git a/app/electron/mapHostManager.test.js b/app/electron/mapHostManager.test.js
index 64cd0da29e..a3a4e70b41 100644
--- a/app/electron/mapHostManager.test.js
+++ b/app/electron/mapHostManager.test.js
@@ -120,6 +120,76 @@ test("same-provider redirects work without leaking redirect headers or provider
router.destroy();
});
+test("new AMap CSP sources reject cross-host redirects before fetching the unauthorized destination", async () => {
+ for (const [from, to] of [
+ ["webapi.amap.com", "jsapi.amap.com"], ["webapi.amap.com", "jsapi-service.amap.com"],
+ ["jsapi.amap.com", "webapi.amap.com"], ["jsapi-service.amap.com", "restapi.amap.com"],
+ ["jsapi.amap.com", "jsapi-service.amap.com"], ["jsapi-service.amap.com", "jsapi.amap.com"],
+ ]) {
+ const reports = [], tracked = [];
+ const {ses, router} = setupRouter({provider: "amap", report: code => reports.push(code),
+ trackFetch: (url, delta) => tracked.push([url, delta])});
+ ses.fetch = async (url, options) => {
+ ses.fetches.push({url, options});
+ return new Response(null, {status: 302, headers: {Location: "https://" + to + "/private?key=secret"}});
+ };
+ const initialURL = "https://" + from + "/start";
+ const response = await ses.protocols.https(new Request(initialURL));
+ assert.equal(response.status, 403, from + " -> " + to);
+ assert.deepEqual(ses.fetches.map(fetch => fetch.url), [initialURL]);
+ assert.deepEqual(reports, ["providerRequestDenied"]);
+ assert.equal(response.headers.get("location"), null);
+ assert.equal(tracked.reduce((total, entry) => total + entry[1], 0), 0);
+ router.destroy();
+ }
+});
+
+test("new AMap sources permit same-host path redirects with unchanged anonymous forwarding", async () => {
+ for (const hostname of ["jsapi.amap.com", "jsapi-service.amap.com"]) {
+ const reports = [];
+ const {ses, router} = setupRouter({provider: "amap", report: code => reports.push(code)});
+ ses.fetch = async (url, options) => {
+ ses.fetches.push({url, options});
+ return url.endsWith("/start") ? new Response(null, {status: 307, headers: {Location: "/end"}}) :
+ new Response("provider-body", {headers: {"Content-Type": "application/javascript", "Set-Cookie": "secret"}});
+ };
+ const response = await ses.protocols.https(new Request("https://" + hostname + "/start", {
+ headers: {Cookie: "secret", Authorization: "secret"},
+ }));
+ assert.equal(await response.text(), "provider-body");
+ assert.deepEqual(ses.fetches.map(fetch => fetch.url), ["https://" + hostname + "/start", "https://" + hostname + "/end"]);
+ for (const {options} of ses.fetches) {
+ assert.equal(options.credentials, "omit");
+ assert.equal(options.redirect, "manual");
+ assert.deepEqual(options.headers, {Referer: origin + "/"});
+ }
+ assert.equal(response.headers.get("set-cookie"), null);
+ assert.deepEqual(reports, []);
+ router.destroy();
+ }
+});
+
+test("existing AMap cross-host redirects retain behavior but cannot enter a new source on a later hop", async () => {
+ for (const enterNewSource of [false, true]) {
+ const reports = [];
+ const {ses, router} = setupRouter({provider: "amap", report: code => reports.push(code)});
+ ses.fetch = async (url, options) => {
+ ses.fetches.push({url, options});
+ if (url === "https://webapi.amap.com/start") {
+ return new Response(null, {status: 302, headers: {Location: "https://restapi.amap.com/end"}});
+ }
+ return enterNewSource ? new Response(null, {status: 302, headers: {Location: "https://jsapi.amap.com/private"}}) :
+ new Response("existing-provider-body");
+ };
+ const response = await ses.protocols.https(new Request("https://webapi.amap.com/start"));
+ assert.equal(response.status, enterNewSource ? 403 : 200);
+ assert.deepEqual(ses.fetches.map(fetch => fetch.url), ["https://webapi.amap.com/start", "https://restapi.amap.com/end"]);
+ assert.deepEqual(reports, enterNewSource ? ["providerRequestDenied"] : []);
+ await response.body?.cancel();
+ router.destroy();
+ }
+});
+
test("destroy aborts in-flight provider requests, clears session state and leaves orphan traffic denied", async () => {
const {ses, router} = setupRouter();
let signal;
diff --git a/app/electron/mapHostPolicy.js b/app/electron/mapHostPolicy.js
index 682de434c8..cde7e260a0 100644
--- a/app/electron/mapHostPolicy.js
+++ b/app/electron/mapHostPolicy.js
@@ -15,7 +15,7 @@ const mapHostFiles = Object.freeze({
const providerHosts = Object.freeze({
openfreemap: ["tiles.openfreemap.org"],
- amap: ["webapi.amap.com", "restapi.amap.com", "vdata.amap.com", "a.amap.com", "*.is.autonavi.com"],
+ amap: ["jsapi.amap.com", "jsapi-service.amap.com", "webapi.amap.com", "restapi.amap.com", "vdata.amap.com", "a.amap.com", "*.is.autonavi.com"],
tencent: ["map.qq.com", "apis.map.qq.com", "*.map.qq.com"],
baidu: ["api.map.baidu.com", "*.map.bdimg.com", "*.bdimg.com", "*.map.baidu.com"],
});
@@ -149,9 +149,9 @@ const createMapContentSecurityPolicy = (origin, provider) => {
styles += " " + assets + "maplibre-gl.css";
workers = "blob:";
} else if (provider === "amap") {
- scripts += " https://webapi.amap.com https://restapi.amap.com";
+ scripts += " https://webapi.amap.com https://restapi.amap.com https://jsapi-service.amap.com";
workers = "blob:";
- connect = "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com";
+ connect = "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com https://jsapi.amap.com";
images += " https://webapi.amap.com https://a.amap.com https://*.is.autonavi.com";
} else if (provider === "tencent") {
scripts += " https://map.qq.com";
diff --git a/app/electron/mapHostPolicy.test.js b/app/electron/mapHostPolicy.test.js
index a77816b601..7618e2e9fb 100644
--- a/app/electron/mapHostPolicy.test.js
+++ b/app/electron/mapHostPolicy.test.js
@@ -114,17 +114,30 @@ test("bootstrap CSP has sandbox isolation and only fixed local assets plus the s
assert.throws(() => createMapContentSecurityPolicy("http://example.com/a", "openfreemap"));
});
-test("AMap permits only its named REST script and blob workers without expanding network access", () => {
+test("AMap permits only approved script and connection origins with isolated blob workers", () => {
const csp = createMapContentSecurityPolicy(origin, "amap");
const directives = Object.fromEntries(csp.split(";").filter(value => value.trim()).map(value => {
const [name, ...sources] = value.trim().split(/\s+/);
return [name, sources];
}));
- assert.deepEqual(directives["script-src"], [origin + "/stage/build/map/host.js", "https://webapi.amap.com", "https://restapi.amap.com"]);
+ assert.deepEqual(directives["script-src"], [origin + "/stage/build/map/host.js", "https://webapi.amap.com", "https://restapi.amap.com", "https://jsapi-service.amap.com"]);
assert.deepEqual(directives["worker-src"], ["blob:"]);
- assert.deepEqual(directives["connect-src"], ["https://webapi.amap.com", "https://restapi.amap.com", "https://vdata.amap.com"]);
+ assert.deepEqual(directives["connect-src"], ["https://webapi.amap.com", "https://restapi.amap.com", "https://vdata.amap.com", "https://jsapi.amap.com"]);
assert.deepEqual(directives["frame-src"], ["'none'"]);
assert.deepEqual(directives.sandbox, ["allow-scripts"]);
+ for (const host of ["jsapi.amap.com", "jsapi-service.amap.com"]) {
+ assert.equal(getMapRequestPolicy("https://" + host + "/resource", "GET", origin, "amap").type, "provider");
+ for (const url of ["http://" + host + "/", "https://" + host + ":8443/", "https://user@" + host + "/",
+ "https://" + host + ".example/", "https://sub." + host + "/"]) {
+ assert.equal(getMapRequestPolicy(url, "GET", origin, "amap"), undefined);
+ }
+ for (const provider of ["openfreemap", "tencent", "baidu"]) {
+ assert.equal(getMapRequestPolicy("https://" + host + "/", "GET", origin, provider), undefined);
+ assert.equal(createMapContentSecurityPolicy(origin, provider).includes(host), false);
+ }
+ }
+ assert.equal(directives["script-src"].includes("https://jsapi.amap.com"), false);
+ assert.equal(directives["connect-src"].includes("https://jsapi-service.amap.com"), false);
for (const value of ["https://unknown.example/a", "http://restapi.amap.com/a", origin + "/api/system/getConf", "file:///tmp/a"]) {
assert.equal(getMapRequestPolicy(value, "GET", origin, "amap"), undefined);
}
diff --git a/app/src/protyle/render/av/map/bootstrap.ts b/app/src/protyle/render/av/map/bootstrap.ts
index 0e629f3893..60383a5826 100644
--- a/app/src/protyle/render/av/map/bootstrap.ts
+++ b/app/src/protyle/render/av/map/bootstrap.ts
@@ -15,9 +15,9 @@ export const getAVMapLockedPolicy = (provider: AVMapProvider): string => {
workers = "blob:";
break;
case "amap":
- scripts = "https://webapi.amap.com https://restapi.amap.com";
+ scripts = "https://webapi.amap.com https://restapi.amap.com https://jsapi-service.amap.com";
workers = "blob:";
- connect = "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com";
+ connect = "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com https://jsapi.amap.com";
images += " https://webapi.amap.com https://a.amap.com https://*.is.autonavi.com";
break;
case "tencent":
diff --git a/app/src/protyle/render/av/map/providersLoader.test.ts b/app/src/protyle/render/av/map/providersLoader.test.ts
index 798384a8f9..0f49bf5a83 100644
--- a/app/src/protyle/render/av/map/providersLoader.test.ts
+++ b/app/src/protyle/render/av/map/providersLoader.test.ts
@@ -264,9 +264,9 @@ describe("isolated provider loading", () => {
});
it("allows only the approved AMap REST script and blob worker while keeping child frames disabled", () => {
const policy = getAVMapLockedPolicy("amap");
- assert.match(policy, /(?:^|; )script-src https:\/\/webapi\.amap\.com https:\/\/restapi\.amap\.com;/);
+ assert.match(policy, /(?:^|; )script-src https:\/\/webapi\.amap\.com https:\/\/restapi\.amap\.com https:\/\/jsapi-service\.amap\.com;/);
assert.match(policy, /worker-src blob:; child-src 'none'; frame-src 'none';/);
- assert.match(policy, /connect-src https:\/\/webapi\.amap\.com https:\/\/restapi\.amap\.com https:\/\/vdata\.amap\.com;/);
+ assert.match(policy, /connect-src https:\/\/webapi\.amap\.com https:\/\/restapi\.amap\.com https:\/\/vdata\.amap\.com https:\/\/jsapi\.amap\.com;/);
assert.equal(/unsafe-eval|script-src[^;]*unsafe-inline|https:\/\/\*\.amap/.test(policy), false);
for (const provider of ["tencent", "baidu"] as AVMapProvider[]) {
assert.match(getAVMapLockedPolicy(provider), /worker-src 'none'; child-src 'none';/);
diff --git a/kernel/server/map_host.go b/kernel/server/map_host.go
index dac7a1ff77..75d0076dee 100644
--- a/kernel/server/map_host.go
+++ b/kernel/server/map_host.go
@@ -59,9 +59,9 @@ func mapHostPolicy(host, provider string) (string, bool) {
styles += " " + local + "maplibre-gl.css"
workers = "blob:"
case "amap":
- scripts += " https://webapi.amap.com https://restapi.amap.com"
+ scripts += " https://webapi.amap.com https://restapi.amap.com https://jsapi-service.amap.com"
workers = "blob:"
- connect = "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com"
+ connect = "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com https://jsapi.amap.com"
images += " https://webapi.amap.com https://a.amap.com https://*.is.autonavi.com"
case "tencent":
scripts += " https://map.qq.com"
diff --git a/kernel/server/map_host_test.go b/kernel/server/map_host_test.go
index a8a21c6063..53a791e41b 100644
--- a/kernel/server/map_host_test.go
+++ b/kernel/server/map_host_test.go
@@ -65,9 +65,9 @@ func TestMapHostAMapRestrictedWorkersAndScripts(t *testing.T) {
}
}
for name, want := range map[string]string{
- "script-src": "localhost:6806/stage/build/map/host.js https://webapi.amap.com https://restapi.amap.com",
+ "script-src": "localhost:6806/stage/build/map/host.js https://webapi.amap.com https://restapi.amap.com https://jsapi-service.amap.com",
"worker-src": "blob:",
- "connect-src": "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com",
+ "connect-src": "https://webapi.amap.com https://restapi.amap.com https://vdata.amap.com https://jsapi.amap.com",
"frame-src": "'none'",
"sandbox": "allow-scripts",
} {