]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/resource_backend.rb
Bump qs from 6.11.0 to 6.11.1
[rails.git] / lib / osm_community_index / resource_backend.rb
1 # A backend for FrozenRecord
2
3 module OsmCommunityIndex
4   module ResourceBackend
5     def self.filename(_model)
6       "resources.json"
7     end
8
9     def self.load(file_path)
10       resources = JSON.parse(File.read(file_path))
11       resources["resources"].values.map! do |v|
12         v["strings"]["url"] = nil unless valid_url? v["strings"]["url"]
13       end
14       resources["resources"].values
15     end
16
17     # This is to avoid any problems if upstream contains urls with `script:` or
18     # similar schemes, i.e. to guard against supply-chain attacks.
19     # Unfortunately the validates_url gem doesn't support `mailto:` or similar
20     # urls. This method is based on their approach to validation.
21     def self.valid_url?(url)
22       return true if url.nil?
23
24       schemes = %w[http https mailto xmpp]
25       uri = URI.parse(url)
26       scheme = uri&.scheme
27
28       valid_raw_url = scheme && url =~ /\A#{URI::DEFAULT_PARSER.make_regexp([scheme])}\z/
29       valid_scheme = scheme && schemes.include?(scheme)
30       return true if valid_raw_url && valid_scheme
31
32       false
33     rescue URI::InvalidURIError, URI::InvalidComponentError
34       false
35     end
36   end
37 end