fix(client): validate file extension whitelist on rename
Issue:
Fixes #4102
Currently, the p5.js Web Editor blocks invalid file extensions (such as .obj) during file creation via the file modal. However, it does not perform the same validation when renaming a file. This allows users to bypass the file extension whitelist entirely by creating a valid file (e.g., test.js) and then renaming it to an invalid extension (e.g., test.obj).
This PR fixes the bypass by validating file extensions against the whitelist during the rename operation.
Demo:
(No complex UI layout modifications—it triggers the pre-existing toast error message when an invalid extension is supplied during rename, identical to the behavior in the file creation modal, and reverts the file to its previous valid name).
Changes:
- FileNode.jsx:
- Imported
CREATE_FILE_REGEX(fromserver/utils/fileUtils.js) andshowToast(mapped asshowToastActionto resolve ESLintno-shadowrules). - Destructured
showToastin component props and declared it inpropTypes. - Updated the
validateFileNamemethod to check that renaming a file matchesCREATE_FILE_REGEX. - If the validation fails, it triggers
showToastwith the translation keyNewFileModal.InvalidTypeand reverts the filename.
- Imported
- FileNode.unit.test.jsx:
- Mocked the
showToastprop in the test setup. - Adjusted unit tests to use valid whitelisted extensions (
.jsand.json) for renaming scenarios. - Added a new unit test case
cannot change to an invalid extensionto verify that invalid renames are blocked and triggershowToast.
- Mocked the
I have verified that this pull request:
- [x] has no linting errors (
npm run lint) - [x] has no test errors (
npm run test) - [x] has no typecheck errors (
npm run typecheck) - [x] is from a uniquely-named feature branch and is up to date with the
developbranch. - [x] is descriptively named and links to an issue number, i.e.
Fixes #4102 - [x] meets the standards outlined in the accessibility guidelines