
Improvement identified in #2790. Add a prefix to the diff-id when it is used multiple times in different "how to fix it in XYZ" sections to avoid ambiguity and pedantically follow the spec: > A single and unique diff-id should be used only once for each type of code example as shown in the description of a rule. Obvious typos around `diff-type` were fixed.
40 lines
887 B
Plaintext
40 lines
887 B
Plaintext
== How to fix it in Node.js
|
|
|
|
=== Code examples
|
|
|
|
The following code is vulnerable to arbitrary code execution because it dynamically
|
|
runs JavaScript code built from untrusted data.
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,javascript,diff-id=11,diff-type=noncompliant]
|
|
----
|
|
function (req, res) {
|
|
let operation = req.query.operation
|
|
eval(`product_${operation}()`) // Noncompliant
|
|
res.send("OK")
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,javascript,diff-id=11,diff-type=compliant]
|
|
----
|
|
const allowed = ["add", "remove", "update"]
|
|
|
|
let operationId = req.query.operationId
|
|
const operation = allowed[operationId]
|
|
eval(`product_${operation}()`)
|
|
res.send("OK")
|
|
----
|
|
|
|
=== How does this work?
|
|
|
|
include::../../common/fix/introduction.adoc[]
|
|
|
|
include::../../common/fix/parameters.adoc[]
|
|
|
|
include::../../common/fix/allowlist.adoc[]
|
|
|
|
The example compliant code uses such a binding approach.
|