Skip to content

A platform for community discussion. Free, open, simple.

DEV: Gracefully handle user avatar download SSRF errors (#21523)

Ted Johansson59867cc091c0
diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb
index 20f0e1d21bb..838b5cd182e 100644
--- a/app/models/user_avatar.rb
+++ b/app/models/user_avatar.rb
@@ -144,8 +144,8 @@ class UserAvatar < ActiveRecord::Base
user.update!(uploaded_avatar_id: upload.id)
end
end
- rescue Net::ReadTimeout, OpenURI::HTTPError
- # skip saving, we are not connected to the net
+ rescue Net::ReadTimeout, OpenURI::HTTPError, FinalDestination::SSRFError
+ # Skip saving. We are not connected to the net, or SSRF checks failed.
ensure
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
end
diff --git a/lib/final_destination.rb b/lib/final_destination.rb
index 893b38e9c58..c722fdc2410 100644
--- a/lib/final_destination.rb
+++ b/lib/final_destination.rb
@@ -8,6 +8,9 @@ require "url_helper"
# Determine the final endpoint for a Web URI, following redirects
class FinalDestination
+ class SSRFError < SocketError
+ end
+
MAX_REQUEST_TIME_SECONDS = 10
MAX_REQUEST_SIZE_BYTES = 5_242_880 # 1024 * 1024 * 5
diff --git a/lib/final_destination/ssrf_detector.rb b/lib/final_destination/ssrf_detector.rb
index 7a3bf1f0369..dcb20644f73 100644
--- a/lib/final_destination/ssrf_detector.rb
+++ b/lib/final_destination/ssrf_detector.rb
@@ -2,9 +2,9 @@
class FinalDestination
module SSRFDetector
- class DisallowedIpError < SocketError
+ class DisallowedIpError < SSRFError
end
- class LookupFailedError < SocketError
+ class LookupFailedError < SSRFError
end
# This is a list of private IPv4 IP ranges that are not allowed to be globally reachable as given by
diff --git a/lib/retrieve_title.rb b/lib/retrieve_title.rb
index ddbeb628386..8ea76ecec29 100644
--- a/lib/retrieve_title.rb
+++ b/lib/retrieve_title.rb
@@ -9,7 +9,7 @@ module RetrieveTitle
max_redirects: max_redirects,
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
)
- rescue Net::ReadTimeout, FinalDestination::SSRFDetector::LookupFailedError
+ rescue Net::ReadTimeout, FinalDestination::SSRFError
# do nothing for Net::ReadTimeout errors
end
diff --git a/spec/models/user_avatar_spec.rb b/spec/models/user_avatar_spec.rb
index 779babad2a8..96341d261fb 100644
--- a/spec/models/user_avatar_spec.rb
+++ b/spec/models/user_avatar_spec.rb
@@ -175,6 +175,12 @@ RSpec.describe UserAvatar do
expect(user.user_avatar.custom_upload_id).to eq(nil)
end
end
+
+ it "doesn't error out if the remote request fails" do
+ FileHelper.stubs(:download).raises(FinalDestination::SSRFDetector::LookupFailedError.new)
+
+ expect { UserAvatar.import_url_for_user(anything, user) }.not_to raise_error
+ end
end
describe "ensure_consistency!" do