Skip to content

Commit fcfe489

Browse files
authored
php: dangerous eval (#115)
1 parent 07e04ea commit fcfe489

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
function test_dangerous_eval() {
4+
$user_input = $_GET['input'];
5+
6+
// These should be flagged
7+
// <expect-error>
8+
eval($user_input);
9+
10+
// <expect-error>
11+
eval("echo " . $user_input . "hi");
12+
13+
// String interpolation
14+
// <expect-error>
15+
eval("echo $user_input");
16+
17+
// Superglobal (outside our control) sources
18+
// <expect-error>
19+
eval($_GET['username']);
20+
21+
// These are safe and should not be flagged
22+
// constants
23+
eval('echo "Hello, World!"');
24+
25+
}
26+
27+
function test_edge_cases() {
28+
// Should not flag eval in variable names
29+
$evaluation_result = 100;
30+
31+
// Should not flag commented-out eval
32+
// eval($user_input);
33+
}

checkers/php/dangerous_eval.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
language: php
2+
name: dangerous_eval
3+
message: "Avoid using eval() with dynamic inputs as it can lead to remote code execution (RCE) vulnerabilities"
4+
category: security
5+
severity: critical
6+
7+
pattern: |
8+
;; Match direct eval calls with variable input
9+
(expression_statement
10+
(function_call_expression
11+
function: (name) @function (#eq? @function "eval")
12+
arguments: (arguments
13+
(argument
14+
(variable_name) @user_input
15+
)
16+
)
17+
)
18+
) @dangerous_eval
19+
20+
;; Match eval calls with string concatenation
21+
(expression_statement
22+
(function_call_expression
23+
function: (name) @function (#eq? @function "eval")
24+
arguments: (arguments
25+
(argument
26+
(binary_expression
27+
left: [
28+
(encapsed_string)
29+
(binary_expression)
30+
]
31+
right: [
32+
(encapsed_string)
33+
(variable_name) @user_input
34+
]
35+
)
36+
)
37+
)
38+
)
39+
) @dangerous_eval
40+
41+
;; Match eval calls with interpolated strings containing variables
42+
(expression_statement
43+
(function_call_expression
44+
function: (name) @function (#eq? @function "eval")
45+
arguments: (arguments
46+
(argument
47+
(encapsed_string
48+
(variable_name) @user_input
49+
)
50+
)
51+
)
52+
)
53+
) @dangerous_eval
54+
55+
;; Match eval calls with superglobal input sources
56+
(expression_statement
57+
(function_call_expression
58+
function: (name) @function (#eq? @function "eval")
59+
arguments: (arguments
60+
(argument
61+
(subscript_expression
62+
(variable_name (name) @superglobal)
63+
(#match? @superglobal "^_(GET|POST|REQUEST|COOKIE|SERVER|ENV|FILES|SESSION)$")
64+
)
65+
)
66+
)
67+
)
68+
) @dangerous_eval
69+
70+
exclude:
71+
- "tests/**"
72+
- "vendor/**"
73+
- "**/test_*.php"
74+
- "**/*_test.php"
75+
76+
description: |
77+
The use of eval() in PHP without validating the input can lead to the execution
78+
of arbitrary code, resulting in potential remote code execution (RCE) vulnerabilities.

0 commit comments

Comments
 (0)