Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for http header X-Frame-Options #2816

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 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 @@ -261,12 +262,14 @@
# Configured in etc/webfront/webfront.conf:
# [security]
# needs_tls = yes
# frames_allow = self

SECURE_BROWSER_XSS_FILTER = True # Does no harm

_websecurity_config = WebSecurityConfigParser()
_needs_tls = bool(_websecurity_config.getboolean('security', 'needs_tls'))
_needs_tls = bool(_websecurity_config.getboolean('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'

Check warning on line 26 in python/nav/web/security.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/security.py#L26

Added line #L26 was not covered by tests
return 'SAMEORIGIN'
Loading