openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
141 lines (140 loc) • 4.82 kB
JavaScript
import { StringDecoder } from "node:string_decoder";
//#region extensions/browser/src/browser/cdp-ax.ts
function axValue(e) {
if (!e || typeof e !== "object" || !("value" in e)) return "";
const t = e.value;
if (typeof t === "string") return t;
if (typeof t === "number" || typeof t === "boolean") return String(t);
return "";
}
//#endregion
//#region extensions/browser/src/browser/sensitive-field-values.ts
const REDACTED_FIELD_VALUE = "[redacted]";
/** Roles whose snapshot entries can carry a form field value. */
const VALUE_FIELD_ROLES = /* @__PURE__ */ new Set([
"combobox",
"searchbox",
"spinbutton",
"textbox"
]);
const SENSITIVE_AUTOCOMPLETE_TOKENS = /* @__PURE__ */ new Set([
"cc-csc",
"cc-number",
"current-password",
"new-password",
"one-time-code"
]);
function parseFieldDescriptor(e) {
if (!e || typeof e !== "object") return null;
const { tagName: t, type: n, autocomplete: r } = e;
if (typeof t !== "string" || typeof n !== "string" && n !== null || typeof r !== "string" && r !== null) return null;
return {
tagName: t,
type: n,
autocomplete: r
};
}
function isSensitiveFieldDescriptor(e) {
if (e.tagName.toLowerCase() === "input" && e.type?.toLowerCase() === "password") return true;
return (e.autocomplete ?? "").toLowerCase().split(/\s+/).some((e) => SENSITIVE_AUTOCOMPLETE_TOKENS.has(e));
}
/** Return valued form fields from a structured snapshot in snapshot order. */
function findValuedSnapshotFields(e) {
const t = [];
const n = [e];
for (let e = n.pop(); e; e = n.pop()) {
if (e.value !== void 0 && String(e.value).trim() && VALUE_FIELD_ROLES.has(e.role?.trim().toLowerCase() ?? "")) t.push(e);
for (const t of e.children?.toReversed() ?? []) n.push(t);
}
return t;
}
/** Mask a structured snapshot field value and accessibility text descendants. */
function redactSnapshotFieldValue(e) {
const t = [e];
const n = /* @__PURE__ */ new Set();
for (let r = t.pop(); r; r = t.pop()) {
if (n.has(r)) continue;
n.add(r);
if (r.value !== void 0 && String(r.value).trim()) r.value = REDACTED_FIELD_VALUE;
if (r !== e && r.name?.trim()) r.name = REDACTED_FIELD_VALUE;
for (const e of r.children ?? []) t.push(e);
}
}
async function describeFieldViaCdp(e, t) {
const n = (await e("DOM.describeNode", { backendNodeId: t }).catch(() => null))?.node;
if (!n?.nodeName) return null;
const r = n.attributes ?? [];
const attribute = (e) => {
for (let t = 0; t + 1 < r.length; t += 2) if (r[t].toLowerCase() === e) return r[t + 1];
};
return {
tagName: n.nodeName,
type: attribute("type"),
autocomplete: attribute("autocomplete")
};
}
/**
* Mask sensitive field values in captured AX nodes before any snapshot format renders them.
* Chromium masks type=password values but not one-time-code or card fields, and full trees
* repeat the value as StaticText/InlineTextBox descendants. A valued field whose DOM node
* cannot be described is masked.
*/
async function redactSensitiveAXNodeValues(e, t) {
const n = new Map(t.map((e) => [e.nodeId, e]));
const redact = (e) => {
const t = [e];
const r = /* @__PURE__ */ new Set();
for (let i = t.pop(); i; i = t.pop()) {
if (r.has(i)) continue;
r.add(i);
if (axValue(i.value)) i.value = { value: REDACTED_FIELD_VALUE };
if (i !== e && axValue(i.name)) i.name = { value: REDACTED_FIELD_VALUE };
for (const e of i.childIds ?? []) {
const r = n.get(e);
if (r) t.push(r);
}
}
};
await Promise.all(t.filter((e) => axValue(e.value) && VALUE_FIELD_ROLES.has(axValue(e.role).toLowerCase())).map(async (t) => {
const n = t.backendDOMNodeId ? await describeFieldViaCdp(e, t.backendDOMNodeId) : null;
if (!n || isSensitiveFieldDescriptor(n)) redact(t);
}));
}
//#endregion
//#region extensions/browser/src/browser/bounded-utf8-tail.ts
/** Byte-bounded UTF-8 tail storage for browser subprocess diagnostics. */
function decodeUtf8Tail(e) {
let t = 0;
while (t < e.length && (e[t] & 192) === 128) t += 1;
return new StringDecoder("utf8").write(e.subarray(t));
}
function createBoundedUtf8Tail(e) {
const t = Buffer.allocUnsafe(Math.max(0, e));
let n = 0;
return {
append(r) {
const i = Buffer.isBuffer(r) ? r : Buffer.from(r);
if (i.length === 0 || e <= 0) return;
if (i.length >= e) {
i.copy(t, 0, i.length - e);
n = e;
return;
}
const a = Math.max(0, n + i.length - e);
if (a > 0) {
t.copyWithin(0, a, n);
n -= a;
}
i.copy(t, n);
n += i.length;
},
text() {
return decodeUtf8Tail(t.subarray(0, n));
},
clear() {
n = 0;
}
};
}
//#endregion
export { isSensitiveFieldDescriptor as a, redactSnapshotFieldValue as c, findValuedSnapshotFields as i, axValue as l, REDACTED_FIELD_VALUE as n, parseFieldDescriptor as o, VALUE_FIELD_ROLES as r, redactSensitiveAXNodeValues as s, createBoundedUtf8Tail as t };