Skip to content

Escape regex metacharacters in siteApiNamespace before interpolating into RegExp #368

@jkmassel

Description

@jkmassel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions