Skip to content

Avoid wrapping nil input#2259

Merged
ioquatix merged 2 commits intorack:mainfrom
tt:avoid-wrapping-nil-input
Nov 9, 2024
Merged

Avoid wrapping nil input#2259
ioquatix merged 2 commits intorack:mainfrom
tt:avoid-wrapping-nil-input

Conversation

@tt
Copy link
Contributor

@tt tt commented Nov 8, 2024

Rack ships with the Rack::RewindableInput::Middleware middleware for compatibility with older apps.

However, this middleware wraps rack.input unconditionally meaning that a nil input will also be wrapped potentially leading to exceptions.

Concretely, I have an older app with a middleware that's trying to req.body&.read and req.body&.rewind which for GET and HEAD requests now cause the following error:

NoMethodError: undefined method `read' for nil
   ~/.gem/ruby/3.3.6/gems/rack-3.1.8/lib/rack/rewindable_input.rb:96:in `make_rewindable'
   ~/.gem/ruby/3.3.6/gems/rack-3.1.8/lib/rack/rewindable_input.rb:41:in `read'

This middleware should probably interrogate req.request_method.

Even so, it seems that for backwards compatibility (and to avoid the possibility of runtime errors overall), it would be great to never wrap a nil value.

Copy link
Member

@ioquatix ioquatix left a comment

Choose a reason for hiding this comment

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

This looks okay to me.

@ioquatix ioquatix requested a review from jeremyevans November 8, 2024 09:57
@ioquatix ioquatix merged commit 3ea31c6 into rack:main Nov 9, 2024
@ioquatix
Copy link
Member

ioquatix commented Nov 9, 2024

Thanks for your contribution. I hope we can release this in Rack 3.2 sometime before the end of this year.

@tt tt deleted the avoid-wrapping-nil-input branch November 9, 2024 14:22
@tt
Copy link
Contributor Author

tt commented May 19, 2025

@ioquatix, is there a new timeline for when Rack 3.2 is expected to ship?

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.

3 participants