Skip to content

Commit

Permalink
Update AddSmsForm to remove any invisible unicode characers
Browse files Browse the repository at this point in the history
  • Loading branch information
cuu508 committed Oct 20, 2020
1 parent 7534f18 commit a37e83a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
## Improvements
- Add a tooltip to the 'confirmation link' label (#436)
- Update API to allow specifying channels by names (#440)
- When saving a phone number, remove any invisible unicode characers

## v1.17.0 - 2020-10-14

Expand Down
18 changes: 11 additions & 7 deletions hc/front/forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import timedelta as td
import json
import re
from urllib.parse import quote, urlencode

from django import forms
from django.forms import URLField
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from hc.front.validators import (
CronExpressionValidator,
TimezoneValidator,
Expand Down Expand Up @@ -193,18 +193,22 @@ def get_value(self):
return json.dumps(dict(self.cleaned_data), sort_keys=True)


phone_validator = RegexValidator(
regex="^\+\d{5,15}$", message="Invalid phone number format."
)


class AddSmsForm(forms.Form):
error_css_class = "has-error"
label = forms.CharField(max_length=100, required=False)
value = forms.CharField(max_length=16, validators=[phone_validator])
value = forms.CharField()
down = forms.BooleanField(required=False, initial=True)
up = forms.BooleanField(required=False, initial=True)

def clean_value(self):
v = self.cleaned_data["value"]

stripped = v.encode("ascii", "ignore").decode("ascii")
if not re.match(r"^\+\d{5,15}$", stripped):
raise forms.ValidationError("Invalid phone number format.")

return stripped


class ChannelNameForm(forms.Form):
name = forms.CharField(max_length=100, required=False)
Expand Down
20 changes: 15 additions & 5 deletions hc/front/tests/test_add_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def test_it_creates_channel(self):
self.assertEqual(c.project, self.project)

def test_it_rejects_bad_number(self):
form = {"value": "not a phone number address"}

self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Invalid phone number format.")
for v in ["not a phone number address", False, 15, "+123456789A"]:
form = {"value": v}
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Invalid phone number format.")

def test_it_trims_whitespace(self):
form = {"value": " +1234567890 "}
Expand All @@ -65,3 +65,13 @@ def test_it_requires_rw_access(self):
self.client.login(username="bob@example.org", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)

def test_it_strips_invisible_formatting_characters(self):
form = {"label": "My Phone", "value": "\u202c+1234567890\u202c"}

self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)

c = Channel.objects.get()
self.assertEqual(c.phone_number, "+1234567890")

0 comments on commit a37e83a

Please sign in to comment.