Skip to content

Commit

Permalink
require event IDs to match ^[\w_-]+$
Browse files Browse the repository at this point in the history
Before this change, event IDs could be totally arbitrary strings.
That could make for ugly URLs. Plus, for the work I'm doing now
to support file attachments, I need to be able to use the event ID
as part of a local filesystem path. That means excluding invalid
characters like "/" or "$" or "%". Spaces are nasty too.

Since we've only ever made event IDs that match this pattern anyway,
I figure we should just enforce it as a rule.

#7
  • Loading branch information
srabraham committed Feb 26, 2025
1 parent 9b41ee0 commit 225bbb8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/ims/model/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
from datetime import datetime as DateTime
from datetime import timedelta as TimeDelta
from datetime import timezone as TimeZone
from string import ascii_letters
from typing import Any, cast

from hypothesis.strategies import (
SearchStrategy,
booleans,
characters,
composite,
dictionaries,
emails,
Expand Down Expand Up @@ -248,7 +250,23 @@ def events(draw: Callable[..., Any]) -> Event:
"""
Strategy that generates :class:`Event` values.
"""
return Event(id=draw(text(min_size=1)))
# The Event ID can consist of characters that match
# \w_-

allow_letters = [*list(ascii_letters), "-", "_"]
return Event(
id=draw(
text(
min_size=1,
alphabet=characters(
# Include the digits 0-9 too
min_codepoint=0x30,
max_codepoint=0x39,
include_characters=allow_letters,
),
)
)
)


@composite
Expand Down
9 changes: 9 additions & 0 deletions src/ims/store/_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from datetime import datetime as DateTime
from json import loads
from pathlib import Path
from re import search as reSearch
from textwrap import dedent
from types import MappingProxyType
from typing import Any, ClassVar, NoReturn, TypeVar, cast
Expand Down Expand Up @@ -366,6 +367,14 @@ async def createEvent(self, event: Event) -> None:
"""
See :meth:`IMSDataStore.createEvent`.
"""
# Require basic cleanliness for EventID, since it's used in IMS URLs
# and in filesystem directory paths.
eventIdPattern = r"^[\w-]+$"
if not reSearch(eventIdPattern, event.id):
raise ValueError(
f"wanted EventID to match '{eventIdPattern}', got '{event.id}'"
)

await self.runOperation(self.query.createEvent, {"eventID": event.id})

self._log.info(
Expand Down
2 changes: 1 addition & 1 deletion src/ims/store/test/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def test_createEvent(self) -> None:
"""
:meth:`IMSDataStore.createEvent` creates the given event.
"""
for eventName in ("Foo", "Foo Bar"):
for eventName in ("Foo", "Foo-Bar"):
event = Event(id=eventName)

store = await self.store()
Expand Down
4 changes: 2 additions & 2 deletions src/ims/store/test/street.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def test_concentricStreets(self) -> None:
"""
for event, streetID, streetName in (
(Event(id="Foo"), "A", "Alpha"),
(Event(id="Foo Bar"), "B", "Bravo"),
(Event(id="Foo-Bar"), "B", "Bravo"),
(Event(id="XYZZY"), "C", "Charlie"),
):
store = await self.store()
Expand All @@ -61,7 +61,7 @@ async def test_createConcentricStreet(self) -> None:
"""
for event, streetID, streetName in (
(Event(id="Foo"), "A", "Alpha"),
(Event(id="Foo Bar"), "B", "Bravo"),
(Event(id="Foo-Bar"), "B", "Bravo"),
(Event(id="XYZZY"), "C", "Charlie"),
):
store = await self.store()
Expand Down

0 comments on commit 225bbb8

Please sign in to comment.