Skip to content

Commit fb9a2f8

Browse files
authored
#681 validate name_format query parameter with regexp to prevent mali… (#682)
* #681 validate name_format query parameter with regexp to prevent malicious code injection * #681 typo corrected and ASCII quotes replaced with Unicode glyphs
1 parent f17b845 commit fb9a2f8

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

gramps_webapi/api/resources/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from gramps_webapi.types import ResponseReturnValue
3838

3939
from ...auth.const import PERM_ADD_OBJ, PERM_DEL_OBJ, PERM_EDIT_OBJ
40-
from ...const import GRAMPS_OBJECT_PLURAL
40+
from ...const import GRAMPS_OBJECT_PLURAL, NAME_FORMAT_REGEXP
4141
from ..auth import require_permissions
4242
from ..cache import request_cache_decorator
4343
from ..search import SearchIndexer, get_search_indexer
@@ -210,7 +210,7 @@ class GrampsObjectResource(GrampsObjectResourceHelper, Resource):
210210
"locale": fields.Str(
211211
load_default=None, validate=validate.Length(min=1, max=5)
212212
),
213-
"name_format": fields.Str(validate=validate.Length(min=1)),
213+
"name_format": fields.Str(validate=validate.Regexp(NAME_FORMAT_REGEXP)),
214214
"profile": fields.DelimitedList(
215215
fields.Str(validate=validate.Length(min=1)),
216216
validate=validate.ContainsOnly(
@@ -377,7 +377,7 @@ class GrampsObjectsResource(GrampsObjectResourceHelper, Resource):
377377
"soundex": fields.Boolean(load_default=False),
378378
"strip": fields.Boolean(load_default=False),
379379
"filemissing": fields.Boolean(load_default=False),
380-
"name_format": fields.Str(validate=validate.Length(min=1)),
380+
"name_format": fields.Str(validate=validate.Regexp(NAME_FORMAT_REGEXP)),
381381
},
382382
location="query",
383383
)

gramps_webapi/api/resources/timeline.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from . import ProtectedResource
4444
from .emit import GrampsJSONEncoder
4545
from .filters import apply_filter
46+
from ...const import NAME_FORMAT_REGEXP
4647
from .util import (
4748
get_person_profile_for_object,
4849
get_place_profile_for_object,
@@ -540,7 +541,7 @@ class PersonTimelineResource(ProtectedResource, GrampsJSONEncoder):
540541
"keys": fields.DelimitedList(fields.Str(validate=validate.Length(min=1))),
541542
"last": fields.Boolean(load_default=True),
542543
"locale": fields.Str(load_default=None),
543-
"name_format": fields.Str(validate=validate.Length(min=1)),
544+
"name_format": fields.Str(validate=validate.Regexp(NAME_FORMAT_REGEXP)),
544545
"offspring": fields.Integer(
545546
load_default=1, validate=validate.Range(min=1, max=5)
546547
),
@@ -633,7 +634,7 @@ class FamilyTimelineResource(ProtectedResource, GrampsJSONEncoder):
633634
"events": fields.DelimitedList(fields.Str(validate=validate.Length(min=1))),
634635
"keys": fields.DelimitedList(fields.Str(validate=validate.Length(min=1))),
635636
"locale": fields.Str(load_default=None),
636-
"name_format": fields.Str(validate=validate.Length(min=1)),
637+
"name_format": fields.Str(validate=validate.Regexp(NAME_FORMAT_REGEXP)),
637638
"page": fields.Integer(load_default=0, validate=validate.Range(min=1)),
638639
"pagesize": fields.Integer(load_default=20, validate=validate.Range(min=1)),
639640
"ratings": fields.Boolean(load_default=False),

gramps_webapi/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,6 @@
200200
TELEMETRY_ENDPOINT = "https://telemetry-cloud-run-442080026669.europe-west1.run.app"
201201
TELEMETRY_TIMESTAMP_KEY = "telemetry_last_sent"
202202
TELEMETRY_SERVER_ID_KEY = "telemetry_server_uuid"
203+
204+
# Regular expression for allowed values of the `name_format` query parameter.
205+
NAME_FORMAT_REGEXP = r"^(%[%tTfFlLcCxXiImMyYoOrRpPqQsSnNgG]|%[0-2][mMyY]|[ \u0022\u0027,.:;\]\[\(\)\{\}\&\@])*$"

tests/test_endpoints/test_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ def test_get_events_handle_parameter_profile_expected_result_with_name_format(se
782782
"""Test response as expected."""
783783
rv = check_success(
784784
self,
785-
TEST_URL + "a5af0eb6dd140de132c?profile=all&name_format=Given%20SURNAME",
785+
TEST_URL + "a5af0eb6dd140de132c?profile=all&name_format=%25f%20%25M",
786786
)
787787
self.assertEqual(
788788
rv["profile"],

tests/test_endpoints/test_people.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ def test_get_people_handle_parameter_name_format_expected_result_name_display(se
12901290
rv = check_success(
12911291
self,
12921292
TEST_URL
1293-
+ "0PWJQCZYFXOS0HGREE?profile=all&name_format=Given%20%28Common%29%20SURNAME",
1293+
+ "0PWJQCZYFXOS0HGREE?profile=all&name_format=%25f%20%28%25x%29%20%25M",
12941294
)
12951295
self.assertEqual(
12961296
rv["profile"]["name_display"], "Mary Grace Elizabeth (Mary) WARNER"

0 commit comments

Comments
 (0)