Skip to content

browser: Fix JS native objects override #4709

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

Merged
merged 2 commits into from
Apr 21, 2025
Merged

Conversation

inancgumus
Copy link
Contributor

@inancgumus inancgumus commented Apr 18, 2025

What?

Avoids a page under the test to overwrite native JavaScript objects, like Set and Map, by injecting a temporary and invisible frame in execution contexts. We can extend this to support other native objects in the future. However, for now, this solves crucial issues for users.

Note: I will update the release notes after I get the approvals, but before the merge.

Why?

See #4687 for details.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link

Related PR(s)/Issue(s)

Updates: #4687

@inancgumus inancgumus added this to the v1.0.0 milestone Apr 18, 2025
@inancgumus inancgumus self-assigned this Apr 18, 2025
@inancgumus inancgumus force-pushed the fix/native-objs-override branch from b648350 to 67bd13f Compare April 18, 2025 15:57
@inancgumus inancgumus marked this pull request as ready for review April 18, 2025 16:06
@inancgumus inancgumus requested a review from a team as a code owner April 18, 2025 16:06
@inancgumus inancgumus requested review from codebien, joanlopez and mstoykov and removed request for a team and joanlopez April 18, 2025 16:06
@inancgumus inancgumus force-pushed the fix/native-objs-override branch from 67bd13f to a8a9c8c Compare April 18, 2025 16:13
Comment on lines +22 to +33
document.documentElement.appendChild(iframe);
const win = iframe.contentWindow;
document.documentElement.removeChild(iframe);
Copy link
Contributor

Choose a reason for hiding this comment

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

I am hopeful that adding and removing an iframe before it has any chance of actually redrawing is enough for this to not have side effects.

But it seems kind of risky. Unfortunately my quick search hasn't shown another way to do this, so 🤷

I guess another more involved option will be to not depend on map or set ... but to be honest someone breaking them seems pretty ... strange and likely a much bigger problem for them than this script not working.

Copy link
Contributor Author

@inancgumus inancgumus Apr 18, 2025

Choose a reason for hiding this comment

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

I am hopeful that adding and removing an iframe before it has any chance of actually redrawing is enough for this to not have side effects. But it seems kind of risky. Unfortunately my quick search hasn't shown another way to do this, so 🤷

This user can test their webpage on other test engines without issues. This never-painting iframe method resolves the user's issue, and all our integration and E2E tests pass. Other test engines use a similar approach.

I guess another more involved option will be to not depend on map or set ... but to be honest someone breaking them seems pretty ... strange and likely a much bigger problem for them than this script not working.

I tried that approach before concluding on this one. It can quickly become error-prone and complicated, since we already have a working (injected) script. Besides, users can also overwrite other native objects (e.g., not just Set), making it work fine for their webpages. We'll eventually end up using nothing native in our script 😅 Just because users shim native objects that shouldn't break k6. Still, I'm open to other solutions 🙇

@inancgumus inancgumus force-pushed the fix/native-objs-override branch from a8a9c8c to f06b891 Compare April 18, 2025 16:41
Avoids a page under the test to overwrite native JavaScript objects,
like Set and Map by injecting a temporary and invisible frame in an
execution context. We can extend this to support other native objects
in the future. However, for now, this solves one of the user's issue.
@inancgumus inancgumus force-pushed the fix/native-objs-override branch 3 times, most recently from cc900fe to c17431d Compare April 18, 2025 18:00
codebien
codebien previously approved these changes Apr 18, 2025
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

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

LGTM, but why only Set and Map? Can we open an issue for all the other missing functions, please? 🙇

@codebien
Copy link
Contributor

@inancgumus However, the CI seems to fail on TestPageMustUseNativeJavaScriptObjects. Is that expected?

@inancgumus
Copy link
Contributor Author

inancgumus commented Apr 18, 2025

@codebien, thanks for the review 🙇

The CI seems to fail on TestPageMustUseNativeJavaScriptObjects. Is that expected?

No, it's definitely not 😅 I'm trying to fix the flakiness on CI 🤞 It runs fine locally.

LGTM, but why only Set and Map? Can we open an issue for all the other missing functions, please? 🙇

I believe listing all the other objects would be cumbersome in an issue, as we would need to update that issue every time we add (or remove) a new native object in our injected script (error-prone and tedious). We used Set and Map here to fix the user's immediate issue. However, I can keep the main issue open instead of closing it after this PR.

@inancgumus inancgumus force-pushed the fix/native-objs-override branch 5 times, most recently from 792b5c6 to 7eb7351 Compare April 18, 2025 19:36
@inancgumus inancgumus force-pushed the fix/native-objs-override branch from 7eb7351 to c424e2c Compare April 18, 2025 20:00
@inancgumus
Copy link
Contributor Author

@codebien I've simplified the test and also eliminated the flakiness.

@inancgumus inancgumus requested review from codebien and mstoykov April 18, 2025 20:28
@inancgumus inancgumus merged commit 3c079cc into master Apr 21, 2025
28 checks passed
@inancgumus inancgumus deleted the fix/native-objs-override branch April 21, 2025 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants