security: fix path traversal and query injection in resource names - #96
Open
tamirse wants to merge 5 commits into
Open
security: fix path traversal and query injection in resource names#96tamirse wants to merge 5 commits into
tamirse wants to merge 5 commits into
Conversation
Caller-supplied resource names and IDs are no longer interpolated directly into request paths. This prevents malicious input containing `../` from retargeting API calls, and `?` or `#` from injecting query parameters or fragments. Values are now percent-encoded as single path segments, and `.` `..` or empty values are rejected. The HTTP client also includes a backstop to refuse requests that attempt to escape the API base path.
Expands the path traversal protection to cover additional service methods, including `instances.is_available()` and `clusters.is_available()`, and various container and job deployment operations. Enhances path parameter validation to reject unused keys, preventing silent errors from misspelled or stale parameters. Non-string path values are now gracefully coerced to strings for backward compatibility. Reorders token refresh to occur after path validation, preventing unnecessary network calls when path parameters are invalid.
Strengthens path traversal protection by explicitly rejecting resource names or IDs that contain relative path segments (`.` or `..`), are empty, or `None`. Previously, such values were merely percent-encoded, which is insufficient as `requests` decodes unreserved characters or intermediaries unescape encoded slashes, re-introducing vulnerabilities. This also expands protection to `InferenceClient` paths, ensuring they cannot escape their deployment's base URL. All HTTP verb methods now delegate to a central `_request` method, standardizing path validation and ensuring comprehensive coverage. This introduces two breaking changes: - Resource names or IDs containing a relative path segment, an empty value, or `None` now raise `ValueError`. - Resource names and IDs are always percent-encoded by the client; pre-encoded values will be double-encoded. Callers should pass raw names.
Moves path traversal detection to `verda.helpers.has_relative_path_segment` to unify checks across `HTTPClient` and `InferenceClient`, ensuring consistent protection against `.` and `..` segments, even when percent-encoded. Expands `_encode_path_segment` to explicitly reject path parameters that are not `str`, `int`, or `UUID`, preventing ambiguous coercion (e.g., `bytes` to `"b'abc'"`) that would result in confusing 404s. Hardens `_add_base_url` to reject paths containing query strings or fragments, preventing URL component injection. Adds a dynamic test to ensure all service methods accepting path parameters are covered by traversal tests. **Breaking changes:** - A path value that is not `str`, `int`, or `UUID` now raises `ValueError`. - A `/` within a resource name is now consistently encoded as `%2F`, ensuring it remains a single path segment. Servers that reject or refuse to decode encoded slashes may break.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Caller-supplied resource names and IDs are no longer interpolated directly into request paths. This prevents malicious input containing
../from retargeting API calls, and?or#from injecting query parameters or fragments.Values are now percent-encoded as single path segments, and
...or empty values are rejected. The HTTP client also includes a backstop to refuse requests that attempt to escape the API base path.