Module: Aspera::ServletHelpers

Overview

Mixin for WEBrick servlets of local web servers: JSON responses, and check of requests that a web page could send.

A web page open in the browser of the user can send requests to a server that the browser reaches, also on localhost:

  • Cross-site request forgery: a form, or a fetch with a CORS-safelisted Content-Type (text/plain, form), is sent cross-origin without preflight.
  • DNS rebinding: a host name of the attacker that resolves to 127.0.0.1 makes the page same-origin with the server, so it also reads responses.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.host_name(host) ⇒ String

Returns Host name, without port and IPv6 brackets.

Parameters:

  • host (String) —

    Value of header Host, e.g. [::1]:8080

Returns:

  • (String) —

    Host name, without port and IPv6 brackets



31
32
33
34
# File 'lib/aspera/servlet_helpers.rb', line 31

def host_name(host)
  return host[/\A\[([^\]]*)\]/, 1].to_s if host.start_with?('[')
  host.split(':').first.to_s
end

.origin(url) ⇒ String

Returns Origin of the URL, as in header Origin, e.g. https://example.com:8443.

Parameters:

Returns:

  • (String) —

    Origin of the URL, as in header Origin, e.g. https://example.com:8443



23
24
25
26
27
# File 'lib/aspera/servlet_helpers.rb', line 23

def origin(url)
  uri = URI.parse(url)
  port = ":#{uri.port}" unless uri.port.eql?(uri.default_port)
  "#{uri.scheme}://#{uri.host}#{port}".downcase
end

.same_origin?(origin, host) ⇒ Boolean

Scheme is not compared: header Host has none

Parameters:

  • origin (String) —

    Value of header Origin, e.g. http://localhost:8080

  • host (String) —

    Value of header Host, e.g. localhost:8080

Returns:

  • (Boolean) —

    true if the origin is the one of the requested server



40
41
42
43
# File 'lib/aspera/servlet_helpers.rb', line 40

def same_origin?(origin, host)
  default_port = origin.start_with?('https://') ? ':443' : ':80'
  origin.sub(%r{\Ahttps?://}, '').delete_suffix(default_port).eql?(host.delete_suffix(default_port))
end

Instance Method Details

#authorization(request, scheme) ⇒ String?

Returns Credentials of header Authorization if it has this scheme, else nil.

Parameters:

  • request (WEBrick::HTTPRequest)
  • scheme (String) —

    Authentication scheme, e.g. Basic or Bearer

Returns:

  • (String, nil) —

    Credentials of header Authorization if it has this scheme, else nil



76
77
78
79
# File 'lib/aspera/servlet_helpers.rb', line 76

def authorization(request, scheme)
  request_scheme, credentials = request['Authorization'].to_s.split(' ', 2)
  credentials.to_s if request_scheme.to_s.casecmp?(scheme)
end

#basic_authorized?(request, expected) ⇒ Boolean

Check the credentials of HTTP Basic authentication, in constant time

Parameters:

  • request (WEBrick::HTTPRequest)
  • expected (String) —

    Expected username:password

Returns:

  • (Boolean) —

    true if the request has the expected credentials



85
86
87
# File 'lib/aspera/servlet_helpers.rb', line 85

def basic_authorized?(request, expected)
  OpenSSL.secure_compare(authorization(request, 'Basic').to_s.unpack1('m').to_s, expected)
end

#check_request(request, json: false, allowed_hosts: [], allowed_origins: []) ⇒ Object

Refuse a request that a web page in a browser could send. Clients that are not browsers send no Origin, and the Host of the URL they call.

  • Host: when the server listens on loopback, a loopback name or address, or one of allowed_hosts (DNS rebinding)
  • Origin: when present, the origin of the server, or one of allowed_origins (cross-site request)
  • Content-Type: with json, a JSON type, which a browser sends cross-origin only after a preflight request OPTIONS, not answered (cross-site request)

Parameters:

  • request (WEBrick::HTTPRequest)
  • json (Boolean) (defaults to: false) —

    true if the body, if any, is parsed as JSON

  • allowed_hosts (Array<String>) (defaults to: []) —

    Other values accepted in Host: host name, or host:port

  • allowed_origins (Array<String>) (defaults to: []) —

    Other values accepted in Origin, e.g. https://example.com

Raises:

  • (WEBrick::HTTPStatus::Forbidden) —

    if Host or Origin is refused

  • (WEBrick::HTTPStatus::UnsupportedMediaType) —

    if the body is not JSON



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aspera/servlet_helpers.rb', line 99

def check_request(request, json: false, allowed_hosts: [], allowed_origins: [])
  host = request['Host']&.downcase
  if host && WebServerSimple.loopback?(@server[:BindAddress].to_s)
    name = ServletHelpers.host_name(host)
    host_allowed = WebServerSimple.loopback?(name) || allowed_hosts.any? { |allowed_host| [name, host].include?(allowed_host.downcase) }
    raise WEBrick::HTTPStatus::Forbidden, "Host is not a loopback address: #{host}" unless host_allowed
  end
  origin = request['Origin']&.downcase
  origin_allowed = origin.nil? || (host && ServletHelpers.same_origin?(origin, host)) || allowed_origins.any? { |allowed_origin| allowed_origin.downcase.eql?(origin) }
  raise WEBrick::HTTPStatus::Forbidden, "Origin is not the one of the server: #{origin}" unless origin_allowed
  return unless json && !request.body.to_s.empty?
  media_type = request['Content-Type'].to_s.split(';').first.to_s.strip.downcase
  raise WEBrick::HTTPStatus::UnsupportedMediaType, "Content-Type shall be #{Mime::JSON}: #{media_type}" unless Mime.json?(media_type)
end

#json_error(response, status, error, description = nil, headers: {}) ⇒ nil

Set a JSON error response, in the format of OAuth 2.0 (RFC 6749 §5.2)

Parameters:

  • response (WEBrick::HTTPResponse)
  • status (Integer) —

    HTTP status code

  • error (String) —

    Error code or message

  • description (String, nil) (defaults to: nil) —

    Details

  • headers (Hash{String => String}) (defaults to: {}) —

    Other headers of the response, e.g. WWW-Authenticate

Returns:

  • (nil)


67
68
69
70
71
# File 'lib/aspera/servlet_helpers.rb', line 67

def json_error(response, status, error, description = nil, headers: {})
  body = {error: error}
  body[:error_description] = description unless description.nil?
  json_response(response, status, body, headers: headers)
end

#json_response(response, status, body, headers: {}) ⇒ nil

Set a JSON response

Parameters:

  • response (WEBrick::HTTPResponse)
  • status (Integer) —

    HTTP status code

  • body (Hash, Array) —

    Body of the response

  • headers (Hash{String => String}) (defaults to: {}) —

    Other headers of the response, e.g. Cache-Control

Returns:

  • (nil)


52
53
54
55
56
57
58
# File 'lib/aspera/servlet_helpers.rb', line 52

def json_response(response, status, body, headers: {})
  response.status = status
  response['Content-Type'] = Mime::JSON
  headers.each { |name, value| response[name] = value }
  response.body = JSON.generate(body)
  nil
end