mirror of
https://github.com/quantum5/django-csp-advanced.git
synced 2025-04-24 19:31:56 -04:00
Add python3 support.
This commit is contained in:
parent
854af23268
commit
591ea6f2fd
|
@ -1,7 +1,10 @@
|
||||||
language: python
|
language: python
|
||||||
python:
|
python:
|
||||||
- "2.7"
|
- "2.7"
|
||||||
|
- "3.5"
|
||||||
|
- "3.6"
|
||||||
- pypy
|
- pypy
|
||||||
|
- pypy3
|
||||||
env:
|
env:
|
||||||
- DJANGO_VERSION=">=1.8,<1.9"
|
- DJANGO_VERSION=">=1.8,<1.9"
|
||||||
- DJANGO_VERSION=">=1.9,<1.10"
|
- DJANGO_VERSION=">=1.9,<1.10"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
|
from django.utils import six
|
||||||
|
|
||||||
|
|
||||||
class InvalidCSPError(ValueError):
|
class InvalidCSPError(ValueError):
|
||||||
pass
|
pass
|
||||||
|
@ -74,7 +76,7 @@ class CSPCompiler(object):
|
||||||
|
|
||||||
def compile(self):
|
def compile(self):
|
||||||
pieces = []
|
pieces = []
|
||||||
for name, value in self.csp.iteritems():
|
for name, value in six.iteritems(self.csp):
|
||||||
if name in self.CSP_LISTS:
|
if name in self.CSP_LISTS:
|
||||||
if value:
|
if value:
|
||||||
pieces.append(self.compile_list(name, value))
|
pieces.append(self.compile_list(name, value))
|
||||||
|
@ -126,5 +128,5 @@ class CSPCompiler(object):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def ensure_str(name, value):
|
def ensure_str(name, value):
|
||||||
if not isinstance(value, basestring):
|
if not isinstance(value, six.string_types):
|
||||||
raise InvalidCSPError('Values for %s must be a string type, not %s', (name, type(value)))
|
raise InvalidCSPError('Values for %s must be a string type, not %s', (name, type(value)))
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import MiddlewareNotUsed
|
from django.core.exceptions import MiddlewareNotUsed
|
||||||
|
from django.utils import six
|
||||||
|
|
||||||
from csp_advanced.csp import CSPCompiler, InvalidCSPError
|
from csp_advanced.csp import CSPCompiler, InvalidCSPError
|
||||||
from csp_advanced.utils import is_callable_csp_dict, call_csp_dict, merge_csp_dict
|
from csp_advanced.utils import is_callable_csp_dict, call_csp_dict, merge_csp_dict
|
||||||
|
@ -12,11 +13,11 @@ class AdvancedCSPMiddleware(object):
|
||||||
def __init__(self, get_response=None):
|
def __init__(self, get_response=None):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
self.enforced_csp = getattr(settings, 'ADVANCED_CSP', None) or {}
|
self.enforced_csp = getattr(settings, 'ADVANCED_CSP', None) or {}
|
||||||
self.enforced_csp_is_str = isinstance(self.enforced_csp, basestring)
|
self.enforced_csp_is_str = isinstance(self.enforced_csp, six.string_types)
|
||||||
self.enforced_csp_callable = is_callable_csp_dict(self.enforced_csp)
|
self.enforced_csp_callable = is_callable_csp_dict(self.enforced_csp)
|
||||||
self.report_csp = getattr(settings, 'ADVANCED_CSP_REPORT_ONLY', None) or {}
|
self.report_csp = getattr(settings, 'ADVANCED_CSP_REPORT_ONLY', None) or {}
|
||||||
self.report_csp_callable = is_callable_csp_dict(self.report_csp)
|
self.report_csp_callable = is_callable_csp_dict(self.report_csp)
|
||||||
self.report_csp_is_str = isinstance(self.enforced_csp, basestring)
|
self.report_csp_is_str = isinstance(self.enforced_csp, six.string_types)
|
||||||
self.report_only_csp = not self.enforced_csp
|
self.report_only_csp = not self.enforced_csp
|
||||||
|
|
||||||
if not self.enforced_csp and not self.report_csp:
|
if not self.enforced_csp and not self.report_csp:
|
||||||
|
|
|
@ -5,9 +5,9 @@ from django.http import HttpResponse
|
||||||
from django.test import SimpleTestCase, RequestFactory, override_settings
|
from django.test import SimpleTestCase, RequestFactory, override_settings
|
||||||
from django.utils.decorators import decorator_from_middleware_with_args
|
from django.utils.decorators import decorator_from_middleware_with_args
|
||||||
|
|
||||||
from csp import CSPCompiler, InvalidCSPError
|
from csp_advanced.csp import CSPCompiler, InvalidCSPError
|
||||||
from csp_advanced.middleware import AdvancedCSPMiddleware
|
from csp_advanced.middleware import AdvancedCSPMiddleware
|
||||||
from utils import call_csp_dict, merge_csp_dict, is_callable_csp_dict
|
from csp_advanced.utils import call_csp_dict, merge_csp_dict, is_callable_csp_dict
|
||||||
|
|
||||||
|
|
||||||
class CSPCompileTest(SimpleTestCase):
|
class CSPCompileTest(SimpleTestCase):
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
|
from django.utils import six
|
||||||
|
|
||||||
|
|
||||||
def is_callable_csp_dict(data):
|
def is_callable_csp_dict(data):
|
||||||
if callable(data):
|
if callable(data):
|
||||||
return True
|
return True
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
return False
|
return False
|
||||||
return any(callable(value) for value in data.itervalues())
|
return any(callable(value) for value in six.itervalues(data))
|
||||||
|
|
||||||
|
|
||||||
def call_csp_dict(data, request, response):
|
def call_csp_dict(data, request, response):
|
||||||
|
@ -11,7 +14,7 @@ def call_csp_dict(data, request, response):
|
||||||
return data(request, response)
|
return data(request, response)
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
for key, value in data.iteritems():
|
for key, value in six.iteritems(data):
|
||||||
if callable(value):
|
if callable(value):
|
||||||
result[key] = value(request, response)
|
result[key] = value(request, response)
|
||||||
else:
|
else:
|
||||||
|
@ -21,7 +24,7 @@ def call_csp_dict(data, request, response):
|
||||||
|
|
||||||
def merge_csp_dict(template, override):
|
def merge_csp_dict(template, override):
|
||||||
result = template.copy()
|
result = template.copy()
|
||||||
for key, value in override.iteritems():
|
for key, value in six.iteritems(override):
|
||||||
if key not in result:
|
if key not in result:
|
||||||
result[key] = value
|
result[key] = value
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in a new issue