Skip to content
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

Fix password and username in http.url #5038

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,25 @@ export const getRequestInfo = (

/**
* Makes sure options is of type string or object
Copy link
Contributor

Choose a reason for hiding this comment

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

You may want to update this function description to better represent the new logic added in this PR.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, thanks

I will do that

* If a string, make sure the URL has no authentication credentials (username/password)
* @param options for the request
*/

export const isValidOptionsType = (options: unknown): boolean => {
if (!options) {
return false;
}

const type = typeof options;
return type === 'string' || (type === 'object' && !Array.isArray(options));

if (type === 'string') {
const parsedUrl = url.parse(options as string);
Copy link
Contributor

Choose a reason for hiding this comment

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

According to https://nodejs.org/api/url.html#urlparseurlstring-parsequerystring-slashesdenotehost this line could throw and also is discouraged due to security issues. The replacement would be then URL.parse but it won't be available in Node.js versions prev to v22.1.0. Maybe this could be extracted in to a utility function

function parseUrl(input: string): URL | null {
  if (URL.parse) {
    return URL.parse(input);
  }
  try {
    return url.parse(input);
  } catch {}
  return null;
}

and use it here

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I’ll extract the logic into a utility function to handle the compatibility and update the PR shortly.

Copy link
Member

Choose a reason for hiding this comment

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

I think the util function should be as follows, avoid using the deprecated url.parse:

// This is identical to `URL.parse`.
function parseUrl(input: string, base?: string): URL | null {
  try {
    return new URL(input, base);
  } catch {}
  return null;
}

Copy link
Member

Choose a reason for hiding this comment

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

@david-luna @legendecas this was recently the source a of a bug report #5060 - I opened a PR #5091 that may conflict with this PR. url.parse() and new URL() are different enough that replacing url.parse() was quite the headache, I'd appreciate a review 🙂

Copy link
Contributor

@david-luna david-luna Oct 28, 2024

Choose a reason for hiding this comment

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

@legendecas using the constructor seems a better solution in terms of version support :)

isValidOptionsType will inspect the returned URL object to decide if the param is valid or not. So if null is returned (new URL() throws) I guess the author of this PR would make the function fallback to checking the type (string or object). There is another situation that will throw an error which is if the instrumentation is running on a Node.js version earlier than v18.17.0 where the URL constructor is not available.

This means for Node.js <v18.17.0 all strings will be considered valid so for these versions the fix wont apply. I wonder if it's okay to have this partial fix/support 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

This means for Node.js <v18.17.0 all strings will be considered valid so for these versions the fix wont apply. I wonder if it's okay to have this partial fix/support 🤔

I withdraw this part. I looked in the wrong place in the documentation. The new URL constructor is available and @legendecas solution is the way to go.

if (!parsedUrl.auth) {
return true;
}
}

return type === 'object' && !Array.isArray(options);
};

export const extractHostnameAndPort = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,15 @@ describe('Utility', () => {
});

describe('isValidOptionsType()', () => {
['', false, true, 1, 0, []].forEach(options => {
[
'',
false,
true,
1,
0,
'https://username:[email protected]/',
[],
].forEach(options => {
it(`should return false with the following value: ${JSON.stringify(
options
)}`, () => {
Expand Down