为什么JS中的类/函数构造函数将字符串作为表达式求值?

问题描述 投票:1回答:1

[我看了eslint-plugin-security中的规则之一,发现用户输入理论上可能导致远程代码执行错误。

const a = class {};
console.log(a['constructor']);
a['constructor']('console.log(1)')();

function b() {}
console.log(b['constructor']);
b['constructor']('console.log(2)')();

const c = {}
console.log(c['constructor'])
console.log(c['constructor']('console.log(3)')());

从摘要中很容易看出,类和函数的构造函数似乎在解析字符串并将其评估为有效代码。由于某种原因对象没有表现出这种行为。

为什么甚至允许发生这种情况? JavaScript的哪些功能需要函数/类构造函数的此行为?我假设它是JavaScript工作方式不可或缺的一部分,否则我不明白为什么未从语言中删除它。

javascript node.js security
1个回答
2
投票

问题是类的.constructorFunction,并且使用字符串调用Function构造函数会从该字符串创建一个函数,然后调用该函数会导致执行该字符串的代码:

const a = class {};
console.dir(a['constructor'] === Function);
a['constructor']('console.log(1)')();

与这没什么不同

Function('console.log(1)')();

只是类的constructor属性恰好指向同一件事。

对象可以显示相同的属性,如果您两次浏览到.constructor属性(第一个访问Object构造函数,第二个访问Function构造函数:]

const a = {};
console.dir(a['constructor'].constructor === Function);
a['constructor'].constructor('console.log(1)')();

如果您允许任意访问任何对象的属性,并且还允许使用任意参数调用这些属性,则几乎可以执行任何操作。原型链(和.constructor)属性为[[有用,但像许多事物一样,它们可能会被滥用。

© www.soinside.com 2019 - 2024. All rights reserved.