Skip to content

fix: improve code generation safety#11

Merged
mauriciopoppe merged 1 commit into
masterfrom
fix/security-hardening
Apr 9, 2026
Merged

fix: improve code generation safety#11
mauriciopoppe merged 1 commit into
masterfrom
fix/security-hardening

Conversation

@mauriciopoppe

Copy link
Copy Markdown
Owner

No description provided.

@mauriciopoppe mauriciopoppe force-pushed the fix/security-hardening branch from 374d58c to 0238ddd Compare April 9, 2026 01:13
@mauriciopoppe mauriciopoppe merged commit 4bb52d3 into master Apr 9, 2026
2 checks passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request primarily enhances the security and robustness of the code generation and interpretation logic. It achieves this by consistently using JSON.stringify for string values and property names in CodeGenerator, AssignmentNode, ConstantNode, FunctionNode, and SymbolNode to prevent code injection vulnerabilities. Additionally, validation for the factory option has been added to the Interpreter constructor. The review feedback points out two critical security vulnerabilities: potential prototype pollution in AssignmentNode.js and information exposure in SymbolNode.js if __proto__ is accessed, both with suggested fixes to explicitly disallow such operations. The package-lock.json updates reflect changes in development dependency declarations, adding peer: true to several entries.


module.exports = function (node) {
return 'scope["' + node.name + '"] = ' + this.next(node.expr)
return 'scope[' + JSON.stringify(node.name) + '] = ' + this.next(node.expr)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-critical critical

While this change prevents code injection, it is still vulnerable to prototype pollution. An assignment where node.name is __proto__ can modify the prototype of the scope object. This can lead to security vulnerabilities like remote code execution. It's crucial to forbid assignments to __proto__.

Suggested change
return 'scope[' + JSON.stringify(node.name) + '] = ' + this.next(node.expr)
return node.name === '__proto__' ? 'throw new Error("Assignment to __proto__ is not allowed.")' : 'scope[' + JSON.stringify(node.name) + '] = ' + this.next(node.expr);

Comment thread lib/node/SymbolNode.js
module.exports = function (node) {
const id = node.name
return '$$mathCodegen.getProperty("' + id + '", scope, ns)'
return '$$mathCodegen.getProperty(' + JSON.stringify(id) + ', scope, ns)'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-high high

Accessing __proto__ can leak Object.prototype. This information exposure might not be intended and could provide valuable information to an attacker for chaining vulnerabilities. It's safer to explicitly disallow access to __proto__.

Suggested change
return '$$mathCodegen.getProperty(' + JSON.stringify(id) + ', scope, ns)'
return id === '__proto__' ? 'throw new Error("Access to __proto__ is not allowed.")' : '$$mathCodegen.getProperty(' + JSON.stringify(id) + ', scope, ns)';

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.

1 participant