Skip to content

Security: where operator allowlist, JSON path injection, MySQL prepares/utf8mb4, bounded caches - #4

Open
DavidWilliamBalbino wants to merge 5 commits into
mainfrom
security/hardening-1
Open

DavidWilliamBalbino wants to merge 5 commits into
mainfrom
security/hardening-1

Conversation

@DavidWilliamBalbino

Copy link
Copy Markdown
Contributor

Security fixes from the 2026-11-20 PAM/Nimbus/Arcano audit.

Findings fixed

  • A5: string where operators must be on an allowlist (Compiler::condition()), so where($col, $_GET['op'], $v) can no longer inject SQL. Anything else throws CompilerException; use a Fragment/Expression for exotic operators.
  • A6: JSON path segments containing ', " or \ are refused (DriverException) instead of escaped. The old regex (\+)?' matched +, not a backslash, which allowed blind injection on MySQL with the default sql_mode.
  • M11: MySQL defaults to PDO::ATTR_EMULATE_PREPARES => false (server-side prepares), SET NAMES utf8mb4 and charset=utf8mb4 in the DSN for TCP and socket configs. Both stay overridable through options / charset.
  • M12: the prepared statement cache (Driver::QUERY_CACHE_LIMIT = 512 per connection) and the compiled SQL cache (CompilerCache::LIMIT = 2048) drop their oldest entry when full. The insert/update plan cache (StatementPlans) was already capped at 1024 per database; that cap now has a test.

Tests

  • tests/Database/Unit/Driver/OperatorAllowlistTest.php
  • tests/Database/Unit/Injection/JsonExpressionTest.php (new cases)
  • tests/Database/Unit/Config/MySQL/SafeDefaultsTest.php
  • tests/Database/Unit/Driver/BoundedCachesTest.php
  • tests/ORM/Unit/Command/StatementPlansTest.php

Results run locally:

  • unit: OK (883 tests).
  • postgres (postgres:17-alpine): OK (2796 tests).
  • mysql (mysql:8.0): OK (2640 tests).
  • sqlite: 1 error, SingleFileStrategyTest::testInit ("Can't autowire Migration\OrmSecondary…"). It fails the same way on origin/main and passes when run alone, so it depends on test order and was there before this PR.

Compiler::condition() concatenated the operator string into the
statement, so where($column, $_GET['op'], $value) with
"= 1 OR 1=1 OR id =" was SQL injection. String operators are now
checked against a list of known operators (comparison, LIKE/ILIKE, IN,
BETWEEN, IS [NOT] [DISTINCT FROM], REGEXP, SIMILAR TO, Postgres and
bitwise operators); others throw CompilerException. A Fragment still
passes any operator explicitly.
JsonExpression::wrapPath() escaped quotes with the regex "/(\\+)?'/",
which PHP reads as (\+)?' and so matches a literal plus, not
backslashes: in MySQL's default sql_mode orderBy("data->x\\' OR
SLEEP(5) -- ") ended the string literal. Path segments with a quote,
double quote or backslash now throw DriverException instead of being
escaped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant