@namzu/browser 0.1.1 → 2.0.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/dist/host.js CHANGED
@@ -4,7 +4,7 @@ import { chromium, } from 'playwright-core';
4
4
  import { classifyHumanRequired, isCredentialField, } from './classifier.js';
5
5
  import { detectBrowserEnvironment, runnableBrowserPlan, } from './detect.js';
6
6
  import { BrowserHumanRequiredError, BrowserOriginMismatchError, BrowserOutcomeUnknownError, BrowserSiteDeniedError, BrowserStaleRefError, BrowserUnavailableError, ProfileBusyError, } from './errors.js';
7
- import { countCredentialFields, fieldFacts, focusedFieldFacts, } from './page-scripts.js';
7
+ import { countCredentialFields, fieldFacts } from './page-scripts.js';
8
8
  import { BrowserSitePolicy, DEFAULT_BROWSER_SITE_RULES } from './policy.js';
9
9
  import { BrowserLeaseStore, BrowserProfileError, BrowserProfileStore, DEFAULT_BROWSER_PROFILE, } from './profiles.js';
10
10
  import { PLAYWRIGHT_CORE_VERSION, SnapshotPager, captureAriaTree, locateRef, renderAriaTree, } from './snapshot.js';
@@ -19,6 +19,8 @@ const SNAPSHOT_PAGE_MAX = 20_000;
19
19
  const EVENT_SETTLE_MS = 150;
20
20
  /** Smallest frame that can hold a CAPTCHA a person is meant to solve. */
21
21
  const CAPTCHA_FRAME_MIN_PX = 30;
