Skip to content
Browse files

Prevent infinite recursions from Response#to_ary

Closes #419
  • Loading branch information...
1 parent e2c530c commit 5b251a986962f3577e14c01a698fab9dd21383ba @raggi raggi committed Nov 2, 2012
Showing with 9 additions and 1 deletion.
  1. +2 −0 lib/rack/body_proxy.rb
  2. +1 −1 lib/rack/response.rb
  3. +6 −0 test/spec_response.rb
View
2 lib/rack/body_proxy.rb
@@ -5,6 +5,7 @@ def initialize(body, &block)
end
def respond_to?(*args)
+ return false if args.first.to_s =~ /^to_ary$/
super or @body.respond_to?(*args)
end
@@ -23,6 +24,7 @@ def closed?
end
def method_missing(*args, &block)
+ super if args.first.to_s =~ /^to_ary$/
@body.__send__(*args, &block)
end
end
View
2 lib/rack/response.rb
@@ -76,7 +76,7 @@ def finish(&block)
header.delete "Content-Length"
[status.to_i, header, []]
else
- [status.to_i, header, self]
+ [status.to_i, header, BodyProxy.new(self){}]
end
end
alias to_a finish # For *response
View
6 test/spec_response.rb
@@ -280,4 +280,10 @@ def object_with_each.each
res.close
res.body.should.be.closed
end
+
+ it "wraps the body from #to_ary to prevent infinite loops" do
+ res = Rack::Response.new
+ res.finish.last.should.not.respond_to?(:to_ary)
+ lambda { res.finish.last.to_ary }.should.raise(NoMethodError)
+ end
end

3 comments on commit 5b251a9

@alindeman

Nice. Thank you.

@hannesg
hannesg commented on 5b251a9 Nov 4, 2012

Is there a good reason why you use

… args.first.to_s =~ /^to_ary$/

Instead of

… args.first.to_s == 'to_ary'

???

@chneukirchen
Official Rack repositories member

Use a string comparision, note ^ $ on multiline strings.

Please sign in to comment.
Something went wrong with that request. Please try again.