wordpress-mobile / wordpress-mobile/GutenbergKit

Escape regex metacharacters in siteApiNamespace before interpolating into RegExp

Open
#368 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29
Forks
6
Avg merge
1d 9h
Merged PRs (30d)
41

Description

Summary

In src/utils/api-fetch.js, apiPathModifierMiddleware interpolates siteApiNamespace values directly into new RegExp() without escaping regex metacharacters:

const namespaceRegex = new RegExp( `(${ siteApiNamespace.join( '|' ) })` );

If a namespace contained characters like ., +, (, ), *, etc., they would be interpreted as regex syntax rather than matched literally.

Risk

Low — siteApiNamespace comes from the native app bridge config (not user input), so exploitation requires a compromised host app. But it's a latent correctness bug: a namespace like wp/v2.1 would match wp/v2X (. matches any character).

Suggested fix

Escape each namespace before joining:

const escaped = siteApiNamespace.map( ( ns ) =>
    ns.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' )
);
const namespaceRegex = new RegExp( `(${ escaped.join( '|' ) })` );

Existing tests in src/utils/api-fetch.test.js pass with this change.

Found during adversarial code review of #TBD.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/utils/api-fetch.js at apiPathModifierMiddleware, then review the related cases in src/utils/api-fetch.test.js. Verify that namespaces containing regex metacharacters are matched literally and that the existing tests still pass; add coverage for the reported namespace example if needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
mobile-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.