rails-mini-profiler icon indicating copy to clipboard operation
rails-mini-profiler copied to clipboard

Profiling file upload produces a "string contains null byte" argument error

Open Qapybara28 opened this issue 2 years ago • 3 comments

Issue Summary

Multipart request body when uploading a file contains NULL bytes and the following error occurs when persisting the instance of ProfiledRequest on postgresql.

Rails -- Exception: ArgumentError: string contains null byte

Steps to Reproduce

  1. Monitor a file upload with rails-mini-profiler

Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?

It should handle this case properly without crashing.

Dirty workaround

config/environments/development.rb:

config.to_prepare do
    RailsMiniProfiler::ProfiledRequest.class_eval do
      before_save do
        self.request_body = request_body.to_s
                                        .encode('UTF-8', invalid: :replace, undef: :replace)
                                        .delete("\000")
      end
    end
  end

Edit: Fix typos

Qapybara28 avatar Jun 30 '22 14:06 Qapybara28

It should handle this case properly without crashing.

I agree :grin:

Thanks for the great description and workaround, really helps with debugging. I'll take a look :+1:

hschne avatar Jun 30 '22 16:06 hschne

To be exhaustive: Same problem arise when sending binary content in the response (pdf, etc.)

Qapybara28 avatar Jul 07 '22 14:07 Qapybara28

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 05 '22 18:09 stale[bot]

As mentionned above, the response body should be sanitized too. Should I create a new issue ?

Qapybara28 avatar Oct 24 '22 08:10 Qapybara28

@Qapybara28 My bad, reading is hard. I created a new issue for that, and will tackle that ASAP - unless you want to create a PR yourself :wink:

Thanks a lot for bringing it up! :raised_hands:

hschne avatar Oct 24 '22 08:10 hschne