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

Bug: Fail to disable eval() #44516

Closed
caoccao opened this issue Sep 5, 2022 · 3 comments
Closed

Bug: Fail to disable eval() #44516

caoccao opened this issue Sep 5, 2022 · 3 comments
Labels
v8 engine Issues and PRs related to the V8 dependency.

Comments

@caoccao
Copy link

caoccao commented Sep 5, 2022

Version

v16.17.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

No response

What steps will reproduce the bug?

Background

I'm embedding Node.js in a C++ application and want to disable eval() for security concern.

Reproduce

  • Build command: vcbuild.bat static without-intl
  • C++ code: v8Context->AllowCodeGenerationFromStrings(false);

How often does it reproduce? Is there a required condition?

This is a consistent behavior malfunction. It doesn't require any conditions.

What is the expected behavior?

The expected result is calling eval() returns an error EvalError: Code generation from strings disallowed for this context. However, in v16.17.0 the eval() still works. It used to be working well in v16.16.0.

What do you see instead?

The eval() is not disabled.

Additional information

No response

@Trott Trott added the v8 engine Issues and PRs related to the V8 dependency. label Sep 5, 2022
@legendecas
Copy link
Member

legendecas commented Sep 5, 2022

ModifyCodeGenerationFromStringsCallback is invoked when AllowCodeGenerationFromStrings is false. Node.js has set its ModifyCodeGenerationFromStringsCallback to check an internal allow code generation from strings flag.

In order to propagate the AllowCodeGenerationFromStrings to Node.js internal flag slot, context->AllowCodeGenerationFromStrings should be set before node::InitializeContext, or start node with --disallow-code-generation-from-strings (#44324).

Alternatively, the embedder can set its own callback of ModifyCodeGenerationFromStringsCallback.

@caoccao
Copy link
Author

caoccao commented Sep 5, 2022

Thank you for the detailed explanation.

In my use case, the application may allow-disallow-allow-disallow... the code generation from strings in one context. It seems setting ModifyCodeGenerationFromStringsCallback is the only option. The question now is: will that break anything?

@caoccao
Copy link
Author

caoccao commented Sep 14, 2022

Update

I added the following line and it works.

// After V8 isolate is initialized.
v8Isolate->SetModifyCodeGenerationFromStringsCallback(nullptr);

@caoccao caoccao closed this as completed Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

No branches or pull requests

3 participants