Dynamic method dispatch (ARCH3132)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Key typescript.security.dynamic-method-dispatch · Severity warning · Category security · CWE CWE-470
Taking the method name to call straight from user input lets attackers invoke unintended functions. Constrain callable methods with an allowlist.
Incorrect
Section titled “Incorrect”const service: Record<string, Function> = new UserService() as any;
app.post("/rpc", (req, res) => { const result = service[req.body.method](req.body.args); res.json({ result });});Correct
Section titled “Correct”const handlers: Record<string, (args: unknown) => unknown> = { getProfile: (a) => getProfile(a), listOrders: (a) => listOrders(a),};
app.post("/rpc", (req, res) => { const fn = handlers[req.body.method]; if (!fn) return res.sendStatus(400); res.json({ result: fn(req.body.args) });});Restrict callable methods to an explicit allowlist (dispatch table); never use the raw name directly.