2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-06-08 14:23:48 +02:00
|
|
|
This rule will check that:
|
|
|
|
|
|
|
|
* the sql query is not built using a concatenation
|
|
|
|
* there is at least a call to bindParm between the call to prepare and fetch on the PDO connection object
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-06-08 14:23:48 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,php]
|
2021-06-08 14:23:48 +02:00
|
|
|
----
|
|
|
|
$id = $_GET['id'];
|
|
|
|
try {
|
|
|
|
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
|
|
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
|
|
|
|
$stmt = $conn->prepare('SELECT * FROM myTable WHERE id = ' + $id);
|
|
|
|
|
|
|
|
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
|
|
|
|
echo $row->name;
|
|
|
|
}
|
|
|
|
} catch(PDOException $e) {
|
|
|
|
echo 'ERROR: ' . $e->getMessage();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-06-08 14:23:48 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,php]
|
2021-06-08 14:23:48 +02:00
|
|
|
----
|
|
|
|
$id = $_GET['id'];
|
|
|
|
try {
|
|
|
|
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
|
|
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
|
|
|
|
$stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
|
|
|
|
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
|
|
|
|
|
|
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
|
|
|
|
echo $row->name;
|
|
|
|
}
|
|
|
|
} catch(PDOException $e) {
|
|
|
|
echo 'ERROR: ' . $e->getMessage();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Resources
|
2021-11-01 15:00:32 +01:00
|
|
|
|
|
|
|
* https://owasp.org/Top10/A03_2021-Injection/[OWASP Top 10 2021 Category A3] - Injection
|
2022-08-18 10:33:50 +02:00
|
|
|
* https://cwe.mitre.org/data/definitions/89[MITRE, CWE-89] - Improper Neutralization of Special Elements used in an SQL Command
|
2021-11-01 15:00:32 +01:00
|
|
|
|
2021-06-08 14:23:48 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-08 14:23:48 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== on 28 Jul 2015, 11:15:41 Ann Campbell wrote:
|
|
|
|
\[~alexandre.gigleux] isn't this just like the subtask I closed yesterday? I rolled the gist of that one into the other subtask...
|
|
|
|
|
|
|
|
=== on 28 Jul 2015, 11:25:14 Alexandre Gigleux wrote:
|
|
|
|
That's correct. Creating it as a SubTask was not correct hence why I created again as a Task.
|
|
|
|
|
|
|
|
=== on 28 Jul 2015, 11:32:20 Ann Campbell wrote:
|
|
|
|
There's no need for this RSpec [~alexandre.gigleux]. I've already rolled the gist of it into the other subtask.
|
|
|
|
|
2021-06-08 14:23:48 +02:00
|
|
|
endif::env-github,rspecator-view[]
|