From f894b32d263ac0e60389406a0b931c82a1b7360e Mon Sep 17 00:00:00 2001 From: M9nx Date: Mon, 7 Sep 2026 08:39:27 +0300 Subject: [PATCH] fix(safety): exclude PDO method calls from exec() detection Resolves #19 The exec() danger pattern used a simple word-boundary regex that matched any call whose name ended in 'exec', including PHP PDO method calls such as $pdo->exec() and PDO::exec(). Apply negative look-behinds for '->' and '::' so that only standalone function invocations (the global PHP exec() / Python exec()) are flagged. Method-call forms are safe SQL-execution APIs and must not generate false-positive safety warnings. Changes: - semantic_code_intelligence/llm/safety.py: update exec() pattern from \bexec\s*\( to (?)(?exec(), $connection->exec(), and PDO::exec() are not flagged --- semantic_code_intelligence/llm/safety.py | 2 +- .../tests/test_phase12.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/semantic_code_intelligence/llm/safety.py b/semantic_code_intelligence/llm/safety.py index 3b4e900..b6b9b79 100644 --- a/semantic_code_intelligence/llm/safety.py +++ b/semantic_code_intelligence/llm/safety.py @@ -23,7 +23,7 @@ (r"\brm\s+-rf\s+/", "Destructive rm -rf / command"), # Dynamic code execution (r"\beval\s*\(", "eval() call — avoid dynamic code execution"), - (r"\bexec\s*\(", "exec() call — avoid dynamic code execution"), + (r"(?)(?exec() is a PDO method call and must NOT be flagged.""" + assert self.validator.is_safe("$pdo->exec('CREATE TABLE foo (id INT)')") + + def test_connection_arrow_exec_not_flagged(self): + """PHP $connection->exec() is a PDO method call and must NOT be flagged.""" + assert self.validator.is_safe("$connection->exec($sql)") + + def test_pdo_static_exec_not_flagged(self): + """PHP PDO::exec() static call must NOT be flagged.""" + assert self.validator.is_safe("PDO::exec($statement)") + + def test_pdo_exec_multiline_not_flagged(self): + """Multi-line PHP code using PDO method calls should pass the safety check.""" + php_code = ( + "$pdo = new PDO($dsn, $user, $pass);\n" + "$pdo->exec('SET NAMES utf8mb4');\n" + "$connection->exec($migrationSql);\n" + ) + assert self.validator.is_safe(php_code) + # ========================================================================= # VSCode streaming context tests