feat(gax): implement secure rest-special-uri-chars guidelines for fallback transcoder - #9123
feat(gax): implement secure rest-special-uri-chars guidelines for fallback transcoder#9123danieljbruce wants to merge 2 commits into
Conversation
…lback transcoder We implement a highly secure, approved, and robust solution to the path traversal and parameter injection vulnerabilities in the REST fallback layer of our client libraries. Specifically: - We updated `encodeWithoutSlashes` (matching `*`) to accept `propertyName`, fail immediately and throw on exactly '.' or '..' with an error, and percent-encode all non-unreserved characters. - We updated `encodeWithSlashes` (matching `**`) to accept `propertyName`, fail with an error if any segment of the matched value is exactly '.' or '..', and percent-encode all characters except unreserved ones and '/'. - We updated `applyPattern` to correctly capture and validate wildcards matching single and double asterisks against dot and two-dot segments, throwing their corresponding descriptive errors. - We added comprehensive unit tests validating single/double asterisk patterns, unsafe and safe traversals, and percent-encoding in `core/packages/gax/test/unit/transcoding.ts`. - We added an integration test in `packages/google-cloud-dialogflow-cx/test/gapic_sessions_v3.ts` that asserts path traversal attempts against fallback/REST transport mode are blocked and rejected with clear validation errors. Co-authored-by: danieljbruce <8935272+danieljbruce@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request introduces path traversal validation to the REST fallback transcoding logic in google-gax. It ensures that single-asterisk (*) and double-asterisk (**) wildcard segments in path templates do not resolve to "." or ".." segments, throwing errors if invalid paths are detected. The changes also update encoding functions and add comprehensive unit tests. The review feedback highlights two potential runtime TypeError crashes: one when calling value.toString() on potentially null or undefined array elements in buildQueryStringComponents, and another when calling groupVal.split('/') on optional, unmatched regex capturing groups in applyPattern.
| `${prefix}${encodeWithoutSlashes(key, key)}=${encodeWithoutSlashes( | ||
| value.toString(), | ||
| key, | ||
| )}`, |
There was a problem hiding this comment.
If an element in the array is null or undefined, calling value.toString() will throw a TypeError. Adding a defensive check to handle null/undefined values safely prevents potential runtime crashes.
| `${prefix}${encodeWithoutSlashes(key, key)}=${encodeWithoutSlashes( | |
| value.toString(), | |
| key, | |
| )}`, | |
| `${prefix}${encodeWithoutSlashes(key, key)}=${encodeWithoutSlashes( | |
| value === null || value === undefined ? 'null' : value.toString(), | |
| key, | |
| )}` |
| const capturedGroups = match.slice(1); | ||
| for (let i = 0; i < capturedGroups.length; i++) { | ||
| const groupVal = capturedGroups[i]; | ||
| const wcType = wildcards[i]; |
There was a problem hiding this comment.
If a capturing group in the matched regex is optional and does not match, groupVal can be undefined. Calling groupVal.split('/') on an undefined value will throw a TypeError. Adding a defensive check to skip null or undefined group values ensures robustness.
const capturedGroups = match.slice(1);
for (let i = 0; i < capturedGroups.length; i++) {
const groupVal = capturedGroups[i];
if (groupVal === undefined || groupVal === null) {
continue;
}
const wcType = wildcards[i];…lback transcoder We implement a highly secure, approved, and robust solution to the path traversal and parameter injection vulnerabilities in the REST fallback layer of our client libraries. Specifically: - We updated `encodeWithoutSlashes` (matching `*`) to accept `propertyName`, fail immediately and throw on exactly '.' or '..' with an error, and percent-encode all non-unreserved characters. - We updated `encodeWithSlashes` (matching `**`) to accept `propertyName`, fail with an error if any segment of the matched value is exactly '.' or '..', and percent-encode all characters except unreserved ones and '/'. - We updated `applyPattern` to correctly capture and validate wildcards matching single and double asterisks against dot and two-dot segments, throwing their corresponding descriptive errors. - We added comprehensive unit tests validating single/double asterisk patterns, unsafe and safe traversals, and percent-encoding in `core/packages/gax/test/unit/transcoding.ts`. - We added an integration test in `packages/google-cloud-dialogflow-cx/test/gapic_sessions_v3.ts` that asserts path traversal attempts against fallback/REST transport mode are blocked and rejected with clear validation errors. All changes are fully commented to explain their purpose. Co-authored-by: danieljbruce <8935272+danieljbruce@users.noreply.github.com>
This PR implements the approved, lang-neutral rest-special-uri-chars security guidelines for the REST fallback transcoding layer. This provides full protection against both path traversal and parameter injection attacks. It also includes comprehensive unit tests in google-gax and integration-level tests in google-cloud-dialogflow-cx to verify safety.
PR created automatically by Jules for task 462466416141264086 started by @danieljbruce