Skip to content

Closes #1263: Fix sanitizeContext() passing a string instead of a RegExp - #1350

Open
faisalahammad wants to merge 3 commits into
wp-media:developfrom
faisalahammad:fix/1263-sanitize-context-regexp
Open

faisalahammad wants to merge 3 commits into
wp-media:developfrom
faisalahammad:fix/1263-sanitize-context-regexp

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #1263

sanitizeContext() in files-list.js and media-modal.js passed the string '/[^a-z0-9_-]/gi' to .replace(). A string needle is matched as plain text, so the function removed nothing and any value went through unchanged. Users see no difference with the real contexts (wp, custom-folders, ngg), but the client side check now works like sanitize_key() in PHP.

Type of change

  • New feature (non-breaking change which adds functionality).
  • Bug fix (non-breaking change which fixes an issue).
  • Enhancement (non-breaking change which improves an existing functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as before).
  • Sub-task of #(issue number)
  • Chore
  • Release

Detailed scenario

What was tested

Automated:

  • npm run test:unit: 49 tests pass, with 5 new sanitizeContext tests in Tests/js/helpers.test.js (strips bad characters, keeps valid contexts, lowercases, falls back to wp, handles undefined, null and numbers).
  • npm run build: clean, the minified files contain the real RegExp.

Manual (WordPress with the built zip):

  • Console: imagify.sanitizeContext( 'WP<script>' ) returns wpscript, imagify.sanitizeContext( '<>!' ) returns wp.
  • Media Library list view and media modal: optimize, restore and re-optimize update in place, no JS errors.
  • Custom Folders (Other Media): optimize and restore a file, the row updates, no JS errors.

How to test

  1. Install the plugin from this branch with a valid API key.
  2. On any Imagify admin page, run in the console:
    imagify.sanitizeContext( 'WP<script>' );     // "wpscript"
    imagify.sanitizeContext( 'custom-folders' ); // "custom-folders"
    imagify.sanitizeContext( '<>!' );            // "wp"
  3. Media > Library: optimize, restore and re-optimize an image in list view and in the media modal.
  4. Media > Other Media: optimize and restore a custom folder file.
  5. Expected: all actions update in place and the console has no errors.

Affected Features & Quality Assurance Scope

  • Media Library optimize, restore and re-optimize buttons (list view and media modal), from media-modal.js.
  • Custom Folders / Other Media row actions, from files-list.js.

Technical description

Documentation

Before (same code in both files):

sanitizeContext: function ( context ) {
	context = context.replace( '/[^a-z0-9_-]/gi', '' ).toLowerCase();
	return context ? context : 'wp';
},

After, the logic lives once in assets/js/helpers.js and is exposed on window.imagify, the same way sanitizeId already is:

function sanitizeContext( context ) {
	context = context.replace( /[^a-z0-9_-]/gi, '' ).toLowerCase();
	return context ? context : 'wp';
}

Both modules now call w.imagify.sanitizeContext( context ). files-list and media-modal already load after helpers through admin, so no script dependency change is needed.

New dependencies

None.

Risks

Low. Valid contexts come out unchanged. The server still sanitizes the context with imagify_sanitize_context(), so this only makes the client side match.

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I implemented built-in tests to cover the new/changed code.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

Unticked items justification

None.

Additional Checks

  • In the case of complex code, I wrote comments to explain it.
  • When possible, I prepared ways to observe the implemented system (logs, data, etc.)
  • I added error handling logic when using functions that could throw errors (HTTP/API request, filesystem, etc.)

- Move sanitizeContext() to assets/js/helpers.js and expose it on window.imagify, like sanitizeId
- files-list.js and media-modal.js now call the shared helper
- Add Jest tests for the helper
- Rebuild the minified files

The old code passed the string '/[^a-z0-9_-]/gi' to .replace(), so it
looked for that exact text and removed nothing. Now it uses a real RegExp
and matches sanitize_key() on the PHP side.

Fixes wp-media#1263
@codacy-production

codacy-production Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 0 duplication

Metric Results
Complexity 4
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@remyperona remyperona left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the RegExp fix is correct and moving the logic into helpers.js alongside sanitizeId reads well. files-list and media-modal both depend on admin → helpers, so the shared helper is always defined when they call it. Two small suggestions inline.

The red Playwright E2E and Code Coverage checks look unrelated: the failing E2E specs cover the settings and bulk pages (which don't load this code) and the API key secret is empty on this fork PR; the coverage job's PHPUnit run passed and it failed at the Codacy upload step.

Comment thread assets/js/helpers.js Resolved
Comment thread Tests/js/helpers.test.js Resolved
- Cast the context to a string, undefined and null fall back to wp
- Add a Jest case for undefined, null and numeric input

Addresses PR feedback.

Refs wp-media#1350

@faisalahammad faisalahammad left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remyperona Thanks for the review. I pushed 5e99a5a with both changes:

  • sanitizeContext() now accepts non-string input. undefined and null return wp, numbers are cast to a string.
  • New Jest case for undefined, null and 123.

I also merged the latest develop into the branch. Jest (49 tests) and npm run build pass. Could you take another look?

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.

sanitizeContext() sanitizes nothing: a string literal is passed to .replace() instead of a RegExp

2 participants