Multipart file uploads fail validation with ActionDispatch::Request (UploadedFile isn't a Hash)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 68/100
Hướng nghiên cứu
Bắt đầu từ Validators::MultipartRequestBody#collect_file_uploads, hàm này chỉ thay thế các giá trị khi value.key?(:tempfile). Chạy repro với ActionDispatch::Request và kiểm tra các đối tượng ActionDispatch::Http::UploadedFile mà nó trả về. Hoàn thành khi request của Rails được xác thực giống request của Rack và một spec bao phủ cả hai loại request.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Written by Claude
A multipart file field described as type: string, format: binary passes validation with a Rack::Request, but fails with an ActionDispatch::Request:
Request body invalid: value at `/file` is not a string
This affects Rails request specs that use assert_api_conform, since it validates the Rails request.
Cause
Validators::MultipartRequestBody#collect_file_uploads swaps in a placeholder string only for Rack's upload hashes (value.key?(:tempfile)). ActionDispatch::Request#POST converts those hashes into ActionDispatch::Http::UploadedFile objects. Those aren't hashes, so the placeholder never replaces them, and the schema sees an object where it expects a string. main has the same check.
Repro (openapi_first 4.2.1, actionpack 8.1.4.1)
# openapi.yaml
openapi: 3.1.0
info: { title: Upload, version: 1.0.0 }
paths:
/uploads:
post:
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
required: [file]
properties:
file: { type: string, format: binary }
responses:
'204': { description: Stored }
require 'openapi_first'
require 'action_dispatch'
require 'rack/test'
definition = OpenapiFirst.load(File.join(__dir__, 'openapi.yaml'))
upload = Rack::Test::UploadedFile.new(StringIO.new('hello'), 'text/plain', original_filename: 'hello.txt')
body = Rack::Test::Utils.build_multipart('file' => upload)
content_type = "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}"
rack_env = Rack::MockRequest.env_for('/uploads', method: 'POST', input: body, 'CONTENT_TYPE' => content_type)
rails_env = Rack::MockRequest.env_for('/uploads', method: 'POST', input: body, 'CONTENT_TYPE' => content_type)
p definition.validate_request(Rack::Request.new(rack_env)).valid? # => true
p definition.validate_request(ActionDispatch::Request.new(rails_env)).valid? # => false
Workaround
Validate a Rack::Request built over the same env. Rack keeps its parsed form hash in the env, so uploads arrive as hashes with :tempfile:
module ValidateAsRackRequest
def assert_api_conform(status: nil, api: :default)
rails_request = @request
@request = Rack::Request.new(rails_request.env)
super
ensure
@request = rails_request
end
end
Possible fix
Treat any value that responds to tempfile as an upload, alongside Rack's hash. That covers ActionDispatch::Http::UploadedFile and Rack::Multipart::UploadedFile.
- Ngôn ngữ chính
- Ruby
- Star
- 222
- Fork
- 27
- Merge trung bình
- 17 giờ 15 phút
- Pull request đã merge (30 ngày)
- 6
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ahx/openapi_first
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
ahx/openapi_first#484 · 4 bình luận · 1 reaction ·
-
OpenAPI 3.2 supportCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởenhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 22/100
ahx/openapi_first#469 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
ahx/openapi_first#458 · 4 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 12/100
ahx/openapi_first#425 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
ahx/openapi_first#418 · 1 bình luận · 1 reaction ·
Tất cả issue của ahx/openapi_first
Issue tương tự
-
Bug Ruby
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
getsentry/sentry-ruby#3211 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
internetee/registry#2984 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
interest-zsv
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
hitobito/hitobito_zsv#6 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
ecosyste-ms/issues#862 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Redis Lua error `@user_script:52: ERR value is not an integer or out of range` when `window_size` is configuredCó thể đã có người làm @mimosa đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày