Repository navigation
fix(drizzle): match % _ and \ literally in like and contains - #18326
Open
LudwigJMarx wants to merge 2 commits into
Open
LudwigJMarx wants to merge 2 commits into
LudwigJMarx wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
On Postgres and SQLite,
likeandcontainsnow 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 useslike, so a search for_returns every document with a non-empty title.where: { title: { like: <value> } }with two posts per case, onmain@5448061ac:50%50% off50% off,500 off50% off,500 off50% offa_ba_ba_b,axba_b,axba_ba\ba\baba\ba\bHow?
New helper
escapeLikeValueinpackages/drizzle/src/utilities/. It puts a\before%,_and\. The four places that build a pattern from a search value call it:drizzle/src/queries/parseParams.tslike, one pattern per worddrizzle/src/find/buildPolymorphicJoinWhere.tslikein polymorphic joinsdrizzle/src/queries/sanitizeQueryValue.tscontains, single valuedrizzle/src/queries/sanitizeQueryValue.tscontains,hasManyarrayPostgres uses
\as the default LIKE escape character. SQLite has no default, sodb-sqliteanddb-d1-sqlitenow addESCAPE '\'tolikeandcontains.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:
parseParams.ts, which uses%on purpose. Its values contain no\, so the newESCAPEclause does not change them.parseParams.ts.escapeSQLValueallows 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.
collections-rest: like and contains, 6 casesfields: hasMany textcontainswith an arraybuildPolymorphicJoinWhere.spec: bound parametersThe existing test
like - special charactersincludes%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,containsor a path match (access-control,collections-rest,database,fields,folders,hierarchy,joins,localization,plugin-mcp,relationships,sort,trash,versions):The "before" runs are without the
fieldstest, which I added later. That test alone fails on both databases before the fix. The unit specs ofdrizzle,db-sqlite,db-postgresanddb-d1-sqlitepass, 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 sameESCAPEclause asdb-sqlite.The pre-commit hook fails on
test/collections-rest/int.spec.tsandtest/fields/int.spec.tsonmain, without this change. It reports 6 errors incollections-restand 11 errors infields, allvitest/no-identical-titleorvitest/no-conditional-expect. The findings are the same before and after this change, so I committed with--no-verify.Environment
The same code is on
3.x@9106bf8c1. I can open a backport.Fixes #18325