Skip to content

Commit

Permalink
validate SSL certs by default (lobsters#692)
Browse files Browse the repository at this point in the history
Required for integrations (GitHub, Twitter, etc.) but deliberately
disabled when fetching titles.
  • Loading branch information
jbbarth authored and pushcx committed Jun 18, 2019
1 parent d0d2c9b commit 1d96694
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,9 @@ def fetched_attributes
begin
s = Sponge.new
s.timeout = 3
# User submitted URLs may have an incorrect https certificate, but we
# don't want to fail the retrieval for this. Security risk is minimal.
s.ssl_verify = false
user_agent = { "User-agent" => "#{Rails.application.domain} for #{fetching_ip}" }
@fetched_content = s.fetch(url, :get, nil, nil, user_agent, 3).body.force_encoding('utf-8')
rescue
Expand Down
6 changes: 4 additions & 2 deletions extras/sponge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Sponge
MAX_TIME = 60
MAX_DNS_TIME = 5

attr_accessor :debug, :last_res, :timeout
attr_accessor :debug, :last_res, :timeout, :ssl_verify

# rfc3330
BAD_NETS = [
Expand All @@ -60,12 +60,14 @@ class Sponge
# old api
def self.fetch(url, headers = {}, limit = 10)
s = Sponge.new
s.ssl_verify = false # backward compatibility
s.fetch(url, "get", nil, nil, headers, limit)
end

def initialize
@cookies = {}
@timeout = MAX_TIME
@ssl_verify = OpenSSL::SSL::VERIFY_PEER
end

def set_cookie(host, name, val)
Expand Down Expand Up @@ -156,7 +158,7 @@ def fetch(url, method = :get, fields = nil, raw_post_data = nil, headers = {}, l
host.use_ssl = true
host.address = uri.host
host.custom_conn_address = ip.to_s
host.verify_mode = OpenSSL::SSL::VERIFY_NONE
host.verify_mode = self.ssl_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
end

send_headers = headers.dup
Expand Down
3 changes: 3 additions & 0 deletions script/send_new_webmentions
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ end
def send_webmention(source, target, endpoint)
sp = Sponge.new
sp.timeout = 10
# Don't check SSL certificate here for backward compatibility, security risk
# is minimal.
sp.ssl_verify = false
sp.fetch(endpoint.to_s, :post, {
"source" => URI.encode_www_form_component(source),
"target" => URI.encode_www_form_component(target),
Expand Down

0 comments on commit 1d96694

Please sign in to comment.