Skip to content

Commit 0eaae1f

Browse files
committed
test(pdo): cover non-ASCII sequence names and injection-payload regression for lastInsertId
Add coverage to pdo_278_lastinsertid_seq.phpt for the parameterized sequence-name lookup: a sequence whose name contains non-ASCII (Unicode) characters now resolves correctly via lastInsertId(), and an injection-style name is treated as a literal value that matches no sequence (returns an empty string) rather than altering the query.
1 parent 1d6279b commit 0eaae1f

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

test/functional/pdo_sqlsrv/pdo_278_lastinsertid_seq.phpt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,30 @@ try {
5555
// defaults to $tableName2 -- because it returns the last inserted row id value
5656
$lastRow = $conn->lastInsertId();
5757

58-
if ($lastSeq == 3 && $lastRow == 1) {
58+
// The sequence name passed to lastInsertId() is bound as a parameter. Verify
59+
// a sequence whose name contains non-ASCII (Unicode) characters resolves
60+
// correctly -- previously the name was interpreted using the system code page
61+
// and such lookups could fail to match.
62+
$unicodeSeq = 'séquence_Ñ_日本';
63+
$conn->exec("IF OBJECT_ID(N'$unicodeSeq', 'SO') IS NOT NULL DROP SEQUENCE [$unicodeSeq]");
64+
$conn->exec("CREATE SEQUENCE [$unicodeSeq] AS INTEGER START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 100");
65+
$conn->query("SELECT NEXT VALUE FOR [$unicodeSeq]")->fetchColumn();
66+
$lastUnicodeSeq = $conn->lastInsertId($unicodeSeq);
67+
68+
// Because the name is parameterized, a SQL-injection payload is treated as a
69+
// literal sequence name: it matches no sequence and returns an empty string
70+
// instead of altering the query.
71+
$lastInjection = $conn->lastInsertId("x' UNION ALL SELECT DB_NAME()--");
72+
73+
if ($lastSeq == 3 && $lastRow == 1 && $lastUnicodeSeq == 1 && $lastInjection === '') {
5974
echo "Done\n";
6075
} else {
6176
echo "sequence value or identity does not match as expected\n";
6277
}
6378
dropTable($conn, $tableName1);
6479
dropTable($conn, $tableName2);
6580
$conn->exec("DROP SEQUENCE $sequenceName");
81+
$conn->exec("DROP SEQUENCE [$unicodeSeq]");
6682
unset($stmt);
6783
}
6884
unset($conn);

0 commit comments

Comments
 (0)