22
+ const REMEMBERED_DOCUMENTS_MAX = 64;
23
+ const SCREENSHOT_INPUT_MASK_COLOR = '#666666';
22
24
  function firstLine(error) {
23
25
  const text = error instanceof Error ? error.message : String(error);
24
26
  return (text.split('\n')[0] ?? '').slice(0, 300);
@@ -51,8 +53,6 @@ function quoteShort(value, max = 80) {
51
53
  const flat = value.replace(/\s+/g, ' ').trim();
52
54
  return JSON.stringify(flat.length > max ? `${flat.slice(0, max - 1)}…` : flat);
53
55
  }
54
- /** Keys that type nothing: allowed while a credential field has focus. */
55
- const NON_TYPING_KEYS = new Set(['Tab', 'Shift+Tab', 'Escape', 'Enter']);
56
56
  /**
57
57
  * A {@link BrowserHost} that runs Chromium (or an installed Chrome or Edge)
58
58
  * in this process with Playwright, on a persistent profile. Inside WSL it
@@ -73,7 +73,7 @@ const NON_TYPING_KEYS = new Set(['Tab', 'Shift+Tab', 'Escape', 'Enter']);
73
73
  * anything else is blocked before it is sent; a redirect is caught when it
74
74
  * lands, and the tab is cleared to `about:blank`;
75
75
  * - acting needs the live origin at `ask` or `act`;
76
- * - a sign-in, second factor, CAPTCHA, bot wall or HTTP credential prompt
76
+ * - a sign-in, second factor, CAPTCHA, bot wall or HTTP authentication challenge
77
77
  * stops the call with `browser_human_required`;
78
78
  * - nothing is ever typed into a password or one-time-code field;
79
79
  * - downloads are cancelled and reported; a leave-page prompt is answered so
@@ -332,7 +332,13 @@ export class PlaywrightBrowserHost {
332
332
  if (known)
333
333
  return known;
334
334
  this.tabCounter += 1;
335
- const tab = { id: `t${this.tabCounter}`, page };
335
+ const tab = {
336
+ id: `t${this.tabCounter}`,
337
+ page,
338
+ mainFrameCommits: 0,
339
+ mainDocumentResponses: 0,
340
+ responsesByLoader: new Map(),
341
+ };
336
342
  this.tabs.set(tab.id, tab);
337
343
  this.tabOf.set(page, tab);
338
344
  if (this.activeTabId === undefined)
@@ -341,13 +347,19 @@ export class PlaywrightBrowserHost {
341
347
  this.notes.push(`The page opened a new tab, ${tab.id}; it is not the active tab. Use tabs list or select to look at it.`);
342
348
  }
343
349
  page.on('framenavigated', (frame) => {
344
- if (frame === page.mainFrame())
350
+ if (frame === page.mainFrame()) {
351
+ tab.mainFrameCommits += 1;
345
352
  this.checkLanding(tab);
353
+ }
346
354
  });
347
355
  page.on('response', (response) => {
348
356
  try {
349
357
  if (response.request().isNavigationRequest() && response.frame() === page.mainFrame()) {
358
+ const headers = response.headers();
359
+ tab.mainDocumentResponses += 1;
350
360
  tab.status = response.status();
361
+ tab.wwwAuthenticate = headers['www-authenticate'];
362
+ tab.proxyAuthenticate = headers['proxy-authenticate'];
351
363
  }
352
364
  }
353
365
  catch {
@@ -382,7 +394,7 @@ export class PlaywrightBrowserHost {
382
394
  return;
383
395
  }
384
396
  this.pendingDialog = { tab, dialog };
385
- this.notes.push(`A ${dialog.type()} dialog is open on tab ${tab.id}. Take a snapshot to read it, then answer it with browser_act dialog.`);
397
+ this.notes.push(`A ${dialog.type()} dialog is open on tab ${tab.id}. Take a snapshot to read it, then dismiss it with browser_act dialog or ask the operator to use the browser window.`);
386
398
  }
387
399
  /** The route handler: stop top-level navigations the policy does not allow. */
388
400
  async screen(route) {
@@ -464,7 +476,12 @@ export class PlaywrightBrowserHost {
464
476
  }
465
477
  async pageInfo(tab) {
466
478
  const url = tab.page.url();
467
- return { origin: originOf(url), url, title: await this.title(tab), tab: tab.id };
479
+ return {
480
+ origin: originOf(url),
481
+ url,
482
+ title: await this.title(tab),
483
+ tab: tab.id,
484
+ };
468
485
  }
469
486
  result(extra) {
470
487
  const message = this.drainNotes();
@@ -514,11 +531,114 @@ export class PlaywrightBrowserHost {
514
531
  url,
515
532
  title: await this.title(tab),
516
533
  ...(tab.status !== undefined ? { status: tab.status } : {}),
534
+ ...(tab.wwwAuthenticate !== undefined ? { wwwAuthenticate: tab.wwwAuthenticate } : {}),
535
+ ...(tab.proxyAuthenticate !== undefined
536
+ ? { proxyAuthenticate: tab.proxyAuthenticate }
537
+ : {}),
517
538
  ...counts,
518
539
  frameUrls,
519
540
  }, this.options.signInAddresses ? { signInAddresses: this.options.signInAddresses } : {});
520
541
  if (reason)
521
542
  throw this.humanRequired(reason, tab);
543
+ if (tab.status === 401) {
544
+ throw new Error('The site returned HTTP 401 without a WWW-Authenticate challenge. Access was denied or the response is incomplete; no HTTP password prompt was identified. Tell the user the site refused this request.');
545
+ }
546
+ if (tab.status === 407) {
547
+ throw new Error('The proxy returned HTTP 407 without a Proxy-Authenticate challenge. Access was denied or the response is incomplete; no proxy password prompt was identified. Tell the user the proxy refused this request.');
548
+ }
549
+ }
550
+ documentResponse(tab) {
551
+ return {
552
+ status: tab.status,
553
+ wwwAuthenticate: tab.wwwAuthenticate,
554
+ proxyAuthenticate: tab.proxyAuthenticate,
555
+ };
556
+ }
557
+ clearDocumentResponse(tab) {
558
+ tab.status = undefined;
559
+ tab.wwwAuthenticate = undefined;
560
+ tab.proxyAuthenticate = undefined;
561
+ }
562
+ setDocumentResponse(tab, response) {
563
+ tab.status = response.status;
564
+ tab.wwwAuthenticate = response.wwwAuthenticate;
565
+ tab.proxyAuthenticate = response.proxyAuthenticate;
566
+ }
567
+ /** Chromium's loader ID belongs to a document, and survives same-document history moves. */
568
+ async loaderId(tab) {
569
+ try {
570
+ tab.cdp ??= await tab.page.context().newCDPSession(tab.page);
571
+ const tree = (await tab.cdp.send('Page.getFrameTree'));
572
+ const id = tree.frameTree?.frame?.loaderId;
573
+ return typeof id === 'string' && id.length > 0 ? id : undefined;
574
+ }
575
+ catch {
576
+ tab.cdp = undefined;
577
+ return undefined;
578
+ }
579
+ }
580
+ rememberDocumentResponse(tab, loaderId, response) {
581
+ tab.responsesByLoader.delete(loaderId);
582
+ tab.responsesByLoader.set(loaderId, response);
583
+ if (tab.responsesByLoader.size > REMEMBERED_DOCUMENTS_MAX) {
584
+ const oldest = tab.responsesByLoader.keys().next().value;
585
+ if (oldest !== undefined)
586
+ tab.responsesByLoader.delete(oldest);
587
+ }
588
+ }
589
+ async beginDocumentResponseChange(tab) {
590
+ const loaderId = await this.loaderId(tab);
591
+ const previous = this.documentResponse(tab);
592
+ if (loaderId !== undefined)
593
+ this.rememberDocumentResponse(tab, loaderId, previous);
594
+ const checkpoint = {
595
+ previous,
596
+ ...(loaderId !== undefined ? { loaderId } : {}),
597
+ responsesBefore: tab.mainDocumentResponses,
598
+ commitsBefore: tab.mainFrameCommits,
599
+ };
600
+ this.clearDocumentResponse(tab);
601
+ return checkpoint;
602
+ }
603
+ async finishDocumentResponseChange(tab, checkpoint) {
604
+ if (this.pendingDialog?.tab === tab) {
605
+ // CDP page queries wait behind an open JavaScript dialog. The page
606
+ // cannot commit a navigation until the dialog is answered.
607
+ this.pendingDialog.checkpoint = checkpoint;
608
+ this.setDocumentResponse(tab, checkpoint.previous);
609
+ return;
610
+ }
611
+ const currentLoader = await this.loaderId(tab);
612
+ const freshResponse = tab.mainDocumentResponses !== checkpoint.responsesBefore;
613
+ const committed = (currentLoader !== undefined &&
614
+ checkpoint.loaderId !== undefined &&
615
+ currentLoader !== checkpoint.loaderId) ||
616
+ tab.mainFrameCommits !== checkpoint.commitsBefore;
617
+ if (currentLoader !== undefined && currentLoader === checkpoint.loaderId) {
618
+ // A hash change, pushState, no-op, or failed request left this document in place.
619
+ this.setDocumentResponse(tab, checkpoint.previous);
620
+ }
621
+ else if (freshResponse && committed) {
622
+ // A new main-document response and a committed navigation agree.
623
+ }
624
+ else if (freshResponse) {
625
+ this.setDocumentResponse(tab, checkpoint.previous);
626
+ throw new Error('The browser received a document response but could not confirm that it replaced the current page. Its access status is unknown; do not treat this as a successful page load.');
627
+ }
628
+ else if (!freshResponse && currentLoader && tab.responsesByLoader.has(currentLoader)) {
629
+ // Back-forward cache restores a document without another response event.
630
+ this.setDocumentResponse(tab, tab.responsesByLoader.get(currentLoader));
631
+ }
632
+ else if (!freshResponse && !committed) {
633
+ // CDP can be unavailable while the browser is closing; no frame moved.
634
+ this.setDocumentResponse(tab, checkpoint.previous);
635
+ }
636
+ else if (!freshResponse && originOf(tab.page.url()) !== 'null') {
637
+ throw new Error('The browser moved to a page without a response that could be verified. Its access status is unknown; do not treat this as a successful page load.');
638
+ }
639
+ if (currentLoader !== undefined) {
640
+ this.rememberDocumentResponse(tab, currentLoader, this.documentResponse(tab));
641
+ }
522
642
  }
523
643
  // -------------------------------------------------------------------------
524
644
  // Calls
@@ -578,15 +698,21 @@ export class PlaywrightBrowserHost {
578
698
  this.policy.approve(verdict.origin);
579
699
  }
580
700
  await this.dismissPendingDialog(tab);
581
- tab.status = undefined;
701
+ const checkpoint = await this.beginDocumentResponseChange(tab);
582
702
  try {
583
703
  await tab.page.goto(verdict.url, { waitUntil: 'domcontentloaded' });
584
704
  }
585
705
  catch (error) {
706
+ await this.finishDocumentResponseChange(tab, checkpoint).catch(() => undefined);
707
+ if (/ERR_UNEXPECTED_PROXY_AUTH/.test(firstLine(error))) {
708
+ await this.settle();
709
+ throw new Error('The browser refused an HTTP 407 proxy response before the page loaded. Proxy access is unavailable or its response is invalid; no usable password prompt was identified. Tell the user the proxy refused this request.');
710
+ }
586
711
  if (!/ERR_BLOCKED_BY_CLIENT|ERR_ABORTED|Download is starting/.test(firstLine(error))) {
587
712
  throw new Error(`Could not open ${verdict.url}: ${firstLine(error)}`);
588
713
  }
589
714
  }
715
+ await this.finishDocumentResponseChange(tab, checkpoint);
590
716
  return this.afterNavigation(tab);
591
717
  }
