我如何更改此对象 属性 分配以避免原型污染

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() 时它仍然会导致异常

, either use Object.create(null) (which isn't easy with TypeScript,不幸的是)或切换到正确的 ES6 Map<string, string[]>