Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebHID endowment #1219

Closed
wants to merge 11 commits into from
Closed

Add WebHID endowment #1219

wants to merge 11 commits into from

Conversation

montelaidev
Copy link
Contributor

Creates a new endowment to allow for webhid usage in snaps.

@montelaidev montelaidev requested a review from a team as a code owner February 21, 2023 06:40
@Mrtenz Mrtenz changed the title Feat/webhid endowment Add WebHID endowment Feb 21, 2023
@@ -29,6 +29,10 @@ export async function createWindow(
// MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
// Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489
iframe.setAttribute('src', uri);

// Enable WebHID to be executed in the iframe.
iframe.setAttribute('allow', 'hid');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be interesting if we could set this conditionally based on the permission 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The snaps iframe is created before the snaps controller, so i'm not sure if we can conditionally set that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it would be better if we didn't give HID to any iframes that don't need it, hmm 🤔

Copy link
Member

@Mrtenz Mrtenz Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iframe is created by the execution service, after the snaps controller starts a snap, right? We could pass the endowments to the execution service when creating the iframe, and only set this attribute based on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@FrederikBolding FrederikBolding Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maarten is right, the iframe is created when executeSnap is called on an execution service. The IframeExecutionService does not create the iframe before it is needed to execute snap code. So we can pass along the endowments from snapData and use that.

For reference:
https://github.com/MetaMask/snaps-monorepo/blob/main/packages/snaps-controllers/src/services/AbstractExecutionService.ts#L340

https://github.com/MetaMask/snaps-monorepo/blob/main/packages/snaps-controllers/src/services/AbstractExecutionService.ts#L220

https://github.com/MetaMask/snaps-monorepo/blob/main/packages/snaps-controllers/src/services/iframe/IframeExecutionService.ts#L40

@FrederikBolding
Copy link
Member

Also this needs a rebase for CI to run @montelaidev

@socket-security
Copy link

socket-security bot commented Feb 21, 2023

Socket Security Pull Request Report

👍 No new dependency issues detected in pull request

Pull request report summary
Issue Status
Install scripts ✅ 0 issues
Native code ✅ 0 issues
Bin script shell injection ✅ 0 issues
Unresolved require ✅ 0 issues
Invalid package.json ✅ 0 issues
HTTP dependency ✅ 0 issues
Git dependency ✅ 0 issues
Potential typo squat ✅ 0 issues
Known Malware ✅ 0 issues
Telemetry ✅ 0 issues
Protestware/Troll package ✅ 0 issues
Bot Commands

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore foo@1.0.0 bar@* or ignore all packages with @SocketSecurity ignore-all

Powered by socket.dev

@codecov
Copy link

codecov bot commented Mar 14, 2023

Codecov Report

Merging #1219 (b6c8e77) into main (9d77052) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1219      +/-   ##
==========================================
+ Coverage   95.27%   95.29%   +0.01%     
==========================================
  Files         141      143       +2     
  Lines        4361     4374      +13     
  Branches      718      718              
==========================================
+ Hits         4155     4168      +13     
  Misses        206      206              
Impacted Files Coverage Δ
...es/snaps-controllers/src/snaps/endowments/index.ts 100.00% <ø> (ø)
...ts/src/common/endowments/commonEndowmentFactory.ts 100.00% <ø> (ø)
...ges/snaps-controllers/src/snaps/endowments/enum.ts 100.00% <100.00%> (ø)
...s/snaps-controllers/src/snaps/endowments/webhid.ts 100.00% <100.00%> (ø)
...ution-environments/src/common/endowments/webhid.ts 100.00% <100.00%> (ø)
packages/snaps-utils/src/iframe.ts 85.71% <100.00%> (+1.09%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

navigator: {
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/hid
// https://wicg.github.io/webhid/#dom-navigator-hid
hid: rootRealmGlobal.navigator?.hid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be hardened using harden. Can you verify that it still works after doing that?

Also, I wonder if there is more locking down of the object required since it is an EventTarget 🤔

Thoughts @weizman

@@ -29,6 +29,10 @@ export async function createWindow(
// MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
// Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489
iframe.setAttribute('src', uri);

// Enable WebHID to be executed in the iframe.
iframe.setAttribute('allow', 'hid');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it would be better if we didn't give HID to any iframes that don't need it, hmm 🤔

*
* @returns The {@link navigator} object with only access to hid.
*/
function createHID() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have a discussion about this endowment in general since it is not available in Firefox. Any snap that tries to leverage it will not work in FF. In the past we have not added endowments that aren't platform agnostic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the snap manifest can have a target field?

{
  "targets": {
    "chrome": ">=89",
  },
}

@montelaidev montelaidev closed this Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants