Add CSRF protection and input validation#1077
Conversation
🌍 i18n String Review Report📊 Summary
➕ Added Strings (2) - Click to expand
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the media “replace file” flow in the attachment editor by adding CSRF protection and additional server-side validation to reduce the risk of unauthorized requests and file type switching.
Changes:
- Exposes a replace-file nonce to the attachment edit JS and sends it with the upload request.
- Adds nonce verification and stricter attachment ID validation in the AJAX replace endpoint.
- Verifies the uploaded file’s detected MIME type matches the original attachment before replacing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
inc/media_rename/attachment_edit.php |
Adds nonce creation/localization and strengthens AJAX handler validation (nonce, attachment ID, MIME checks). |
assets/js/single-attachment.js |
Sends the nonce along with the AJAX replace-file upload request. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function uploadFile() { | ||
| var formData = new FormData(); | ||
| formData.append("action", "optml_replace_file"); | ||
| formData.append("optml_replace_nonce", OMAttachmentEdit.nonce); | ||
| formData.append("attachment_id", OMAttachmentEdit.attachmentId); |
| check_ajax_referer( 'optml_replace_media_nonce', 'optml_replace_nonce' ); | ||
|
|
| check_ajax_referer( 'optml_replace_media_nonce', 'optml_replace_nonce' ); | ||
|
|
||
| $id = absint( $_POST['attachment_id'] ?? 0 ); | ||
|
|
||
| if ( ! $id ) { |
be01897 to
e22772f
Compare
|
@claude[agent] replace and use the existing suggested string instead of new one added and check if there are possible matches for those other 2 new strings added mentioned in comment #1077 (comment) |
Agent-Logs-Url: https://github.com/Codeinwp/optimole-wp/sessions/a9c6ff77-c741-4d3d-9004-4d12974a746f Co-authored-by: vytisbulkevicius <36594177+vytisbulkevicius@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Codeinwp/optimole-wp/sessions/a9c6ff77-c741-4d3d-9004-4d12974a746f Co-authored-by: vytisbulkevicius <36594177+vytisbulkevicius@users.noreply.github.com>
Replaced For the other 2 new strings ( Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
🎉 This PR is included in version 4.2.7 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
All Submissions:
Changes proposed in this Pull Request:
Add nonce verification via check_ajax_referer(), validate attachment ID with absint() and post-type check, and verify uploaded file MIME type matches the original attachment using wp_check_filetype_and_ext() to prevent type-switching attacks. Expose nonce to JS via wp_localize_script and send it with each upload.
Closes https://github.com/Codeinwp/optimole-service/issues/1703 .
Other information: