From 4b8da913beea1f348f76cef414b91754efa92e23 Mon Sep 17 00:00:00 2001 From: Rockwell Windsor Rice Date: Sun, 4 Oct 2026 12:44:52 -0500 Subject: [PATCH] fix(rails-security): cover extension and storage destination in upload guidance - rules/ruby/security.md calls for content type, extension, size, and storage destination; the example and checklist covered only detected type and size - Adds an extension allowlist, a private-service attachment with signed URLs, and updates the verification checklist to match --- skills/rails-security/SKILL.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/skills/rails-security/SKILL.md b/skills/rails-security/SKILL.md index ac215ae5a..215517f2d 100644 --- a/skills/rails-security/SKILL.md +++ b/skills/rails-security/SKILL.md @@ -287,14 +287,15 @@ does not apply; token or session-cookie APIs still need it. ## File Uploads -The `content_type` a client sends is attacker-controlled. Validate against what the file -actually contains, and constrain size: +The `content_type` a client sends is attacker-controlled. Validate the detected type, +the extension, and the size, and choose the storage destination deliberately: ```ruby class Document < ApplicationRecord has_one_attached :file ALLOWED = %w[application/pdf image/png image/jpeg].freeze + ALLOWED_EXTENSIONS = %w[.pdf .png .jpg .jpeg].freeze MAX_BYTES = 10.megabytes validate :acceptable_file @@ -309,10 +310,20 @@ class Document < ApplicationRecord # identify sets content_type from the file's own bytes, not the client's header. file.blob.identify unless file.blob.identified? errors.add(:file, "type not allowed") unless ALLOWED.include?(file.blob.content_type) + + extension = File.extname(file.blob.filename.to_s).downcase + errors.add(:file, "extension not allowed") unless ALLOWED_EXTENSIONS.include?(extension) end end ``` +Attach to a private service and serve through a signed, expiring URL. A public bucket +makes every upload world-readable regardless of the checks above. + +```ruby +has_one_attached :file, service: :private_documents +``` + Never interpolate a user-supplied path into `send_file`: ```ruby @@ -452,7 +463,7 @@ confidence threshold. - [ ] No raw SQL string interpolation; `order` and `pluck` inputs allowlisted - [ ] Every `html_safe` and `raw` reviewed - [ ] CSRF exemptions scoped to specific actions with another auth mechanism -- [ ] Uploads validated by detected type and size +- [ ] Uploads validated by detected type, extension, and size, and stored on a private service - [ ] Auth endpoints rate-limited; webhook signatures compared in constant time - [ ] No secrets in version control; master key excluded - [ ] `bundle-audit` and `brakeman` clean