powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: AppSync Events wildcard route matches channels sharing a segment prefix

Open LemonCondor opened this issue 1 month ago • 4 comments

Expected Behaviour

With handlers on /default/* and /default/users/*, a publish to /default/usersettings should be handled by /default/*, because usersettings is not inside the users segment.

Current Behaviour

/default/usersettings is routed to the /default/users/* handler. Channels like /default/users-archive/1 are routed there too.

find_best_route (event_handler/events_appsync/functions.py) turns a trailing [^/]+ into .* with pattern[:-6] + ".*". "[^/]+" is only 5 characters, so the preceding / is cut as well. /default/users/* becomes ^/default/users.*$ instead of the ^/default/users/.*$ documented in the function's docstring. It then wins over /default/* as the more specific route.

Code snippet

from aws_lambda_powertools.event_handler import AppSyncEventsResolver
from aws_lambda_powertools.event_handler.events_appsync.functions import find_best_route

app = AppSyncEventsResolver()

@app.on_publish("/default/*")
def generic(payload):
    return {"handler": "generic"}

@app.on_publish("/default/users/*")
def users(payload):
    return {"handler": "users"}

print(find_best_route({"/default/*": 1, "/default/users/*": 1}, "/default/usersettings"))
# prints "/default/users/*", expected "/default/*"

Possible Solution

Strip only the wildcard: pattern[: -len("[^/]+")] + ".*". I have a fix with regression tests ready and can open a PR.

Steps to Reproduce

Run the snippet above. Publishing an event to /default/usersettings through app.resolve(...) returns {"handler": "users"}.

Powertools for AWS Lambda (Python) version

latest (develop, 3.35.0)

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

No response

LemonCondor avatar Oct 13 '26 09:10 LemonCondor

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

boring-cyborg[bot] avatar Oct 13 '26 09:10 boring-cyborg[bot]

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out our GitHub Discussions: Join the conversation

[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

This is now released under 3.37.0 version!

github-actions[bot] avatar Oct 22 '26 10:10 github-actions[bot]