-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathaction-policy.ts
More file actions
299 lines (261 loc) · 7.85 KB
/
action-policy.ts
File metadata and controls
299 lines (261 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { readFileSync, statSync } from 'node:fs';
import { resolve } from 'node:path';
export interface ActionPolicy {
default: 'allow' | 'deny';
allow?: string[];
deny?: string[];
}
export type PolicyDecision = 'allow' | 'deny' | 'confirm';
const ACTION_CATEGORIES: Record<string, string> = {
navigate: 'navigate',
back: 'navigate',
forward: 'navigate',
reload: 'navigate',
tab_new: 'navigate',
click: 'click',
dblclick: 'click',
tap: 'click',
fill: 'fill',
type: 'fill',
// The `keyboard` action is a compound command that dispatches to sub-actions
// (type, inserttext, press, down, up). Its primary use is text input, so it
// maps to 'fill'. The interact-like sub-actions (press, down, up) are less
// common and don't have separate top-level action names in the protocol.
keyboard: 'fill',
inserttext: 'fill',
select: 'fill',
multiselect: 'fill',
check: 'fill',
uncheck: 'fill',
clear: 'fill',
selectall: 'fill',
setvalue: 'fill',
download: 'download',
waitfordownload: 'download',
upload: 'upload',
evaluate: 'eval',
evalhandle: 'eval',
addscript: 'eval',
addinitscript: 'eval',
snapshot: 'snapshot',
screenshot: 'snapshot',
pdf: 'snapshot',
diff_snapshot: 'snapshot',
diff_screenshot: 'snapshot',
diff_url: 'snapshot',
scroll: 'scroll',
scrollintoview: 'scroll',
wait: 'wait',
waitforurl: 'wait',
waitforloadstate: 'wait',
waitforfunction: 'wait',
gettext: 'get',
content: 'get',
innerhtml: 'get',
innertext: 'get',
inputvalue: 'get',
url: 'get',
title: 'get',
getattribute: 'get',
count: 'get',
boundingbox: 'get',
styles: 'get',
isvisible: 'get',
isenabled: 'get',
ischecked: 'get',
responsebody: 'get',
route: 'network',
unroute: 'network',
requests: 'network',
state_save: 'state',
state_load: 'state',
cookies_set: 'state',
storage_set: 'state',
credentials: 'state',
hover: 'interact',
focus: 'interact',
drag: 'interact',
press: 'interact',
keydown: 'interact',
keyup: 'interact',
mousemove: 'interact',
mousedown: 'interact',
mouseup: 'interact',
wheel: 'interact',
dispatch: 'interact',
// These are always allowed (internal/meta operations)
launch: '_internal',
close: '_internal',
tab_list: '_internal',
tab_switch: '_internal',
tab_close: '_internal',
window_new: '_internal',
frame: '_internal',
mainframe: '_internal',
dialog: '_internal',
session: '_internal',
console: '_internal',
errors: '_internal',
cookies_get: '_internal',
cookies_clear: '_internal',
storage_get: '_internal',
storage_clear: '_internal',
state_list: '_internal',
state_show: '_internal',
state_clear: '_internal',
state_clean: '_internal',
state_rename: '_internal',
highlight: '_internal',
bringtofront: '_internal',
trace_start: '_internal',
trace_stop: '_internal',
har_start: '_internal',
har_stop: '_internal',
video_start: '_internal',
video_stop: '_internal',
recording_start: '_internal',
recording_stop: '_internal',
recording_restart: '_internal',
profiler_start: '_internal',
profiler_stop: '_internal',
clipboard: '_internal',
viewport: '_internal',
useragent: '_internal',
device: '_internal',
geolocation: '_internal',
permissions: '_internal',
emulatemedia: '_internal',
offline: '_internal',
headers: '_internal',
addstyle: 'eval',
expose: 'eval',
timezone: '_internal',
locale: '_internal',
pause: '_internal',
setcontent: 'eval',
screencast_start: '_internal',
screencast_stop: '_internal',
input_mouse: '_internal',
input_keyboard: '_internal',
input_touch: '_internal',
webauthn_enable: '_internal',
webauthn_add_virtual_authenticator: '_internal',
auth_save: '_internal',
auth_login: '_internal',
auth_list: '_internal',
auth_delete: '_internal',
auth_show: '_internal',
confirm: '_internal',
deny: '_internal',
// Find/semantic locator actions (read-only element resolution)
getbyrole: 'get',
getbytext: 'get',
getbylabel: 'get',
getbyplaceholder: 'get',
getbyalttext: 'get',
getbytitle: 'get',
getbytestid: 'get',
nth: 'get',
};
// User-facing categories used in policy files. '_internal' is excluded because
// internal actions always bypass policy. 'unknown' is intentionally not a value
// in ACTION_CATEGORIES -- it is only the fallback return of getActionCategory()
// for unrecognized actions. If a user puts "unknown" in a policy file,
// loadPolicyFile will warn about it as unrecognized, which is correct.
export const KNOWN_CATEGORIES = new Set(
Object.values(ACTION_CATEGORIES).filter((c) => c !== '_internal')
);
export function getActionCategory(action: string): string {
return ACTION_CATEGORIES[action] ?? 'unknown';
}
export function loadPolicyFile(policyPath: string): ActionPolicy {
const resolved = resolve(policyPath);
const content = readFileSync(resolved, 'utf-8');
const policy = JSON.parse(content) as ActionPolicy;
if (policy.default !== 'allow' && policy.default !== 'deny') {
throw new Error(
`Invalid action policy: "default" must be "allow" or "deny", got "${policy.default}"`
);
}
for (const list of [policy.allow, policy.deny]) {
if (!list) continue;
for (const category of list) {
if (!KNOWN_CATEGORIES.has(category)) {
console.warn(
`[agent-browser] Warning: unrecognized action category "${category}" in policy file. ` +
`Known categories: ${[...KNOWN_CATEGORIES].sort().join(', ')}`
);
}
}
}
return policy;
}
let cachedPolicyPath: string | null = null;
let cachedPolicyMtimeMs = 0;
let cachedPolicy: ActionPolicy | null = null;
const RELOAD_CHECK_INTERVAL_MS = 5_000;
let lastCheckMs = 0;
export function initPolicyReloader(policyPath: string, policy: ActionPolicy): void {
cachedPolicyPath = resolve(policyPath);
cachedPolicyMtimeMs = statSync(cachedPolicyPath).mtimeMs;
cachedPolicy = policy;
}
export function reloadPolicyIfChanged(): ActionPolicy | null {
if (!cachedPolicyPath) return cachedPolicy;
const now = Date.now();
if (now - lastCheckMs < RELOAD_CHECK_INTERVAL_MS) return cachedPolicy;
lastCheckMs = now;
try {
const currentMtime = statSync(cachedPolicyPath).mtimeMs;
if (currentMtime !== cachedPolicyMtimeMs) {
cachedPolicy = loadPolicyFile(cachedPolicyPath);
cachedPolicyMtimeMs = currentMtime;
}
} catch {
// File may have been removed; keep using cached policy
}
return cachedPolicy;
}
export function checkPolicy(
action: string,
policy: ActionPolicy | null,
confirmCategories: Set<string>
): PolicyDecision {
const category = getActionCategory(action);
// Internal actions are always allowed
if (category === '_internal') return 'allow';
// Explicit deny takes precedence over confirmation
if (policy?.deny?.includes(category)) return 'deny';
// Check if this category requires confirmation
if (confirmCategories.has(category)) return 'confirm';
if (!policy) return 'allow';
// Explicit allow list
if (policy.allow?.includes(category)) return 'allow';
return policy.default;
}
export function describeAction(action: string, command: Record<string, unknown>): string {
const category = getActionCategory(action);
switch (action) {
case 'navigate':
return `Navigate to ${command.url}`;
case 'evaluate':
case 'evalhandle':
return `Evaluate JavaScript: ${String(command.script ?? '').slice(0, 80)}`;
case 'fill':
return `Fill ${command.selector}`;
case 'type':
return `Type into ${command.selector}`;
case 'click':
return `Click ${command.selector}`;
case 'dblclick':
return `Double-click ${command.selector}`;
case 'tap':
return `Tap ${command.selector}`;
case 'download':
return `Download via ${command.selector} to ${command.path}`;
case 'upload':
return `Upload files to ${command.selector}`;
default:
return `${category}: ${action}`;
}
}