Skip to content

Commit 8a002b9

Browse files
committed
Validate with a regex for valid chars
1 parent aab8f17 commit 8a002b9

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

readthedocs/projects/forms.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
unicode_literals,
1010
)
1111

12+
import re
1213
from random import choice
1314

1415
from builtins import object
@@ -800,4 +801,8 @@ def clean_name(self):
800801
raise forms.ValidationError(
801802
_("Variable name can't contain spaces"),
802803
)
804+
elif not re.fullmatch('[a-zA-Z0-9_]+', name):
805+
raise forms.ValidationError(
806+
_('Only letters, numbers and underscore are allowed'),
807+
)
803808
return name

readthedocs/rtd_tests/tests/test_project_forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,17 @@ def test_use_invalid_names(self):
509509
form.errors['name'],
510510
)
511511

512+
data = {
513+
'name': 'INVALID_CHAR*',
514+
'value': 'string here',
515+
}
516+
form = EnvironmentVariableForm(data, project=self.project)
517+
self.assertFalse(form.is_valid())
518+
self.assertIn(
519+
'Only letters, numbers and underscore are allowed',
520+
form.errors['name'],
521+
)
522+
512523
data = {
513524
'name': '__INVALID',
514525
'value': 'string here',

0 commit comments

Comments
 (0)