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

Verify feature detection message #369

Merged

Conversation

lewisl9029
Copy link
Contributor

@lewisl9029 lewisl9029 commented Apr 21, 2023

I was also running into intermittent feature detection failures similar to the ones mentioned in #368 once I upgraded to 1.7.1 on https://reflame.app, so I made a custom build with some additional logging on the message callback function and found a pretty big clue:

Screenshot 2023-04-20 at 4 28 18 PM

Looks like the root of the issue might be that we're not verifying the source of the message, so anything that calls postMessage before es-module-shims (in this case the react dev tools extension) can cause feature detection to fail, often non-deterministically due to races.

Looking at this previous related issue, I think this is the more likely cause for that one too rather than some security policy (i.e. something on the page called postMessage with undefined as the message). So I removed the previous branch where we continue to resolve feature detection even after failing validation, and instead opting to return early and wait for the next message. WDYT?

Also, I added the debug build as an exported module so it can be accessed from the npm package as well.

@lewisl9029 lewisl9029 marked this pull request as ready for review April 21, 2023 00:18
@lewisl9029
Copy link
Contributor Author

Hmm... Looks like we have a test failure for Firefox 67.0. Not sure if it's a flake or a real failure (can't seem to trigger a retry from my side).

supportsImportMeta = data[1];
supportsCssAssertions = data[2];
supportsJsonAssertions = data[3];
const isFeatureDetectionMessage = Array.isArray(data) && data[0] === 'esms'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could potentially make this a bit more resistant to forgery by generating some random bytes instead of this hardcoded string. But I don't see what a potential attacker has to gain by taking over feature detection, so there might not be a need?

Copy link
Owner

Choose a reason for hiding this comment

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

Agreed, we don't need to be forge resistant.

Copy link
Owner

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Thanks for working this one out. I tend to agree it seems more likely security blocking would block the messaging entirely, as opposed to passing primitives, so I'm happy to land this under that assumption.

supportsImportMeta = data[1];
supportsCssAssertions = data[2];
supportsJsonAssertions = data[3];
const isFeatureDetectionMessage = Array.isArray(data) && data[0] === 'esms'
Copy link
Owner

Choose a reason for hiding this comment

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

Agreed, we don't need to be forge resistant.

@guybedford guybedford merged commit 14e78af into guybedford:main Apr 23, 2023
@lewisl9029 lewisl9029 deleted the verify-feature-detection-message branch April 23, 2023 01:23
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.

2 participants