]> git.openstreetmap.org Git - rails.git/commitdiff
Make safe_referer handle invalid URIs
authorTom Hughes <tom@compton.nu>
Tue, 23 Nov 2021 11:27:02 +0000 (11:27 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 23 Nov 2021 11:27:02 +0000 (11:27 +0000)
app/controllers/application_controller.rb

index 517b11e140818818dd7ab16b7219a39f04337720..d0898bf5e4f1b5fa098128c851c703da096c4b86 100644 (file)
@@ -381,18 +381,22 @@ class ApplicationController < ActionController::Base
 
   # clean any referer parameter
   def safe_referer(referer)
-    referer = URI.parse(referer)
+    begin
+      referer = URI.parse(referer)
+
+      if referer.scheme == "http" || referer.scheme == "https"
+        referer.scheme = nil
+        referer.host = nil
+        referer.port = nil
+      elsif referer.scheme || referer.host || referer.port
+        referer = nil
+      end
 
-    if referer.scheme == "http" || referer.scheme == "https"
-      referer.scheme = nil
-      referer.host = nil
-      referer.port = nil
-    elsif referer.scheme || referer.host || referer.port
+      referer = nil if referer&.path&.first != "/"
+    rescue URI::InvalidURIError
       referer = nil
     end
 
-    referer = nil if referer&.path&.first != "/"
-
     referer.to_s
   end