Require authentication for Active Storage direct uploads - #487
Conversation
Active Storage mounts its direct-upload write endpoints -- POST /rails/active_storage/direct_uploads and the disk-service PUT at /rails/active_storage/disk/:token -- on framework controllers that inherit from ActiveStorage::BaseController, so they never pass through ApplicationController's require_authentication. Anyone who can read a public page can lift a CSRF token and Rails session cookie, POST to the metadata endpoint, and receive a signed disk PUT URL without holding a Writebook session. That is enough to allocate ActiveStorage::Blob rows and persist bytes to disk anonymously. The blobs stay unattached (attachment requires an authenticated editor) and nothing purges them, so an unauthenticated caller can grow storage without bound. Because the recommended self-host layout co-locates uploaded files and the SQLite database on one /rails/storage volume, that growth eventually makes database writes fail -- database or disk is full -- blocking account creation and ordinary use until an administrator frees space and purges the blobs. Writebook uploads attachments through ActionText::Markdown::UploadsController as a normal multipart POST and does not use direct uploads at all, so these endpoints have no legitimate caller. Require a valid Writebook session before the metadata endpoint allocates a blob or the disk endpoint accepts an upload; both return 401 to anonymous callers. Serving (disk#show, representations, blob redirects) is unchanged.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
Pull request overview
Adds session authentication to Active Storage write endpoints, preventing anonymous blob and disk allocation while preserving public downloads.
Tip
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.
Changes:
- Protects direct-upload metadata and disk PUT endpoints.
- Adds integration coverage for anonymous access and public downloads.
- Introduces a reusable Active Storage authentication concern.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
app/controllers/concerns/active_storage_authentication.rb |
Implements session-cookie authentication. |
config/initializers/active_storage_authentication.rb |
Applies authentication to Active Storage write actions. |
test/controllers/active_storage_authentication_test.rb |
Tests authentication and public download behavior. |
Suppressed comments (3)
test/controllers/active_storage_authentication_test.rb:9
- Use the path helper for this integration-test request; the test does not need a full URL or cross-host behavior.
post rails_direct_uploads_url, params: blob_params, as: :json
test/controllers/active_storage_authentication_test.rb:19
- Use the path helper for this integration-test request; the test does not need a full URL or cross-host behavior.
post rails_direct_uploads_url, params: blob_params, as: :json
test/controllers/active_storage_authentication_test.rb:27
- Use the path helper for this integration-test request; the test does not need a full URL or cross-host behavior.
post rails_direct_uploads_url, params: blob_params, as: :json
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0c5cab1520
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Active Storage mounts its direct-upload write endpoints —
POST /rails/active_storage/direct_uploadsand the disk-service PUT at/rails/active_storage/disk/:token— on framework controllers that inherit fromActiveStorage::BaseController, so they never pass throughApplicationController'srequire_authentication. Anyone who can read a public page can lift a CSRF token and Rails session cookie, POST to the metadata endpoint, and receive a signed disk PUT URL without holding a Writebook session.That is enough to allocate
ActiveStorage::Blobrows and persist bytes to disk anonymously. The blobs stay unattached (attachment requires an authenticated editor) and nothing purges them, so an unauthenticated caller can grow storage without bound. Because the recommended self-host layout co-locates uploaded files and the SQLite database on one/rails/storagevolume, that growth eventually makes database writes fail (database or disk is full) — blocking account creation and ordinary use until an administrator frees space and purges the blobs.Fix
Writebook uploads attachments through
ActionText::Markdown::UploadsControlleras a normal multipart POST and does not use direct uploads at all (no server- or JS-sideDirectUploadcaller), so these endpoints have no legitimate caller. Require a valid Writebook session before the metadata endpoint allocates a blob or the disk endpoint accepts an upload; both return 401 to anonymous callers. Serving (disk#show, representations, blob redirects) is unchanged.Tests
test/controllers/active_storage_authentication_test.rb:POST /rails/active_storage/direct_uploads→ 401, allocates no blobdisk#show) → 200 (serving stays public)Full suite green; rubocop and brakeman clean.