Module: Aspera::ServletHelpers
- Included in:
- Cli::Mcp::HttpServlet, OAuth::Server::Pkce, Plugins::Faspex4::GWServlet, Plugins::Faspex5::Faspex4PostProcServlet, Plugins::Node::NodeEmulatorServlet, WebAuth::Servlet
- Defined in:
- lib/aspera/servlet_helpers.rb
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
fetchwith a CORS-safelistedContent-Type(text/plain, form), is sent cross-origin without preflight. - DNS rebinding: a host name of the attacker that resolves to
127.0.0.1makes the page same-origin with the server, so it also reads responses.
Class Method Summary collapse
-
.host_name(host) ⇒ String
Host name, without port and IPv6 brackets.
-
.origin(url) ⇒ String
Origin of the URL, as in header
Origin, e.g. -
.same_origin?(origin, host) ⇒ Boolean
Scheme is not compared: header
Hosthas none.
Instance Method Summary collapse
-
#authorization(request, scheme) ⇒ String?
Credentials of header
Authorizationif it has this scheme, elsenil. -
#basic_authorized?(request, expected) ⇒ Boolean
Check the credentials of HTTP Basic authentication, in constant time.
-
#check_request(request, json: false, allowed_hosts: [], allowed_origins: []) ⇒ Object
Refuse a request that a web page in a browser could send.
-
#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).
-
#json_response(response, status, body, headers: {}) ⇒ nil
Set a JSON response.
Class Method Details
.host_name(host) ⇒ String
Returns 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.
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
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.
76 77 78 79 |
# File 'lib/aspera/servlet_helpers.rb', line 76 def (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
85 86 87 |
# File 'lib/aspera/servlet_helpers.rb', line 85 def (request, expected) OpenSSL.secure_compare((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 ofallowed_hosts(DNS rebinding)Origin: when present, the origin of the server, or one ofallowed_origins(cross-site request)Content-Type: withjson, a JSON type, which a browser sends cross-origin only after a preflight requestOPTIONS, not answered (cross-site request)
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)
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
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 |