Skip to content

Commit 7689aca

Browse files
authored
UA 2779 | [django-oidc-provider] should address security warnings (#8)
* it builds, tests pass, and I removed a couple bandit warnings of the Severity: High Confidence: High variety * formatting fix in settings * added the orm back into the version string in a PEP 440 compliant way. * Implemented suggestions from the PR. Tests still pass
1 parent 27eeca3 commit 7689aca

18 files changed

+76
-100
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#
6363
# This is also used if you do content translation via gettext catalogs.
6464
# Usually you set "language" from the command line for these cases.
65-
language = None
65+
language = 'en'
6666

6767
# There are two options for replacing |today|: either, you set today to some
6868
# non-false value, then it is used:

example/app/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
44

5+
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
56

67
SECRET_KEY = 'c14d549c574e4d8cf162404ef0b04598'
78

example/app/urls.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
from django.contrib.auth import views as auth_views
2-
try:
3-
from django.urls import include, url
4-
except ImportError:
5-
from django.conf.urls import include, url
2+
from django.urls import include, re_path
63
from django.contrib import admin
74
from django.views.generic import TemplateView
85

96

107
urlpatterns = [
11-
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
12-
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
13-
url(r'^accounts/logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
14-
url(r'^', include('oidc_provider.urls', namespace='oidc_provider')),
15-
url(r'^admin/', admin.site.urls),
8+
re_path(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
9+
re_path(r'^accounts/login/$', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
10+
re_path(r'^accounts/logout/$', auth_views.LogoutView.as_view(next_page='/'), name='logout'),
11+
re_path(r'^', include('oidc_provider.urls', namespace='oidc_provider')),
12+
re_path(r'^admin/', admin.site.urls),
1613
]

example/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
django
2-
https://github.com/juanifioren/django-oidc-provider/archive/master.zip
1+
django==3.2.18
2+
../

oidc_provider/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
2-
default_app_config = 'oidc_provider.apps.OIDCProviderConfig'

oidc_provider/lib/endpoints/authorize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from urlparse import urlsplit, parse_qs, urlunsplit
1010
except ImportError:
1111
from urllib.parse import urlsplit, parse_qs, urlunsplit, urlencode
12-
from uuid import uuid4
12+
from secrets import token_hex
1313

1414
from django.utils import timezone
1515

@@ -206,7 +206,7 @@ def create_response_uri(self):
206206
redirect_uri_parsed.scheme, redirect_uri_parsed.netloc)
207207

208208
# Create random salt.
209-
salt = md5(uuid4().hex.encode()).hexdigest()
209+
salt = token_hex()
210210

211211
# The generation of suitable Session State values is based
212212
# on a salted cryptographic hash of Client ID, origin URL,

oidc_provider/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,4 @@ def __unicode__(self):
264264

265265
@property
266266
def kid(self):
267-
return u'{0}'.format(md5(self.key.encode('utf-8')).hexdigest() if self.key else '')
267+
return u'{0}'.format(md5(self.key.encode('utf-8'), usedforsecurity=False).hexdigest() if self.key else '')

oidc_provider/tests/app/urls.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
from django.contrib.auth import views as auth_views
2-
try:
3-
from django.urls import include, url
4-
except ImportError:
5-
from django.conf.urls import include, url
2+
from django.urls import re_path, include
63
from django.contrib import admin
74
from django.views.generic import TemplateView
85

96

107
urlpatterns = [
11-
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
12-
url(r'^accounts/login/$',
8+
re_path(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
9+
re_path(r'^accounts/login/$',
1310
auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
14-
url(r'^accounts/logout/$',
11+
re_path(r'^accounts/logout/$',
1512
auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
16-
url(r'^openid/', include('oidc_provider.urls', namespace='oidc_provider')),
17-
url(r'^admin/', admin.site.urls),
13+
re_path(r'^openid/', include('oidc_provider.urls', namespace='oidc_provider')),
14+
re_path(r'^admin/', admin.site.urls),
1815
]

oidc_provider/tests/cases/test_authorize_endpoint.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,18 @@ def test_response_uri_is_properly_constructed(self):
276276
parsed = urlsplit(response['Location'])
277277
params = parse_qs(parsed.query or parsed.fragment)
278278
state = params['state'][0]
279-
self.assertEquals(self.state, state, msg="State returned is invalid or missing")
279+
assert self.state == state, "State returned is invalid or missing"
280280

281281
is_code_ok = is_code_valid(url=response['Location'],
282282
user=self.user,
283283
client=self.client)
284-
self.assertTrue(is_code_ok, msg='Code returned is invalid or missing')
284+
assert is_code_ok, 'Code returned is invalid or missing'
285285

286-
self.assertEquals(
287-
set(params.keys()), {'state', 'code'},
288-
msg='More than state or code appended as query params')
286+
assert set(params.keys()) == {'state', 'code'}, \
287+
'More than state or code appended as query params'
289288

290-
self.assertTrue(
291-
response['Location'].startswith(self.client.default_redirect_uri),
292-
msg='Different redirect_uri returned')
289+
assert response['Location'].startswith(self.client.default_redirect_uri), \
290+
'Different redirect_uri returned'
293291

294292
def test_unknown_redirect_uris_are_rejected(self):
295293
"""
@@ -395,7 +393,7 @@ def test_prompt_login_parameter(self, logout_function):
395393

396394
response = self._auth_request('get', data, is_user_authenticated=True)
397395
self.assertIn(settings.get('OIDC_LOGIN_URL'), response['Location'])
398-
self.assertTrue(logout_function.called_once())
396+
logout_function.assert_called_once()
399397
self.assertNotIn(
400398
quote('prompt=login'),
401399
response['Location'],
@@ -662,7 +660,7 @@ def test_public_client_implicit_auto_approval(self):
662660

663661
response = self._auth_request('get', data, is_user_authenticated=True)
664662
response_text = response.content.decode('utf-8')
665-
self.assertEquals(response_text, '')
663+
assert response_text == ''
666664
components = urlsplit(response['Location'])
667665
fragment = parse_qs(components[4])
668666
self.assertIn('access_token', fragment)

oidc_provider/tests/cases/test_claims.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import unicode_literals
22

33
from django.test import TestCase
4-
from django.utils.six import text_type
54
from django.utils.translation import override as override_language
65

76
from oidc_provider.lib.claims import ScopeClaims, StandardScopeClaims, STANDARD_CLAIMS
@@ -49,19 +48,16 @@ def test_clean_dic(self):
4948
'phone_number': '',
5049
}
5150
clean_dict = self.scopeClaims._clean_dic(dict_to_clean)
52-
self.assertEquals(
53-
clean_dict,
54-
{
55-
'family_name': 'Doe',
56-
'given_name': 'John',
57-
'name': 'John Doe',
58-
'email': u'[email protected]'
59-
}
60-
)
51+
assert clean_dict == {
52+
'family_name': 'Doe',
53+
'given_name': 'John',
54+
'name': 'John Doe',
55+
'email': u'[email protected]',
56+
}
6157

6258
def test_locale(self):
6359
with override_language('fr'):
64-
self.assertEqual(text_type(StandardScopeClaims.info_profile[0]), 'Profil de base')
60+
assert str(StandardScopeClaims.info_profile[0]) == 'Profil de base'
6561

6662
def test_scopeclaims_class_inheritance(self):
6763
# Generate example class that will be used for `OIDC_EXTRA_SCOPE_CLAIMS` setting.

0 commit comments

Comments
 (0)