Skip to content

--disallow-code-generation-from-strings does not work as documented #58221

Open

Description

@ChALkeR

Doc: https://nodejs.org/docs/latest/api/cli.html#--disallow-code-generation-from-strings

It is advertised to cover built-in language features, but it doesn't cover data imports

No Node.js modules are directly imported here

// run with node and node --disallow-code-generation-from-strings
const payload = 'console.log("evaluated code from string")'
try { eval(payload) } catch (e) { console.log(e.message) }
try { new Function(payload)() } catch (e) { console.log(e.message) }
try { await import('data:text/javascript,' + payload) } catch (e) { console.log(e.message) }

Output:

% node --disallow-code-generation-from-strings 1.js
Code generation from strings disallowed for this context
Code generation from strings disallowed for this context
evaluated code from string

Activity

  1. ChALkeR commented on May 8, 2025

    @ChALkeR
    MemberAuthor

    For comparison, this is what browsers do under CSP:
    Image

  2. changed the title [-]--disallow-code-generation-from-strings does not work as advertised[/-] [+]--disallow-code-generation-from-strings does not work as documented[/+] on May 9, 2025
  3. edilson258 commented on May 10, 2025

    @edilson258

    wait, the official nodejs docs only covers eval and new Function and those does not allow code from strings.

    In the example U brought, only the last line (bellow) executes the code even with the flag --disallow-code-generation-from-strings set

    try { await import('data:text/javascript,' + payload) } catch (e) { console.log(e.message) }

    and the docs does not cover that

    refs: https://nodejs.org/docs/latest-v16.x/api/cli.html#--disallow-code-generation-from-strings

  4. ChALkeR commented on May 10, 2025

    @ChALkeR
    MemberAuthor

    Doc states:

    Make built-in language features like ...

    1. import() is a built-in language feature, defined in the ECMAScript specification.
    2. "like" is not exhaustive
    3. This is analogous to script-src CSP for browsers blocking unsafe-eval, and that one blocks data imports too.
    4. Having built-in import() as a dynamic eval defeats the purpose of this flag to a significant extent
  5. edilson258 commented on May 11, 2025

    @edilson258

    @ChALkeR you are right!

    @mojodna @bmizerany need your opinion about this please.

  6. legendecas commented on May 14, 2025

    @legendecas
    Member

    The flag --disallow-code-generation-from-strings only covers features that are guarded by ECMA262 host hook HostEnsureCanCompileStrings. The exhausted API list is

    There are many APIs in Node.js' module system, and node:vm, that allows creating script and modules dynamically. I think the doc for --disallow-code-generation-from-strings should also explicit exclude Node.js' module APIs.

  7. jimmywarting commented on May 17, 2025

    @jimmywarting

    Um, maybe it should not cover that?
    maybe import() should be restricted instead to what you can and can't import
    such that you only allow imports from "self", http, https and no data: url

  8. github-actions commented on Apr 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  9. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 20, 2026
  10. jendrikw commented on Apr 20, 2026

    @jendrikw

    not stale

  11. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 21, 2026
  12. github-actions commented on Jul 20, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  13. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 20, 2026
  14. jendrikw commented on Jul 23, 2026

    @jendrikw

    bump

  15. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions