Skip to content

Commit

Permalink
Add simple clickjacking prevention
Browse files Browse the repository at this point in the history
Support X-Frame-Options with a default of SAMEORIGIN.
  • Loading branch information
hmpf committed Feb 26, 2024
1 parent 68f918b commit c732d24
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions python/nav/django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@

# Middleware
MIDDLEWARE = (
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'nav.web.auth.middleware.AuthenticationMiddleware',
Expand Down Expand Up @@ -262,12 +263,14 @@
# Example conf:
# [security]
# needs_ssl = yes
# frames_allow = self

SECURE_BROWSER_XSS_FILTER = True # Does no harm

_websecurity_config = WebSecurityConfigParser()
_needs_tls = bool(_websecurity_config.getboolean('security', 'needs_tls'))
SESSION_COOKIE_SECURE = _needs_tls
X_FRAME_OPTIONS = _websecurity_config.get_x_frame_options()

# Hack for hackers to use features like debug_toolbar etc.
# https://code.djangoproject.com/wiki/SplitSettings (Rob Golding's method)
Expand Down
20 changes: 18 additions & 2 deletions python/nav/web/security.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
from pathlib import Path

from nav.config import NAVConfigParser
from nav.config import NavConfigParserDefaultSection


class WebSecurityConfigParser(NAVConfigParser):
class WebSecurityConfigParser(NavConfigParserDefaultSection):
SECTION = "security"
DEFAULT_CONFIG_FILES = [str(Path('webfront') / 'webfront.conf')]
DEFAULT_CONFIG = u"""
[security]
needs_tls=no
allow_frames=self
"""
FRAMES_OPTION = 'allow_frames'
FRAMES_DEFAULT = 'self'

def __init__(self):
super().__init__(self.SECTION)

# clickjacking-settings

def get_x_frame_options(self):
"Translate CSP frame ancestors to the old X-Frame-Options header"
frames_flag = self.get(self.FRAMES_OPTION) or self.FRAMES_DEFAULT
if frames_flag == 'none':
return 'DENY'
return 'SAMEORIGIN'

0 comments on commit c732d24

Please sign in to comment.