From 895c63569e1d6e408bf9f1e0c5e1b3ce9c45e970 Mon Sep 17 00:00:00 2001 From: ZeroPath Date: Mon, 7 Apr 2025 03:04:39 +0000 Subject: [PATCH 1/2] fix: add SQL injection protection using addslashes for input sanitization --- test/index.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/index.php b/test/index.php index 0a56b64..aeaf1af 100644 --- a/test/index.php +++ b/test/index.php @@ -6,8 +6,9 @@ die('Invalid input'); } -// Convert to string and apply strict XSS protection +// Convert to string and apply both XSS and SQL injection protection $input = (string)$_GET['test']; +$input = addslashes($input); // Add SQL injection protection echo htmlspecialchars($input, ENT_QUOTES | ENT_HTML5, 'UTF-8', true); ?> From 3366cbdc0c62453f622dc3a1615c5c3cb6f56deb Mon Sep 17 00:00:00 2001 From: ZeroPath Date: Mon, 7 Apr 2025 03:06:00 +0000 Subject: [PATCH 2/2] fix: implement PDO prepared statements to prevent SQL injection --- test/index.php | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/test/index.php b/test/index.php index aeaf1af..322ad88 100644 --- a/test/index.php +++ b/test/index.php @@ -1,14 +1,34 @@ PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => false + ]); + + // Validate input exists + if (!isset($_GET['test'])) { + die('Invalid input'); + } -// Convert to string and apply both XSS and SQL injection protection -$input = (string)$_GET['test']; -$input = addslashes($input); // Add SQL injection protection -echo htmlspecialchars($input, ENT_QUOTES | ENT_HTML5, 'UTF-8', true); + // Convert to string and apply XSS protection for output + $input = (string)$_GET['test']; + + // Prepare and execute the query safely + $stmt = $pdo->prepare('SELECT * FROM yourtable WHERE column = ?'); + $stmt->execute([$input]); + + // Fetch and display results with XSS protection + $result = $stmt->fetch(PDO::FETCH_ASSOC); + if ($result) { + echo htmlspecialchars(json_encode($result), ENT_QUOTES | ENT_HTML5, 'UTF-8', true); + } +} catch (PDOException $e) { + // Log the error securely (don't expose details to users in production) + error_log($e->getMessage()); + die('An error occurred'); +} ?>