]> git.openstreetmap.org Git - rails.git/blobdiff - lib/osm_community_index/resource_backend.rb
Merge pull request #5333 from AntonKhorev/auth-buttons-same-as-preferred
[rails.git] / lib / osm_community_index / resource_backend.rb
index e16d951a409805bf1283398239f565bdc5e20173..3ed14e6de3982aedfa3ec6e8336f9f509fe44d1e 100644 (file)
@@ -8,7 +8,30 @@ module OsmCommunityIndex
 
     def self.load(file_path)
       resources = JSON.parse(File.read(file_path))
+      resources["resources"].values.map! do |v|
+        v["strings"]["url"] = nil unless valid_url? v["strings"]["url"]
+      end
       resources["resources"].values
     end
+
+    # This is to avoid any problems if upstream contains urls with `script:` or
+    # similar schemes, i.e. to guard against supply-chain attacks.
+    # Unfortunately the validates_url gem doesn't support `mailto:` or similar
+    # urls. This method is based on their approach to validation.
+    def self.valid_url?(url)
+      return true if url.nil?
+
+      schemes = %w[http https mailto xmpp]
+      uri = URI.parse(url)
+      scheme = uri&.scheme
+
+      valid_raw_url = scheme && url =~ /\A#{URI::DEFAULT_PARSER.make_regexp([scheme])}\z/
+      valid_scheme = scheme && schemes.include?(scheme)
+      return true if valid_raw_url && valid_scheme
+
+      false
+    rescue URI::InvalidURIError, URI::InvalidComponentError
+      false
+    end
   end
 end