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

Onion location header #695

Merged
merged 3 commits into from
Jun 24, 2020
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
14 changes: 14 additions & 0 deletions common/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from common.models import FooterSettings


class OnionLocationMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)

footer_settings = FooterSettings.for_site(request.site)
if footer_settings.securedrop_onion_address:
response['Onion-Location'] = footer_settings.securedrop_onion_address
return response
19 changes: 19 additions & 0 deletions common/migrations/0004_auto_20200624_1553.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.12 on 2020-06-24 15:53

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0003_customimage_file_hash'),
]

operations = [
migrations.AlterField(
model_name='footersettings',
name='securedrop_onion_address',
field=models.CharField(default='secrdrop5wyphb5x.onion', help_text='Address for the securedrop.org onion service. Displayed in the site footer and the Onion-Location header. Must begin with http or https and end with .onion', max_length=255, validators=[django.core.validators.RegexValidator(message='Enter a valid .onion address.', regex='\\.onion$'), django.core.validators.URLValidator(message='Onion address must be a valid URL beginning with http or https', schemes=['http', 'https'])], verbose_name='SecureDrop Onion Address'),
),
]
9 changes: 7 additions & 2 deletions common/models/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.validators import RegexValidator
from django.core.validators import RegexValidator, URLValidator
from django.db import models

from wagtail.contrib.settings.models import BaseSetting, register_setting
Expand Down Expand Up @@ -44,14 +44,19 @@ class FooterSettings(BaseSetting):
'SecureDrop Onion Address',
max_length=255,
default='secrdrop5wyphb5x.onion',
validators=[RegexValidator(regex=r'\.onion$', message="Enter a valid .onion address.")]
validators=[
RegexValidator(regex=r'\.onion$', message="Enter a valid .onion address."),
URLValidator(schemes=['http', 'https'], message='Onion address must be a valid URL beginning with http or https'),
],
help_text='Address for the securedrop.org onion service. Displayed in the site footer and the Onion-Location header. Must begin with http or https and end with .onion',
)

panels = [
FieldPanel('title'),
SnippetChooserPanel('main_menu'),
FieldPanel('donation_url'),
PageChooserPanel('contribute_link'),
FieldPanel('securedrop_onion_address'),
MultiFieldPanel(
[
FieldPanel('release_key_description'),
Expand Down
1 change: 1 addition & 0 deletions securedrop/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@

# Middleware for content security policy
'csp.middleware.CSPMiddleware',
'common.middleware.OnionLocationMiddleware',
]

ROOT_URLCONF = 'securedrop.urls'
Expand Down