Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix(webui): normalize trace parent origin

myhloli612d3a36e47a
diff --git a/mineru/resources/gradio_trace_html.js b/mineru/resources/gradio_trace_html.js
index 4edaac5d..795ba9cd 100644
--- a/mineru/resources/gradio_trace_html.js
+++ b/mineru/resources/gradio_trace_html.js
@@ -1,5 +1,7 @@
// 此脚本运行于隔离的结果 iframe,全部内容定位在本窗口完成。
(config) => {
+ // 与 MessageEvent.origin 使用同一 URL 规范化规则,统一默认端口和域名大小写。
+ const parentOrigin = new URL(config.parentOrigin).origin;
let pageLabel = config.pageLabels[navigator.language.startsWith("zh") ? 1 : 0];
const nodes = new Map();
const entries = [];
@@ -23,7 +25,7 @@
document.body.append(navigation);
// 所有事件带产物身份,父窗口仍需核对实际发送窗口。
const send = (message) =>
- parent.postMessage({ ...message, channel: "mineru-trace", documentId: config.documentId }, config.parentOrigin);
+ parent.postMessage({ ...message, channel: "mineru-trace", documentId: config.documentId }, parentOrigin);
// 页码条贴近选中内容并限制在当前 iframe 的可见范围内。
const positionNavigation = () => {
if (!selected) return;
@@ -84,7 +86,7 @@
const data = event.data;
if (
event.source !== parent ||
- event.origin !== config.parentOrigin ||
+ event.origin !== parentOrigin ||
data?.channel !== "mineru-trace" ||
data.documentId !== config.documentId
)
diff --git a/tests/unittest/test_kit_gradio_trace.cjs b/tests/unittest/test_kit_gradio_trace.cjs
index dbbede55..28ac3d53 100644
--- a/tests/unittest/test_kit_gradio_trace.cjs
+++ b/tests/unittest/test_kit_gradio_trace.cjs
@@ -58,8 +58,11 @@ window.__mineruTrace.reset();
console.log("Trace identity, handshake and bidirectional routing passed");
// 执行完整正文脚本,验证滚动后的跨页按钮避开目录工具栏并受视口边界限制。
-const checkHtmlNavigation = (offset, blockTop, expectedTop) => {
+const checkHtmlNavigation = (
+ offset, blockTop, expectedTop, configuredOrigin = location.origin, eventOrigin = location.origin
+) => {
const htmlHandlers = {};
+ const targetOrigins = [];
const navigation = {
style: {}, offsetHeight: 30,
setAttribute() {}, replaceChildren() {}, append() {}
@@ -72,9 +75,10 @@ const checkHtmlNavigation = (offset, blockTop, expectedTop) => {
setAttribute() {}, removeAttribute() {},
getBoundingClientRect: () => ({ top: blockTop })
};
- const htmlParent = { postMessage() {} };
+ // 记录实际发送目标,验证 ready 与后续消息使用同一规范化来源。
+ const htmlParent = { postMessage: (_message, origin) => targetOrigins.push(origin) };
const context = {
- parent: htmlParent, navigator: { language: "en" }, innerHeight: 600,
+ parent: htmlParent, navigator: { language: "en" }, innerHeight: 600, URL,
document: {
querySelectorAll: () => [selected],
createElement: tag => tag === "nav" ? navigation : { setAttribute() {}, addEventListener() {} },
@@ -87,9 +91,15 @@ const checkHtmlNavigation = (offset, blockTop, expectedTop) => {
};
const source = fs.readFileSync(path.join(__dirname, "../../mineru/resources/gradio_trace_html.js"), "utf8");
vm.runInNewContext(`(${source})(${JSON.stringify({
- pageLabels: ["Source page", "来源页"], documentId: "one", parentOrigin: location.origin, pageIndices: [2, 5]
+ pageLabels: ["Source page", "来源页"], documentId: "one", parentOrigin: configuredOrigin, pageIndices: [2, 5]
})})`, context);
- htmlHandlers.message({ source: htmlParent, origin: location.origin, data: {
+ assert.deepEqual(targetOrigins, [eventOrigin]);
+ // 同一父窗口传来的不同来源消息仍须拒绝,避免规范化放宽来源边界。
+ htmlHandlers.message({ source: htmlParent, origin: "https://foreign.test", data: {
+ channel: "mineru-trace", documentId: "one", type: "select", id: entry.id, pageIdx: 2, scroll: false
+ } });
+ assert.equal(navigation.hidden, true);
+ htmlHandlers.message({ source: htmlParent, origin: eventOrigin, data: {
channel: "mineru-trace", documentId: "one", type: "select", id: entry.id, pageIdx: 2, scroll: false
} });
htmlHandlers.scroll();
@@ -100,6 +110,9 @@ checkHtmlNavigation("52px", -100, 52);
checkHtmlNavigation("", -100, 8);
checkHtmlNavigation("52px", 250, 250);
checkHtmlNavigation("52px", 700, 562);
+checkHtmlNavigation("52px", -100, 52, "https://mineru.test:443", "https://mineru.test");
+checkHtmlNavigation("52px", -100, 52, "HTTP://MINERU.TEST:80", "http://mineru.test");
+checkHtmlNavigation("52px", -100, 52, "https://mineru.test:8443", "https://mineru.test:8443");
console.log("HTML source-page controls respect the outline toolbar and viewport");
// 执行 PDF 溯源入口,用放大后的真实边界语义检查横纵两个方向的定位。