Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 43104a8

Browse files
committed
Throw Exception for invalid PDO param name
1 parent fec7d8c commit 43104a8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Adapter/Driver/Pdo/Pdo.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ public function getPrepareType()
304304
public function formatParameterName($name, $type = null)
305305
{
306306
if ($type === null && ! is_numeric($name) || $type == self::PARAMETERIZATION_NAMED) {
307+
// @see https://bugs.php.net/bug.php?id=43130
308+
if (preg_match('/[^a-zA-Z0-9_]/', $name)) {
309+
throw new Exception\RuntimeException(sprintf(
310+
"The PDO param %s contains characters not allowed. " .
311+
"You can use only letter, digit, and underscore (_)",
312+
$name
313+
));
314+
}
307315
return ':' . $name;
308316
}
309317

test/Adapter/Driver/Pdo/PdoTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ public function getParamsAndType()
4444
{
4545
return [
4646
[ 'foo', null, ':foo' ],
47+
[ 'foo_bar', null, ':foo_bar' ],
48+
[ '123foo', null, ':123foo' ],
4749
[ 1, null, '?' ],
4850
[ '1', null, '?' ],
4951
[ 'foo', Pdo::PARAMETERIZATION_NAMED, ':foo' ],
52+
[ 'foo_bar', Pdo::PARAMETERIZATION_NAMED, ':foo_bar' ],
53+
[ '123foo', Pdo::PARAMETERIZATION_NAMED, ':123foo' ],
5054
[ 1, Pdo::PARAMETERIZATION_NAMED, ':1' ],
5155
[ '1', Pdo::PARAMETERIZATION_NAMED, ':1' ],
5256
];
@@ -60,4 +64,23 @@ public function testFormatParameterName($name, $type, $expected)
6064
$result = $this->pdo->formatParameterName($name, $type);
6165
$this->assertEquals($expected, $result);
6266
}
67+
68+
public function getInvalidParamName()
69+
{
70+
return [
71+
[ 'foo%' ],
72+
[ 'foo-' ],
73+
[ 'foo$' ],
74+
[ 'foo0!' ]
75+
];
76+
}
77+
78+
/**
79+
* @dataProvider getInvalidParamName
80+
* @expectedException Zend\Db\Exception\RuntimeException
81+
*/
82+
public function testFormatParameterNameWithInvalidCharacters($name)
83+
{
84+
$this->pdo->formatParameterName($name);
85+
}
6386
}

0 commit comments

Comments
 (0)