Skip to content

Commit

Permalink
Merge pull request #993 from hartwork/serve-security-txt
Browse files Browse the repository at this point in the history
Serve file `/.well-known/security.txt` (RFC 9116)
  • Loading branch information
hartwork committed Apr 20, 2024
2 parents 4efc164 + f67ac1d commit 08195e4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions wnpp_debian_net/static/well-known/security.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Contact: mailto:sebastian@pipping.org
Expires: 9999-12-31T23:59:59.999Z
Preferred-Languages: en, de
3 changes: 2 additions & 1 deletion wnpp_debian_net/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from .views.favicon import favicon_urlpatterns
from .views.front_page import FrontPageView
from .views.rss_feed import WnppNewsFeedView
from .views.security_txt import security_txt_urlpatterns
from .views.static_files import staticfiles_urlpatterns

urlpatterns = [
path('', FrontPageView.as_view(), name='front_page'),
path('admin/', admin.site.urls),
path('news.php5', WnppNewsFeedView(), name='news'),
] + favicon_urlpatterns() + staticfiles_urlpatterns()
] + favicon_urlpatterns() + staticfiles_urlpatterns() + security_txt_urlpatterns()
18 changes: 18 additions & 0 deletions wnpp_debian_net/views/security_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2024 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GNU Affero GPL v3 or later

import pkg_resources
from django.urls import re_path
from django.views.static import serve


def security_txt_urlpatterns(name='security_txt'):
return [
re_path('^(?:\\.well-known/)?(?P<path>security\\.txt)$',
serve,
kwargs={
'document_root': pkg_resources.resource_filename('wnpp_debian_net',
'static/well-known'),
},
name=name),
]
32 changes: 32 additions & 0 deletions wnpp_debian_net/views/tests/test_security_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2024 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GNU Affero GPL v3 or later

from django.http import FileResponse
from django.test import TestCase
from django.urls import reverse_lazy
from parameterized import parameterized


class SecurityTxtTest(TestCase):

@parameterized.expand([
(
'default location',
'/.well-known/security.txt',
),
(
'legacy location',
'/security.txt',
),
(
'through reverse',
reverse_lazy('security_txt', kwargs={'path': 'security.txt'}),
),
])
def test_file_served_properly(self, _label, url):
self.assertTrue(url.endswith('/security.txt'))

response = self.client.get(url)

self.assertIsInstance(response, FileResponse)
self.assertEqual(response.headers['Content-Type'], 'text/plain')

0 comments on commit 08195e4

Please sign in to comment.