Require an admin session for ActiveStorage direct uploads - #85
Open
xrendan wants to merge 1 commit into
Open
Conversation
POST /rails/active_storage/direct_uploads ships unauthenticated, so anyone could create blob records and obtain presigned R2 PUT URLs (free storage abuse). Nothing in the app uses direct uploads — the admin markdown editor proxies through /admin/uploads — so gate the endpoint behind the same admin check as Admin::BaseController instead of leaving it open. Named before_action keeps the decoration idempotent under to_prepare reloads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /rails/active_storage/direct_uploadsis drawn by Rails with no authentication and is currently reachable in production. Anyone can createActiveStorage::Blobrecords and obtain presigned R2 PUT URLs — i.e. free writes into our storage bucket plus unbounded junk rows inactive_storage_blobs.Nothing in the app uses direct uploads anymore: the admin markdown editor uploads through
POST /admin/uploads(#84), and all other file fields are plain multipart form uploads. Rather than deleting the route (draw_routes = falsewould also remove the blob-serving routes we rely on), this gates the endpoint behind the same admin check asAdmin::BaseController, keeping it usable from the admin if direct uploads are ever wanted for large files.Changes
config/initializers/active_storage_direct_uploads.rb: decoratesActiveStorage::DirectUploadsControllerinto_preparewith abefore_actionrequiring a signed-in admin (wardenuser withadmin?), returning 401 otherwise. Uses a named method so the callback dedupes ifto_prepareruns again on reload.Testing
direct_uploadpayload returned.