Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
2.9 KB
Raw Blame
# The browser origins allowed to make cross-origin requests to this instance.
#
# Configured with ALLOWED_ORIGINS (comma-separated, full origins including the
# scheme), falling back to APP_DOMAIN for the common single-host deploy.
#
# When neither is set the list is empty, which means no Access-Control-Allow-Origin
# header is sent and browsers apply their same-origin default. That is the safe
# outcome, and the boot warning tells the operator how to widen it.
module CorsOrigins
  module_function

  LOOPBACK_HOSTS = %w[localhost 127.0.0.1 ::1].freeze

  # The match is exact: an origin is a scheme, host and port, and prefix
  # matching is how "https://app.example.com.evil.test" gets let in.
  #
  # Development also accepts loopback origins on any port. Clients served by a
  # local dev server, such as `flutter run -d chrome`, pick a port at random,
  # and those relied on the wildcard this list replaced.
  def allowed?(origin)
    list.include?(origin) || (Rails.env.development? && loopback?(origin))
  end

  def loopback?(origin)
    uri = parse(origin)
    !uri.nil? && %w[http https].include?(uri.scheme) && LOOPBACK_HOSTS.include?(uri.hostname)
  end

  def list
    explicit_origins.presence || app_domain_origin
  end

  def explicit_origins
    ENV["ALLOWED_ORIGINS"].to_s.split(",").filter_map { |origin| normalize(origin) }
  end

  def app_domain_origin
    domain = ENV["APP_DOMAIN"].to_s.strip
    return [] if domain.empty?

    # APP_DOMAIN is documented as a bare domain, but the WebAuthn initializer
    # already copes with operators pasting a full URL, so do the same here
    # rather than building "https://https://app.example.com".
    origin = domain.match?(%r{\Ahttps?://}i) ? domain : "#{scheme}://#{domain}"
    [ normalize(origin) ].compact
  end

  # Only reached through the APP_DOMAIN fallback, and only as good as what the
  # app knows about how it is served. Behind a proxy that terminates TLS while
  # neither RAILS_FORCE_SSL nor RAILS_ASSUME_SSL is set, this says http while
  # the browser sends https, and a legitimate origin is refused. Set
  # ALLOWED_ORIGINS explicitly on those deploys.
  def scheme
    config = Rails.application.config
    ssl = config.force_ssl || (config.respond_to?(:assume_ssl) && config.assume_ssl)
    ssl ? "https" : "http"
  end

  # An Origin header carries a lowercased scheme and host, no path and no
  # trailing slash. Operators write the value by hand, often copied from a
  # browser's address bar, so normalize what they give us instead of failing
  # the comparison silently.
  def normalize(origin)
    uri = parse(origin)
    return nil if uri.nil? || uri.scheme.nil? || uri.host.nil?

    port = uri.port && uri.port != uri.default_port ? ":#{uri.port}" : ""
    "#{uri.scheme.downcase}://#{uri.host.downcase}#{port}"
  end

  def parse(origin)
    value = origin.to_s.strip
    URI.parse(value) unless value.empty?
  rescue URI::InvalidURIError
    nil
  end
end