Skip to content

Commit cefc5fb

Browse files
committed
Add simple clickjacking prevention
Support X-Frame-Options with a default of SAMEORIGIN.
1 parent 57dd64d commit cefc5fb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

python/nav/django/settings.py

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126

127127
# Middleware
128128
MIDDLEWARE = (
129+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
129130
'django.middleware.common.CommonMiddleware',
130131
'django.contrib.sessions.middleware.SessionMiddleware',
131132
'nav.web.auth.middleware.AuthenticationMiddleware',
@@ -260,12 +261,14 @@
260261
# Example conf:
261262
# [security]
262263
# needs_ssl = yes
264+
# frames_allow = self
263265

264266
SECURE_BROWSER_XSS_FILTER = True # Does no harm
265267

266268
_websecurity_config = WebSecurityConfigParser()
267269
_needs_tls = bool(_websecurity_config.getboolean('security', 'needs_tls'))
268270
SESSION_COOKIE_SECURE = _needs_tls
271+
X_FRAME_OPTIONS = _websecurity_config.get_x_frame_options()
269272

270273
# Hack for hackers to use features like debug_toolbar etc.
271274
# https://code.djangoproject.com/wiki/SplitSettings (Rob Golding's method)

python/nav/web/security.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
from pathlib import Path
22

3-
from nav.config import NAVConfigParser
3+
from nav.config import NavConfigParserDefaultSection
44

55

6-
class WebSecurityConfigParser(NAVConfigParser):
6+
class WebSecurityConfigParser(NavConfigParserDefaultSection):
7+
SECTION = "security"
78
DEFAULT_CONFIG_FILES = [str(Path('webfront') / 'webfront.conf')]
89
DEFAULT_CONFIG = u"""
910
[security]
1011
needs_tls=no
12+
allow_frames=self
1113
"""
14+
FRAMES_OPTION = 'allow_frames'
15+
FRAMES_DEFAULT = 'self'
16+
17+
def __init__(self):
18+
super().__init__(self.SECTION)
19+
20+
# clickjacking-settings
21+
22+
def get_x_frame_options(self):
23+
"Translate CSP frame ancestors to the old X-Frame-Options header"
24+
frames_flag = self.get(self.FRAMES_OPTION) or self.FRAMES_DEFAULT
25+
if frames_flag == 'none':
26+
return 'DENY'
27+
return 'SAMEORIGIN'

0 commit comments

Comments
 (0)