Skip to content

Require authentication for Active Storage direct uploads - #487

Merged
jeremy merged 3 commits into
mainfrom
security/require-auth-direct-uploads
Sep 1, 2026
Merged

Require authentication for Active Storage direct uploads#487
jeremy merged 3 commits into
mainfrom
security/require-auth-direct-uploads

Conversation

@jeremy

@jeremy jeremy commented Aug 31, 2026

Copy link
Copy Markdown
Member

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.

Fix

Writebook uploads attachments through ActionText::Markdown::UploadsController as a normal multipart POST and does not use direct uploads at all (no server- or JS-side DirectUpload caller), 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:

  • anonymous POST /rails/active_storage/direct_uploads → 401, allocates no blob
  • authenticated metadata POST → 200, allocates a blob
  • anonymous disk-service PUT → 401
  • anonymous disk-service download (disk#show) → 200 (serving stays public)

Full suite green; rubocop and brakeman clean.

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.
Copilot AI balanced review requested due to automatic review settings August 31, 2026 18:41
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-31T20:08:28.871054Z febfb40 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/controllers/active_storage_authentication_test.rb Outdated
Comment thread test/controllers/active_storage_authentication_test.rb

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread test/controllers/active_storage_authentication_test.rb Outdated
Comment thread test/controllers/active_storage_authentication_test.rb
@jeremy
jeremy merged commit 3f98703 into main Sep 1, 2026
7 checks passed
@jeremy
jeremy deleted the security/require-auth-direct-uploads branch September 1, 2026 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants