|
| 1 | +from django.http import HttpResponse, HttpResponseNotFound, HttpRequest |
| 2 | + |
| 3 | +from django_async_extensions.amiddleware.clickjacking import ( |
| 4 | + AsyncXFrameOptionsMiddleware, |
| 5 | +) |
| 6 | + |
| 7 | + |
| 8 | +async def get_response_empty(request): |
| 9 | + return HttpResponse() |
| 10 | + |
| 11 | + |
| 12 | +async def get_response_404(request): |
| 13 | + return HttpResponseNotFound() |
| 14 | + |
| 15 | + |
| 16 | +class TestXFrameOptionsMiddleware: |
| 17 | + """ |
| 18 | + Tests for the X-Frame-Options clickjacking prevention middleware. |
| 19 | + """ |
| 20 | + |
| 21 | + async def test_same_origin(self, settings): |
| 22 | + """ |
| 23 | + The X_FRAME_OPTIONS setting can be set to SAMEORIGIN to have the |
| 24 | + middleware use that value for the HTTP header. |
| 25 | + """ |
| 26 | + settings.X_FRAME_OPTIONS = "SAMEORIGIN" |
| 27 | + r = await AsyncXFrameOptionsMiddleware(get_response_empty)(HttpRequest()) |
| 28 | + assert r.headers["X-Frame-Options"] == "SAMEORIGIN" |
| 29 | + |
| 30 | + settings.X_FRAME_OPTIONS = "sameorigin" |
| 31 | + r = await AsyncXFrameOptionsMiddleware(get_response_empty)(HttpRequest()) |
| 32 | + assert r.headers["X-Frame-Options"] == "SAMEORIGIN" |
| 33 | + |
| 34 | + async def test_deny(self, settings): |
| 35 | + """ |
| 36 | + The X_FRAME_OPTIONS setting can be set to DENY to have the middleware |
| 37 | + use that value for the HTTP header. |
| 38 | + """ |
| 39 | + settings.X_FRAME_OPTIONS = "DENY" |
| 40 | + r = await AsyncXFrameOptionsMiddleware(get_response_empty)(HttpRequest()) |
| 41 | + assert r.headers["X-Frame-Options"] == "DENY" |
| 42 | + |
| 43 | + settings.X_FRAME_OPTIONS = "deny" |
| 44 | + r = await AsyncXFrameOptionsMiddleware(get_response_empty)(HttpRequest()) |
| 45 | + assert r.headers["X-Frame-Options"] == "DENY" |
| 46 | + |
| 47 | + async def test_defaults_sameorigin(self, settings): |
| 48 | + """ |
| 49 | + If the X_FRAME_OPTIONS setting is not set then it defaults to |
| 50 | + DENY. |
| 51 | + """ |
| 52 | + settings.X_FRAME_OPTIONS = None |
| 53 | + del settings.X_FRAME_OPTIONS # restored by override_settings |
| 54 | + r = await AsyncXFrameOptionsMiddleware(get_response_empty)(HttpRequest()) |
| 55 | + assert r.headers["X-Frame-Options"] == "DENY" |
| 56 | + |
| 57 | + async def test_dont_set_if_set(self, settings): |
| 58 | + """ |
| 59 | + If the X-Frame-Options header is already set then the middleware does |
| 60 | + not attempt to override it. |
| 61 | + """ |
| 62 | + |
| 63 | + async def same_origin_response(request): |
| 64 | + response = HttpResponse() |
| 65 | + response.headers["X-Frame-Options"] = "SAMEORIGIN" |
| 66 | + return response |
| 67 | + |
| 68 | + async def deny_response(request): |
| 69 | + response = HttpResponse() |
| 70 | + response.headers["X-Frame-Options"] = "DENY" |
| 71 | + return response |
| 72 | + |
| 73 | + settings.X_FRAME_OPTIONS = "DENY" |
| 74 | + r = await AsyncXFrameOptionsMiddleware(same_origin_response)(HttpRequest()) |
| 75 | + assert r.headers["X-Frame-Options"] == "SAMEORIGIN" |
| 76 | + |
| 77 | + settings.X_FRAME_OPTIONS = "SAMEORIGIN" |
| 78 | + r = await AsyncXFrameOptionsMiddleware(deny_response)(HttpRequest()) |
| 79 | + assert r.headers["X-Frame-Options"] == "DENY" |
| 80 | + |
| 81 | + async def test_response_exempt(self, settings): |
| 82 | + """ |
| 83 | + If the response has an xframe_options_exempt attribute set to False |
| 84 | + then it still sets the header, but if it's set to True then it doesn't. |
| 85 | + """ |
| 86 | + |
| 87 | + async def xframe_exempt_response(request): |
| 88 | + response = HttpResponse() |
| 89 | + response.xframe_options_exempt = True |
| 90 | + return response |
| 91 | + |
| 92 | + async def xframe_not_exempt_response(request): |
| 93 | + response = HttpResponse() |
| 94 | + response.xframe_options_exempt = False |
| 95 | + return response |
| 96 | + |
| 97 | + settings.X_FRAME_OPTIONS = "SAMEORIGIN" |
| 98 | + r = await AsyncXFrameOptionsMiddleware(xframe_not_exempt_response)( |
| 99 | + HttpRequest() |
| 100 | + ) |
| 101 | + assert r.headers["X-Frame-Options"] == "SAMEORIGIN" |
| 102 | + |
| 103 | + r = await AsyncXFrameOptionsMiddleware(xframe_exempt_response)(HttpRequest()) |
| 104 | + assert r.headers.get("X-Frame-Options") is None |
| 105 | + |
| 106 | + async def test_is_extendable(self, settings): |
| 107 | + """ |
| 108 | + The XFrameOptionsMiddleware method that determines the X-Frame-Options |
| 109 | + header value can be overridden based on something in the request or |
| 110 | + response. |
| 111 | + """ |
| 112 | + |
| 113 | + class OtherXFrameOptionsMiddleware(AsyncXFrameOptionsMiddleware): |
| 114 | + # This is just an example for testing purposes... |
| 115 | + def get_xframe_options_value(self, request, response): |
| 116 | + if getattr(request, "sameorigin", False): |
| 117 | + return "SAMEORIGIN" |
| 118 | + if getattr(response, "sameorigin", False): |
| 119 | + return "SAMEORIGIN" |
| 120 | + return "DENY" |
| 121 | + |
| 122 | + async def same_origin_response(request): |
| 123 | + response = HttpResponse() |
| 124 | + response.sameorigin = True |
| 125 | + return response |
| 126 | + |
| 127 | + settings.X_FRAME_OPTIONS = "DENY" |
| 128 | + r = await OtherXFrameOptionsMiddleware(same_origin_response)(HttpRequest()) |
| 129 | + assert r.headers["X-Frame-Options"] == "SAMEORIGIN" |
| 130 | + |
| 131 | + request = HttpRequest() |
| 132 | + request.sameorigin = True |
| 133 | + r = await OtherXFrameOptionsMiddleware(get_response_empty)(request) |
| 134 | + assert r.headers["X-Frame-Options"] == "SAMEORIGIN" |
| 135 | + |
| 136 | + settings.X_FRAME_OPTIONS = "SAMEORIGIN" |
| 137 | + r = await OtherXFrameOptionsMiddleware(get_response_empty)(HttpRequest()) |
| 138 | + assert r.headers["X-Frame-Options"] == "DENY" |
0 commit comments