[英]How can I change this the object property assignment to avoid prototype pollution
我有一行代码看起来像
gMapping[userName] = gMapping[userName] || [];
我看到了 Snyk 提出的 Prototype 污染漏洞。 如何解决?
相关代码:
const gMapping: { [user_name: string]: string[] } = {};
// Map records to dictionaries
dbRecs.forEach(rec => {
const userName = rec.user_name;
const groupId = rec.group_id;
gMapping[userName] = gMapping[userName] || [];
gMapping[userName].push(groupId);
}
});
问题是userName
可能是"__proto__"
。 我不确定这在您的情况下是否可以利用,但是在尝试在Object.prototype
上调用.push()
时仍然会导致异常。
为避免此问题,请使用Object.create(null)
(不幸的是,使用 TypeScript 并不容易)或切换到正确的 ES6 Map<string, string[]>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.