Multipart file uploads fail validation with ActionDispatch::Request (UploadedFile isn't a Hash)

Đang mở Phù hợp với người mới
#516 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rails, ruby
Lĩnh vực
api

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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ahx/openapi_first

Tất cả issue của ahx/openapi_first

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.