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

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)

1 participant