592
718
  async afterNavigation(tab) {
@@ -599,7 +725,7 @@ export class PlaywrightBrowserHost {
599
725
  async history(which) {
600
726
  const tab = await this.activeTab();
601
727
  await this.dismissPendingDialog(tab);
602
- tab.status = undefined;
728
+ const checkpoint = await this.beginDocumentResponseChange(tab);
603
729
  // `commit`, then the document as a bonus: a page restored from the
604
730
  // back-forward cache fires no `domcontentloaded`, so waiting for it
605
731
  // timed out after a history move that had long finished (measured
@@ -617,9 +743,11 @@ export class PlaywrightBrowserHost {
617
743
  : await tab.page.reload(options);
618
744
  }
619
745
  catch (error) {
746
+ await this.finishDocumentResponseChange(tab, checkpoint).catch(() => undefined);
620
747
  if (!/ERR_BLOCKED_BY_CLIENT|ERR_ABORTED/.test(firstLine(error)))
621
748
  throw error;
622
749
  }
750
+ await this.finishDocumentResponseChange(tab, checkpoint);
623
751
  if (response !== null) {
624
752
  await tab.page
625
753
  .waitForLoadState('domcontentloaded', { timeout: HISTORY_LOAD_WAIT_MS })
@@ -709,11 +837,24 @@ export class PlaywrightBrowserHost {
709
837
  async screenshot(ref, fullPage) {
710
838
  const tab = await this.activeTab();
711
839
  if (this.pendingDialog?.tab === tab) {
712
- throw new Error('A dialog is open; answer it with browser_act dialog before taking a screenshot.');
840
+ throw new Error('A dialog is open; dismiss it with browser_act dialog before taking a screenshot.');
713
841
  }
842
+ // Screenshots are image blocks sent to the model. Mask DOM-addressable
843
+ // inputs regardless of their names, including child frames and refs.
844
+ // Closed shadow roots and page-drawn text remain outside this mask.
845
+ const mask = tab.page.frames().map((frame) => frame.locator('input, textarea'));
714
846
  const data = ref !== undefined
715
- ? await (await this.resolveRef(tab, ref)).screenshot({ type: 'png' })
716
- : await tab.page.screenshot({ type: 'png', fullPage: fullPage === true });
847
+ ? await (await this.resolveRef(tab, ref)).screenshot({
848
+ type: 'png',
849
+ mask,
850
+ maskColor: SCREENSHOT_INPUT_MASK_COLOR,
851
+ })
852
+ : await tab.page.screenshot({
853
+ type: 'png',
854
+ fullPage: fullPage === true,
855
+ mask,
856
+ maskColor: SCREENSHOT_INPUT_MASK_COLOR,
857
+ });
717
858
  const bytes = new Uint8Array(data);
718
859
  const size = pngSize(bytes);
719
860
  return this.result({
@@ -822,19 +963,69 @@ export class PlaywrightBrowserHost {
822
963
  for (const tab of this.tabs.values()) {
823
964
  if (tab.page.isClosed())
824
965
  continue;
825
- out.push({ ...(await this.pageInfo(tab)), active: tab.id === this.activeTabId });
966
+ out.push({
967
+ ...(await this.pageInfo(tab)),
968
+ active: tab.id === this.activeTabId,
969
+ });
826
970
  }
827
971
  return out;
828
972
  }
829
973
  // -------------------------------------------------------------------------
830
974
  // Acting
831
975
  // -------------------------------------------------------------------------
832
- async guardField(tab, locator) {
976
+ async guardField(locator) {
833
977
  const facts = await locator.evaluate(fieldFacts);
834
- if (isCredentialField(facts))
835
- throw this.humanRequired('credential-field', tab);
978
+ if (isCredentialField(facts)) {
979
+ throw new Error('This action targets a password or one-time-code control. Ask the operator to use the browser window.');
980
+ }
836
981
  return facts;
837
982
  }
983
+ /** Refuse direct form submission while a form holds a password or code input. */
984
+ async guardCredentialForm(locator, action, key) {
985
+ const context = await locator.evaluate((element) => {
986
+ const label = element.closest('label');
987
+ const labelledField = label?.control
988
+ ? {
989
+ tag: label.control.tagName.toLowerCase(),
990
+ type: label.control instanceof HTMLInputElement ? label.control.type.toLowerCase() : '',
991
+ autocomplete: label.control.getAttribute('autocomplete') ?? '',
992
+ name: label.control.getAttribute('name') ?? '',
993
+ id: label.control.id,
994
+ label: label.textContent ?? '',
995
+ }
996
+ : undefined;
997
+ const button = element.closest('button, input[type="submit"], input[type="image"]');
998
+ const associated = (button ?? element);
999
+ const form = associated.form ?? element.closest('form');
1000
+ const fields = form
1001
+ ? Array.from(form.elements)
1002
+ .filter((field) => field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement)
1003
+ .map((field) => ({
1004
+ tag: field.tagName.toLowerCase(),
1005
+ type: field instanceof HTMLInputElement ? field.type.toLowerCase() : '',
1006
+ autocomplete: field.getAttribute('autocomplete') ?? '',
1007
+ name: field.getAttribute('name') ?? '',
1008
+ id: field.id,
1009
+ label: [
1010
+ field.getAttribute('aria-label') ?? '',
1011
+ field.getAttribute('placeholder') ?? '',
1012
+ ...Array.from(field.labels ?? []).map((label) => label.textContent ?? ''),
1013
+ ].join(' '),
1014
+ }))
1015
+ : [];
1016
+ return { isFormButton: button !== null, labelledField, fields };
1017
+ });
1018
+ if (context.labelledField && isCredentialField(context.labelledField)) {
1019
+ throw new Error('This action targets a password or one-time-code control.');
1020
+ }
1021
+ if (!context.fields.some(isCredentialField))
1022
+ return;
1023
+ if ((action === 'click' && context.isFormButton) ||
1024
+ (action === 'press' && (context.isFormButton || /enter|return/i.test(key ?? ''))) ||
1025
+ action === 'type-submit') {
1026
+ throw new Error('This action could submit a form containing a password or one-time-code control. Ask the operator to use the browser window.');
1027
+ }
1028
+ }
838
1029
  async runAct(action) {
839
1030
  const tab = await this.activeTab();
840
1031
  await this.settle();
@@ -849,12 +1040,20 @@ export class PlaywrightBrowserHost {
849
1040
  if (action.action === 'dialog')
850
1041
  return this.answerDialog(tab, action);
851
1042
  if (this.pendingDialog?.tab === tab) {
852
- throw new Error('A dialog is open on this tab; answer it with browser_act dialog first.');
1043
+ throw new Error('A dialog is open on this tab; dismiss it with browser_act dialog first.');
853
1044
  }
854
- await this.perform(tab, action);
855
- await this.settle();
856
- this.checkLanding(tab);
857
- await this.settle();
1045
+ const checkpoint = await this.beginDocumentResponseChange(tab);
1046
+ try {
1047
+ await this.perform(tab, action);
1048
+ await this.settle();
1049
+ this.checkLanding(tab);
1050
+ await this.settle();
1051
+ }
1052
+ catch (error) {
1053
+ await this.finishDocumentResponseChange(tab, checkpoint).catch(() => undefined);
1054
+ throw error;
1055
+ }
1056
+ await this.finishDocumentResponseChange(tab, checkpoint);
858
1057
  await this.checkHuman(tab);
859
1058
  const after = this.tabs.get(tab.id) ?? tab;
860
1059
  if (action.snapshot)
@@ -865,20 +1064,29 @@ export class PlaywrightBrowserHost {
865
1064
  const pending = this.pendingDialog;
866
1065
  if (!pending || pending.tab !== tab)
867
1066
  throw new Error('No dialog is open on this tab.');
1067
+ if (action.accept) {
1068
+ throw new Error('The agent cannot accept a page-created dialog. Dismiss it, or ask the operator to use the browser window.');
1069
+ }
868
1070
  this.pendingDialog = undefined;
1071
+ const checkpoint = pending.checkpoint ?? {
1072
+ previous: this.documentResponse(tab),
1073
+ responsesBefore: tab.mainDocumentResponses,
1074
+ commitsBefore: tab.mainFrameCommits,
1075
+ };
1076
+ this.clearDocumentResponse(tab);
869
1077
  try {
870
- if (action.accept)
871
- await pending.dialog.accept(action.promptText);
872
- else
873
- await pending.dialog.dismiss();
1078
+ await pending.dialog.dismiss();
874
1079
  }
875
1080
  catch (error) {
1081
+ this.setDocumentResponse(tab, checkpoint.previous);
876
1082
  throw new BrowserOutcomeUnknownError('dialog', error);
877
1083
  }
878
- this.notes.unshift(action.accept ? 'The dialog was accepted.' : 'The dialog was dismissed.');
1084
+ this.notes.unshift('The dialog was dismissed.');
879
1085
  await this.settle();
880
1086
  this.checkLanding(tab);
881
1087
  await this.settle();
1088
+ await this.finishDocumentResponseChange(tab, checkpoint);
1089
+ await this.checkHuman(tab);
882
1090
  if (action.snapshot)
883
1091
  return this.result({ snapshot: await this.takeSnapshot(tab) });
884
1092
  return this.result({ page: await this.pageInfo(tab) });
@@ -892,19 +1100,24 @@ export class PlaywrightBrowserHost {
892
1100
  switch (action.action) {
893
1101
  case 'click': {
894
1102
  const locator = await this.resolveRef(tab, action.ref);
1103
+ await this.guardField(locator);
1104
+ await this.guardCredentialForm(locator, 'click');
895
1105
  await locator.click({ trial: true });
896
1106
  await this.unknownOnFailure('click', () => action.doubleClick ? locator.dblclick() : locator.click());
897
1107
  return;
898
1108
  }
899
1109
  case 'hover': {
900
1110
  const locator = await this.resolveRef(tab, action.ref);
1111
+ await this.guardField(locator);
901
1112
  await locator.hover({ trial: true });
902
1113
  await this.unknownOnFailure('hover', () => locator.hover());
903
1114
  return;
904
1115
  }
905
1116
  case 'type': {
906
1117
  const locator = await this.resolveRef(tab, action.ref);
907
- await this.guardField(tab, locator);
1118
+ await this.guardField(locator);
1119
+ if (action.submit)
1120
+ await this.guardCredentialForm(locator, 'type-submit');
908
1121
  await locator.click({ trial: true });
909
1122
  await this.unknownOnFailure('type', async () => {
910
1123
  await locator.fill(action.text);
@@ -917,7 +1130,7 @@ export class PlaywrightBrowserHost {
917
1130
  const plan = [];
918
1131
  for (const field of action.fields) {
919
1132
  const locator = await this.resolveRef(tab, field.ref);
920
- const facts = await this.guardField(tab, locator);
1133
+ const facts = await this.guardField(locator);
921
1134
  if (facts.kind === 'file') {
922
1135
  throw new Error(`${field.ref} is a file input; use browser_act upload for it.`);
923
1136
  }
@@ -948,27 +1161,22 @@ export class PlaywrightBrowserHost {
948
1161
  case 'press': {
949
1162
  if (action.ref !== undefined) {
950
1163
  const locator = await this.resolveRef(tab, action.ref);
951
- const facts = await locator.evaluate(fieldFacts);
952
- if (isCredentialField(facts) && !NON_TYPING_KEYS.has(action.key)) {
953
- throw this.humanRequired('credential-field', tab);
954
- }
1164
+ await this.guardField(locator);
1165
+ await this.guardCredentialForm(locator, 'press', action.key);
955
1166
  await this.unknownOnFailure('press', () => locator.press(action.key));
956
1167
  return;
957
1168
  }
958
- const focused = await tab.page.evaluate(focusedFieldFacts).catch(() => null);
959
- if (focused && isCredentialField(focused) && !NON_TYPING_KEYS.has(action.key)) {
960
- throw this.humanRequired('credential-field', tab);
961
- }
962
- await this.unknownOnFailure('press', () => tab.page.keyboard.press(action.key));
963
- return;
1169
+ throw new Error('An untargeted key could reach a password or one-time-code control, including one inside a closed shadow root. Use a specific control instead.');
964
1170
  }
965
1171
  case 'upload': {
966
1172
  const locator = await this.resolveRef(tab, action.ref);
1173
+ await this.guardField(locator);
967
1174
  const facts = await locator.evaluate(fieldFacts);
968
1175
  if (facts.kind === 'file') {
969
1176
  await this.unknownOnFailure('upload', () => locator.setInputFiles(action.path));
970
1177
  return;
971
1178
  }
1179
+ await this.guardCredentialForm(locator, 'click');
972
1180
  await locator.click({ trial: true });
973
1181
  await this.unknownOnFailure('upload', async () => {
974
1182
  const [chooser] = await Promise.all([