1 # A backend for FrozenRecord
 
   3 module OsmCommunityIndex
 
   5     def self.filename(_model)
 
   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"]
 
  14       resources["resources"].values
 
  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?
 
  24       schemes = %w[http https mailto xmpp]
 
  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
 
  33     rescue URI::InvalidURIError, URI::InvalidComponentError