-
Notifications
You must be signed in to change notification settings - Fork 700
fix(common): prevent path traversal and parameter injection in REST clients #9152
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,20 +212,28 @@ export class Service { | |
| uriComponents.push(reqOpts.uri); | ||
|
|
||
| if (isAbsoluteUrl) { | ||
| uriComponents.splice(0, uriComponents.indexOf(reqOpts.uri)); | ||
| const url = new URL(reqOpts.uri); | ||
| const encodedPath = util.encodeURIPath(url.pathname); | ||
| url.pathname = encodedPath; | ||
| let res = url.toString(); | ||
| if (!reqOpts.uri.endsWith('/') && res.endsWith('/')) { | ||
| res = res.slice(0, -1); | ||
| } | ||
| reqOpts.uri = res; | ||
| } else { | ||
|
Comment on lines
214
to
+223
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. Applying if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri
.replace(trimSlashesRegex, '')
.replace(/\/:/g, ':');
} else { |
||
| reqOpts.uri = uriComponents | ||
| .map(uriComponent => { | ||
| const trimSlashesRegex = /^\/*|\/*$/g; | ||
| const trimmed = uriComponent.replace(trimSlashesRegex, ''); | ||
| return util.encodeURIPath(trimmed); | ||
| }) | ||
| .join('/') | ||
| // Some URIs have colon separators. | ||
| // Bad: https://.../projects/:list | ||
| // Good: https://.../projects:list | ||
| .replace(/\/:/g, ':'); | ||
| } | ||
|
|
||
| reqOpts.uri = uriComponents | ||
| .map(uriComponent => { | ||
| const trimSlashesRegex = /^\/*|\/*$/g; | ||
| return uriComponent.replace(trimSlashesRegex, ''); | ||
| }) | ||
| .join('/') | ||
| // Some URIs have colon separators. | ||
| // Bad: https://.../projects/:list | ||
| // Good: https://.../projects:list | ||
| .replace(/\/:/g, ':'); | ||
|
|
||
| const requestInterceptors = this.getRequestInterceptors(); | ||
|
|
||
| (arrify as unknown as (arg1: any) => any[])(reqOpts.interceptors_!).forEach( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -562,17 +562,25 @@ class ServiceObject<T, K extends BaseMetadata> extends EventEmitter { | |
| const uriComponents = [this.baseUrl, this.id || '', reqOpts.uri]; | ||
|
|
||
| if (isAbsoluteUrl) { | ||
| uriComponents.splice(0, uriComponents.indexOf(reqOpts.uri)); | ||
| const url = new URL(reqOpts.uri); | ||
| const encodedPath = util.encodeURIPath(url.pathname); | ||
| url.pathname = encodedPath; | ||
| let res = url.toString(); | ||
| if (!reqOpts.uri.endsWith('/') && res.endsWith('/')) { | ||
| res = res.slice(0, -1); | ||
| } | ||
| reqOpts.uri = res; | ||
| } else { | ||
|
Comment on lines
564
to
+573
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. Applying if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri.replace(trimSlashesRegex, '');
} else { |
||
| reqOpts.uri = uriComponents | ||
| .filter(x => x!.trim()) // Limit to non-empty strings. | ||
| .map(uriComponent => { | ||
| const trimSlashesRegex = /^\/*|\/*$/g; | ||
| const trimmed = uriComponent!.replace(trimSlashesRegex, ''); | ||
| return util.encodeURIPath(trimmed); | ||
| }) | ||
| .join('/'); | ||
| } | ||
|
|
||
| reqOpts.uri = uriComponents | ||
| .filter(x => x!.trim()) // Limit to non-empty strings. | ||
| .map(uriComponent => { | ||
| const trimSlashesRegex = /^\/*|\/*$/g; | ||
| return uriComponent!.replace(trimSlashesRegex, ''); | ||
| }) | ||
| .join('/'); | ||
|
|
||
| const childInterceptors = Array.isArray(reqOpts.interceptors_) | ||
| ? reqOpts.interceptors_ | ||
| : []; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,20 +234,28 @@ export class Service { | |
| uriComponents.push(reqOpts.uri); | ||
|
|
||
| if (isAbsoluteUrl) { | ||
| uriComponents.splice(0, uriComponents.indexOf(reqOpts.uri)); | ||
| const url = new URL(reqOpts.uri); | ||
| const encodedPath = util.encodeURIPath(url.pathname); | ||
| url.pathname = encodedPath; | ||
| let res = url.toString(); | ||
| if (!reqOpts.uri.endsWith('/') && res.endsWith('/')) { | ||
| res = res.slice(0, -1); | ||
| } | ||
| reqOpts.uri = res; | ||
| } else { | ||
|
Comment on lines
236
to
+245
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. Applying if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri
.replace(trimSlashesRegex, '')
.replace(/\/:/g, ':');
} else { |
||
| reqOpts.uri = uriComponents | ||
| .map(uriComponent => { | ||
| const trimSlashesRegex = /^\/*|\/*$/g; | ||
| const trimmed = uriComponent.replace(trimSlashesRegex, ''); | ||
| return util.encodeURIPath(trimmed); | ||
| }) | ||
| .join('/') | ||
| // Some URIs have colon separators. | ||
| // Bad: https://.../projects/:list | ||
| // Good: https://.../projects:list | ||
| .replace(/\/:/g, ':'); | ||
| } | ||
|
|
||
| reqOpts.uri = uriComponents | ||
| .map(uriComponent => { | ||
| const trimSlashesRegex = /^\/*|\/*$/g; | ||
| return uriComponent.replace(trimSlashesRegex, ''); | ||
| }) | ||
| .join('/') | ||
| // Some URIs have colon separators. | ||
| // Bad: https://.../projects/:list | ||
| // Good: https://.../projects:list | ||
| .replace(/\/:/g, ':'); | ||
|
|
||
| const requestInterceptors = this.getRequestInterceptors(); | ||
| const interceptorArray = Array.isArray(reqOpts.interceptors_) | ||
| ? reqOpts.interceptors_ | ||
|
|
||
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.
Applying
util.encodeURIPathtourl.pathnameof an absolute URL will double-encode any already percent-encoded characters (e.g.,%20becomes%2520,%2Fbecomes%252F). Absolute URLs (such as those returned by the Google API backend for resumable uploads or media downloads) are already fully formed and encoded. Re-encoding them will break these requests. We should only apply the encoding and validation logic to non-absolute URLs.