Skip to content

Commit 5e0c279

Browse files
committed
Add simple clickjacking prevention
Support X-Frame-Options with a default of SAMEORIGIN.
1 parent 459cb29 commit 5e0c279

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

python/nav/django/settings.py

+4-1
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',
@@ -261,12 +262,14 @@
261262
# Configured in etc/webfront/webfront.conf:
262263
# [security]
263264
# needs_tls = yes
265+
# frames_allow = self
264266

265267
SECURE_BROWSER_XSS_FILTER = True # Does no harm
266268

267269
_websecurity_config = WebSecurityConfigParser()
268-
_needs_tls = bool(_websecurity_config.getboolean('security', 'needs_tls'))
270+
_needs_tls = bool(_websecurity_config.getboolean('needs_tls'))
269271
SESSION_COOKIE_SECURE = _needs_tls
272+
X_FRAME_OPTIONS = _websecurity_config.get_x_frame_options()
270273

271274
# Hack for hackers to use features like debug_toolbar etc.
272275
# 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)