Class: OpenehrRails::Opt::RemoteFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/openehr_rails/opt/remote_fetcher.rb

Overview

Fetches an OPT file over HTTP(S) (used for URL arguments to the scaffold/fhir_profile generators and the admin UI's URL-import field). Validates that the response actually parses as an operational template before handing it back, so callers never write garbage into app/templates/operational.

Defined Under Namespace

Classes: FetchError

Constant Summary collapse

MAX_REDIRECTS =
5
MAX_BODY_BYTES =
10 * 1024 * 1024
OPEN_TIMEOUT =
10
READ_TIMEOUT =
20
ALLOWED_SCHEMES =
%w[http https].freeze
BLOCKED_IP_RANGES =

Loopback, private, and link-local ranges (the latter includes the 169.254.169.254 cloud-metadata endpoint AWS/GCP/Azure all use). Checked against every hop (initial URL and every redirect target, since parse_uri runs on each via #get), not just the first.

[
  IPAddr.new('127.0.0.0/8'),
  IPAddr.new('10.0.0.0/8'),
  IPAddr.new('172.16.0.0/12'),
  IPAddr.new('192.168.0.0/16'),
  IPAddr.new('169.254.0.0/16'),
  IPAddr.new('::1/128'),
  IPAddr.new('fc00::/7'),
  IPAddr.new('fe80::/10')
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RemoteFetcher

Returns a new instance of RemoteFetcher.



44
45
46
# File 'lib/openehr_rails/opt/remote_fetcher.rb', line 44

def initialize(url)
  @url = url
end

Class Method Details

.fetch(url) ⇒ Object



40
41
42
# File 'lib/openehr_rails/opt/remote_fetcher.rb', line 40

def self.fetch(url)
  new(url).fetch
end

Instance Method Details

#fetchObject



48
49
50
51
52
53
# File 'lib/openehr_rails/opt/remote_fetcher.rb', line 48

def fetch
  body = get(@url, MAX_REDIRECTS)
  validate_size!(body)
  validate_opt!(body)
  body
end