Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions templates/agent/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver
from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent
from aws_lambda_powertools.utilities.typing import LambdaContext
from pydantic import ValidationError

from templates.agent.models import Item
from templates.agent.settings import Settings
from templates.models import Entity
from templates.repository import Repository

settings = Settings() # type: ignore
Expand All @@ -29,6 +31,11 @@ def get_item(item_id: str) -> dict:
The item details or an error message.
"""
logger.info("Retrieving item", extra={"itemId": item_id})
try:
Entity(id=item_id) # Validate ID format before querying repository
except ValidationError:
return {"error": "Invalid item ID format"}

try:
item = repository.get_item(item_id)
if not item:
Expand Down
6 changes: 6 additions & 0 deletions templates/graphql/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from templates.graphql.models import Item
from templates.graphql.settings import Settings
from templates.models import Entity
from templates.repository import Repository

settings = Settings() # type: ignore
Expand All @@ -29,6 +30,11 @@ def get_item(id: str) -> dict | None:
Returns:
The item if found, or None.
"""
try:
Entity(id=id) # Validate ID format before querying repository
except ValidationError:
raise RuntimeError("Invalid item ID format") from None

try:
if (item := repository.get_item(id)) is None:
return None
Expand Down
1 change: 1 addition & 0 deletions templates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ class Entity(Object):
default_factory=lambda: str(uuid4()),
min_length=1,
max_length=50,
pattern=r"^[a-zA-Z0-9-_]+$",
)