-
Notifications
You must be signed in to change notification settings - Fork 15
refactor: replace eval with ShadowRealm for safe expression evaluation #103
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -163,6 +163,34 @@ async function generatorToolExample() { | |||||
| const _message = await response.getMessage(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Safely evaluate a math expression using ShadowRealm | ||||||
| * | ||||||
| * ShadowRealm provides a secure, isolated JavaScript execution environment | ||||||
| * that prevents access to the host realm's globals (no access to DOM, fetch, | ||||||
| * filesystem, etc.). This makes it safe for evaluating untrusted expressions. | ||||||
| * | ||||||
| * Browser support: Chrome 124+, Edge 124+, Firefox 128+, Safari 18+ | ||||||
| * Node.js support: Available behind --experimental-shadow-realm flag (v19+) | ||||||
| * See: https://tc39.es/proposal-shadowrealm/ | ||||||
| */ | ||||||
| async function safeEvaluateMath(expression: string): Promise<number> { | ||||||
| // ShadowRealm is a TC39 Stage 3 proposal for isolated JavaScript execution | ||||||
| // It creates a separate realm with its own global object, preventing access | ||||||
| // to the host environment's APIs (no DOM, no Node.js APIs, no fetch, etc.) | ||||||
| const realm = new ShadowRealm(); | ||||||
|
||||||
|
|
||||||
| // The expression is evaluated in complete isolation - even if malicious code | ||||||
| // is injected, it cannot access anything outside the shadow realm | ||||||
| const result = await realm.evaluate(`(${expression})`); | ||||||
|
||||||
| const result = await realm.evaluate(`(${expression})`); | |
| const result = realm.evaluate(`(${expression})`); |
Outdated
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapping the expression in parentheses allows arbitrary JavaScript expressions, not just math operations. For example, "1, console.log('hi'), 2" would be valid. Consider validating the expression contains only allowed characters (digits, operators, parentheses, whitespace) before evaluation:
// Validate expression contains only safe math characters
if (!/^[\d\s+\-*/.()]+$/.test(expression)) {
throw new Error('Expression contains invalid characters');
}
const result = realm.evaluate(expression);
Outdated
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since safeEvaluateMath() should be synchronous (not async), remove the await keyword here:
const _result = safeEvaluateMath(expression);| const _result = await safeEvaluateMath(expression); | |
| const _result = safeEvaluateMath(expression); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function signature declares
asyncand returnsPromise<number>, but sinceShadowRealm.prototype.evaluate()is synchronous (not a Promise), this function should not be async. Change to:This also requires updating the call site at line 236 to remove
await.