-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingCodeVisitor.php
More file actions
132 lines (116 loc) · 4.78 KB
/
Copy pathBlockingCodeVisitor.php
File metadata and controls
132 lines (116 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace PivotPHP\ReactPHP\Security;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
/**
* AST Visitor for detecting blocking code
*/
final class BlockingCodeVisitor extends NodeVisitorAbstract
{
private array $violations;
private string $context;
public function __construct(array &$violations, string $context)
{
$this->violations = &$violations;
$this->context = $context;
}
public function enterNode(Node $node)
{
// Check function calls
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name) {
$functionName = $node->name->toString();
if (isset(BlockingCodeDetector::BLOCKING_FUNCTIONS[$functionName])) {
$this->violations[] = [
'type' => 'blocking_function',
'severity' => 'error',
'function' => $functionName,
'line' => $node->getLine(),
'context' => $this->context,
'message' => "Blocking function '$functionName' will freeze the server",
'suggestion' => BlockingCodeDetector::BLOCKING_FUNCTIONS[$functionName],
];
} elseif (isset(BlockingCodeDetector::WARNING_FUNCTIONS[$functionName])) {
$this->violations[] = [
'type' => 'unsafe_function',
'severity' => 'warning',
'function' => $functionName,
'line' => $node->getLine(),
'context' => $this->context,
'message' => "Function '$functionName' may cause issues in ReactPHP",
'suggestion' => BlockingCodeDetector::WARNING_FUNCTIONS[$functionName],
];
}
}
// Check for exit/die (language constructs, not functions)
if ($node instanceof Node\Expr\Exit_) {
$type = $node->getAttribute('kind') === Node\Expr\Exit_::KIND_DIE ? 'die' : 'exit';
$this->violations[] = [
'type' => 'blocking_function',
'severity' => 'error',
'function' => $type,
'line' => $node->getLine(),
'context' => $this->context,
'message' => "Language construct '$type' kills the entire server",
'suggestion' => BlockingCodeDetector::BLOCKING_FUNCTIONS[$type],
];
}
// Check for global variable access
if ($node instanceof Node\Expr\Variable) {
$varName = is_string($node->name) ? $node->name : null;
if ($varName !== null && in_array($varName, ['GLOBALS', '_SESSION', '_SERVER', '_ENV'], true)) {
$this->violations[] = [
'type' => 'global_access',
'severity' => 'warning',
'variable' => '$' . $varName,
'line' => $node->getLine(),
'context' => $this->context,
'message' => "Global variable \$$varName is shared across all requests",
'suggestion' => 'Use request attributes or dependency injection',
];
}
}
// Check for static variables in functions
if ($node instanceof Node\Stmt\Static_) {
$this->violations[] = [
'type' => 'static_variable',
'severity' => 'warning',
'line' => $node->getLine(),
'context' => $this->context,
'message' => 'Static variables persist across requests',
'suggestion' => 'Use class properties with proper lifecycle management',
];
}
// Check for infinite loops
if ($node instanceof Node\Stmt\While_ && $this->isPotentiallyInfinite($node->cond)) {
$this->violations[] = [
'type' => 'infinite_loop',
'severity' => 'error',
'line' => $node->getLine(),
'context' => $this->context,
'message' => 'Potentially infinite loop will block the server',
'suggestion' => 'Add timeout or use ReactPHP periodic timers',
];
}
return null;
}
/**
* Check if a condition might be infinite
*/
private function isPotentiallyInfinite(Node $condition): bool
{
// Check for while(true) or while(1)
if ($condition instanceof Node\Expr\ConstFetch) {
$name = $condition->name->toString();
return $name === 'true';
}
if ($condition instanceof Node\Scalar\LNumber && $condition->value === 1) {
return true;
}
return false;
}
public function getViolations(): array
{
return $this->violations;
}
}