-
Notifications
You must be signed in to change notification settings - Fork 12
fix: validate path parameters and prevent traversal/injection in REST transcoder #67
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
quartzmo
wants to merge
8
commits into
googleapis:main
Choose a base branch
from
quartzmo:gapic-common-validation
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
8 commits
Select commit
Hold shift + click to select a range
75929fa
fix: validate path parameters and prevent traversal/injection in REST…
quartzmo e52d7fb
fix: simplify rest transcoder double-wildcard validation to fail alwa…
quartzmo 25e80c6
docs: clarify validation rules and empty segment handling in validate…
quartzmo 865c1cc
docs: use standard (*) and path (**) indicators in validate_field_bin…
quartzmo a8270fd
refactor: unify standard and path parameter validation methods in RES…
quartzmo 844b429
docs: document that bind_uri_values! raises Gapic::Common::Error
quartzmo d0210db
fix(rest): unescape path parameter values and format validation messages
quartzmo 37844ca
fix(rest): remove ? and # checks and rely on escaping
quartzmo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,19 +111,61 @@ def transcode request | |
| # @return [Hash{String, String}] | ||
| # Name to value hash of the variables for the uri template expansion. | ||
| # The values are percent-escaped with slashes potentially preserved. | ||
| # @raise [Gapic::Common::Error] If any parameter value fails path traversal or injection validation. | ||
| def bind_uri_values! http_binding, request_hash | ||
| http_binding.field_bindings.to_h do |field_binding| | ||
| field_path_camel = field_binding.field_path.split(".").map { |part| camel_name_for part }.join(".") | ||
| field_value = extract_scalar_value! request_hash, field_path_camel, field_binding.regex | ||
|
|
||
| if field_value | ||
| validate_field_binding! field_binding, field_value | ||
| field_value = field_value.split("/").map { |segment| percent_escape segment }.join("/") | ||
| end | ||
|
|
||
| [field_binding.field_path, field_value] | ||
| end | ||
| end | ||
|
|
||
| # Validates a user-supplied parameter value bound to a standard (*) or path (**) URI template variable | ||
| # to prevent directory traversal and parameter injection exploits. | ||
| # | ||
| # @param field_binding [HttpBinding::FieldBinding] The field binding template metadata. | ||
| # @param field_value [String] The parameter value to validate. | ||
| # @raise [Gapic::Common::Error] If validation fails. | ||
| def validate_field_binding! field_binding, field_value | ||
| validate_path_binding! field_binding, field_value | ||
| end | ||
|
|
||
| # Validates standard (*) and path (**) parameters by ensuring that no segment in the parameter | ||
| # value is a directory traversal segment (. or ..). | ||
| # | ||
| # Validation Mechanism: | ||
| # 1. URL-decodes the parameter value to ensure all encoded dot (`%2e` / `%2E`) | ||
| # and slash (`%2f` / `%2F`) segments are expanded. | ||
| # 2. Splits the decoded parameter value by slash (`/`) using `-1` limit to preserve all segments. | ||
| # 3. Checks each segment. If any segment matches `.` or `..`, it immediately raises | ||
| # a `Gapic::Common::Error`, aborting the request. | ||
| # 4. Empty segments (e.g. duplicate slashes `//` or trailing slashes `/`) are allowed | ||
| # by this linter and passed to the server, which handles normalization or returns 400. | ||
| # | ||
| # @param field_binding [HttpBinding::FieldBinding] The field binding template metadata. | ||
| # @param field_value [String] The parameter value to validate. | ||
| # @raise [Gapic::Common::Error] If validation fails. | ||
| def validate_path_binding! field_binding, field_value | ||
| unescaped_value = CGI.unescape field_value | ||
| segments = unescaped_value.split("/", -1) | ||
| segments.each do |segment| | ||
| next unless segment == "." || segment == ".." | ||
| if field_binding.preserve_slashes | ||
| raise ::Gapic::Common::Error, | ||
| "Value for #{field_binding.field_path} must not contain segments that are exactly . or .." | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: separate these by |
||
| else | ||
| raise ::Gapic::Common::Error, | ||
| "Invalid value #{segment} for #{field_binding.field_path}" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Percent-escapes a string. | ||
| # @param str [String] String to escape. | ||
| # @return [String] Escaped string. | ||
|
|
||
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.
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.
super nit: we could just call these methods independently on L121 and L122 instead of wrapping
validate_path_binding!.