Skip to content

Reject eval in expression-only parsing - #1223

Merged
schungx merged 1 commit into
rhaiscript:mainfrom
Rockyrok77:expression-reject-eval
Nov 20, 2026
Merged

schungx merged 1 commit into
rhaiscript:mainfrom
Rockyrok77:expression-reject-eval

Conversation

@Rockyrok77

Copy link
Copy Markdown
Contributor

eval_expression rejects assignments, but eval inside the expression does not:

let mut scope = Scope::new();
scope.push("balance", 10 as INT);

engine.eval_expression_with_scope::<Dynamic>(&mut scope, "balance = 1000000");
// Err: Syntax error: Unexpected '=' (line 1, position 9)

engine.eval_expression_with_scope::<Dynamic>(&mut scope, r#"eval("balance = 1000000")"#);
// Ok(()), and scope now holds balance = 1000000

The Book says expression-only evaluation allows no statements, "not even variable assignment", and no loops. parse_global_expr enforces that with DISALLOW_STATEMENTS_IN_BLOCKS, but parse_fn_call still accepts a call to eval, which compiles its argument as a full script and runs it in the caller's scope. So an expression from an untrusted user can assign to the host's scope variables, run eval("while true {}"), or eval("import \"/path/file\" as m; m::f()") to load and run a script file through the default FileModuleResolver.

The check sits in parse_fn_call because that flag is set only for expression-only parsing and closures inside an expression inherit it, so [1].map(|v| eval(...)) is rejected too. Full scripts are unaffected. Engine::parse_json also parses through parse_global_expr, so eval in JSON text is rejected as well; #1221 deals with function calls there in general.

@schungx
schungx merged commit 3ca0166 into rhaiscript:main Nov 20, 2026
54 of 56 checks passed
@Rockyrok77

Copy link
Copy Markdown
Contributor Author

thanks for taking this one, glad the expression path is closed off now.

@schungx

schungx commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

I'm wondering... Can we still support eval inside expressions but restrict it to an expression only?

Probably not as the AST does not retain such info. It is all caught at compile time.

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