Skip to content

Commit

Permalink
Remove polynomial regexps
Browse files Browse the repository at this point in the history
https://github.com/dimagi/commcare-hq/security/code-scanning/273
https://github.com/dimagi/commcare-hq/security/code-scanning/361
https://github.com/dimagi/commcare-hq/security/code-scanning/363

 isdigit() method of builtins.str instance
     Return True if the string is a digit string, False otherwise.

     A string is a digit string if all characters in the string are digits and there
     is at least one character in the string.

The commtrack one is well covered by tests, the others I verified
locally
  • Loading branch information
esoergel committed Dec 6, 2024
1 parent 0124e52 commit 06d326a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions corehq/apps/commtrack/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ def encode_if_needed(val):


def _fetch_ending_numbers(s):
matcher = re.compile(r"\d*$")
return matcher.search(s).group()
postfix = ''
for char in s[::-1]:
if not char.isdigit():
break
postfix = char + postfix
return postfix


def generate_code(object_name, existing_codes):
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/registration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def clean_phone_number(self):
phone_number = re.sub(r'\s|\+|\-', '', phone_number)
if phone_number == '':
return None
elif not re.match(r'\d+$', phone_number):
elif not phone_number.isdigit():
raise forms.ValidationError(gettext(
"%s is an invalid phone number." % phone_number
))
Expand Down
2 changes: 1 addition & 1 deletion corehq/apps/users/views/mobile/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def post(self, request, *args, **kwargs):
if self.request.POST['form_type'] == "add-phonenumber":
phone_number = self.request.POST['phone_number']
phone_number = re.sub(r'\s', '', phone_number)
if re.match(r'\d+$', phone_number):
if phone_number.isdigit():
is_new_phone_number = phone_number not in self.editable_user.phone_numbers
self.editable_user.add_phone_number(phone_number)
self.editable_user.save(spawn_task=True)
Expand Down

0 comments on commit 06d326a

Please sign in to comment.