-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-pghf-347x-c2gj
* Fix CVE-2021-30459 by creating signature from all data fields. Create a signature based on all fields in the form and attach to validate that the data being sent back is what the server generated initially. Change the hashing algorithm to SHA256 Force the values to a string for signing. Remove hashing mechanism from forms. Support sha1 algorithm for django < 3.1 * Bump version to 3.2.1
- Loading branch information
1 parent
8b280e1
commit 38e1bd7
Showing
16 changed files
with
212 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
|
||
from django import forms | ||
from django.core import signing | ||
from django.core.exceptions import ValidationError | ||
from django.utils.encoding import force_str | ||
|
||
|
||
class SignedDataForm(forms.Form): | ||
"""Helper form that wraps a form to validate its contents on post. | ||
class PanelForm(forms.Form): | ||
# fields | ||
On render: | ||
form = SignedDataForm(initial=PanelForm(initial=data).initial) | ||
On POST: | ||
signed_form = SignedDataForm(request.POST) | ||
if signed_form.is_valid(): | ||
panel_form = PanelForm(signed_form.verified_data) | ||
if panel_form.is_valid(): | ||
# Success | ||
Or wrap the FBV with ``debug_toolbar.decorators.signed_data_view`` | ||
""" | ||
|
||
salt = "django_debug_toolbar" | ||
signed = forms.CharField(required=True, widget=forms.HiddenInput) | ||
|
||
def __init__(self, *args, **kwargs): | ||
initial = kwargs.pop("initial", None) | ||
if initial: | ||
initial = {"signed": self.sign(initial)} | ||
super().__init__(*args, initial=initial, **kwargs) | ||
|
||
def clean_signed(self): | ||
try: | ||
verified = json.loads( | ||
signing.Signer(salt=self.salt).unsign(self.cleaned_data["signed"]) | ||
) | ||
return verified | ||
except signing.BadSignature: | ||
raise ValidationError("Bad signature") | ||
|
||
def verified_data(self): | ||
return self.is_valid() and self.cleaned_data["signed"] | ||
|
||
@classmethod | ||
def sign(cls, data): | ||
items = sorted(data.items(), key=lambda item: item[0]) | ||
return signing.Signer(salt=cls.salt).sign( | ||
json.dumps({key: force_str(value) for key, value in items}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.