fix(common): prevent path traversal and parameter injection in REST clients - #9152
fix(common): prevent path traversal and parameter injection in REST clients#9152danieljbruce wants to merge 1 commit into
Conversation
Adds `encodeWithSlashes`, `encodeWithoutSlashes`, and `encodeURIPath` helper functions to `@google-cloud/common` and `@google-cloud/storage`'s common module to validate URI path variables against path traversal (`.` and `..` segments) and percent-encode reserved/URL-unsafe characters. Integrates URI path transcoding into `Service` and `ServiceObject` request handlers. 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 URI encoding and path traversal protection by adding new utility functions (encodeWithSlashes, encodeWithoutSlashes, and encodeURIPath) and integrating them into the ServiceObject and Service classes across both core/common and handwritten/storage. It also adds comprehensive unit tests to verify these security improvements. However, the reviewer identified a critical issue where applying the new encoding logic to absolute URLs will cause double-encoding of already percent-encoded characters, which would break requests like resumable uploads or media downloads. It is recommended to restrict this encoding and validation logic to non-absolute URLs as suggested.
| 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 { |
There was a problem hiding this comment.
Applying util.encodeURIPath to url.pathname of an absolute URL will double-encode any already percent-encoded characters (e.g., %20 becomes %2520, %2F becomes %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.
if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri.replace(trimSlashesRegex, '');
} else {| 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 { |
There was a problem hiding this comment.
Applying util.encodeURIPath to url.pathname of an absolute URL will double-encode any already percent-encoded characters (e.g., %20 becomes %2520, %2F becomes %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.
if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri
.replace(trimSlashesRegex, '')
.replace(/\/:/g, ':');
} else {| 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 { |
There was a problem hiding this comment.
Applying util.encodeURIPath to url.pathname of an absolute URL will double-encode any already percent-encoded characters (e.g., %20 becomes %2520, %2F becomes %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.
if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri.replace(trimSlashesRegex, '');
} else {| 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 { |
There was a problem hiding this comment.
Applying util.encodeURIPath to url.pathname of an absolute URL will double-encode any already percent-encoded characters (e.g., %20 becomes %2520, %2F becomes %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.
if (isAbsoluteUrl) {
const trimSlashesRegex = /^\/*|\/*$/g;
reqOpts.uri = reqOpts.uri
.replace(trimSlashesRegex, '')
.replace(/\/:/g, ':');
} else {
Fixes path traversal and parameter injection vulnerabilities in Node.js REST clients by validating
.and..path segments and percent-encoding URL-unsafe characters.PR created automatically by Jules for task 10569082496054868007 started by @danieljbruce