2011-05-31 06:27:47 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
|
|
def xframe_options_deny(view_func):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Modify a view function so its response has the X-Frame-Options HTTP
|
2011-05-31 06:27:47 +08:00
|
|
|
header set to 'DENY' as long as the response doesn't already have that
|
2017-01-25 04:36:07 +08:00
|
|
|
header set. Usage:
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
@xframe_options_deny
|
|
|
|
def some_view(request):
|
|
|
|
...
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
@wraps(view_func)
|
2022-05-22 14:20:29 +08:00
|
|
|
def wrapper_view(*args, **kwargs):
|
2011-05-31 06:27:47 +08:00
|
|
|
resp = view_func(*args, **kwargs)
|
2015-05-14 02:51:18 +08:00
|
|
|
if resp.get("X-Frame-Options") is None:
|
2011-05-31 06:27:47 +08:00
|
|
|
resp["X-Frame-Options"] = "DENY"
|
|
|
|
return resp
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
return wrapper_view
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def xframe_options_sameorigin(view_func):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Modify a view function so its response has the X-Frame-Options HTTP
|
2011-05-31 06:27:47 +08:00
|
|
|
header set to 'SAMEORIGIN' as long as the response doesn't already have
|
2017-01-25 04:36:07 +08:00
|
|
|
that header set. Usage:
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
@xframe_options_sameorigin
|
|
|
|
def some_view(request):
|
|
|
|
...
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
@wraps(view_func)
|
2022-05-22 14:20:29 +08:00
|
|
|
def wrapper_view(*args, **kwargs):
|
2011-05-31 06:27:47 +08:00
|
|
|
resp = view_func(*args, **kwargs)
|
2015-05-14 02:51:18 +08:00
|
|
|
if resp.get("X-Frame-Options") is None:
|
2011-05-31 06:27:47 +08:00
|
|
|
resp["X-Frame-Options"] = "SAMEORIGIN"
|
|
|
|
return resp
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
return wrapper_view
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def xframe_options_exempt(view_func):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Modify a view function by setting a response variable that instructs
|
|
|
|
XFrameOptionsMiddleware to NOT set the X-Frame-Options HTTP header. Usage:
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
@xframe_options_exempt
|
|
|
|
def some_view(request):
|
|
|
|
...
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
@wraps(view_func)
|
2022-05-22 14:20:29 +08:00
|
|
|
def wrapper_view(*args, **kwargs):
|
2011-05-31 06:27:47 +08:00
|
|
|
resp = view_func(*args, **kwargs)
|
|
|
|
resp.xframe_options_exempt = True
|
|
|
|
return resp
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2022-05-22 14:26:21 +08:00
|
|
|
return wrapper_view
|