Skip to content

Commit 24fb7eb

Browse files
Merge pull request #1218 from NASA-IMPACT/1217-add-data-validation-to-the-feedback-form-api-to-restrict-html-content
HTML validator has been set at serializer level
2 parents cf09271 + 395119c commit 24fb7eb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ For each PR made, an entry should be added to this changelog. It should contain
1212
- etc.
1313

1414
## Changelog
15+
16+
- 1217-add-data-validation-to-the-feedback-form-api-to-restrict-html-content
17+
- Description: The feedback form API does not currently have any form of data validation on the backend which makes it easy for the user with the endpoint to send in data with html tags. We need to have a validation scheme on the backend to protect this from happening.
18+
- Changes:
19+
- Defined a class `HTMLFreeCharField` which inherits `serializers.CharField`
20+
- Used regex to catch any HTML content comming in as an input to form fields
21+
- Called this class within the serializer for necessary fields

feedback/serializers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
import re
2+
13
from rest_framework import serializers
24

35
from .models import ContentCurationRequest, Feedback
46

57

8+
class HTMLFreeCharField(serializers.CharField):
9+
def to_internal_value(self, data):
10+
value = super().to_internal_value(data)
11+
12+
if re.search(r"<[^>]+>", value):
13+
raise serializers.ValidationError("HTML tags are not allowed in this field")
14+
15+
return value
16+
17+
618
class FeedbackSerializer(serializers.ModelSerializer):
19+
20+
name = HTMLFreeCharField()
21+
subject = HTMLFreeCharField()
22+
comments = HTMLFreeCharField()
23+
source = HTMLFreeCharField()
24+
725
class Meta:
826
model = Feedback
927
fields = [

0 commit comments

Comments
 (0)