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

Prevent users from passing arbitrary parser options to Jint #1831

Merged
merged 7 commits into from
Apr 9, 2024

Conversation

adams85
Copy link
Contributor

@adams85 adams85 commented Apr 9, 2024

As suggested here, this PR extracts some changes from #1820 to make them happen earlier.

This means some breaking changes to the public API which prevent users from passing arbitrary parser options to Jint. Only a subset of the parser options should be adjustable by end users, while the rest should be reserved for Jint to achieve the desired parser behavior when it needs to parse code dynamically (eval, Function ctor, shadow realms, etc.)

The PR also...

  • makes sure that the options used to parse the root code are preserved in ExecutionContext for dynamic code parsing - e.g. when the script is parsed in non-tolerant mode, dynamic code should be as well. (In the case of eval this doesn't work currently as Esprima doesn't provide the necessary settings, so tolerant mode must be enabled no matter what. This will be solved though once the migration to Acornima happens.)
  • fixes a minor bug when eval is used in a shadow realm,
  • adds an alias for Esprima.Ast.Module to reduce further the amount of changes related to Acornima migration.

@adams85 adams85 changed the title Preserve options for dynamic code parsing Prevent users from passing arbitrary parser options to Jint Apr 9, 2024
Copy link
Collaborator

@lahma lahma left a comment

Choose a reason for hiding this comment

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

Lookin good, thanks for doing this! I added some comments/thoughts. This could very well be next 3.1 release, I usually scan through compilation problems with the usual suspects utilizing Jint so I can check if further PRs needed to refine some things.

using Jint.Runtime;
using Jint.Runtime.Modules;

namespace Jint.Tests.Runtime
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: for new files can use file-scoped namespaces

using System.Text.RegularExpressions;

namespace Jint
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: could use file-scoped namespace


namespace Jint
{
public interface IParseOptions
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not a native speaker, but would IParsingOptions be more natura, also ScriptParsingOptions - just a thought

Copy link
Contributor Author

@adams85 adams85 Apr 9, 2024

Choose a reason for hiding this comment

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

Neither am I... :) The problem with ScriptParsingOptions is that then we should also have ScriptPreparingOptions. Sounds a bit weird. How about ParseScriptOptions and PrepareScriptOptions maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I just saw #1831 (comment). I'm fine with IParsingOptions/IPreparationOptions as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm fine with either way, I guess we need an Oxford scholar for this 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Naming things and cache invalidation, the two hard problems in software engineering :D

I ran a quick search on the ASP.NET Core repo. Although there are some counter-examples but it's mostly [...]ingOptions or [...]tionOptions, so I'm convinced. :D

bool Tolerant { get; init; }
}

public record class ScriptParseOptions : IParseOptions
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe sealed?

/// Defaults to <see langword="true"/>.
/// </summary>
/// <remarks>
/// Please note that there is a plan to change the default to <see langword="false"/> in the next major version.
Copy link
Collaborator

Choose a reason for hiding this comment

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

next major of Acornima? so should this just be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I meant the next major version of Jint.

Or we could just make this BC in v3.1 as the related APIs will be broken anyway. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not just quite sure if it's best approach on interpreter side to be strict-by-default. For parsing it makes sense, but for anyone just "having a good time with JS" do we need to restrict parsing and allowed constructs? But again seems that NodeJS throws error on console from using return so maybe it makes sense.

Copy link
Contributor Author

@adams85 adams85 Apr 9, 2024

Choose a reason for hiding this comment

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

I wrote about this in the other PR: "[...] I can only speak for myself, but I'd be surprised if I fed some script to Jint which is invalid per spec and get some results instead of SyntaxError. I mean I think relaxing the rules should be a deliberate choice, not the default. [...]"

But again seems that NodeJS throws error on console from using return so maybe it makes sense.

Disabling tolerant mode doesn't affect top level return because that's controlled by another switch than tolerant mode. That option would be on for scripts by default: https://github.com/sebastienros/jint/pull/1831/files#diff-54aceb67398ce1901835b606789cff7f0ac2abb5136a75480469b5c732d19b11R40

Copy link
Contributor Author

@adams85 adams85 Apr 9, 2024

Choose a reason for hiding this comment

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

Let's not break this behavior in a minor version, so I'd say let's leave it as is for now and reconsider it when v4 is on the table.

/// <remarks>
/// Please note that there is a plan to change the default to <see langword="false"/> in the next major version.
/// </remarks>
bool Tolerant { get; init; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

live below (came back to this), should we just initialize with true?

Copy link
Contributor Author

@adams85 adams85 Apr 9, 2024

Choose a reason for hiding this comment

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

I'm not sure I get this. This is an interface, we can't initialize the property here, can't we? It's initialized here: https://github.com/sebastienros/jint/pull/1831/files#diff-5196d539da48e2bc4acd76456adfff1ca098ad422ce71e90849e98258c066633R62

Copy link
Collaborator

Choose a reason for hiding this comment

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

Lazy eyes, sorry.

@@ -0,0 +1,52 @@
namespace Jint
{
public interface IPrepareOptions<out TParseOptions>
Copy link
Collaborator

Choose a reason for hiding this comment

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

not my grammar, but IPreparationOptions?

@lahma lahma merged commit e1e2d6d into sebastienros:main Apr 9, 2024
3 checks passed
@lahma
Copy link
Collaborator

lahma commented Apr 9, 2024

Thank you once again!

@adams85
Copy link
Contributor Author

adams85 commented Apr 9, 2024

Sure thing!

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