Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(browser): 转义提取脚本中的标签和行分隔符

Dash743247e84681
diff --git a/packages/browser-control-runtime/src/dom-read.ts b/packages/browser-control-runtime/src/dom-read.ts
index abadb25ed5..f54d7e29de 100644
--- a/packages/browser-control-runtime/src/dom-read.ts
+++ b/packages/browser-control-runtime/src/dom-read.ts
@@ -101,6 +101,20 @@ function normalizeFields(
return out;
}
+/** JSON quoting plus script-delimiter/line-separator escaping for JS source. */
+function javascriptLiteral(value: unknown): string {
+ return JSON.stringify(value).replace(
+ /[<>\u2028\u2029]/g,
+ (char) =>
+ ({
+ "<": "\\u003c",
+ ">": "\\u003e",
+ "\u2028": "\\u2028",
+ "\u2029": "\\u2029",
+ })[char]!,
+ );
+}
+
/**
* Compile an extract spec into a zero-arg JS function source string suitable for
* `act:evaluate`. The function runs in page context and returns
@@ -115,8 +129,8 @@ export function buildExtractFnSource(spec: ExtractSpec): string {
};
// The whole spec + hint are embedded as JSON literals — selectors/attrs are
// data, not code, so there is no injection surface.
- const specLiteral = JSON.stringify(normalized);
- const hintLiteral = JSON.stringify(EXTRACT_FIELD_HINT);
+ const specLiteral = javascriptLiteral(normalized);
+ const hintLiteral = javascriptLiteral(EXTRACT_FIELD_HINT);
// querySelector is guarded: an invalid selector (e.g. natural language, or the
// "h3 a@title" attr-in-selector mistake) is collected into `bad` and reported
// as ok:false + hint, instead of throwing and killing the whole extract.
diff --git a/packages/browser-control-runtime/upstream/MAINTAINING.md b/packages/browser-control-runtime/upstream/MAINTAINING.md
index 66f186cac6..4390d064d5 100644
--- a/packages/browser-control-runtime/upstream/MAINTAINING.md
+++ b/packages/browser-control-runtime/upstream/MAINTAINING.md
@@ -199,6 +199,7 @@ pnpm --filter @cindy/browser-control-runtime test # 契约 + SSRF guard
### 固定 DOM 读取的生命周期边界(2026-10-27)
+- 生成提取函数时,在 JSON 引号转义之外,对 `<`、`>` 和 U+2028/U+2029 做 JavaScript 字面量转义;参数还原为原值,避免脚本分隔符混入生成源码。保持 `Runtime.evaluate` 的浏览器原生执行超时及宿主总时限。
- 无 guard 的配方 extract 与直接 extract 使用相同结构化读取。含 guard 的配方合并身份校验与提取后,必须删除 `domRead` 元数据,保持普通 evaluate 路径,避免 runtime 忽略合并函数而绕过 guard。
- 读取沿用已有弹窗中断信号,并给 CDP 命令设置总时限。弹窗期间 Chrome 可能暂停 detach:及时返回 `blockedByDialog`,原 scoped session 的 finally 在 Chrome 恢复后完成释放,不新增连接缓存或重试。
- 读取前后的主 frame `loaderId` 必须一致,防止同网址刷新后返回旧文档的结果;页面 URL 与导航策略检查仍保留。同网址内的动态内容没有通用“已加载”判据,调用方仍需等待真实业务文字或元素。
diff --git a/packages/lizi-mcps/src/browser/extract.test.ts b/packages/lizi-mcps/src/browser/extract.test.ts
index cd37d9fd6e..e95fa2d017 100644
--- a/packages/lizi-mcps/src/browser/extract.test.ts
+++ b/packages/lizi-mcps/src/browser/extract.test.ts
@@ -26,6 +26,28 @@ describe('buildExtractFnSource', () => {
expect(src).toContain(JSON.stringify('a[href="x"]:not(.y)'));
});
+ // CodeQL #423: JSON quoting alone leaves script delimiters and JS line
+ // separators in executable source. Exercise the generated function as well
+ // as its code/data boundary; selector and attribute values must round-trip.
+ it('escapes script delimiters and line separators without altering DOM read data', () => {
+ const payload = '</script><script>throw new Error("injected")</script>\u2028\u2029';
+ const src = buildExtractFnSource({ fields: { value: { selector: payload, attr: payload } } });
+ expect(src).not.toMatch(/<\/?script>|[\u2028\u2029]/u);
+ const received: string[] = [];
+ const g = globalThis as unknown as { document?: unknown };
+ const previous = g.document;
+ g.document = { querySelector: (selector: string) => {
+ received.push(selector);
+ return { getAttribute: (attribute: string) => { received.push(attribute); return 'read result'; } };
+ } };
+ try {
+ expect(compile(src)()).toEqual({ ok: true, count: 1, records: [{ value: 'read result' }] });
+ expect(received).toEqual([payload, payload]);
+ } finally {
+ g.document = previous;
+ }
+ });
+
it('expands a string field shorthand to a text-type field', () => {
const src = buildExtractFnSource({ fields: { title: '.t' } });
expect(src).toContain('"title":{"selector":".t","type":"text"}');