-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityTest.php
More file actions
323 lines (272 loc) · 13.3 KB
/
SecurityTest.php
File metadata and controls
323 lines (272 loc) · 13.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* @package InitORM\QueryBuilder
* @license MIT
*/
declare(strict_types=1);
namespace Test\InitORM\QueryBuilder;
use InitORM\QueryBuilder\Drivers\MySqlDriver;
use InitORM\QueryBuilder\Drivers\PostgreSqlDriver;
use InitORM\QueryBuilder\Exceptions\QueryBuilderInvalidArgumentException;
use InitORM\QueryBuilder\Helper\SqlValueDetector;
use InitORM\QueryBuilder\QueryBuilder;
/**
* Security regression tests covering both the defenses added in v2.0.0 and
* the documented residual risks. See docs/en/security.md for the full
* threat-model write-up.
*
* Defenses:
* - V3 — identifier escape rejects ";" and "--"
* - V4 — LIKE wildcards (%, _) and the escape char (\) in supplied values
* are auto-escaped before being concatenated into the LIKE pattern
* - V6 — bind-placeholder regex tightened to ^:\w+$
*
* Documented residual risks (NOT fixed in code — application-level concern):
* - V1 — function-shaped strings ("NOW()", "CURRENT_USER()") in a value
* slot are inlined as SQL, bypassing the parameter bag
* - V2 — dotted column references ("users.password") in a value slot are
* inlined as a column reference
* - V5 — orderBy() does not whitelist column names
*
* The "documented residual risk" tests pin the current behavior so future
* refactors do not silently broaden the attack surface.
*/
class SecurityTest extends AbstractQueryBuilderUnit
{
// -----------------------------------------------------------------------
// V3 — escapeIdentifier rejects query-breakout sequences
// -----------------------------------------------------------------------
public function testEscapeIdentifierRejectsSemicolon(): void
{
$this->expectException(QueryBuilderInvalidArgumentException::class);
$this->expectExceptionMessage('forbidden SQL sequence (;)');
(new MySqlDriver())->escapeIdentifier('users; DROP TABLE x');
}
public function testEscapeIdentifierRejectsDoubleHyphenCommentLeader(): void
{
$this->expectException(QueryBuilderInvalidArgumentException::class);
$this->expectExceptionMessage('forbidden SQL sequence (--)');
(new MySqlDriver())->escapeIdentifier('users -- comment');
}
public function testEscapeIdentifierRejectsForbiddenSequenceEvenOnGenericDriver(): void
{
// GenericDriver is the no-op driver. The validation runs *before*
// the no-op return, so even unquoted drivers gain defense-in-depth.
$qb = new QueryBuilder(); // GenericDriver
$this->expectException(QueryBuilderInvalidArgumentException::class);
$qb->getDriver()->escapeIdentifier('users; DROP TABLE x');
}
public function testFromWithSemicolonInjectionRaises(): void
{
$this->expectException(QueryBuilderInvalidArgumentException::class);
$this->db->from('users; DROP TABLE x; --');
}
public function testWhereWithDoubleHyphenInjectionRaises(): void
{
$this->expectException(QueryBuilderInvalidArgumentException::class);
$this->db->from('users')->where('name -- ', 'admin');
}
public function testPostgreSqlDriverAlsoRejectsForbiddenSequences(): void
{
// PostgreSQL allows multi-statement queries by default, which makes
// the validation particularly important on that driver.
$this->expectException(QueryBuilderInvalidArgumentException::class);
(new PostgreSqlDriver())->escapeIdentifier('users; DROP');
}
public function testLegitimateIdentifiersWithOperatorsStillWork(): void
{
$driver = new MySqlDriver();
// JOIN-style ON expressions go through escapeIdentifier; operators
// like = and > must continue to pass through.
$this->assertSame('`a` = `b`', $driver->escapeIdentifier('a = b'));
$this->assertSame('`a`.`b` AND `c`.`d`', $driver->escapeIdentifier('a.b AND c.d'));
}
// -----------------------------------------------------------------------
// V4 — LIKE wildcard auto-escape
// -----------------------------------------------------------------------
public function testLikeEscapesPercentSignInUserValue(): void
{
$this->db->from('user')->like('name', '50%');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
// The user's literal "%" is escaped — the surrounding wildcards
// ("both" type) are still added by the builder.
$this->assertSame('%50\\%%', $params[':name']);
}
public function testLikeEscapesUnderscoreInUserValue(): void
{
$this->db->from('user')->like('name', 'a_b');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
$this->assertSame('%a\\_b%', $params[':name']);
}
public function testLikeEscapesBackslashFirstThenWildcards(): void
{
$this->db->from('user')->like('name', 'a\\b%c');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
// The \ is doubled, then the % is escaped.
$this->assertSame('%a\\\\b\\%c%', $params[':name']);
}
public function testStartLikeEscapesWildcardsInValue(): void
{
$this->db->from('user')->startLike('name', 'a%b');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
$this->assertSame('a\\%b%', $params[':name']);
}
public function testEndLikeEscapesWildcardsInValue(): void
{
$this->db->from('user')->endLike('name', 'a%b');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
$this->assertSame('%a\\%b', $params[':name']);
}
public function testLikeWithRawQueryValueBypassesEscapeAsDocumented(): void
{
// Opt-out: when the caller deliberately passes RawQuery, the value
// is inlined verbatim and the wildcards survive untouched.
$this->db->from('user')->like('name', $this->db->raw("'custom%pattern'"));
$sql = $this->db->generateSelectQuery();
$this->assertEquals(
"SELECT * FROM user WHERE name LIKE 'custom%pattern'",
$sql,
);
}
public function testLikeWithExplicitNamedPlaceholderBypassesEscape(): void
{
// ":foo" placeholder is recognized by isSqlParameter() and emitted
// verbatim. Useful when the caller has pre-bound the parameter and
// wants full control over the pattern.
$this->db->from('user')->like('name', $this->db->raw(':needle'));
$sql = $this->db->generateSelectQuery();
$this->assertEquals(
'SELECT * FROM user WHERE name LIKE :needle',
$sql,
);
}
public function testWildcardAttackOnlyMatchesEscapedRowsAfterFix(): void
{
// Pre-fix, like('name', '%') would compile to LIKE '%%%' which is
// semantically LIKE '%' (matches every row). Post-fix the % is
// escaped so the pattern only matches rows containing the literal
// "%" character.
$this->db->from('user')->like('name', '%');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
$this->assertSame('%\\%%', $params[':name']);
}
// -----------------------------------------------------------------------
// V6 — placeholder regex tightened
// -----------------------------------------------------------------------
public function testIsSqlParameterAcceptsCanonicalPlaceholderShapes(): void
{
$this->assertTrue(SqlValueDetector::isSqlParameter('?'));
$this->assertTrue(SqlValueDetector::isSqlParameter(':foo'));
$this->assertTrue(SqlValueDetector::isSqlParameter(':foo_1'));
$this->assertTrue(SqlValueDetector::isSqlParameter(':a_b_c'));
}
public function testIsSqlParameterRejectsValuesContainingParens(): void
{
// Pre-V6 the character class [(\w)]+ permitted "(" and ")" inside
// placeholder names — never a legal PDO bind name. Now rejected.
$this->assertFalse(SqlValueDetector::isSqlParameter(':foo()'));
$this->assertFalse(SqlValueDetector::isSqlParameter(':((('));
$this->assertFalse(SqlValueDetector::isSqlParameter(':foo)bar'));
}
public function testIsSqlParameterRejectsNonStringAndShellLikeValues(): void
{
$this->assertFalse(SqlValueDetector::isSqlParameter('foo')); // no leading :
$this->assertFalse(SqlValueDetector::isSqlParameter(':')); // bare colon
$this->assertFalse(SqlValueDetector::isSqlParameter(':foo bar')); // space
$this->assertFalse(SqlValueDetector::isSqlParameter(5)); // not a string
$this->assertFalse(SqlValueDetector::isSqlParameter(null)); // not a string
}
// -----------------------------------------------------------------------
// V1 / V2 — DOCUMENTED RESIDUAL RISKS
//
// The auto-detection in SqlValueDetector::isSqlParameterOrFunction() is
// intentionally lenient so that callers can write
// $qb->set('updated_at', 'NOW()')
// and have it inlined without ceremony. The trade-off is that the same
// lenience applies when a value happens to look like a function call or
// a dotted column reference — including when that value originates from
// user input. Application code MUST NOT forward unsanitized user input
// into the value slot of where()/set() etc. The tests below pin the
// current behavior so refactors do not silently regress.
//
// See docs/en/security.md §V1, §V2 for the corresponding warnings.
// -----------------------------------------------------------------------
public function testFunctionShapedValueIsInlinedAsDocumentedRisk(): void
{
// ⚠️ This documents a known residual risk, not desired behavior for
// user-controlled inputs. Callers MUST sanitize / validate the
// value before passing it in.
$this->db->from('user')->where('id', 'CURRENT_USER()');
$sql = $this->db->generateSelectQuery();
// The value is inlined as a SQL function call rather than being
// parameterized.
$this->assertStringContainsString('id = CURRENT_USER()', $sql);
}
public function testFunctionWithArgumentsIsParameterizedNotInlined(): void
{
// The function-detection regex requires *empty* parentheses; calls
// with arguments (e.g. SLEEP(10)) do NOT match and are routed to
// the parameter bag.
$this->db->from('user')->where('id', 'SLEEP(10)');
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
$this->assertSame('SLEEP(10)', $params[':id']);
}
public function testDottedColumnReferenceIsInlinedAsDocumentedRisk(): void
{
// ⚠️ Same caveat — "table.column" shape in a value slot is treated
// as a SQL column reference.
$this->db->from('user')->where('id', 'users.password');
$sql = $this->db->generateSelectQuery();
$this->assertStringContainsString('id = users.password', $sql);
}
public function testNonFunctionStringValueIsAlwaysParameterized(): void
{
// Sanity: ordinary user input goes through the parameter bag.
$this->db->from('user')->where('id', "Robert'); DROP TABLE Students; --");
$this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
$this->assertSame("Robert'); DROP TABLE Students; --", $params[':id']);
}
// -----------------------------------------------------------------------
// V5 — ORDER BY column whitelist is an application concern
// -----------------------------------------------------------------------
public function testOrderByOnlyEscapesIdentifierDoesNotWhitelist(): void
{
// The builder escapes the column identifier but does not constrain
// it to a predefined whitelist. Callers MUST whitelist before
// passing user-supplied sort columns.
$this->db->from('user')->orderBy('password', 'ASC');
$sql = $this->db->generateSelectQuery();
$this->assertStringContainsString('ORDER BY password ASC', $sql);
}
public function testOrderByDirectionIsValidatedAgainstAscDesc(): void
{
$this->expectException(QueryBuilderInvalidArgumentException::class);
$this->db->from('user')->orderBy('id', 'INJECTED;--');
}
// -----------------------------------------------------------------------
// Integration — end-to-end: a hostile input scenario that lands safely
// -----------------------------------------------------------------------
public function testEndToEndPdoSafeWithHostileStringInput(): void
{
// What the application MUST do: pass user input as a *value*, never
// as an identifier. Below, "name" is a hard-coded column, and the
// attacker-controlled string lands in the parameter bag.
$attackerInput = "'; DROP TABLE users; --";
$this->db->from('user')->where('name', $attackerInput);
$sql = $this->db->generateSelectQuery();
$params = $this->db->getParameter()->all();
// SQL is parameterized; the attacker string is in the bag, never in
// the compiled SQL.
$this->assertEquals('SELECT * FROM user WHERE name = :name', $sql);
$this->assertSame($attackerInput, $params[':name']);
$this->assertStringNotContainsString('DROP', $sql);
}
}