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

Allow disabling eval patching #29

Merged
merged 7 commits into from
Jan 14, 2025
Merged

Allow disabling eval patching #29

merged 7 commits into from
Jan 14, 2025

Conversation

merlinfuchs
Copy link
Member

@merlinfuchs merlinfuchs commented Jan 9, 2025

This adds an option to FriendlyCaptchaSDK that allows users to disable the patching of window.eval which happens to break some websites. Users that use the site.js version can create a frc-disable-eval-patching meta tag similar to the frc-api-endpoint meta tag for configuring the API endpoint.

To make this possible we had to defer the patching until the SDK is created. I believe this is fine because we tell users to create one global SDK instance.

We considered two other options for the configuration in the site.js version:

  • Putting in the query URL (<script src="site.js?disableEvalPatching=1">), the downside is that users would have to put it on both the compat and non-compat script tag.
  • Putting data attributes on the script tags (script data-disable-eval-patching="1" src="site.js" />), same downside as above and in the module version `import.meta´ doesn't expose the dataset of the script element directly.

@merlinfuchs merlinfuchs marked this pull request as ready for review January 9, 2025 09:18
@merlinfuchs merlinfuchs requested a review from gzuidhof January 9, 2025 09:18
Copy link
Contributor

@gzuidhof gzuidhof left a comment

Choose a reason for hiding this comment

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

Thank you for this :)

I think it's okay to defer patching until when the SDK is created. I suppose in some way we could patch everything else beforehand.. but I don't think the complexity is worth it.

I think it is preferable to still create the Signals object beforehand, but to only set up the patching in a deferred way, what do you think? As we collect behavioral signals (e.g. mouse movement data), and some sites may only create a SDK instance just in time 🤔).


One change I would propose is adding a boolean to the Signals struct somewhere that indicates this setting is true. To save on some bytes we usually use some short name, so something like dep for disableEvalPatching could work. I think we can set that value in getSignals

const m: HTMLMetaElement | null = document.querySelector(`meta[name="frc-disable-eval-patching"]`);
if (!m) return false;

return m.content === "true";
Copy link
Contributor

Choose a reason for hiding this comment

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

I would consider return !!m.content (so any truthy value is enabled) 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I initially only checked if the element was present at all, but I thought some users might set it to false and expect it to be enabled 🤔 !!"false" is still evaluates to true ...

I'm happy to change it anyway, this is just why I decided to explicitly check for true.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's tricky.. In HTML often even an empty attribute is truthy 😰

Like <button disabled> I think is equivalent to <button disabled="">.. I'm not sure what the least "surprising" approach is

Copy link
Member Author

Choose a reason for hiding this comment

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

I did some research but didn't find anything specific to meta tags. You are right that the presence of an attribute usually means it's true: If an HTML tag contains a boolean attribute — no matter the value of that attribute — the attribute is set to true on that element. (MDM)
So I think changing it to !!m.content is the right thing.

src/signals/collect.ts Outdated Show resolved Hide resolved
src/signals/collectStacktrace.ts Outdated Show resolved Hide resolved
src/sdk/sdk.ts Outdated Show resolved Hide resolved
@merlinfuchs merlinfuchs self-assigned this Jan 9, 2025
@merlinfuchs merlinfuchs merged commit cd3abf2 into main Jan 14, 2025
2 checks passed
@merlinfuchs merlinfuchs deleted the fix-eval-patching branch January 14, 2025 09:53
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