Skip to content

Commit f259a77

Browse files
fix(event_handler): match AppSync Events wildcard routes on segment boundaries (#8546)
find_best_route() turned a trailing "/*" into ".*" by slicing six characters off "[^/]+", which also removed the preceding "/". A route such as "/default/users/*" was compiled to ^/default/users.*$ and matched "/default/usersettings", so that channel was dispatched to the more specific but wrong handler instead of "/default/*". Strip only the wildcard pattern so the separator is kept. Co-authored-by: Guillaume Marty <279286709+LemonCondor@users.noreply.github.com> Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent 699c16b commit f259a77

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎aws_lambda_powertools/event_handler/events_appsync/functions.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def pattern_to_regex(route):
7171
"""
7272
Convert a route pattern to a regex pattern with caching.
7373
Examples:
74-
/default/v1/* -> ^/default/v1/[^/]+$
74+
/default/v1/* -> ^/default/v1/.*$
7575
/default/v1/users/* -> ^/default/v1/users/.*$
7676
7777
Parameters
@@ -87,9 +87,11 @@ def pattern_to_regex(route):
8787
# Escape special regex chars but convert * to regex pattern
8888
pattern = re.escape(route).replace("\\*", "[^/]+")
8989

90-
# If pattern ends with [^/]+, replace with .* for multi-segment match
90+
# If pattern ends with [^/]+, replace with .* for multi-segment match.
91+
# Only the wildcard is replaced: the preceding "/" must be kept so that
92+
# "/default/users/*" does not match "/default/usersettings".
9193
if pattern.endswith("[^/]+"):
92-
pattern = pattern[:-6] + ".*"
94+
pattern = pattern[: -len("[^/]+")] + ".*"
9395

9496
# Compile and return the regex pattern
9597
return re.compile(f"^{pattern}$")

‎tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_resolvers.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,37 @@ def phones_handler(payload):
10241024
assert result == expected_result
10251025

10261026

1027+
def test_wildcard_route_does_not_match_sibling_segment_with_same_prefix(lambda_context, mock_event):
1028+
"""Test that a wildcard route only matches whole path segments."""
1029+
# GIVEN a publish event on a channel that only shares a string prefix with a more specific route
1030+
mock_event["info"]["channel"]["path"] = "/default/usersettings"
1031+
mock_event["events"] = [
1032+
{"id": "123", "payload": {"data": "settings data"}},
1033+
]
1034+
1035+
# GIVEN an AppSyncEventsResolver with a generic and a more specific route
1036+
app = AppSyncEventsResolver()
1037+
1038+
@app.on_publish(path="/default/*")
1039+
def default_handler(payload):
1040+
return {"handler": "default", "data": payload["data"]}
1041+
1042+
@app.on_publish(path="/default/users/*")
1043+
def users_handler(payload):
1044+
return {"handler": "users", "data": payload["data"]}
1045+
1046+
# WHEN we resolve the event
1047+
result = app.resolve(mock_event, lambda_context)
1048+
1049+
# THEN the generic route should be used, since /default/usersettings is not under /default/users/
1050+
expected_result = {
1051+
"events": [
1052+
{"id": "123", "payload": {"handler": "default", "data": "settings data"}},
1053+
],
1054+
}
1055+
assert result == expected_result
1056+
1057+
10271058
def test_integration_with_external_service(lambda_context, mock_event):
10281059
"""Test integration with an external service using mocks."""
10291060
# GIVEN a sample publish event
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from aws_lambda_powertools.event_handler.events_appsync.functions import find_best_route
4+
5+
6+
@pytest.mark.parametrize(
7+
"path, expected_route",
8+
[
9+
("/default/users/123", "/default/users/*"),
10+
("/default/users/123/profile", "/default/users/*"),
11+
("/default/usersettings", "/default/*"),
12+
("/default/users-archive/1", "/default/*"),
13+
("/other/channel", "/*"),
14+
],
15+
)
16+
def test_find_best_route_wildcard_matches_whole_segments(path, expected_route):
17+
# GIVEN routes where a more specific route shares a string prefix with sibling channels
18+
routes = {
19+
"/*": {"func": lambda x: x, "aggregate": False},
20+
"/default/*": {"func": lambda x: x, "aggregate": False},
21+
"/default/users/*": {"func": lambda x: x, "aggregate": False},
22+
}
23+
24+
# WHEN finding the best route
25+
result = find_best_route(routes, path)
26+
27+
# THEN the wildcard only matches whole path segments
28+
assert result == expected_route

0 commit comments

Comments
 (0)