omowright 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +282 -2
- package/TOOLS.md +366 -0
- package/bin/omowright-cloak.mjs +112 -0
- package/package.json +57 -4
- package/presets/agent-events/SKILL.md +75 -0
- package/presets/captcha/SKILL.md +129 -0
- package/presets/chrome/SKILL.md +84 -0
- package/presets/cloak-bridge/SKILL.md +43 -0
- package/presets/network/SKILL.md +126 -0
- package/presets/visual-browse/SKILL.md +103 -0
- package/skills/omowright/SKILL.md +193 -0
- package/skills/omowright/references/1password.md +118 -0
- package/skills/omowright/references/browserskill.md +186 -0
- package/skills/omowright/references/eval-kernel.md +100 -0
- package/skills/omowright/references/frames-layers-human.md +123 -0
- package/skills/omowright/references/quickstart.md +96 -0
- package/skills/omowright/references/stealth.md +174 -0
- package/src/agent-tabs.js +160 -0
- package/src/bridge-schemas.js +44 -0
- package/src/bridge-transport.js +57 -0
- package/src/browser-process.js +122 -0
- package/src/bsk/browser-signals.js +90 -0
- package/src/bsk/browsers.js +114 -0
- package/src/bsk/connect.js +45 -0
- package/src/bsk/daemon-info.js +100 -0
- package/src/bsk/external-extension.js +110 -0
- package/src/bsk/identify-browser.js +88 -0
- package/src/bsk/install-cli.js +81 -0
- package/src/bsk/ipc-client.js +236 -0
- package/src/bsk/onboard.js +133 -0
- package/src/bsk/screenshot.js +49 -0
- package/src/bsk/session.js +131 -0
- package/src/bsk/snapshot.js +56 -0
- package/src/bsk/targets.js +25 -0
- package/src/captcha-wait.js +74 -0
- package/src/captcha.js +142 -0
- package/src/cdp-errors.js +23 -0
- package/src/cdp-methods.js +61 -0
- package/src/chrome-profile.js +176 -0
- package/src/chrome.js +192 -0
- package/src/cloak-profile.js +266 -0
- package/src/connection.js +135 -0
- package/src/cookies.js +29 -0
- package/src/core.js +1710 -0
- package/src/cua.js +74 -0
- package/src/dialog-policy.js +80 -0
- package/src/emulate.js +127 -0
- package/src/events.js +111 -0
- package/src/frames-snapshot.js +138 -0
- package/src/har.js +103 -0
- package/src/human-handoff.js +122 -0
- package/src/index.js +86 -0
- package/src/injected.js +3 -0
- package/src/instance-lock.js +230 -0
- package/src/instances.js +493 -0
- package/src/internal-capability.js +1 -0
- package/src/launch-endpoint.js +185 -0
- package/src/layers.js +138 -0
- package/src/llm.js +201 -0
- package/src/lock-file.js +130 -0
- package/src/native-host-win32.js +201 -0
- package/src/network-events.js +201 -0
- package/src/network-snoop.js +229 -0
- package/src/open-shadow-query.js +22 -0
- package/src/owner-only.js +109 -0
- package/src/page-bundle.js +1397 -0
- package/src/page-routes.js +94 -0
- package/src/pipe.js +391 -0
- package/src/routes.js +256 -0
- package/src/running-browser.js +85 -0
- package/src/scroll-collect.js +73 -0
- package/src/serial-emitter.js +17 -0
- package/src/shared-connections.js +278 -0
- package/src/snapshot-diff.js +75 -0
- package/src/trace.js +183 -0
- package/src/url-match.js +148 -0
- package/src/viewport.js +12 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
const sessions = new WeakMap();
|
|
2
|
+
const streams = new WeakMap();
|
|
3
|
+
const METHODS = [
|
|
4
|
+
"Network.requestWillBeSent", "Network.responseReceived",
|
|
5
|
+
"Network.loadingFinished", "Network.loadingFailed",
|
|
6
|
+
];
|
|
7
|
+
|
|
8
|
+
// Readiness tracking and user-facing capture share ownership of the domain.
|
|
9
|
+
// A refused enable is not a page-setup failure (embedded transports may refuse it).
|
|
10
|
+
export function acquireNetwork(cdp, sessionId) {
|
|
11
|
+
let active = sessions.get(cdp);
|
|
12
|
+
if (!active) sessions.set(cdp, active = new Map());
|
|
13
|
+
let state = active.get(sessionId);
|
|
14
|
+
if (!state) {
|
|
15
|
+
state = { users: 0 };
|
|
16
|
+
active.set(sessionId, state);
|
|
17
|
+
state.ready = cdp.send("Network.enable", undefined, sessionId).catch(() => {});
|
|
18
|
+
}
|
|
19
|
+
state.users++;
|
|
20
|
+
let released = false;
|
|
21
|
+
const release = () => {
|
|
22
|
+
if (released) return;
|
|
23
|
+
released = true;
|
|
24
|
+
if (--state.users) return;
|
|
25
|
+
active.delete(sessionId);
|
|
26
|
+
// Order disable before a subsequent acquire's enable, even if the first
|
|
27
|
+
// enable has not completed yet.
|
|
28
|
+
cdp.send("Network.disable", undefined, sessionId).catch(() => {});
|
|
29
|
+
};
|
|
30
|
+
release.ready = state.ready;
|
|
31
|
+
return release;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class NetworkBodyUnavailableError extends Error {
|
|
35
|
+
constructor(message, cause) {
|
|
36
|
+
super(message, { cause });
|
|
37
|
+
this.name = "NetworkBodyUnavailableError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function headers(values = {}) {
|
|
42
|
+
return Object.fromEntries(Object.entries(values).map(([key, value]) => [key.toLowerCase(), String(value)]));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function bodyReaders(body) {
|
|
46
|
+
return {
|
|
47
|
+
body,
|
|
48
|
+
async text() { return (await body()).toString("utf8"); },
|
|
49
|
+
async json() { return JSON.parse(await this.text()); },
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function recordFor(cdp, sessionId, params) {
|
|
54
|
+
const request = params.request ?? {};
|
|
55
|
+
const finished = Promise.withResolvers();
|
|
56
|
+
let rawBody;
|
|
57
|
+
const record = {
|
|
58
|
+
exposed: false,
|
|
59
|
+
finish: finished.resolve,
|
|
60
|
+
rawBody() {
|
|
61
|
+
return rawBody ??= finished.promise.then(async failure => {
|
|
62
|
+
try {
|
|
63
|
+
if (failure) throw failure;
|
|
64
|
+
return await cdp.send("Network.getResponseBody", { requestId: params.requestId }, sessionId);
|
|
65
|
+
} catch (cause) {
|
|
66
|
+
throw new NetworkBodyUnavailableError(`Response body is unavailable for ${request.url}`, cause);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
record.request = {
|
|
72
|
+
url: () => request.url ?? params.documentURL ?? "",
|
|
73
|
+
method: () => request.method ?? "GET",
|
|
74
|
+
headers: () => headers(request.headers),
|
|
75
|
+
...bodyReaders(async () => Buffer.from(request.postData ?? "", "utf8")),
|
|
76
|
+
};
|
|
77
|
+
record.setResponse = response => {
|
|
78
|
+
record.response = {
|
|
79
|
+
url: () => response.url ?? record.request.url(),
|
|
80
|
+
method: record.request.method,
|
|
81
|
+
headers: () => headers(response.headers),
|
|
82
|
+
status: () => response.status,
|
|
83
|
+
...bodyReaders(async () => {
|
|
84
|
+
const result = await record.rawBody();
|
|
85
|
+
return Buffer.from(result.body ?? "", result.base64Encoded ? "base64" : "utf8");
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
return record;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// One CDP event path per page, shared by page events, snoops and traces.
|
|
93
|
+
// Network.enable itself is shared per session with the readiness tracker.
|
|
94
|
+
export function subscribeNetwork(page, handlers) {
|
|
95
|
+
let stream = streams.get(page);
|
|
96
|
+
if (!stream) {
|
|
97
|
+
const observers = new Set();
|
|
98
|
+
const inflight = new Map();
|
|
99
|
+
const unsubscribers = [];
|
|
100
|
+
let sessionId;
|
|
101
|
+
let release;
|
|
102
|
+
let closed = false;
|
|
103
|
+
const ready = Promise.resolve(page.resolveSessionId()).then(async id => {
|
|
104
|
+
sessionId = id;
|
|
105
|
+
if (!closed) {
|
|
106
|
+
release = acquireNetwork(page.cdp, id);
|
|
107
|
+
await release.ready;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
ready.catch(() => {});
|
|
111
|
+
const close = (force = false) => {
|
|
112
|
+
if (closed || (!force && (observers.size || inflight.size))) return;
|
|
113
|
+
closed = true;
|
|
114
|
+
for (const record of inflight.values()) record.finish(Error("Page network capture disposed"));
|
|
115
|
+
inflight.clear();
|
|
116
|
+
observers.clear();
|
|
117
|
+
for (const off of unsubscribers) off();
|
|
118
|
+
release?.();
|
|
119
|
+
streams.delete(page);
|
|
120
|
+
};
|
|
121
|
+
const dispatch = (method, params) => {
|
|
122
|
+
let record = inflight.get(params.requestId);
|
|
123
|
+
if (method === "Network.requestWillBeSent") {
|
|
124
|
+
// Redirects reuse request ids; the previous hop has no readable body.
|
|
125
|
+
record?.finish(Error("Request redirected"));
|
|
126
|
+
record = recordFor(page.cdp, sessionId, params);
|
|
127
|
+
inflight.set(params.requestId, record);
|
|
128
|
+
} else if (method === "Network.responseReceived") {
|
|
129
|
+
record?.setResponse(params.response ?? {});
|
|
130
|
+
} else {
|
|
131
|
+
record?.finish(method === "Network.loadingFailed" ? Error(params.errorText ?? "Request failed") : null);
|
|
132
|
+
}
|
|
133
|
+
for (const observer of observers) observer[method]?.(params, record);
|
|
134
|
+
if (method === "Network.loadingFinished" || method === "Network.loadingFailed") {
|
|
135
|
+
// Preserve bodies for returned response objects before the last
|
|
136
|
+
// listener releases the domain (disable clears the browser cache).
|
|
137
|
+
const done = record?.exposed && record.response
|
|
138
|
+
? record.rawBody().catch(() => {}) : Promise.resolve();
|
|
139
|
+
done.finally(() => {
|
|
140
|
+
if (inflight.get(params.requestId) === record) inflight.delete(params.requestId);
|
|
141
|
+
close();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
for (const method of METHODS) {
|
|
146
|
+
unsubscribers.push(page.cdp.on(method, (params, meta) => {
|
|
147
|
+
if (closed) return;
|
|
148
|
+
if (sessionId === undefined) {
|
|
149
|
+
ready.then(() => {
|
|
150
|
+
if (!closed && meta?.sessionId === sessionId) dispatch(method, params);
|
|
151
|
+
}).catch(() => {});
|
|
152
|
+
} else if (meta?.sessionId === sessionId) dispatch(method, params);
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
stream = { observers, close, inflight, ready };
|
|
156
|
+
streams.set(page, stream);
|
|
157
|
+
}
|
|
158
|
+
stream.observers.add(handlers);
|
|
159
|
+
return () => {
|
|
160
|
+
stream.observers.delete(handlers);
|
|
161
|
+
// Unobserved requests without a returned response need no further work.
|
|
162
|
+
for (const [id, record] of stream.inflight) {
|
|
163
|
+
if (!record.exposed && !stream.observers.size) {
|
|
164
|
+
record.finish(Error("Network capture disposed"));
|
|
165
|
+
stream.inflight.delete(id);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
stream.close();
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function disposeNetwork(page) {
|
|
173
|
+
streams.get(page)?.close(true);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function networkReady(page) {
|
|
177
|
+
return streams.get(page)?.ready;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function subscribePageNetworkEvents(page) {
|
|
181
|
+
const emit = (name, value) => page.events.emit(name, value)
|
|
182
|
+
.catch(error => console.warn("[network] page event listener failed", error));
|
|
183
|
+
return subscribeNetwork(page, {
|
|
184
|
+
"Network.requestWillBeSent": (_params, record) => emit("request", record.request),
|
|
185
|
+
"Network.responseReceived": (_params, record) => {
|
|
186
|
+
if (!record) return;
|
|
187
|
+
record.exposed = true;
|
|
188
|
+
emit("response", record.response);
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function networkPredicate(match) {
|
|
194
|
+
if (typeof match === "function") return match;
|
|
195
|
+
if (match instanceof RegExp) return value => {
|
|
196
|
+
match.lastIndex = 0;
|
|
197
|
+
return match.test(value.url());
|
|
198
|
+
};
|
|
199
|
+
if (typeof match === "string") return value => value.url() === match;
|
|
200
|
+
throw new TypeError("urlOrPredicate must be a URL string, RegExp or predicate");
|
|
201
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { subscribeNetwork } from "./network-events.js";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
4
|
+
const JSON_MIME = /^application\/json\s*(;|$)|\+json\s*(;|$)/i;
|
|
5
|
+
|
|
6
|
+
function matchesText(value, pattern) {
|
|
7
|
+
if (pattern === undefined) return true;
|
|
8
|
+
if (value === null || value === undefined) return false;
|
|
9
|
+
if (pattern instanceof RegExp) return pattern.test(String(value));
|
|
10
|
+
return String(value).includes(String(pattern));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function matchesToken(value, expected) {
|
|
14
|
+
if (expected === undefined) return true;
|
|
15
|
+
if (value === null || value === undefined) return false;
|
|
16
|
+
return String(value).toLowerCase() === String(expected).toLowerCase();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function toPredicate(match) {
|
|
20
|
+
if (match === undefined || match === null) return () => true;
|
|
21
|
+
if (typeof match === "function") return match;
|
|
22
|
+
if (typeof match !== "object") throw new TypeError("match must be a predicate function or a descriptor object");
|
|
23
|
+
return entry => matchesText(entry.url, match.url)
|
|
24
|
+
&& matchesToken(entry.method, match.method)
|
|
25
|
+
&& matchesText(entry.mimeType, match.mimeType)
|
|
26
|
+
&& matchesToken(entry.resourceType, match.resourceType);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function startedAtOf(params) {
|
|
30
|
+
const wallTime = Number(params?.wallTime);
|
|
31
|
+
return Number.isFinite(wallTime) && wallTime > 0 ? Math.round(wallTime * 1000) : Date.now();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function decodeBody(entry) {
|
|
35
|
+
if (typeof entry.body !== "string") return null;
|
|
36
|
+
return entry.base64Encoded ? Buffer.from(entry.body, "base64").toString("utf8") : entry.body;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Buffers CDP network traffic for a single page session.
|
|
41
|
+
*
|
|
42
|
+
* Entries are tracked while in flight and committed to the buffer once they finish
|
|
43
|
+
* (or fail), so filters may depend on response-only fields such as status or mimeType.
|
|
44
|
+
*/
|
|
45
|
+
export function createNetworkSnoop(page, options = {}) {
|
|
46
|
+
if (!page?.cdp || typeof page.resolveSessionId !== "function") {
|
|
47
|
+
throw new TypeError("createNetworkSnoop requires a page with cdp and resolveSessionId");
|
|
48
|
+
}
|
|
49
|
+
const { match, bodies = true, maxEntries = 500, maxBodyBytes = 5_000_000 } = options;
|
|
50
|
+
const wanted = toPredicate(match);
|
|
51
|
+
|
|
52
|
+
const inflight = new Map();
|
|
53
|
+
const buffer = [];
|
|
54
|
+
const waiters = new Set();
|
|
55
|
+
const handlers = {};
|
|
56
|
+
let disposed = false;
|
|
57
|
+
let queue = Promise.resolve();
|
|
58
|
+
|
|
59
|
+
function subscribe(method, handler) {
|
|
60
|
+
handlers[method] = handler;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function enqueue(task) {
|
|
64
|
+
queue = queue.then(task).catch(error => console.warn("[network-snoop] entry processing failed", error));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function loadBody(entry, record) {
|
|
68
|
+
if (entry.encodedDataLength > maxBodyBytes) {
|
|
69
|
+
entry.bodySkipped = true;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const result = await record.rawBody();
|
|
74
|
+
entry.body = result?.body ?? null;
|
|
75
|
+
entry.base64Encoded = Boolean(result?.base64Encoded);
|
|
76
|
+
} catch {
|
|
77
|
+
// Bodies expire with the network cache; keep the entry, drop the body.
|
|
78
|
+
entry.body = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function commit(entry) {
|
|
83
|
+
buffer.push(entry);
|
|
84
|
+
while (buffer.length > maxEntries) buffer.shift();
|
|
85
|
+
for (const waiter of [...waiters]) if (waiter.test(entry)) waiter.resolve(entry);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
subscribe("Network.requestWillBeSent", params => {
|
|
89
|
+
const requestId = params?.requestId;
|
|
90
|
+
if (!requestId) return;
|
|
91
|
+
inflight.set(requestId, {
|
|
92
|
+
requestId,
|
|
93
|
+
url: params.request?.url ?? params.documentURL ?? "",
|
|
94
|
+
method: params.request?.method ?? "GET",
|
|
95
|
+
resourceType: params.type ?? null,
|
|
96
|
+
status: null,
|
|
97
|
+
statusText: null,
|
|
98
|
+
mimeType: null,
|
|
99
|
+
requestHeaders: params.request?.headers ?? {},
|
|
100
|
+
responseHeaders: null,
|
|
101
|
+
remoteIPAddress: null,
|
|
102
|
+
postData: params.request?.postData ?? null,
|
|
103
|
+
encodedDataLength: 0,
|
|
104
|
+
body: null,
|
|
105
|
+
base64Encoded: false,
|
|
106
|
+
bodySkipped: false,
|
|
107
|
+
failed: false,
|
|
108
|
+
errorText: null,
|
|
109
|
+
startedAt: startedAtOf(params),
|
|
110
|
+
finishedAt: null,
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
subscribe("Network.responseReceived", params => {
|
|
115
|
+
const entry = inflight.get(params?.requestId);
|
|
116
|
+
if (!entry) return;
|
|
117
|
+
const response = params.response ?? {};
|
|
118
|
+
entry.status = response.status ?? null;
|
|
119
|
+
entry.statusText = response.statusText ?? "";
|
|
120
|
+
entry.mimeType = response.mimeType ?? null;
|
|
121
|
+
entry.responseHeaders = response.headers ?? {};
|
|
122
|
+
entry.remoteIPAddress = response.remoteIPAddress ?? null;
|
|
123
|
+
if (params.type) entry.resourceType = params.type;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
subscribe("Network.loadingFinished", (params, record) => {
|
|
127
|
+
const entry = inflight.get(params?.requestId);
|
|
128
|
+
if (!entry) return;
|
|
129
|
+
inflight.delete(entry.requestId);
|
|
130
|
+
entry.encodedDataLength = Number(params.encodedDataLength ?? 0);
|
|
131
|
+
entry.finishedAt = Date.now();
|
|
132
|
+
enqueue(async () => {
|
|
133
|
+
if (!wanted(entry)) return;
|
|
134
|
+
if (bodies) await loadBody(entry, record);
|
|
135
|
+
commit(entry);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
subscribe("Network.loadingFailed", params => {
|
|
140
|
+
const entry = inflight.get(params?.requestId);
|
|
141
|
+
if (!entry) return;
|
|
142
|
+
inflight.delete(entry.requestId);
|
|
143
|
+
entry.failed = true;
|
|
144
|
+
entry.errorText = params.errorText ?? "";
|
|
145
|
+
entry.encodedDataLength = Number(params.encodedDataLength ?? entry.encodedDataLength);
|
|
146
|
+
entry.finishedAt = Date.now();
|
|
147
|
+
enqueue(async () => { if (wanted(entry)) commit(entry); });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
function pop() {
|
|
151
|
+
return buffer.splice(0, buffer.length);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function peek() {
|
|
155
|
+
return [...buffer];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function popJson() {
|
|
159
|
+
const values = [];
|
|
160
|
+
for (const entry of pop()) {
|
|
161
|
+
if (!JSON_MIME.test(String(entry.mimeType ?? ""))) continue;
|
|
162
|
+
const text = decodeBody(entry);
|
|
163
|
+
if (text === null) continue;
|
|
164
|
+
try {
|
|
165
|
+
values.push(JSON.parse(text));
|
|
166
|
+
} catch {
|
|
167
|
+
// Truncated or non-JSON payload on a JSON content type: drop it silently.
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return values;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function waitFor(match, { timeoutMs = DEFAULT_TIMEOUT_MS } = {}) {
|
|
174
|
+
if (disposed) return Promise.reject(Error("Network snoop is disposed"));
|
|
175
|
+
const predicate = toPredicate(match);
|
|
176
|
+
return new Promise((resolve, reject) => {
|
|
177
|
+
let settled = false;
|
|
178
|
+
const waiter = {};
|
|
179
|
+
const finish = callback => value => {
|
|
180
|
+
if (settled) return;
|
|
181
|
+
settled = true;
|
|
182
|
+
clearTimeout(timer);
|
|
183
|
+
waiters.delete(waiter);
|
|
184
|
+
callback(value);
|
|
185
|
+
};
|
|
186
|
+
waiter.resolve = finish(resolve);
|
|
187
|
+
waiter.reject = finish(reject);
|
|
188
|
+
waiter.test = entry => {
|
|
189
|
+
try {
|
|
190
|
+
return Boolean(predicate(entry));
|
|
191
|
+
} catch (error) {
|
|
192
|
+
waiter.reject(error instanceof Error ? error : Error(String(error)));
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const timer = setTimeout(
|
|
197
|
+
() => waiter.reject(Error(`Timed out waiting for network response after ${timeoutMs}ms`)),
|
|
198
|
+
timeoutMs,
|
|
199
|
+
);
|
|
200
|
+
waiters.add(waiter);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function summary({ max = 50 } = {}) {
|
|
205
|
+
const shown = buffer.slice(0, max);
|
|
206
|
+
const lines = shown.map(entry => [
|
|
207
|
+
entry.method,
|
|
208
|
+
entry.status ?? (entry.failed ? "FAILED" : "-"),
|
|
209
|
+
entry.mimeType ?? "-",
|
|
210
|
+
`${entry.encodedDataLength}B`,
|
|
211
|
+
entry.url,
|
|
212
|
+
].join(" "));
|
|
213
|
+
const hidden = buffer.length - shown.length;
|
|
214
|
+
if (hidden > 0) lines.push(`... +${hidden} more`);
|
|
215
|
+
return lines.join("\n");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function dispose() {
|
|
219
|
+
if (disposed) return;
|
|
220
|
+
disposed = true;
|
|
221
|
+
unsubscribe();
|
|
222
|
+
for (const waiter of [...waiters]) waiter.reject(Error("Network snoop disposed"));
|
|
223
|
+
waiters.clear();
|
|
224
|
+
inflight.clear();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const unsubscribe = subscribeNetwork(page, handlers);
|
|
228
|
+
return { pop, peek, popJson, waitFor, summary, dispose };
|
|
229
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Self-contained because locator queries serialize this into the frame's page or isolated world.
|
|
2
|
+
// Order matches the CSS locator traversal: each scope's own matches, then its open shadow roots.
|
|
3
|
+
export function queryOpenShadowAll(root, selector) {
|
|
4
|
+
const scan = scope => {
|
|
5
|
+
const elements = scope.querySelectorAll("*");
|
|
6
|
+
const shadows = scope instanceof Element && scope.shadowRoot ? [scope.shadowRoot] : [];
|
|
7
|
+
for (let index = 0; index < elements.length; index++) {
|
|
8
|
+
const shadow = elements[index].shadowRoot;
|
|
9
|
+
if (shadow) shadows.push(shadow);
|
|
10
|
+
}
|
|
11
|
+
return { matches: selector === "*" ? elements : scope.querySelectorAll(selector), shadows };
|
|
12
|
+
};
|
|
13
|
+
const top = scan(root);
|
|
14
|
+
if (top.shadows.length === 0) return Array.from(top.matches);
|
|
15
|
+
const results = [];
|
|
16
|
+
const visit = ({ matches, shadows }) => {
|
|
17
|
+
for (let index = 0; index < matches.length; index++) results.push(matches[index]);
|
|
18
|
+
for (const shadow of shadows) visit(scan(shadow));
|
|
19
|
+
};
|
|
20
|
+
visit(top);
|
|
21
|
+
return results;
|
|
22
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Owner-only access for the registry, lock and CloakBrowser profile state.
|
|
2
|
+
// POSIX expresses it with mode bits. On Windows chmod only toggles the
|
|
3
|
+
// read-only attribute, so the same property is a protected DACL that grants the
|
|
4
|
+
// current user alone: inherited entries (the parent's grants to other users or
|
|
5
|
+
// groups) are dropped, and files created inside a directory inherit its grant.
|
|
6
|
+
// Explicit entries already on the object are kept, as macOS keeps a file's
|
|
7
|
+
// extended ACL through a chmod; on a fresh directory those are Windows' own
|
|
8
|
+
// SYSTEM and Administrators entries, the root equivalents.
|
|
9
|
+
// The Windows tools are spawned by absolute path: a bare name is resolved from
|
|
10
|
+
// the process cwd before PATH, and a cwd another user can write would let them
|
|
11
|
+
// substitute the tool. Any failure throws, so a caller never goes on with the
|
|
12
|
+
// inherited ACL in place. A volume without ACLs (FAT32, exFAT, some network
|
|
13
|
+
// shares) cannot hold the grant, so icacls fails there and so does the caller.
|
|
14
|
+
import { execFile } from "node:child_process";
|
|
15
|
+
import { chmod, mkdtemp, readFile, rm } from "node:fs/promises";
|
|
16
|
+
import { tmpdir } from "node:os";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { promisify } from "node:util";
|
|
19
|
+
|
|
20
|
+
const execFileAsync = promisify(execFile);
|
|
21
|
+
const WINDOWS_COMMAND_TIMEOUT_MS = 15_000;
|
|
22
|
+
// Directories this process restricted or found restricted, by lower-cased path.
|
|
23
|
+
const ownerOnlyDirectories = new Set();
|
|
24
|
+
let currentUserSid;
|
|
25
|
+
|
|
26
|
+
export function windowsSystemTool(name, env = process.env) {
|
|
27
|
+
return path.win32.join(env.SystemRoot || env.windir || "C:\\Windows", "System32", name);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function readCurrentUserSid(run) {
|
|
31
|
+
const { stdout } = await run(windowsSystemTool("whoami.exe"), ["/user", "/fo", "csv", "/nh"], {
|
|
32
|
+
timeout: WINDOWS_COMMAND_TIMEOUT_MS,
|
|
33
|
+
windowsHide: true,
|
|
34
|
+
});
|
|
35
|
+
const sid = /"(S-1-[\d-]+)"\s*$/.exec(String(stdout).trim())?.[1];
|
|
36
|
+
if (!sid) throw new Error(`cannot read the current user's SID from whoami: ${String(stdout).trim()}`);
|
|
37
|
+
return sid;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function cachedCurrentUserSid() {
|
|
41
|
+
currentUserSid ??= readCurrentUserSid(execFileAsync).catch((error) => {
|
|
42
|
+
currentUserSid = undefined;
|
|
43
|
+
throw error;
|
|
44
|
+
});
|
|
45
|
+
return currentUserSid;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The directory's DACL as SDDL. `icacls /save` without /T reads this one
|
|
49
|
+
// object only and writes it, as UTF-16, to a file.
|
|
50
|
+
async function readDirectoryDacl(dir, run) {
|
|
51
|
+
const scratch = await mkdtemp(path.join(tmpdir(), "omowright-acl-"));
|
|
52
|
+
try {
|
|
53
|
+
const saved = path.join(scratch, "acl");
|
|
54
|
+
await run(windowsSystemTool("icacls.exe"), [dir, "/save", saved, "/Q"], {
|
|
55
|
+
timeout: WINDOWS_COMMAND_TIMEOUT_MS,
|
|
56
|
+
windowsHide: true,
|
|
57
|
+
});
|
|
58
|
+
const bytes = await readFile(saved);
|
|
59
|
+
const text = bytes.length > 1 && bytes[1] === 0 ? bytes.toString("utf16le") : bytes.toString("utf8");
|
|
60
|
+
return /D:[^\r\n]*/.exec(text)?.[0] ?? "";
|
|
61
|
+
} finally {
|
|
62
|
+
await rm(scratch, { recursive: true, force: true });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// True when the DACL is already what the directory grant below leaves:
|
|
67
|
+
// protected, with the user's one entry being the inheritable full-control
|
|
68
|
+
// allow. SDDL names the built-in Administrator and Guest accounts LA and LG.
|
|
69
|
+
function isOwnerOnlyDirectoryDacl(dacl, sid) {
|
|
70
|
+
const match = /^D:([A-Z]*)((?:\([^()]*\))*)/.exec(dacl);
|
|
71
|
+
if (!match || !match[1].includes("P")) return false;
|
|
72
|
+
const isUser = (trustee) =>
|
|
73
|
+
trustee === sid || (trustee === "LA" && sid.endsWith("-500")) || (trustee === "LG" && sid.endsWith("-501"));
|
|
74
|
+
const own = [...match[2].matchAll(/\(([^()]*)\)/g)].map(([, ace]) => ace.split(";")).filter((fields) => isUser(fields[5]));
|
|
75
|
+
return own.length === 1 && own[0][0] === "A" && own[0][1] === "OICI" && own[0][2] === "FA";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// `platform` and `run` let the security contract above (absolute tool paths,
|
|
79
|
+
// fail closed) be tested off Windows; production callers pass neither.
|
|
80
|
+
export async function restrictToOwner(target, kind, { platform = process.platform, run } = {}) {
|
|
81
|
+
if (platform !== "win32") {
|
|
82
|
+
await chmod(target, kind === "directory" ? 0o700 : 0o600);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const command = run ?? execFileAsync;
|
|
86
|
+
const sid = run ? await readCurrentUserSid(run) : await cachedCurrentUserSid();
|
|
87
|
+
// Re-granting an inheritable entry makes Windows rewrite the inherited
|
|
88
|
+
// entries of everything below the directory, thousands of files in a used
|
|
89
|
+
// browser profile, so a directory that already holds the grant is left as is.
|
|
90
|
+
if (kind === "directory" && isOwnerOnlyDirectoryDacl(await readDirectoryDacl(target, command), sid)) {
|
|
91
|
+
ownerOnlyDirectories.add(path.win32.resolve(target).toLowerCase());
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const grant = kind === "directory" ? `*${sid}:(OI)(CI)F` : `*${sid}:F`;
|
|
95
|
+
await command(windowsSystemTool("icacls.exe"), [target, "/inheritance:r", "/grant:r", grant, "/Q"], {
|
|
96
|
+
timeout: WINDOWS_COMMAND_TIMEOUT_MS,
|
|
97
|
+
windowsHide: true,
|
|
98
|
+
});
|
|
99
|
+
if (kind === "directory") ownerOnlyDirectories.add(path.win32.resolve(target).toLowerCase());
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// For a file the caller has just created. On Windows a file created inside a
|
|
103
|
+
// directory restrictToOwner handled in this process inherits that directory's
|
|
104
|
+
// owner-only grant at creation, so no tool runs for it.
|
|
105
|
+
export async function restrictNewFileToOwner(target, options = {}) {
|
|
106
|
+
const platform = options.platform ?? process.platform;
|
|
107
|
+
if (platform === "win32" && ownerOnlyDirectories.has(path.win32.dirname(path.win32.resolve(target)).toLowerCase())) return;
|
|
108
|
+
await restrictToOwner(target, "file", options);
|
|
109
|
+
}
|