Skip to content

Commit

Permalink
Python 3: Port tests in x-frame-options and worklets (#24085)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziransun committed Jun 11, 2020
1 parent be852b3 commit 77211c4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
14 changes: 7 additions & 7 deletions worklets/resources/credentials.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Returns a valid response when a request has appropriate credentials.
def main(request, response):
cookie = request.cookies.first("cookieName", None)
expected_value = request.GET.first("value", None)
source_origin = request.headers.get("origin", None)
cookie = request.cookies.first(b"cookieName", None)
expected_value = request.GET.first(b"value", None)
source_origin = request.headers.get(b"origin", None)

response_headers = [("Content-Type", "text/javascript"),
("Access-Control-Allow-Origin", source_origin),
("Access-Control-Allow-Credentials", "true")]
response_headers = [(b"Content-Type", b"text/javascript"),
(b"Access-Control-Allow-Origin", source_origin),
(b"Access-Control-Allow-Credentials", b"true")]

if cookie == expected_value:
return (200, response_headers, "")
return (200, response_headers, u"")

return (404, response_headers)
18 changes: 9 additions & 9 deletions worklets/resources/referrer-checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ def main(request, response):
# 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("referer", "").split("?")[0]
referrer_policy = request.GET.first("referrer_policy")
expected_referrer = request.GET.first("expected_referrer", "")
response_headers = [("Content-Type", "text/javascript"),
("Access-Control-Allow-Origin", "*")]
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 == "no-referrer" or referrer_policy == "origin":
if referrer_policy == b"no-referrer" or referrer_policy == b"origin":
if referrer == expected_referrer:
return (200, response_headers, "")
return (200, response_headers, u"")
return (404, response_headers)

if referrer_policy == "same-origin":
if referrer_policy == b"same-origin":
if referrer == expected_referrer:
return (200, response_headers, "")
return (200, response_headers, u"")
return (404, response_headers)
return (404, response_headers)
14 changes: 7 additions & 7 deletions worklets/resources/set-cookie.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
def main(request, response):
name = request.GET.first("name")
value = request.GET.first("value")
source_origin = request.headers.get("origin", None)
name = request.GET.first(b"name")
value = request.GET.first(b"value")
source_origin = request.headers.get(b"origin", None)

response_headers = [("Set-Cookie", name + "=" + value),
("Access-Control-Allow-Origin", source_origin),
("Access-Control-Allow-Credentials", "true")]
return (200, response_headers, "")
response_headers = [(b"Set-Cookie", name + b"=" + value),
(b"Access-Control-Allow-Origin", source_origin),
(b"Access-Control-Allow-Credentials", b"true")]
return (200, response_headers, u"")
10 changes: 5 additions & 5 deletions x-frame-options/support/nested.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def main(request, response):
origin = request.GET.first("origin");
value = request.GET.first("value");
origin = request.GET.first(b"origin");
value = request.GET.first(b"value");
# This is used to solve the race condition we have for postMessages
shouldSucceed = request.GET.first("loadShouldSucceed", "false");
return ([("Content-Type", "text/html")],
"""<!DOCTYPE html>
shouldSucceed = request.GET.first(b"loadShouldSucceed", b"false");
return ([(b"Content-Type", b"text/html")],
b"""<!DOCTYPE html>
<title>XFO.</title>
<body>
<script>
Expand Down
4 changes: 2 additions & 2 deletions x-frame-options/support/redirect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def main(request, response):
response.status = 302
response.headers.set("X-Frame-Options", request.GET.first("value"))
response.headers.set("Location", request.GET.first("url"))
response.headers.set(b"X-Frame-Options", request.GET.first(b"value"))
response.headers.set(b"Location", request.GET.first(b"url"))
12 changes: 6 additions & 6 deletions x-frame-options/support/xfo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
def main(request, response):
headers = [("Content-Type", "text/html"), ("X-Frame-Options", request.GET.first("value"))]
headers = [(b"Content-Type", b"text/html"), (b"X-Frame-Options", request.GET.first(b"value"))]

if "value2" in request.GET:
headers.append(("X-Frame-Options", request.GET.first("value2")))
if b"value2" in request.GET:
headers.append((b"X-Frame-Options", request.GET.first(b"value2")))

if "csp_value" in request.GET:
headers.append(("Content-Security-Policy", request.GET.first("csp_value")))
if b"csp_value" in request.GET:
headers.append((b"Content-Security-Policy", request.GET.first(b"csp_value")))

body = """<!DOCTYPE html>
body = u"""<!DOCTYPE html>
<html>
<head>
<title>XFO.</title>
Expand Down

0 comments on commit 77211c4

Please sign in to comment.