mirror of
https://github.com/discourse/discourse.git
synced 2026-08-08 20:18:23 -05:00
SECURITY: Add FinalDestination::FastImage that's SSRF safe
This commit is contained in:
committed by
Blake Erickson
parent
6dcb099547
commit
39c2f63b35
@@ -193,7 +193,7 @@ module CookedProcessorMixin
|
||||
if upload && upload.width && upload.width > 0
|
||||
@size_cache[url] = [upload.width, upload.height]
|
||||
else
|
||||
@size_cache[url] = FastImage.size(absolute_url)
|
||||
@size_cache[url] = FinalDestination::FastImage.size(absolute_url)
|
||||
end
|
||||
rescue Zlib::BufError, URI::Error, OpenSSL::SSL::SSLError
|
||||
# FastImage.size raises BufError for some gifs, leave it.
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class FinalDestination::FastImage < ::FastImage
|
||||
def initialize(url, options = {})
|
||||
uri = URI(normalized_url(url))
|
||||
options.merge!(http_header: { "Host" => uri.hostname })
|
||||
uri.hostname = resolved_ip(uri)
|
||||
|
||||
super(uri.to_s, options)
|
||||
rescue FinalDestination::SSRFDetector::DisallowedIpError, SocketError, Timeout::Error
|
||||
super("")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def resolved_ip(uri)
|
||||
FinalDestination::SSRFDetector.lookup_and_filter_ips(uri.hostname).first
|
||||
end
|
||||
|
||||
def normalized_url(uri)
|
||||
UrlHelper.normalized_encode(uri)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user