Skip to content

fix: Add scuttle exception for fetch because it is used by chromium image util code when creating notifications#24631

Merged
danjm merged 9 commits intodevelopfrom
scuttle-fetch-browser-image-util
May 23, 2024
Merged

fix: Add scuttle exception for fetch because it is used by chromium image util code when creating notifications#24631
danjm merged 9 commits intodevelopfrom
scuttle-fetch-browser-image-util

Conversation

@danjm
Copy link
Copy Markdown
Contributor

@danjm danjm commented May 20, 2024

Description

MV3 bug that we needto fix: #24518

In particular, we've got:

LavaMoat - property "fetch" of globalThis is inaccessible under scuttling mode.
at get (chrome-extension://lbnhnppncgilfagdfbanifbfolodkdaf/scripts/runtime-lavamoat.js:13190:11)
  at loadImageDataForServiceWorker (extensions::imageUtil:24:22)
  at loadImageData (extensions::imageUtil:111:5)
  at Object.loadAllImages (extensions::imageUtil:134:5)
  at replaceNotificationOptionURLs (extensions::notifications:89:13)
  at extensions::notifications:116:5
  at chrome-extension://lbnhnppncgilfagdfbanifbfolodkdaf/common-6.js:3:431145
  at new Promise (<anonymous>)
  at Proxy.<anonymous> (chrome-extension://lbnhnppncgilfagdfbanifbfolodkdaf/common-6.js:3:430825)
  at Object.apply (chrome-extension://lbnhnppncgilfagdfbanifbfolodkdaf/common-6.js:3:430244)
  at Object._showNotification (chrome-extension://lbnhnppncgilfagdfbanifbfolodkdaf/common-2.js:1:455026)

The problematic code is in not in our codebase but in chromium, here: https://github.com/chromium/chromium/blob/main/extensions/renderer/resources/image_util.js#L23-L25

You can see that function is named loadImageDataForServiceWorker and it is only used in mv3.

This breaks the browser notification feature.

As can be seen from the commit history and the comments in this PR, we attemtped some solutions that would allow us to provide a modified and tamed fetch to be used for this image fetching. However, there are unknown problems - related to how sentry uses fetch and how sentry is initialized - that interfere with the solutions we attempted. So for now, as discussed with @weizman, the latest commit on the PR just adds a scuttling exception for fetch (and for Offscreen Canvas, which is also needed for the browser notification feature).

We will return to the root problem later, and aim to remove these scuttling exceptions.

Related issues

Fixes: #24518

Manual testing steps

  1. yarn dist:mv3
  2. install, onboard
  3. send a transaction
  4. verify that a browser notification is shown once the transaction is confirmed

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@github-actions
Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [91cb685]
Page Load Metrics (954 ± 525 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint63149932512
domContentLoaded9331473
load5125389541094525
domInteractive9331473
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link
Copy Markdown
Contributor

@weizman weizman left a comment

Choose a reason for hiding this comment

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

Looks great,

Some important comments.

Also, a more general (yet very important) comment:

We must cache the APIs we're using as part of this feature in order to not trust they aren't being modified later on. SES assures that in practice, but it's still a more secure approach in case we extend this to use APIs that aren't strictly SES protected intrinsics.

So cache and use the test property of RegExp and Error and fetch and Object.hasOwn and so on.

I can help with that part 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's this for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

uh, nothing... dead code from some earlier iteration

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed with 1dd34f9af5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd move this function to the upper scope (same as tamedFetch)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done in 1dd34f9af5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unscuttled -> tamed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do. Can you explain that suggestion a bit? I am still building up my understanding of how scuttiling works, and I suspect your suggestion here is meaningful and I could learn something by hearing your thoughts on this suggestion

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes of course, gladly.
"Unscuttled" is correct, because it is in fact going to be exempt from the scuttling process.
But it's not accurate enough, because "Unscuttled" really reminds something that is being completely over looked, which isn't the case here.
fetch is going to be "Unscuttled" only because we promise to tame it - that is the only legitimate excuse for excepting it from being scuttled.
That is why I think referring to it as tamed rather than unscuttled is important here - so it's clear that we mutated it in order to justify it from not being scuttled.

Copy link
Copy Markdown
Contributor Author

@danjm danjm May 22, 2024

Choose a reason for hiding this comment

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

done in 5467de13d6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We must use defineProperty and set configurable and writable to false. This will instruct the scuttling process to skip fetch, which would allow you to remove the exception from development/build/index.js

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done in 1dd34f9af5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the importance of the chromeExtensionId below really? I think we can allow fetching images from the chrome-extension scheme for all types of browser extension entities we create

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed in b769e71328

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove this line once #24631 (comment) is fixed

Copy link
Copy Markdown
Contributor

@weizman weizman May 22, 2024

Choose a reason for hiding this comment

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

ACTUALLY - applying the idea behind this PR to the Image exception as well could be very beneficial, as Image is also a very strong capability (just a thought)

@danjm danjm marked this pull request as ready for review May 22, 2024 16:28
@danjm danjm requested review from a team and kumavis as code owners May 22, 2024 16:28
@danjm danjm changed the title Add scuttle exception for fetch because it is used by chromium image util code when creating notifications fix: Add scuttle exception for fetch because it is used by chromium image util code when creating notifications May 22, 2024
@danjm danjm requested review from a team as code owners May 23, 2024 18:06
@danjm danjm force-pushed the scuttle-fetch-browser-image-util branch 3 times, most recently from d5c47aa to dfd197a Compare May 23, 2024 18:09
@danjm danjm force-pushed the scuttle-fetch-browser-image-util branch from dfd197a to 08ab433 Compare May 23, 2024 18:11
@danjm danjm force-pushed the scuttle-fetch-browser-image-util branch from 08ab433 to 3d678d7 Compare May 23, 2024 18:12
@danjm danjm added the team-extension-platform Extension Platform team label May 23, 2024
@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [e48f02d]
Page Load Metrics (729 ± 490 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint57144862311
domContentLoaded9221242
load4624587291021490
domInteractive9221242
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@danjm
Copy link
Copy Markdown
Contributor Author

danjm commented May 23, 2024

An issue for further investigation into the root cause of the problem this PR addresses is here #24759

@danjm danjm merged commit ef2f2f5 into develop May 23, 2024
@danjm danjm deleted the scuttle-fetch-browser-image-util branch May 23, 2024 23:15
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2024
@metamaskbot metamaskbot added release-11.16.6 Issue or pull request that will be included in release 11.16.6 and removed release-11.18.0 labels Jun 4, 2024
@metamaskbot
Copy link
Copy Markdown
Collaborator

Missing release label release-11.16.6 on PR. Adding release label release-11.16.6 on PR and removing other release labels(release-11.18.0), as PR was cherry-picked in branch 11.16.6.

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

Labels

release-11.16.6 Issue or pull request that will be included in release 11.16.6 team-extension-platform Extension Platform team

Projects

Archived in project

5 participants