From a4870d9237f5ee18fb42ecbcf91a4ffef26756a1 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Mon, 11 Mar 2024 09:21:30 +0900 Subject: [PATCH] fix: Connection::escapeString should escape backslashes (#197) * fix: Connection::escapeString should escape backslashes * update changelog * fix --- CHANGELOG.md | 3 +++ src/Connection.php | 4 ++-- tests/ConnectionTest.php | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e93d9d1..64b5185 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Changed - Performance enhancements for `DB::pretend` statements, no longer incurring the overhead of creating transactions (#191) - Set the `$defaultMorphKeyType` in `Schema\Builder` to `uuid` (#192) +Fixed +- `Connection::escapeString` now properly escapes backslashes (#197) + # v7.0.0 (2024-02-21) Added diff --git a/src/Connection.php b/src/Connection.php index fbe214c..2876c05 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -482,8 +482,8 @@ protected function escapeBool($value) protected function escapeString($value) { return str_contains($value, "\n") - ? 'r"""' . addcslashes($value, '"') . '"""' - : '"' . addcslashes($value, '"') . '"'; + ? 'r"""' . addcslashes($value, '"\\') . '"""' + : '"' . addcslashes($value, '"\\') . '"'; } /** diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 74f7f65..95f46e5 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -537,6 +537,7 @@ public function test_escape(): void $this->assertSame('-1', $conn->escape(-1)); $this->assertSame('1.1', $conn->escape(1.1)); $this->assertSame('"a"', $conn->escape('a')); + $this->assertSame('"\"a\\\\\\""', $conn->escape('"a\\"')); $this->assertSame('r"""' . "\n" . '"""', $conn->escape("\n")); $this->assertSame('[]', $conn->escape([])); $this->assertSame('["a"]', $conn->escape(['a']));