summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/worklets/resources/referrer-checker.py
blob: 4c6ea6e5c21d3fa92f511941e0b73f285d57b8cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Returns a valid response when request's |referrer| matches
# |expected_referrer|.
def main(request, response):
    # We want |referrer| to be the referrer header with no query params,
    # because |expected_referrer| will not contain any query params, and
    # thus cannot be compared with the actual referrer header if it were to
    # contain query params. This works fine if the actual referrer has no
    # query params too.
    referrer = request.headers.get(b"referer", b"").split(b"?")[0]
    referrer_policy = request.GET.first(b"referrer_policy")
    expected_referrer = request.GET.first(b"expected_referrer", b"")
    response_headers = [(b"Content-Type", b"text/javascript"),
                        (b"Access-Control-Allow-Origin", b"*")]

    if referrer_policy == b"no-referrer" or referrer_policy == b"origin":
        if referrer == expected_referrer:
            return (200, response_headers, u"")
        return (404, response_headers)

    if referrer_policy == b"same-origin":
        if referrer == expected_referrer:
            return (200, response_headers, u"")
        return (404, response_headers)
    return (404, response_headers)