Skip to content

Conversation

@hamishwillee
Copy link
Collaborator

@hamishwillee hamishwillee commented Dec 19, 2025

The Function() constructor can execute its arguments as JavaScript.
Similarly eval() executes its input as JavaScript.

This updates the TrustedType information for both methods.

Note, I provided an example for eval() but not Function. I wasn't sure if I should do either, because even though this is notionally safer than not using trusted types, there is really no direction on how you can practically and generically transform a script so that it is safe.

For the eval() case I just indicated the use of custom transforming function, for which I provided no implementation. For Function, I added a disclaimer and linked to the eval() example.

Related docs work tracked in #41507

@hamishwillee hamishwillee requested a review from a team as a code owner December 19, 2025 05:57
@hamishwillee hamishwillee requested review from Josh-Cena and wbamberg and removed request for a team December 19, 2025 05:57
@github-actions github-actions bot added Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed labels Dec 19, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 19, 2025

@hamishwillee hamishwillee changed the title TrustedTypes: Function() constructor TrustedTypes: Function() constructor + eval() Dec 22, 2025

- `arg1`, …, `argN` {{optional_inline}}
- : Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript parameter (any of plain [identifier](/en-US/docs/Glossary/Identifier), [rest parameter](/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), or [destructured](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring) parameter, optionally with a [default](/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters)), or a list of such strings separated with commas.
- : {{domxref("TrustedScript")}} instances or strings specifying names to be used by the function as formal argument names.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It feels weird that this is part of JavaScript, but has this dependency on a web API. I don't know enough about the JS docs to know if this is a problem.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Josh-Cena This is a JavaScript native method that on a supporting browser might take a TrustedScript instance instead of a string.

I am fine with this - in environments where TrustedScript is not supported IMO this essentially falls back to just being the string.

However, if you have a specific way you'd like to address this in the Function and eval(), speak now, or forever hold your peace :-)

Copy link
Collaborator Author

@hamishwillee hamishwillee Jan 9, 2026

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll merge if no response before I wander off today. We can always tidy up later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Off I go! Thanks @wbamberg

hamishwillee and others added 2 commits January 9, 2026 10:16
Clarify the risks of using direct eval() in JavaScript, emphasizing security concerns and performance issues.
@Josh-Cena
Copy link
Member

Are you going to update AsyncFunction, GeneratorFunction, and AsyncGeneratorFunction in a different PR or is that an oversight?

@hamishwillee hamishwillee requested a review from wbamberg January 8, 2026 23:22
@hamishwillee
Copy link
Collaborator Author

Thanks for the review @wbamberg - I've accepted all comments, and rolled them into Function() doc too. I am happy for this to go in - so if Josh doesn't comment on https://github.com/mdn/content/pull/42462/files#r2666072963 can you merge please.

Copy link
Collaborator

@wbamberg wbamberg 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 Hamish. I'm approving but will give Josh time to respond as well.

@hamishwillee
Copy link
Collaborator Author

Are you going to update AsyncFunction, GeneratorFunction, and AsyncGeneratorFunction in a different PR or is that an oversight?

Good catch. Will have to be a different PR. Yes this is an oversight.

Not specifically mine though - I've been tracking BCD and this isn't there. It is implemented in Firefox and Chrome though (tested).

@hamishwillee hamishwillee merged commit fab1ac5 into mdn:main Jan 9, 2026
7 checks passed
Anurella added a commit to Anurella/content that referenced this pull request Jan 12, 2026
* 'main' of https://github.com/Anurella/content: (7201 commits)
  Update InterfaceData based on WebRef (mdn#42718)
  fix typo in `PeroformanceResourceTiming.requestStart` (mdn#42724)
  fix: auto-cleanup by bot (mdn#42717)
  fix typo in RTCTransportStats (mdn#42704)
  Fix typo in queuingStrategy variable name (mdn#42706)
  Update guidance on <hr> styling (mdn#42708)
  Fx147: Add relnote for iterator sequencing support (mdn#42713)
  Fix order of arguments in SpeechRecognitionPhrase ctor (mdn#42711)
  Update warnings about event handler attributes (mdn#42347)
  chore(deps-dev): bump fs-extra from 11.3.2 to 11.3.3 (mdn#42468)
  ci(deps): bump actions/download-artifact from 6.0.0 to 7.0.0 (mdn#42404)
  ci(deps): bump actions/upload-artifact from 5.0.0 to 6.0.0 (mdn#42402)
  ci(deps): bump peter-evans/create-pull-request from 7.0.11 to 8.0.0 (mdn#42403)
  Explain value repetition when fewer background values are specified (mdn#42627)
  Add description to background shorthand property (mdn#42695)
  chore(deps): bump @mdn/fred from 1.9.7 to 2.0.0 (mdn#42702)
  chore(deps): bump qs from 6.14.0 to 6.14.1 (mdn#42598)
  Bump node-html-parser from 7.0.1 to 7.0.2 (mdn#42659)
  update documentation to note that Firefox for Android requires CT (mdn#42672)
  TrustedTypes: Function() constructor + eval() (mdn#42462)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants