Repository navigation
fix: keep NUL bytes when escaping strings in the SQLite3 driver - #10841
sgr1806-20 wants to merge 1 commit into
Conversation
SQLite3::escapeString() cannot handle NUL bytes: on PHP < 8.6 it silently truncated the string, and on PHP >= 8.6 it throws a ValueError. Escape the quotes in the driver instead, so binary data is preserved on every version. Fixes codeigniter4#10840
michalsn
left a comment
There was a problem hiding this comment.
Thank you for the report and investigation. I would prefer not to merge this implementation as proposed.
Preserving NUL bytes in _escapeString() does not make them usable inside SQLite string literals. With this patch, a regular Query Builder insert containing "a\0b" still fails with an SQL syntax error. The added test checks the escaped PHP string, but does not verify that the value can actually be stored and read back.
The prepared-query case is different: binding and execution happen before the framework renders the query with its parameters. This patch removes the PHP 8.6 exception during that rendering, which is a real improvement, but it does not fix binary handling generally. We should address that execution/rendering path separately.
For SQLite, an available workaround with Query Builder is an explicit BLOB literal:
$blob = new RawSql("X'" . bin2hex($bytes) . "'");
$db->table('files')->insert(['data' => $blob]);This also works with ordinary $db->query() bindings. Do not pass the RawSql object as a prepared-query execute() parameter: it is bound as data rather than evaluated as SQL. Binary handling in prepared queries also has limitations on the current develop branch.
PHP 8.6 support is being developed on 4.8, so the PHP 8.6 prepared-query issue belongs there. I would also prefer to consider explicit binary-value support on that branch. That support should cover all database drivers through a consistent public API, with appropriate encoding and binding for each driver, and tests covering byte-for-byte round trips and database storage types.
Description
Fixes #10840.
SQLite3\Connection::_escapeString() delegates to SQLite3::escapeString(), which cannot handle NUL bytes:
ValueError: SQLite3::escapeString(): Argument #1 ($string) must not contain any null bytesThat breaks binary/BLOB values escaped through the query builder, e.g.
Database\Live\PreparedQueryTest::testInsertBinaryDatafails on PHP 8.6, and silently corrupts those values on older PHP.The driver now escapes the quotes itself instead of delegating:
There is no PHP version check: the same code path preserves every byte on both 8.5 and 8.6.
BaseConnection::_escapeString()is untouched, and a regression test (EscapeTest::testNullBytesAreKept)covers the NUL case and skips on drivers that are not affected.
Verification —
DatabaseLivegroup (983 tests), stock vs patched, on PHP 8.5.13 and 8.6.0:The patch fixes
EscapeTest::testNullBytesAreKeptandPreparedQueryTest::testInsertBinaryDataand adds no new failures on either version.
This is a bug fix, so it targets
developas documented.4.8needs the same change for thePHP 8.6 work (#10684) and will get it from the usual
develop->4.8merge — happy to retargetor send it straight to
4.8if you would rather have it there.Anything else? While measuring,
Models\ValidationModelTest::testValidationWithGroupNamealsoerrors on PHP 8.6 on
develop(not on4.8). That looks like thegetenv()NUL-byte failure fixedby #10804, which landed on
4.8only. Out of scope here, but worth knowing.Checklist: