|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Database\Builder; |
15 | 15 |
|
| 16 | +use CodeIgniter\Database\BaseBuilder; |
16 | 17 | use CodeIgniter\Test\CIUnitTestCase; |
17 | 18 | use CodeIgniter\Test\Mock\MockConnection; |
| 19 | +use CodeIgniter\Test\Mock\MockQuery; |
18 | 20 | use PHPUnit\Framework\Attributes\Group; |
19 | 21 |
|
20 | 22 | /** |
@@ -90,4 +92,81 @@ public function testGetCompiledDeleteWithLimit(): void |
90 | 92 | EOL; |
91 | 93 | $this->assertSame($expectedSQL, $sql); |
92 | 94 | } |
| 95 | + |
| 96 | + public function testDeleteBatchEscapesWhereBinds(): void |
| 97 | + { |
| 98 | + $db = new MockConnection([]); |
| 99 | + $builder = new BaseBuilder('jobs', $db); |
| 100 | + |
| 101 | + $data = [ |
| 102 | + ['id' => 1, 'name' => 'Derek J'], |
| 103 | + ['id' => 2, 'name' => 'Ahmadinejad'], |
| 104 | + ]; |
| 105 | + |
| 106 | + // A user-controlled value that would break out of the query if it |
| 107 | + // were substituted into the SQL unescaped. |
| 108 | + $malicious = "anything' OR '1'='1"; |
| 109 | + |
| 110 | + $db->shouldReturn('execute', new class () {}); |
| 111 | + $builder->setData($data, null, 'data') |
| 112 | + ->onConstraint(['id' => 'id']) |
| 113 | + ->where('jobs.name', $malicious) |
| 114 | + ->deleteBatch(); |
| 115 | + |
| 116 | + $query = $db->getLastQuery(); |
| 117 | + $this->assertInstanceOf(MockQuery::class, $query); |
| 118 | + |
| 119 | + // The bound value must be escaped and quoted as a single string literal: |
| 120 | + // it is wrapped in single quotes and the inner quotes are doubled, so it |
| 121 | + // cannot break out of the literal and inject SQL. |
| 122 | + $this->assertStringContainsString("'anything'' OR ''1''", $query->getQuery()); |
| 123 | + $this->assertStringNotContainsString("= anything' OR '1'='1", $query->getQuery()); |
| 124 | + } |
| 125 | + |
| 126 | + public function testDeleteBatchEscapesMultipleWhereBinds(): void |
| 127 | + { |
| 128 | + $db = new MockConnection([]); |
| 129 | + $builder = new BaseBuilder('jobs', $db); |
| 130 | + |
| 131 | + $data = [ |
| 132 | + ['id' => 1, 'name' => 'Derek J'], |
| 133 | + ['id' => 2, 'name' => 'Ahmadinejad'], |
| 134 | + ]; |
| 135 | + |
| 136 | + $db->shouldReturn('execute', new class () {}); |
| 137 | + $builder->setData($data, null, 'data') |
| 138 | + ->onConstraint(['id' => 'id']) |
| 139 | + ->where('jobs.name', "anything' OR '1'='1") |
| 140 | + ->where('jobs.id', 1) |
| 141 | + ->deleteBatch(); |
| 142 | + |
| 143 | + $query = $db->getLastQuery(); |
| 144 | + $this->assertInstanceOf(MockQuery::class, $query); |
| 145 | + |
| 146 | + $this->assertStringContainsString('"jobs"."name" = \'anything\'\' OR \'\'1\'\' = \'\'1\'', $query->getQuery()); |
| 147 | + $this->assertStringContainsString('"jobs"."id" = 1', $query->getQuery()); |
| 148 | + } |
| 149 | + |
| 150 | + public function testDeleteBatchEscapesWhereInBinds(): void |
| 151 | + { |
| 152 | + $db = new MockConnection([]); |
| 153 | + $builder = new BaseBuilder('jobs', $db); |
| 154 | + |
| 155 | + $data = [ |
| 156 | + ['id' => 1, 'name' => 'Derek J'], |
| 157 | + ['id' => 2, 'name' => 'Ahmadinejad'], |
| 158 | + ]; |
| 159 | + |
| 160 | + $db->shouldReturn('execute', new class () {}); |
| 161 | + $builder->setData($data, null, 'data') |
| 162 | + ->onConstraint(['id' => 'id']) |
| 163 | + ->whereIn('jobs.name', ["anything' OR '1'='1", 'Ahmadinejad']) |
| 164 | + ->deleteBatch(); |
| 165 | + |
| 166 | + $query = $db->getLastQuery(); |
| 167 | + $this->assertInstanceOf(MockQuery::class, $query); |
| 168 | + |
| 169 | + $this->assertStringContainsString("IN ('anything'' OR ''1''=''1','Ahmadinejad')", $query->getQuery()); |
| 170 | + $this->assertStringNotContainsString("IN anything' OR '1'='1", $query->getQuery()); |
| 171 | + } |
93 | 172 | } |
0 commit comments