Skip to content

fix: keep NUL bytes when escaping strings in the SQLite3 driver - #10841

Open
sgr1806-20 wants to merge 1 commit into
codeigniter4:developfrom
sgr1806-20:fix/sqlite3-null-byte-escaping
Open

sgr1806-20 wants to merge 1 commit into
codeigniter4:developfrom
sgr1806-20:fix/sqlite3-null-byte-escaping

Conversation

@sgr1806-20

Copy link
Copy Markdown

Description

Fixes #10840.

SQLite3\Connection::_escapeString() delegates to SQLite3::escapeString(), which cannot handle NUL bytes:

  • on PHP 8.6+ it throws ValueError: SQLite3::escapeString(): Argument #1 ($string) must not contain any null bytes
  • on PHP <= 8.5 it silently returns only the bytes before the first NUL, so binary values are truncated rather than escaped

That breaks binary/BLOB values escaped through the query builder, e.g.
Database\Live\PreparedQueryTest::testInsertBinaryData fails on PHP 8.6, and silently corrupts those values on older PHP.

The driver now escapes the quotes itself instead of delegating:

return str_replace("'", "''", $str);

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 — DatabaseLive group (983 tests), stock vs patched, on PHP 8.5.13 and 8.6.0:

stock patched
PHP 8.6 4 failures 2 failures
PHP 8.5 4 failures 3 failures

The patch fixes EscapeTest::testNullBytesAreKept and PreparedQueryTest::testInsertBinaryData
and adds no new failures on either version.

This is a bug fix, so it targets develop as documented. 4.8 needs the same change for the
PHP 8.6 work (#10684) and will get it from the usual develop -> 4.8 merge — happy to retarget
or send it straight to 4.8 if you would rather have it there.

Anything else? While measuring, Models\ValidationModelTest::testValidationWithGroupName also
errors on PHP 8.6 on develop (not on 4.8). That looks like the getenv() NUL-byte failure fixed
by #10804, which landed on 4.8 only. Out of scope here, but worth knowing.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

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
@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Dec 2, 2026

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: SQLite3 driver loses binary data when escaping (truncates on PHP < 8.6, throws ValueError on PHP 8.6)

2 participants