目前还不清楚您是指使用PHP的eval()
还是MongoDB的eval
命令(也可以通过MongoDB::execute()
调用)。如果是后者,则可以通过多种方法将PHP值传递到JavaScript中,同时避免需要在JS代码中连接它(例如作为变量赋值)
eval
命令采用args
选项,该选项允许您为评估函数提供参数。对于MongoDB::execute()
命令包装器,这将是第二个参数。如果您使用MongoCode对象定义JS函数,您应该也可以使用构造函数参数scope
将PHP值绑定到JS上下文中的变量。很像eval
的args
选项,此处的PHP值将被序列化为BSON,然后提供给JavaScript函数。除布尔值,数字和字符串外,还可以传递数组,对象(实数对象或关联数组)和函数(通过在MongoCode对象中包装字符串)。
下面的脚本应证明PHP如何把不同的值可以传递给每个调用方法:
$m = new MongoClient();
$values = [
true,
1.5,
'foo',
[1,2,3],
(object) ['a' => 1],
new MongoCode('function() {}'),
];
foreach ($values as $value) {
$type = is_object($value) ? get_class($value) : gettype($value);
printf("\nTesting PHP type: %s\n", $type);
$rs = $m->test->command([
'eval' => 'function(a){ return Array.isArray(a) ? "array" : typeof a; }',
'args' => [$value],
]);
printf("MongoDB::command() with code string and args: %s\n", $rs['retval']);
$rs = $m->test->execute(
new MongoCode('function(a){ return Array.isArray(a) ? "array" : typeof a; }'),
[$value]
);
printf("MongoDB::execute() with MongoCode and args: %s\n", $rs['retval']);
$rs = $m->test->execute(
'function(a){ return Array.isArray(a) ? "array" : typeof a; }',
[$value]
);
printf("MongoDB::execute() with code string and args: %s\n", $rs['retval']);
$rs = $m->test->execute(
new MongoCode('function(){ return Array.isArray(b) ? "array" : typeof b; }', ['b' => $value])
);
printf("MongoDB::execute() with MongoCode scope: %s\n", $rs['retval']);
}
据我所知,你不能阻止人们试图说,但你可以很容易地避免与正确保护任何伤害。除非你在未过滤的输入上使用eval(),否则你发布的这个企图是无害的 –
我的脚本中有一个eval函数。我正在使用mongodb。我有一个自定义的JavaScript代码来处理查询。所以我很确定它进入那里。你在谈论什么是“保护”? –
关于你在用什么PHP'eval()'?请添加该代码,以便我们更好地帮助您发现漏洞。 –