Skip to content

fix(common): prevent path traversal and parameter injection in REST clients - #9152

Draft
danieljbruce wants to merge 1 commit into
mainfrom
fix-rest-uri-transcoding-security-10569082496054868007
Draft

fix(common): prevent path traversal and parameter injection in REST clients#9152
danieljbruce wants to merge 1 commit into
mainfrom
fix-rest-uri-transcoding-security-10569082496054868007

Conversation

@danieljbruce

Copy link
Copy Markdown
Contributor

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

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>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 565 to +574
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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 {

Comment on lines 214 to +223
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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 {

Comment on lines 564 to +573
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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 {

Comment on lines 236 to +245
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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 {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant