-
Notifications
You must be signed in to change notification settings - Fork 302
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
fix: CSP Security hole in tryConvertExpr (new Function(...)) #139
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -7,6 +7,10 @@ import Shader from './Shader'; | |
import Texture from './Texture'; | ||
import Texture2D from './Texture2D'; | ||
import TextureCube from './TextureCube'; | ||
import * as esprimaLoad from 'esprima' | ||
const esprima = esprimaLoad.default?esprimaLoad.default:esprimaLoad; | ||
import * as mexpLoad from 'math-expression-evaluator'; | ||
const Mexp = mexpLoad.default?mexpLoad.default:mexpLoad; | ||
|
||
import registerBuiltinCompositor from './shader/registerBuiltinCompositor'; | ||
|
||
|
@@ -144,7 +148,7 @@ function createNode(nodeInfo, lib, opts) { | |
else { | ||
node.on( | ||
'beforerender', createSizeSetHandler( | ||
name, tryConvertExpr(val) | ||
name, getSafeExpressionEvaluator(val) | ||
) | ||
); | ||
} | ||
|
@@ -197,7 +201,7 @@ function convertParameter(paramInfo) { | |
if (typeof val === 'string') { | ||
val = val.trim(); | ||
param[name] = createSizeParser( | ||
name, tryConvertExpr(val), sizeScale | ||
name, getSafeExpressionEvaluator(val), sizeScale | ||
); | ||
} | ||
else { | ||
|
@@ -289,21 +293,57 @@ function createSizeParser(name, exprFunc, scale) { | |
}; | ||
} | ||
|
||
function tryConvertExpr(string) { | ||
// PENDING | ||
var exprRes = /^expr\((.*)\)$/.exec(string); | ||
if (exprRes) { | ||
try { | ||
var func = new Function('width', 'height', 'dpr', 'return ' + exprRes[1]); | ||
// Try run t | ||
func(1, 1); | ||
function astToMathExpression(node, context) { | ||
switch (node.type) { | ||
case 'BinaryExpression': | ||
let left = astToMathExpression(node.left, context); | ||
let right = astToMathExpression(node.right, context); | ||
return `(${left} ${node.operator} ${right})`; | ||
case 'Literal': | ||
return node.value; | ||
case 'Identifier': | ||
if (['width', 'height', 'dpr'].includes(node.name)) { | ||
return context[node.name]; | ||
} | ||
throw new Error(`Unknown identifier: ${node.name}`); | ||
default: | ||
throw new Error(`Unsupported node type: ${node.type}`); | ||
} | ||
} | ||
|
||
return func; | ||
} | ||
catch (e) { | ||
throw new Error('Invalid expression.'); | ||
function getSafeExpressionEvaluator(expression) { | ||
if (!expression) return; | ||
var exprRes = /^expr\((.*)\)$/.exec(expression); | ||
if (!exprRes) return; | ||
|
||
const ast = esprima.parseScript(exprRes[1]); | ||
|
||
const func = function(width, height, dpr) { | ||
const context = { width, height, dpr }; | ||
const mexp = new Mexp() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the |
||
// Handle array of expressions | ||
if (ast.body[0].type === 'ExpressionStatement' && ast.body[0].expression.type === 'ArrayExpression') { | ||
return ast.body[0].expression.elements.map(element => { | ||
const mathExpression = astToMathExpression(element, context); | ||
return mexp.eval(mathExpression); | ||
}); | ||
} | ||
|
||
// Handle single expression | ||
const mathExpression = astToMathExpression(ast.body[0].expression, context); | ||
return mexp.eval(mathExpression); | ||
}; | ||
|
||
// Try run t | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove debug code |
||
func(1, 1); | ||
return func; | ||
} | ||
catch (e) { | ||
throw new Error('Invalid expression.'); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
|
||
export default createCompositor; |
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.
If the expressions are as simple as the bug implies then rather than ast and all the overhead of generating it perhaps simple str substitution and split could do the job.
This would remove the need for
esprima
and theastToMathExpression()
functionProbably needs some exception handling as well