-
Notifications
You must be signed in to change notification settings - Fork 700
feat(gax): implement secure rest-special-uri-chars guidelines for fallback transcoder #9123
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
Draft
danieljbruce
wants to merge
2
commits into
main
Choose a base branch
from
jules-462466416141264086-f7ebfa31
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.
Draft
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,8 +127,9 @@ export function buildQueryStringComponents( | |
| if (Array.isArray(request[key])) { | ||
| for (const value of request[key] as JSONObject[]) { | ||
| resultList.push( | ||
| `${prefix}${encodeWithoutSlashes(key)}=${encodeWithoutSlashes( | ||
| `${prefix}${encodeWithoutSlashes(key, key)}=${encodeWithoutSlashes( | ||
| value.toString(), | ||
| key, | ||
| )}`, | ||
| ); | ||
| } | ||
|
|
@@ -138,39 +139,67 @@ export function buildQueryStringComponents( | |
| ); | ||
| } else { | ||
| resultList.push( | ||
| `${prefix}${encodeWithoutSlashes(key)}=${encodeWithoutSlashes( | ||
| `${prefix}${encodeWithoutSlashes(key, key)}=${encodeWithoutSlashes( | ||
| request[key] === null ? 'null' : request[key]!.toString(), | ||
| key, | ||
| )}`, | ||
| ); | ||
| } | ||
| } | ||
| return resultList; | ||
| } | ||
|
|
||
| export function encodeWithSlashes(str: string): string { | ||
| // encodeWithSlashes implements the security rules for double-asterisk ("**") pattern matches. | ||
| // We split the path into segments and strictly reject any segment that is exactly "." or "..". | ||
| // This prevents path traversal attacks across any variable directory subpaths. | ||
| export function encodeWithSlashes( | ||
| str: string, | ||
| propertyName = 'resource ID', | ||
| ): string { | ||
| const segments = str.split('/'); | ||
| if (segments.some(segment => segment === '.' || segment === '..')) { | ||
| throw new Error( | ||
| `Value for ${propertyName} must not contain segments that are exactly . or .. .`, | ||
| ); | ||
| } | ||
| // Percent-encode any character that is not in the unreserved set, preserving slashes. | ||
| return str | ||
| .split('') | ||
| .map(c => (c.match(/[-_.~0-9a-zA-Z]/) ? c : encodeURIComponent(c))) | ||
| .map(c => (c.match(/[-_.~/0-9a-zA-Z]/) ? c : encodeURIComponent(c))) | ||
| .join(''); | ||
| } | ||
|
|
||
| export function encodeWithoutSlashes(str: string): string { | ||
| // encodeWithoutSlashes implements the security rules for single-asterisk ("*") pattern matches. | ||
| // We throw a validation error if the matched value is exactly "." or "..", | ||
| // preventing resource ID level traversal attacks. | ||
| export function encodeWithoutSlashes( | ||
| str: string, | ||
| propertyName = 'resource ID', | ||
| ): string { | ||
| if (str === '.' || str === '..') { | ||
| throw new Error(`Invalid value ${str} for ${propertyName}`); | ||
| } | ||
| // Percent-encode any character that is not in the unreserved set, encoding slashes too. | ||
| return str | ||
| .split('') | ||
| .map(c => (c.match(/[-_.~0-9a-zA-Z/]/) ? c : encodeURIComponent(c))) | ||
| .map(c => (c.match(/[-_.~0-9a-zA-Z]/) ? c : encodeURIComponent(c))) | ||
| .join(''); | ||
| } | ||
|
|
||
| function escapeRegExp(str: string) { | ||
| return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| // applyPattern extracts the wildcards from a path template pattern, uses regex to capture | ||
| // the actual parts corresponding to each wildcard (* or **), and validates them using the | ||
| // appropriate encodeWithoutSlashes / encodeWithSlashes security logic to avoid path traversal. | ||
| export function applyPattern( | ||
| pattern: string, | ||
| fieldValue: string, | ||
| propertyName?: string, | ||
| ): string | undefined { | ||
| if (!pattern || pattern === '*') { | ||
| return encodeWithSlashes(fieldValue); | ||
| return encodeWithoutSlashes(fieldValue, propertyName); | ||
| } | ||
|
|
||
| if (!pattern.includes('*') && pattern !== fieldValue) { | ||
|
|
@@ -186,11 +215,42 @@ export function applyPattern( | |
| '$', | ||
| ); | ||
|
|
||
| if (!fieldValue.match(regex)) { | ||
| const match = fieldValue.match(regex); | ||
| if (!match) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return encodeWithoutSlashes(fieldValue); | ||
| // Extract and validate wildcards in the pattern (* or **) | ||
| const wildcardRegex = /\*\*|\*/g; | ||
| let wcMatch; | ||
| const wildcards: string[] = []; | ||
| while ((wcMatch = wildcardRegex.exec(pattern)) !== null) { | ||
| wildcards.push(wcMatch[0]); | ||
| } | ||
|
|
||
| // Map each captured group to its respective wildcard type and validate | ||
| const capturedGroups = match.slice(1); | ||
| for (let i = 0; i < capturedGroups.length; i++) { | ||
| const groupVal = capturedGroups[i]; | ||
| const wcType = wildcards[i]; | ||
|
Comment on lines
+232
to
+235
Contributor
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. If a capturing group in the matched regex is optional and does not match, 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]; |
||
| const propName = propertyName || 'resource ID'; | ||
| if (wcType === '*') { | ||
| // Single-asterisk wildcard validation: cannot be dot or two-dots | ||
| if (groupVal === '.' || groupVal === '..') { | ||
| throw new Error(`Invalid value ${fieldValue} for ${propName}`); | ||
| } | ||
| } else if (wcType === '**') { | ||
| // Double-asterisk wildcard validation: no segments can be dot or two-dots | ||
| const segments = groupVal.split('/'); | ||
| if (segments.some(seg => seg === '.' || seg === '..')) { | ||
| throw new Error( | ||
| `Value for ${propName} must not contain segments that are exactly . or .. .`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return encodeWithSlashes(fieldValue, propertyName); | ||
| } | ||
|
|
||
| function fieldToCamelCase(field: string): string { | ||
|
|
@@ -224,6 +284,7 @@ export function match( | |
| const appliedPattern = applyPattern( | ||
| pattern, | ||
| fieldValue === null ? 'null' : fieldValue!.toString(), | ||
| camelCasedField, | ||
| ); | ||
| if (appliedPattern === undefined) { | ||
| return undefined; | ||
|
|
||
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
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.
If an element in the array is
nullorundefined, callingvalue.toString()will throw aTypeError. Adding a defensive check to handle null/undefined values safely prevents potential runtime crashes.