Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSV delegation to missing StringIO methods #80

Merged

Conversation

gsamokovarov
Copy link
Contributor

@gsamokovarov gsamokovarov commented Mar 13, 2019

If you create a CSV from raw content like:

csv = CSV.new("h1,h2")

You'll get a NoMethodError when calling csv.path but still get true when
you call csv.respond_to?(:path).

This tricks 3rd party libraries like carrierwave which try to call path
on their input, if it responds to it.

See https://github.com/carrierwaveuploader/carrierwave/blob/a91ab69fdd8052cdf5a5e48ef8baf40939e441fb/lib/carrierwave/sanitized_file.rb#L109-L123

This stops me from passing CSV objects as StringIOs into carrierwave
uploads, for example, but the problem can also be manifested in other
3rd party libraries, as responding to a method and returning a
NoMethodError when calling it is still an unexpected behavior.

I have gone through the CSV delegation scheme and made sure that every
method StringIO doesn't respond to returns a meaningful zero value
and does not raise a NoMethodError while used through a CSV Instance.

Ref. ruby/ruby#2094

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

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

Thanks.
Could you check my comments?

test/csv/interface/test_delegation.rb Outdated Show resolved Hide resolved
test/csv/interface/test_delegation.rb Outdated Show resolved Hide resolved
test/csv/interface/test_delegation.rb Outdated Show resolved Hide resolved
test/csv/interface/test_delegation.rb Outdated Show resolved Hide resolved
test/csv/interface/test_delegation.rb Outdated Show resolved Hide resolved
If you create a `CSV` from raw content like:

    csv = CSV.new("h1,h2")

You'll get method missing when calling `csv.path` but still get true
when you call `csv.respond_to?(:path)`.

This tricks 3rd party libraries like `carrierwave` which try to call
`#path` on their input if it responds to it.

See https://github.com/carrierwaveuploader/carrierwave/blob/a91ab69fdd8052cdf5a5e48ef8baf40939e441fb/lib/carrierwave/sanitized_file.rb#L109-L123

This stops me from passing `CSV` objects as `StringIO` into
`carrierwave` uploads, for example, but the problem can also be
manifested in other 3rd party libraries, as responding to a method and
returning a `NoMethodError` when calling it is still an unexpected
behavior.

I have went through the `CSV` delegation scheme and made sure that every
method that `StringIO` doesn't respond to returns a meaningful zero value
and does not raise a `NoMethodError` while used through a `CSV` Instance.
@gsamokovarov
Copy link
Contributor Author

Thanks for taking a look, @kou! I have changed some of the operations to return NotImplementedErrors.

@kou
Copy link
Member

kou commented Mar 17, 2019

Did you push? It seems that there is no new commit.

@gsamokovarov gsamokovarov force-pushed the csv-stringio-missing-methods-delegation branch from 837cb39 to cb83cd7 Compare March 17, 2019 07:59
@gsamokovarov
Copy link
Contributor Author

Yes! I did forget... 🤦‍♂️ Can you check now? 😅

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

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

I could check. :-)

@kou kou merged commit afff391 into ruby:master Mar 17, 2019
@gsamokovarov
Copy link
Contributor Author

Thank you!

@gsamokovarov gsamokovarov deleted the csv-stringio-missing-methods-delegation branch November 30, 2020 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants