Repository navigation
Closes #1263: Fix sanitizeContext() passing a string instead of a RegExp - #1350
faisalahammad wants to merge 3 commits into
Conversation
- 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
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
| Duplication | 0 |
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
left a comment
There was a problem hiding this comment.
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.
- 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
left a comment
There was a problem hiding this comment.
@remyperona Thanks for the review. I pushed 5e99a5a with both changes:
sanitizeContext()now accepts non-string input.undefinedandnullreturnwp, numbers are cast to a string.- New Jest case for
undefined,nulland123.
I also merged the latest develop into the branch. Jest (49 tests) and npm run build pass. Could you take another look?
Description
Closes #1263
sanitizeContext()infiles-list.jsandmedia-modal.jspassed 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 likesanitize_key()in PHP.Type of change
Detailed scenario
What was tested
Automated:
npm run test:unit: 49 tests pass, with 5 newsanitizeContexttests inTests/js/helpers.test.js(strips bad characters, keeps valid contexts, lowercases, falls back towp, handlesundefined,nulland numbers).npm run build: clean, the minified files contain the real RegExp.Manual (WordPress with the built zip):
imagify.sanitizeContext( 'WP<script>' )returnswpscript,imagify.sanitizeContext( '<>!' )returnswp.How to test
Affected Features & Quality Assurance Scope
media-modal.js.files-list.js.Technical description
Documentation
Before (same code in both files):
After, the logic lives once in
assets/js/helpers.jsand is exposed onwindow.imagify, the same waysanitizeIdalready is:Both modules now call
w.imagify.sanitizeContext( context ).files-listandmedia-modalalready load afterhelpersthroughadmin, 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
Code style
Unticked items justification
None.
Additional Checks