Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fix BaseResponse.uri_to_regexp #8193

Merged
merged 4 commits into from
Oct 6, 2024
Merged
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
24 changes: 12 additions & 12 deletions moto/core/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,35 +505,35 @@ def _dispatch(self, request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE:
@staticmethod
def uri_to_regexp(uri: str) -> str:
"""converts uri w/ placeholder to regexp
'/cars/{carName}/drivers/{DriverName}'
-> '^/cars/.*/drivers/[^/]*$'
'/accounts/{AwsAccountId}/namespaces/{Namespace}/groups'
-> '^/accounts/(?P<AwsAccountId>[^/]+)/namespaces/(?P<Namespace>[^/]+)/groups$'

'/cars/{carName}/drivers/{DriverName}/drive'
-> '^/cars/.*/drivers/.*/drive$'
'/trustStores/{trustStoreArn+}'
-> '^/trustStores/(?P<trustStoreArn>.+)$'

"""

def _convert(elem: str, is_last: bool) -> str:
def _convert(elem: str) -> str:
if not re.match("^{.*}$", elem):
# URL-parts sometimes contain a $
# Like Greengrass: /../deployments/$reset
# We don't want to our regex to think this marks an end-of-line, so let's escape it
return elem.replace("$", r"\$")

# When the element ends with +} the parameter can contain a / otherwise not.
slash_allowed = elem.endswith("+}")
name = (
elem.replace("{", "")
.replace("}", "")
.replace("+", "")
.replace("-", "_")
)
if is_last:
return f"(?P<{name}>[^/]+)"
return f"(?P<{name}>.*)"
if slash_allowed:
return f"(?P<{name}>.+)"
return f"(?P<{name}>[^/]+)"

elems = uri.split("/")
num_elems = len(elems)
regexp = "/".join(
[_convert(elem, (i == num_elems - 1)) for i, elem in enumerate(elems)]
)
regexp = "/".join([_convert(elem) for elem in elems])
return f"^{regexp}$"

def _get_action_from_method_and_request_uri(
Expand Down
Loading