Skip to content

Commit

Permalink
feat: add more cases to eval rule
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Oct 9, 2023
1 parent 275a473 commit 346baad
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
10 changes: 10 additions & 0 deletions rules/php/lang/eval_using_user_input.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ patterns:
- variable: USER_INPUT
detection: php_shared_lang_user_input
scope: result
- pattern: assert($<USER_INPUT>$<...>)
filters:
- variable: USER_INPUT
detection: php_shared_lang_user_input
scope: result
- pattern: array_map($<USER_INPUT>$<...>)
filters:
- variable: USER_INPUT
detection: php_shared_lang_user_input
scope: result
languages:
- php
severity: high
Expand Down
74 changes: 72 additions & 2 deletions tests/php/lang/eval_using_user_input/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ exports[`php_lang_eval_using_user_input bad 1`] = `
"snippet": "call_user_func(\\"func_\\" . $_POST[\\"oops\\"], 42)",
"fingerprint": "43b4511ce098191dea3fa024aa7f66cc_1",
"old_fingerprint": "2935b68ea0455100c1ee50d53b6d628a_1",
"code_extract": "call_user_func(\\"func_\\" . $_POST[\\"oops\\"], 42)"
"code_extract": "call_user_func(\\"func_\\" . $_POST[\\"oops\\"], 42);"
},
{
"cwe_ids": [
Expand Down Expand Up @@ -106,7 +106,77 @@ exports[`php_lang_eval_using_user_input bad 1`] = `
"snippet": "call_user_func_array(\\"func_\\" . $_POST[\\"oops\\"], [42])",
"fingerprint": "43b4511ce098191dea3fa024aa7f66cc_2",
"old_fingerprint": "2935b68ea0455100c1ee50d53b6d628a_2",
"code_extract": "call_user_func_array(\\"func_\\" . $_POST[\\"oops\\"], [42])"
"code_extract": "call_user_func_array(\\"func_\\" . $_POST[\\"oops\\"], [42]);"
},
{
"cwe_ids": [
"94",
"95"
],
"id": "php_lang_eval_using_user_input",
"title": "Potential command injection with user input detected.",
"description": "## Description\\nIt is dangerous to use eval with user input, or to compile code with user-supplied data. Such practices can lead to command injection.\\n\\n## Remediations\\n❌ Avoid using code execution methods with unsanitized user input.\\n\\n## Resources\\n- [OWASP Code injection explained](https://owasp.org/www-community/attacks/Code_Injection)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_eval_using_user_input",
"line_number": 8,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 8,
"end": 8,
"column": {
"start": 1,
"end": 31
}
},
"sink": {
"start": 8,
"end": 8,
"column": {
"start": 1,
"end": 31
},
"content": "assert($_POST[\\"oops\\"], \\"oops\\")"
},
"parent_line_number": 8,
"snippet": "assert($_POST[\\"oops\\"], \\"oops\\")",
"fingerprint": "43b4511ce098191dea3fa024aa7f66cc_3",
"old_fingerprint": "2935b68ea0455100c1ee50d53b6d628a_3",
"code_extract": "assert($_POST[\\"oops\\"], \\"oops\\");"
},
{
"cwe_ids": [
"94",
"95"
],
"id": "php_lang_eval_using_user_input",
"title": "Potential command injection with user input detected.",
"description": "## Description\\nIt is dangerous to use eval with user input, or to compile code with user-supplied data. Such practices can lead to command injection.\\n\\n## Remediations\\n❌ Avoid using code execution methods with unsanitized user input.\\n\\n## Resources\\n- [OWASP Code injection explained](https://owasp.org/www-community/attacks/Code_Injection)\\n",
"documentation_url": "https://docs.bearer.com/reference/rules/php_lang_eval_using_user_input",
"line_number": 9,
"full_filename": "/tmp/bearer-scan/bad.php",
"filename": ".",
"source": {
"start": 9,
"end": 9,
"column": {
"start": 1,
"end": 34
}
},
"sink": {
"start": 9,
"end": 9,
"column": {
"start": 1,
"end": 34
},
"content": "array_map($_POST[\\"oops\\"], $array)"
},
"parent_line_number": 9,
"snippet": "array_map($_POST[\\"oops\\"], $array)",
"fingerprint": "43b4511ce098191dea3fa024aa7f66cc_4",
"old_fingerprint": "2935b68ea0455100c1ee50d53b6d628a_4",
"code_extract": "array_map($_POST[\\"oops\\"], $array);"
}
]
}"
Expand Down
7 changes: 5 additions & 2 deletions tests/php/lang/eval_using_user_input/testdata/bad.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

eval("echo " . $_GET["oops"]);

call_user_func("func_" . $_POST["oops"], 42)
call_user_func_array("func_" . $_POST["oops"], [42])
call_user_func("func_" . $_POST["oops"], 42);
call_user_func_array("func_" . $_POST["oops"], [42]);

assert($_POST["oops"], "oops");
array_map($_POST["oops"], $array);
3 changes: 3 additions & 0 deletions tests/php/lang/eval_using_user_input/testdata/ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

call_user_func("func_" . $ok, 42)
call_user_func_array("func_" . $ok, [42])

assert($ok, "ok");
array_map($ok, $array);

0 comments on commit 346baad

Please sign in to comment.