From 83abf8a770521da6ef8dd8db112f12ccac3f1730 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 29 May 2021 16:45:54 +0200 Subject: [PATCH] Implement regex check for short code --- shortener/handlers.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/shortener/handlers.py b/shortener/handlers.py index 64b047a..70fa2ab 100644 --- a/shortener/handlers.py +++ b/shortener/handlers.py @@ -1,4 +1,5 @@ import os +import re from typing import Dict from urllib.parse import urlparse @@ -53,6 +54,12 @@ def _validate_user_input(): raise BadRequestException( message="`short_code` must be longer than 4 characters." ) + reg = re.compile(r"^[a-zA-Z0-9_-]+$") + if not reg.match(short_code): + raise BadRequestException( + message="`short_code` must only contain non accentuated letters, " + "digits, dashes (-) and underscores (_)." + ) if _short_code_exists(short_code): raise BadRequestException(message=f"`{short_code}` is already taken")