Skip to content

Commit 3277ca5

Browse files
committed
Fix username validation in rws.
1 parent 1914a5a commit 3277ca5

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎cmsranking/RankingWebServer.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def authorized(self, request: Request):
131131

132132
def get(self, request: Request, response: Response, key: str):
133133
# Limit charset of keys.
134-
if re.match("^[A-Za-z0-9_]+$", key) is None:
134+
if re.fullmatch("[A-Za-z0-9_]+", key) is None:
135135
return NotFound()
136136
if key not in self.store:
137137
raise NotFound()
@@ -149,7 +149,7 @@ def get_list(self, request: Request, response: Response):
149149

150150
def put(self, request: Request, response: Response, key: str):
151151
# Limit charset of keys.
152-
if re.match("^[A-Za-z0-9_]+$", key) is None:
152+
if re.fullmatch("[A-Za-z0-9_]+", key) is None:
153153
return Forbidden()
154154
if not self.authorized(request):
155155
logger.warning("Unauthorized request.",
@@ -213,7 +213,7 @@ def put_list(self, request: Request, response: Response):
213213

214214
def delete(self, request: Request, response: Response, key: str):
215215
# Limit charset of keys.
216-
if re.match("^[A-Za-z0-9_]+$", key) is None:
216+
if re.fullmatch("[A-Za-z0-9_]+", key) is None:
217217
return NotFound()
218218
if key not in self.store:
219219
raise NotFound()

‎cmsranking/Store.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def load_from_disk(self):
8989

9090
try:
9191
for name in os.listdir(self._path):
92-
# TODO check that the key is '[A-Za-z0-9_]+'
93-
if name[-5:] == '.json' and name[:-5] != '':
92+
if name[-5:] == '.json' and re.fullmatch('[A-Za-z0-9_]+', name[:-5]):
9493
with open(os.path.join(self._path, name), 'rb') as rec:
9594
item = self._entity()
9695
item.set(json.load(rec))
@@ -148,7 +147,9 @@ def create(self, key: str, data: dict):
148147
some properties or if properties are of the wrong type.
149148
150149
"""
151-
if not isinstance(key, str) or key in self._store:
150+
if not isinstance(key, str) or not re.fullmatch('[A-Za-z0-9_]+', key):
151+
raise InvalidKey("Invalid key.")
152+
if key in self._store:
152153
raise InvalidKey("Key already in store.")
153154

154155
# create entity
@@ -232,7 +233,7 @@ def merge_list(self, data_dict: dict[str, dict]):
232233
try:
233234
# FIXME We should allow keys to be arbitrary unicode
234235
# strings, so this just needs to be a non-empty check.
235-
if not re.match('[A-Za-z0-9_]+', key):
236+
if not isinstance(key, str) or not re.fullmatch('[A-Za-z0-9_]+', key):
236237
raise InvalidData("Invalid key")
237238
item = self._entity()
238239
item.set(value)

0 commit comments

Comments
 (0)