Skip to content

fix(drizzle): match % _ and \ literally in like and contains - #18326

Open
LudwigJMarx wants to merge 2 commits into
payloadcms:mainfrom
LudwigJMarx:fix/drizzle-like-wildcards
Open

LudwigJMarx wants to merge 2 commits into
payloadcms:mainfrom
LudwigJMarx:fix/drizzle-like-wildcards

Conversation

@LudwigJMarx

Copy link
Copy Markdown

What?

On Postgres and SQLite, like and contains now match %, _ and \ in the search value literally, as the MongoDB adapter already does.

Why?

The Drizzle adapter puts the search value into a LIKE pattern without escaping. On Postgres and SQLite, % and _ then act as wildcards. On Postgres, \ also acts as the escape character. The admin list search uses like, so a search for _ returns every document with a non-empty title.

where: { title: { like: <value> } } with two posts per case, on main @ 5448061ac:

Search value Expected Postgres 17.10 SQLite MongoDB 7.0.14
50% 50% off 50% off, 500 off 50% off, 500 off 50% off
a_b a_b a_b, axb a_b, axb a_b
a\b a\b ab a\b a\b

How?

New helper escapeLikeValue in packages/drizzle/src/utilities/. It puts a \ before %, _ and \. The four places that build a pattern from a search value call it:

File Operator
drizzle/src/queries/parseParams.ts like, one pattern per word
drizzle/src/find/buildPolymorphicJoinWhere.ts like in polymorphic joins
drizzle/src/queries/sanitizeQueryValue.ts contains, single value
drizzle/src/queries/sanitizeQueryValue.ts contains, hasMany array

Postgres uses \ as the default LIKE escape character. SQLite has no default, so db-sqlite and db-d1-sqlite now add ESCAPE '\' to like and contains.

I did not escape inside the SQLite operator only. The value reaches the operator with the % wildcards that Payload adds. The operator cannot tell them apart from a % in the search value.

Not changed:

  • The internal path match in parseParams.ts, which uses % on purpose. Its values contain no \, so the new ESCAPE clause does not change them.
  • The JSON path in parseParams.ts. escapeSQLValue allows only [\w @.\-+:] there, so % is rejected, but _ is still a wildcard.
  • not_like. It has no new test.

Tests

The first commit adds the tests only. The second commit adds the fix.

Test Before, SQLite Before, Postgres After, both
collections-rest: like and contains, 6 cases 4 fail 6 fail pass
fields: hasMany text contains with an array fail fail pass
buildPolymorphicJoinWhere.spec: bound parameters fail (unit test, no database) pass

The existing test like - special characters includes % and _, but it creates only one post. So it cannot see a query that matches too much.

MongoDB passes all new int tests before and after the fix.

Suites that use like, contains or a path match (access-control, collections-rest, database, fields, folders, hierarchy, joins, localization, plugin-mcp, relationships, sort, trash, versions):

SQLite, before:       4 failed | 1462 passed | 45 skipped | 7 todo (1518)
SQLite, after:        1467 passed | 45 skipped | 7 todo (1519)
Postgres 17.10, before: 6 failed | 1477 passed | 32 skipped | 7 todo (1522)
Postgres 17.10, after:  1484 passed | 32 skipped | 7 todo (1523)

The "before" runs are without the fields test, which I added later. That test alone fails on both databases before the fix. The unit specs of drizzle, db-sqlite, db-postgres and db-d1-sqlite pass, 130 of 130.

I reverted each of the five changes alone. Each revert makes at least one new test fail.

Not run:

  • plugin-import-export, because it needs the S3 emulator on port 4566.
  • db-d1-sqlite, because D1 needs Cloudflare. It gets the same ESCAPE clause as db-sqlite.

The pre-commit hook fails on test/collections-rest/int.spec.ts and test/fields/int.spec.ts on main, without this change. It reports 6 errors in collections-rest and 11 errors in fields, all vitest/no-identical-title or vitest/no-conditional-expect. The findings are the same before and after this change, so I committed with --no-verify.

Environment

main @ 5448061ac
Node.js 24.14.1, pnpm 11.9.0, macOS 25.6 arm64
PostgreSQL 17.10 with PostGIS 3.6.4 (local, no Docker)
SQLite from the monorepo lockfile
MongoDB 7.0.14 (binary from mongodb-memory-server, replica set rs0)

The same code is on 3.x @ 9106bf8c1. I can open a backport.

Fixes #18325

The existing special characters test for like searches one post whose
title contains every special character. A search value of % or _ matches
that post in every adapter, so the test cannot see a query that matches
too much.

Each new case has two documents. The first contains the search value.
The second matches only if the database reads % or _ as a LIKE
wildcard, or \ as the LIKE escape character. Three places get a test:

- collections-rest: like and contains on a text field, 6 cases
- fields: contains with an array value on a hasMany text field
- buildPolymorphicJoinWhere.spec: the bound parameters of a like
  comparison in a polymorphic join

Result on main @ 5448061:

- MongoDB 7.0.14: collections-rest 6 of 6 pass, fields passes. The
  adapter escapes values with escapeRegExp.
- SQLite: collections-rest 4 of 6 fail. like and contains return
  "500 off" for 50% and "axb" for a_b. The \ cases pass, because SQLite
  has no default escape character. fields fails.
- Postgres 17.10: collections-rest 6 of 6 fail. ILIKE also reads \ as
  the escape character, so "a\b" finds "ab" and does not find "a\b".
  fields fails.
- buildPolymorphicJoinWhere.spec fails: the parameters are
  %50%%, %a_b% and %a\b%.
The docs describe like and contains as "string must be present". The
MongoDB adapter matches the value literally. The Drizzle adapters put the
value into a LIKE pattern without escaping. In Postgres and SQLite, % and
_ in the value then act as wildcards, and in Postgres \ acts as the escape
character. The admin list search uses like, so a search for "_" returns
every document with a non-empty title.

Four places build a pattern from a search value:

- parseParams: like, one pattern per word
- buildPolymorphicJoinWhere: like in polymorphic joins
- sanitizeQueryValue: contains, single value and hasMany array

escapeLikeValue puts a \ before %, _ and \ at these four places.
Postgres uses \ as the default LIKE escape character. SQLite has no
default, so db-sqlite and db-d1-sqlite now add ESCAPE '\' to like and
contains.

Rejected: escape inside the SQLite operator only. The value reaches the
operator with the % wildcards that Payload adds, so the operator cannot
tell them apart from a % in the search value.

Not changed:

- The internal path match in parseParams, which uses % on purpose. Its
  values contain no \, so the new ESCAPE clause does not change them.
- The JSON path in parseParams. escapeSQLValue allows only [\w @.\-+:]
  there, so % is rejected, but _ is still a wildcard.
- not_like. It is not in the new tests.

Result with this commit, 13 int suites that use like, contains or a
path match:

- SQLite: 1467 passed, 0 failed.
- Postgres 17.10: 1484 passed, 0 failed.

Before this commit, only the new tests fail in these suites. The unit
specs of drizzle, db-sqlite, db-postgres and db-d1-sqlite pass, 130 of
130.

Suites: access-control, collections-rest, database, fields, folders,
hierarchy, joins, localization, plugin-mcp, relationships, sort, trash,
versions.

plugin-import-export did not run, because it needs the S3 emulator on
port 4566.

I reverted each of the five changes alone. Each revert makes at least
one new test fail. db-d1-sqlite has no test here, because D1 needs
Cloudflare.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

db-postgres, db-sqlite: like and contains read % and _ in the search value as wildcards

1 participant