fix(graphql): escape host in resolve-uris preg_match pattern - #125
Merged
Conversation
get_site_url()/get_next_url() hosts are interpolated straight into the preg_match pattern that gates URL rewriting. On sub-path installs the host contains a slash (e.g. "localhost/us"), which is read as the regex delimiter, so PHP parses the remainder as modifiers and emits "preg_match(): Unknown modifier '|'" on every resolved URI field. Wrap both hosts in preg_quote(..., '/') so slashes and other regex-special characters are treated literally.
There was a problem hiding this comment.
Pull request overview
This PR fixes a regex parsing bug in the WordPress GraphQL URI resolver by properly escaping the backend (WP) and frontend (Next.js) URL fragments before interpolating them into a preg_match() pattern. This prevents warnings (and associated stack traces) on sub-path installs (e.g. /us) and ensures the host/path match works correctly.
Changes:
- Escape
$site_urland$next_urlusingpreg_quote(..., '/')before building thepreg_match()pattern. - Add an inline comment explaining why escaping is required (slashes in sub-path installs).
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.
Problem
graphql_resolve_relative_uris()interpolates the site/FE hosts straight into apreg_matchpattern:The pattern uses
/as its delimiter, but the interpolated hosts are not escaped. On a sub-path install the host contains a slash (e.g.localhost/us), so the first/inside the host is read as the closing delimiter and PHP parses the remainder (us|localhost:3000...) as modifiers — hitting the|:Because this filter runs on
graphql_resolve_fieldfor everyuri/url/link/guid/sourceUrl/mediaItemUrlfield, the warning (plus a full stack trace) is emitted on essentially every GraphQL request, flooding the logs.It's a latent bug on root-domain installs (host has no slash) and only surfaces once the site runs under a sub-path — or if a host ever contains another regex-special char.
Fix
Wrap both hosts in
preg_quote(..., '/')so slashes and other regex-special characters are matched literally.Notes
notice/warning-level entries, not fatals — the site kept working, but the log noise (and per-request stack-trace overhead) is eliminated./us.