Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit be2bab5

Browse files
committed
limit fetching file size & disable XML entity expansion
1 parent 578d3b0 commit be2bab5

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

lib/openid/fetchers.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
require 'net/http'
1111
end
1212

13-
MAX_RESPONSE_KB = 1024
13+
MAX_RESPONSE_KB = 10485760 # 10 MB (can be smaller, I guess)
1414

1515
module Net
1616
class HTTP
@@ -192,19 +192,30 @@ def fetch(url, body=nil, headers=nil, redirect_limit=REDIRECT_LIMIT)
192192
conn = make_connection(url)
193193
response = nil
194194

195+
whole_body = ''
196+
body_size_limitter = lambda do |r|
197+
r.read_body do |partial| # read body now
198+
whole_body << partial
199+
if whole_body.length > MAX_RESPONSE_KB
200+
raise FetchingError.new("Response Too Large")
201+
end
202+
end
203+
whole_body
204+
end
195205
response = conn.start {
196206
# Check the certificate against the URL's hostname
197207
if supports_ssl?(conn) and conn.use_ssl?
198208
conn.post_connection_check(url.host)
199209
end
200210

201211
if body.nil?
202-
conn.request_get(url.request_uri, headers)
212+
conn.request_get(url.request_uri, headers, &body_size_limitter)
203213
else
204214
headers["Content-type"] ||= "application/x-www-form-urlencoded"
205-
conn.request_post(url.request_uri, body, headers)
215+
conn.request_post(url.request_uri, body, headers, &body_size_limitter)
206216
end
207217
}
218+
response.body = whole_body
208219
setup_encoding(response)
209220
rescue Timeout::Error => why
210221
raise FetchingError, "Error fetching #{url}: #{why}"

lib/openid/yadis/xrds.rb

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,33 @@ class XRDSError < StandardError
8888
end
8989

9090
def Yadis::parseXRDS(text)
91-
if text.nil?
92-
raise XRDSError.new("Not an XRDS document.")
93-
end
91+
disable_entity_expansion do
92+
if text.nil?
93+
raise XRDSError.new("Not an XRDS document.")
94+
end
9495

95-
begin
96-
d = REXML::Document.new(text)
97-
rescue RuntimeError => why
98-
raise XRDSError.new("Not an XRDS document. Failed to parse XML.")
99-
end
96+
begin
97+
d = REXML::Document.new(text)
98+
rescue RuntimeError => why
99+
raise XRDSError.new("Not an XRDS document. Failed to parse XML.")
100+
end
100101

101-
if is_xrds?(d)
102-
return d
103-
else
104-
raise XRDSError.new("Not an XRDS document.")
102+
if is_xrds?(d)
103+
return d
104+
else
105+
raise XRDSError.new("Not an XRDS document.")
106+
end
105107
end
106108
end
107109

110+
def Yadis::disable_entity_expansion
111+
_previous_ = REXML::Document::entity_expansion_limit
112+
REXML::Document::entity_expansion_limit = 0
113+
yield
114+
ensure
115+
REXML::Document::entity_expansion_limit = _previous_
116+
end
117+
108118
def Yadis::is_xrds?(xrds_tree)
109119
xrds_root = xrds_tree.root
110120
return (!xrds_root.nil? and

0 commit comments

Comments
 (0)