feat(core): implement path validation and traversal checks in HttpCommand - #27476
feat(core): implement path validation and traversal checks in HttpCommand#27476quartzmo wants to merge 8 commits into
Conversation
…mand Implement dynamic runtime template parsing and validation inside HttpCommand to protect Discovery REST clients from path traversal and parameter injection exploits. Specifically, this change: - Rejects query (?) and fragment (#) characters in path parameter values. - Rejects slashes (/) and dots (. or ..) in simple path variables (standard wildcards). - Validates path traversals in reserved path variables (+ or #, double wildcards) using a segment-boundary traversal validation algorithm. In addition to the validation logic, this commit includes: - Ruby style modernization: cleaned up and auto-corrected string quote style to favor single quotes where appropriate. - Formatted long stub requests to follow style guidelines. Note: If the canonical specification is updated to favor the simpler alternative, this implementation can be easily simplified to "Fail always if dots are found in a double-wildcard value" by replacing the segment-traversal logic with a simple check for any "." or ".." substring.
66dff5d to
c84404d
Compare
torreypayne
left a comment
There was a problem hiding this comment.
This looks awesome! The only thing I'm wondering is if there are any test cases we could extract from the original vulnerability ask to ensure we have coverage. Otherwise LGTM!
| self.url = Addressable::Template.new(url) if url.is_a?(String) | ||
| self.method = method | ||
| self.header = Hash.new | ||
| self.header =({}) |
There was a problem hiding this comment.
I'm curious whether this was a Rubocop fix or was there a technical detail about instantiating a Hash one way vs the other?
There was a problem hiding this comment.
Can we fix the missing space? FWIW, I thinkself.header = {} is sufficient.
| if sent_size | ||
| @opencensus_span.put_message_event OpenCensus::Trace::SpanBuilder::SENT, 1, sent_size | ||
| end | ||
| @opencensus_span.put_message_event OpenCensus::Trace::SpanBuilder::SENT, 1, sent_size if sent_size |
There was a problem hiding this comment.
super nit: I actually like the original pattern (more explicit/readable) FWIW
| self.url = Addressable::Template.new(url) if url.is_a?(String) | ||
| self.method = method | ||
| self.header = Hash.new | ||
| self.header =({}) |
There was a problem hiding this comment.
Can we fix the missing space? FWIW, I thinkself.header = {} is sufficient.
| raise Google::Apis::Error, | ||
| "Path traversal segment #{seg.inspect} is not allowed in parameter #{var_name}: #{value}" | ||
| end | ||
| raise Google::Apis::Error, "Invalid path segment '' in parameter #{var_name}" if seg == '' |
There was a problem hiding this comment.
Invalid path segment '' in parameter #{var_name} doesn't include #{value} and the literal '' can be a bit cryptic.
WDYT about making it clearer:
raise Google::Apis::Error, "Empty path segment (e.g. '//' or trailing '/') is not allowed in parameter #{var_name}: #{value}" if seg == ''
Implement dynamic runtime template parsing and validation inside HttpCommand to protect Discovery REST clients from path traversal and parameter injection exploits.
Specifically, this change:
In addition to the validation logic, this commit includes:
Note: If the canonical specification is updated to favor the simpler alternative, this implementation can be easily simplified to "Fail always if dots are found in a double-wildcard value" by replacing the segment-traversal logic with a simple check for any "." or ".." substring.