Skip to content

fix: attribute hid, usb and serial device permissions to the requesting frame - #53629

Closed
codebytere wants to merge 11 commits into
mainfrom
fix/hid-usb-permission-requesting-frame
Closed

codebytere wants to merge 11 commits into
mainfrom
fix/hid-usb-permission-requesting-frame

Conversation

@codebytere

@codebytere codebytere commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Device permission decisions for WebHID, WebUSB and Web Serial were attributed inconsistently: the hid/usb check handler saw the top-level origin with a null webContents/frame, grants were stored under one origin and looked up or revoked under another, select-serial-port carried no frame, setDevicePermissionHandler was bypassed for devices without a persistent id (and had no frame), forget() never reached other documents' open connections, and WebUSB silently did nothing in in-memory partitions. This makes every hid/usb/serial decision carry the requesting frame and agree on its origin.

  • setPermissionCheckHandler (hid/usb/serial), setDevicePermissionHandler, setUSBProtectedClassesHandler, select-hid-device/select-usb-device/select-serial-port, serial-port-added/removed and *-revoked all identify the requesting frame (details.frame / trailing frame argument) and its origin — including cross-origin and sandboxed (opaque-origin) subframes, which are reported as-is for the app to decide on.
  • Content's HidService/WebUsbServiceImpl/SerialService are keyed on the requesting frame's origin (two carried patches; the CanRequestDevicePermission(rfh) one is upstreamed as https://crrev.com/c/8364427), so grant, check, forget(), revoked events, FIDO/protected-class decisions and OnPermissionRevoked use one origin.
  • forget() notifies content, closing the device in every document of that origin; HasDevicePermission/HasPortPermission are gated on the permission check; chooser selections are always recorded and reported to setDevicePermissionHandler as details.selected (+ deviceId/portId) so the handler is consulted for every device but can honour selections.
  • Serial: per-session observation (only the first session's SerialServices were ever notified), OnPortConnectedStateChanged forwarded, controller deleted with its frame; USB: in-memory partitions get a UsbChooserContext, chooser-handler re-entrancy guard, details.device.configurations[].interfaces is an array; smart-card extension-ID auto-grant removed.
  • Tests: in-process fake HID/USB/serial device managers behind the testing binding and spec/api-device-permissions-spec.ts (28 cases: attribution per topology, chooser identity, handler veto, check gating, cross-document revocation, chooser handler destroying the window, usb details shape, protected-classes identity, serial-port-added frame, connect/disconnect), plus cross-origin / opaque subframe check-handler specs in api-session-spec.ts.

Checklist

Release Notes

Notes: Fixed hid, usb and serial permission checks, device grants and revocations being attributed to the wrong frame or origin; setPermissionCheckHandler, setDevicePermissionHandler, setUSBProtectedClassesHandler, select-serial-port and serial-port-added/-removed now receive the requesting frame, setDevicePermissionHandler receives details.selected, and WebUSB works in in-memory partitions.

ElectronHidDelegate and ElectronUsbDelegate received the top-level document's
origin from content and passed a null frame to the permission check handler,
so a check from a cross-origin iframe delegated allow="hid"/"usb" reported
the embedder's origin, no requestingUrl, isMainFrame false and a null
webContents. Derive the origin from the requesting frame and pass the frame
through, as the serial delegate does, so the handler sees the frame that is
asking and HasDevicePermission matches the grants the choosers store under
that frame's origin.
@codebytere
codebytere requested a review from a team as a code owner September 6, 2026 17:59
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Sep 6, 2026
@codebytere codebytere added semver/patch backwards-compatible bug fixes target/45-x-y PR should also be added to the "45-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. labels Sep 6, 2026
codebytere and others added 6 commits September 7, 2026 04:51
Follow-on to the check-handler change so that all hid/usb device
permission paths agree on the requesting document:

* carry a second patch that constructs HidService/WebUsbServiceImpl with
  the requesting frame's origin, so forget(), usb/hid-device-revoked,
  the FIDO allow-list, protected interface classes and content's
  OnPermissionRevoked comparison use the origin the grant was stored
  under
* notify content's OnPermissionRevoked from the chooser contexts so
  open connections held by other documents of the origin are closed
* gate HasDevicePermission on the hid/usb permission check, so denying
  the check for a document also cuts off getDevices()/open()
* with setDevicePermissionHandler installed, do not consult or populate
  the internal ephemeral grant set; pass details.frame to the handler
* pass details.origin and details.frame to setUSBProtectedClassesHandler
* refuse opaque-origin requesters before any handler runs
* drop the smart-card auto-grant keyed on Chrome extension IDs
* docs: setPermissionCheckHandler webContents/requestingUrl wording,
  new frame/origin fields
Do not refuse opaque-origin requesters on the app's behalf; instead give
setPermissionCheckHandler a live details.frame so the app can attribute
the check itself (frame.origin, frame.parent, frame.top) even when the
requesting document's origin has no URL form.
* record every chooser selection for the session and report it to
  setDevicePermissionHandler as details.selected (with the deviceId the
  chooser reported for hid), so the handler is consulted for every
  device yet can honour selections; without a handler a selection is a
  grant as before
* revoke: copy the origin, always clear the ephemeral entry (usb), and
  notify content from a posted task so app JS never runs under the
  caller's stack
* guard UsbChooserController against the select-usb-device handler
  destroying the requesting frame
* UsbChooserContextFactory: serve in-memory partitions (WebUSB was a
  no-op there)
* usb DeviceInfoToValue: interfaces is a list, configuration sits on the
  device
* de-duplicate persistent device grants
* docs: selected/securityOrigin wording, example honours selections
* select-serial-port, serial-port-added and serial-port-removed carry the
  requesting frame
* HasPortPermission is gated on the serial permission check, passes the
  frame and details.selected/portId to setDevicePermissionHandler, and
  consults the handler for ports without a persistent id
* forget() notifies content so other documents' connections close;
  SerialService compares against the requesting frame's origin (patch)
* forward OnPortConnectedStateChanged so navigator.serial fires
  connect/disconnect for Bluetooth serial ports
* observe every session's SerialChooserContext, not only the first
* delete SerialChooserController with its frame and guard the
  select-serial-port handler destroying it
* testing: in-process fake HID/USB/serial managers behind
  process._linkedBinding('electron_common_testing') and
  spec/api-device-permissions-spec.ts covering attribution across main
  frame / cross-origin iframe / sandboxed iframe, chooser identity,
  handler veto, check gating, cross-document revocation, chooser
  handler destroying the window, usb details shape, protected classes
  identity, serial-port-added frame and connect/disconnect
@MarshallOfSound MarshallOfSound changed the title fix: report the requesting frame for hid and usb permission checks fix: attribute hid, usb and serial device permissions to the requesting frame Sep 7, 2026
The check handler docs describe embeddingOrigin, but only the
permissions.query path set it; hid/usb/serial (and other delegate-driven)
checks left it undefined, so a handler keyed on the embedder failed open
for subframes. Derive it from the requesting frame's main frame whenever
a frame is present and the caller has not supplied one.
…ents

hid-device-revoked, usb-device-revoked, usb-device-added and
usb-device-removed now identify the frame like their serial and hid
counterparts; the carried patch hands the frame to
UsbDelegate::RevokeDevicePermissionWebInitiated() for that.
@MarshallOfSound

Copy link
Copy Markdown
Member

Splitting this: the narrow hid/usb check-handler fix (backportable) and the wider per-frame device permission changes (46 only) are going up as two PRs off our branches. Thanks for the start here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-pr 🌱 PR opened recently semver/patch backwards-compatible bug fixes target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants