-
Notifications
You must be signed in to change notification settings - Fork 448
fix(security): sanitize REST path parameters to prevent injection attacks #1115
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
Open
Linux2010
wants to merge
3
commits into
a2aproject:main
Choose a base branch
from
Linux2010:fix/805-rest-path-id-sanitization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Input sanitization utilities for A2A path parameters. | ||
|
|
||
| This module provides validation functions for resource IDs used in | ||
| REST URL paths, preventing injection of control characters, path | ||
| traversal sequences, and other unsafe inputs. | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
| from a2a.utils.errors import InvalidRequestError | ||
|
|
||
|
|
||
| # Allowed characters for path resource IDs: alphanumeric, hyphen, | ||
| # underscore, and period. This matches the character set typically | ||
| # used for UUIDs, task IDs, and push-notification config IDs. | ||
| _PATH_ID_PATTERN = re.compile(r'^[A-Za-z0-9._-]+$') | ||
|
|
||
| # ASCII control character boundaries. | ||
| _MAX_PRINTABLE_ASCII = 0x20 # First non-control character (space) | ||
| _DEL_ASCII = 0x7F # DEL control character | ||
|
|
||
|
|
||
| def sanitize_path_id(value: str, param_name: str = 'id') -> str: | ||
| """Validate and sanitize a path parameter used as a resource ID. | ||
|
|
||
| Rejects values containing null bytes, newlines, other control | ||
| characters, or any characters outside the safe set | ||
| ``[A-Za-z0-9._-]``. | ||
|
|
||
| Args: | ||
| value: The raw path parameter value. | ||
| param_name: Name of the parameter (for error messages). | ||
|
|
||
| Returns: | ||
| The validated value unchanged. | ||
|
|
||
| Raises: | ||
| InvalidRequestError: If the value contains disallowed characters | ||
| or is empty. | ||
| """ | ||
| if not value: | ||
| raise InvalidRequestError( | ||
| message=f'{param_name} must not be empty', | ||
| ) | ||
| # Reject bare dot and double-dot to prevent path traversal. | ||
| if value in ('.', '..'): | ||
| raise InvalidRequestError( | ||
| message=f'{param_name} cannot be "." or ".."', | ||
| ) | ||
| # Reject null bytes and other control characters (0x00-0x1F, 0x7F). | ||
| if any( | ||
| ord(c) < _MAX_PRINTABLE_ASCII or ord(c) == _DEL_ASCII for c in value | ||
| ): | ||
| raise InvalidRequestError( | ||
| message=f'{param_name} contains control characters', | ||
| ) | ||
| if not _PATH_ID_PATTERN.match(value): | ||
| raise InvalidRequestError( | ||
| message=f'{param_name} contains invalid characters: {value!r}', | ||
| ) | ||
| return value | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current sanitization logic allows single dot (
.) and double dot (..) as valid path IDs because they match the_PATH_ID_PATTERNregex (^[A-Za-z0-9._-]+$). While forward slashes are rejected, allowing.and..can still lead to path traversal or routing bypass/normalization issues when interpolated into URL paths (e.g.,/tasks/..or/tasks/../pushNotificationConfigs).We should explicitly reject
.and..to prevent these security risks.References