From 96b708eb63b8d81dc1684f278d9253ae00ab70fc Mon Sep 17 00:00:00 2001 From: Mayank Singh Date: Sat, 5 Dec 2026 10:27:29 +0530 Subject: [PATCH] fix(event_handler): clear AppSyncEventsResolver context on exception --- .../events_appsync/appsync_events.py | 17 +++-- .../test_appsync_events_unauthorized.py | 74 +++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/aws_lambda_powertools/event_handler/events_appsync/appsync_events.py b/aws_lambda_powertools/event_handler/events_appsync/appsync_events.py index 4cc81504680..ce49a0844b4 100644 --- a/aws_lambda_powertools/event_handler/events_appsync/appsync_events.py +++ b/aws_lambda_powertools/event_handler/events_appsync/appsync_events.py @@ -127,14 +127,15 @@ def resolve( >>> return events.resolve(event, context) """ - self._setup_context(event, context) - - if self.current_event.info.operation == "PUBLISH": - response = self._publish_events(payload=self.current_event.events) - else: - response = self._subscribe_events() - - self.clear_context() + try: + self._setup_context(event, context) + + if self.current_event.info.operation == "PUBLISH": + response = self._publish_events(payload=self.current_event.events) + else: + response = self._subscribe_events() + finally: + self.clear_context() return response diff --git a/tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_unauthorized.py b/tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_unauthorized.py index 9fcc0512aa3..688ed20afdf 100644 --- a/tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_unauthorized.py +++ b/tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_unauthorized.py @@ -1,3 +1,5 @@ +import copy + import pytest from aws_lambda_powertools.event_handler import AppSyncEventsResolver @@ -100,3 +102,75 @@ def handle_events(payload): # THEN the exception must propagate to the caller with pytest.raises(UnauthorizedException): app.resolve(mock_event, lambda_context) + + +def test_publish_clears_context_when_unauthorized_raised(lambda_context, mock_event): + # GIVEN an AppSyncEventsResolver that sets context on one event and denies the next + publish_event = copy.deepcopy(mock_event) + publish_event["info"]["operation"] = "PUBLISH" + publish_event["info"]["channel"]["path"] = "/default/test" + publish_event["events"] = [ + {"id": "123", "payload": {"data": "test data"}}, + {"id": "456", "payload": {"data": "more test data"}}, + ] + + app = AppSyncEventsResolver() + calls = [] + + @app.on_publish(path="/default/*") + def handle_events(payload): + calls.append(payload) + if len(calls) == 1: + app.append_context(user_id="12345") + return {"processed": True} + if len(calls) == 2: + raise UnauthorizedException + return dict(app.context) + + # WHEN the failing batch invocation runs + with pytest.raises(UnauthorizedException): + app.resolve(publish_event, lambda_context) + + # THEN context is cleared and not leaked into the next invocation + assert app.context == {} + + next_event = copy.deepcopy(mock_event) + next_event["info"]["operation"] = "PUBLISH" + next_event["info"]["channel"]["path"] = "/default/test" + next_event["events"] = [{"id": "789", "payload": {"data": "next invocation"}}] + next_result = app.resolve(next_event, lambda_context) + assert next_result == {"events": [{"id": "789", "payload": {}}]} + + +def test_subscribe_clears_context_when_unauthorized_raised(lambda_context, mock_event): + # GIVEN a subscribe event where the handler appends context before denying access + subscribe_event = copy.deepcopy(mock_event) + subscribe_event["info"]["operation"] = "SUBSCRIBE" + subscribe_event["info"]["channel"]["path"] = "/default/test" + + app = AppSyncEventsResolver() + + @app.on_subscribe(path="/default/*") + def handle_subscribe(): + app.append_context(user_id="12345") + raise UnauthorizedException + + # WHEN resolving raises UnauthorizedException + with pytest.raises(UnauthorizedException): + app.resolve(subscribe_event, lambda_context) + + # THEN context must still be cleared + assert app.context == {} + + +def test_resolve_clears_context_when_event_setup_raises(lambda_context): + # GIVEN an AppSyncEventsResolver with pre-populated context + app = AppSyncEventsResolver() + app.append_context(user_id="12345") + + # WHEN resolving an invalid event fails during event setup / inspection + with pytest.raises((AttributeError, KeyError, TypeError, ValueError)): + app.resolve({}, lambda_context) + + # THEN pre-populated context must still be cleared + assert app.context == {}