Commit 63f3b036 authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[WPT/common/security-features] Introduce downgrade redirection

As preparation for migrating wpt/upgrade-insecure-requests
to the /common/security-features generator framework
in https://chromium-review.googlesource.com/c/chromium/src/+/1788551,
this CL introduces `downgrade` redirection type,
which is to be used to test upgrading on redirects.

Bug: 1001422
Change-Id: I590015cdb48c9814135dbd61a91aa318ddeba2fc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1787571Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695934}
parent 54694d0b
...@@ -32,10 +32,15 @@ def __get_swapped_origin_netloc(netloc, subdomain_prefix = "www1."): ...@@ -32,10 +32,15 @@ def __get_swapped_origin_netloc(netloc, subdomain_prefix = "www1."):
# current request URL `request.url`, except for: # current request URL `request.url`, except for:
# - When `swap_scheme` or `swap_origin` is True, its scheme/origin is changed # - When `swap_scheme` or `swap_origin` is True, its scheme/origin is changed
# to the other one. (http <-> https, ws <-> wss, etc.) # to the other one. (http <-> https, ws <-> wss, etc.)
# - For `downgrade`, we redirect to a URL that would be successfully loaded
# if and only if upgrade-insecure-request is applied.
# - `query_parameter_to_remove` parameter is removed from query part. # - `query_parameter_to_remove` parameter is removed from query part.
# Its default is "redirection" to avoid redirect loops. # Its default is "redirection" to avoid redirect loops.
def create_url(request, swap_scheme = False, swap_origin = False, def create_url(request,
query_parameter_to_remove = "redirection"): swap_scheme=False,
swap_origin=False,
downgrade=False,
query_parameter_to_remove="redirection"):
parsed = urlparse.urlsplit(request.url) parsed = urlparse.urlsplit(request.url)
destination_netloc = parsed.netloc destination_netloc = parsed.netloc
...@@ -46,6 +51,24 @@ def create_url(request, swap_scheme = False, swap_origin = False, ...@@ -46,6 +51,24 @@ def create_url(request, swap_scheme = False, swap_origin = False,
port = request.server.config["ports"][scheme][0] port = request.server.config["ports"][scheme][0]
destination_netloc = ":".join([hostname, str(port)]) destination_netloc = ":".join([hostname, str(port)])
if downgrade:
# These rely on some unintuitive cleverness due to WPT's test setup:
# 'Upgrade-Insecure-Requests' does not upgrade the port number,
# so we use URLs in the form `http://[domain]:[https-port]`,
# which will be upgraded to `https://[domain]:[https-port]`.
# If the upgrade fails, the load will fail, as we don't serve HTTP over
# the secure port.
if parsed.scheme == "https":
scheme = "http"
elif parsed.scheme == "wss":
scheme = "ws"
else:
raise ValueError("Downgrade redirection: Invalid scheme '%s'" %
parsed.scheme)
hostname = parsed.netloc.split(':')[0]
port = request.server.config["ports"][parsed.scheme][0]
destination_netloc = ":".join([hostname, str(port)])
if swap_origin: if swap_origin:
destination_netloc = __get_swapped_origin_netloc(destination_netloc) destination_netloc = __get_swapped_origin_netloc(destination_netloc)
...@@ -75,6 +98,8 @@ def preprocess_redirection(request, response): ...@@ -75,6 +98,8 @@ def preprocess_redirection(request, response):
redirect_url = create_url(request, swap_scheme=False) redirect_url = create_url(request, swap_scheme=False)
elif redirection == "swap-scheme": elif redirection == "swap-scheme":
redirect_url = create_url(request, swap_scheme=True) redirect_url = create_url(request, swap_scheme=True)
elif redirection == "downgrade":
redirect_url = create_url(request, downgrade=True)
elif redirection == "keep-origin": elif redirection == "keep-origin":
redirect_url = create_url(request, swap_origin=False) redirect_url = create_url(request, swap_origin=False)
elif redirection == "swap-origin": elif redirection == "swap-origin":
......
...@@ -205,9 +205,11 @@ def validate(spec_json, details): ...@@ -205,9 +205,11 @@ def validate(spec_json, details):
test_expansion_schema, 'source_context_list', test_expansion_schema, 'source_context_list',
spec_json['source_context_list_schema'].keys()) spec_json['source_context_list_schema'].keys())
# Should be consistent with `preprocess_redirection` in
# `/common/security-features/subresource/subresource.py`.
assert_atom_or_list_items_from(test_expansion_schema, 'redirection', [ assert_atom_or_list_items_from(test_expansion_schema, 'redirection', [
'no-redirect', 'keep-origin', 'swap-origin', 'keep-scheme', 'no-redirect', 'keep-origin', 'swap-origin', 'keep-scheme',
'swap-scheme' 'swap-scheme', 'downgrade'
]) ])
for subresource in leaf_values(test_expansion_schema['subresource']): for subresource in leaf_values(test_expansion_schema['subresource']):
assert subresource in valid_subresource_names, "Invalid subresource %s" % subresource assert subresource in valid_subresource_names, "Invalid subresource %s" % subresource
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment