Skip to content

Commit bcf1517

Browse files
authored
refactor(instrumentation-http): avoid deprecated url.parse() in getAbsoluteUrl() (#6089)
1 parent f6c2f62 commit bcf1517

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
5555
* test(otlp-grpc-exporter-base): increase timeout in flaky test [#6042](https://github.com/open-telemetry/opentelemetry-js/pull/6042) @cjihrig
5656
* test(sdk-node): use process.env consistently in tests [#6052](https://github.com/open-telemetry/opentelemetry-js/pull/6052) @cjihrig
5757
* test(sdk-node): ensure process.env is cleaned up between tests [#6066](https://github.com/open-telemetry/opentelemetry-js/pull/6066) @cjihrig
58+
* refactor(instrumentation-http): avoid deprecated url.parse() in getAbsoluteUrl() [#6089](https://github.com/open-telemetry/opentelemetry-js/pull/6089) @cjihrig
5859

5960
## 0.207.0
6061

experimental/packages/opentelemetry-instrumentation-http/src/utils.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,20 @@ export const getAbsoluteUrl = (
116116
}
117117
// Redact sensitive query parameters
118118
if (path.includes('?')) {
119-
const parsedUrl = url.parse(path);
120-
const pathname = parsedUrl.pathname || '';
121-
const query = parsedUrl.query || '';
122-
const searchParams = new URLSearchParams(query);
123-
const sensitiveParamsToRedact: string[] = redactedQueryParams || [];
124-
125-
for (const sensitiveParam of sensitiveParamsToRedact) {
126-
if (
127-
searchParams.has(sensitiveParam) &&
128-
searchParams.get(sensitiveParam) !== ''
129-
) {
130-
searchParams.set(sensitiveParam, STR_REDACTED);
119+
try {
120+
const parsedUrl = new URL(path, 'http://localhost');
121+
const sensitiveParamsToRedact: string[] = redactedQueryParams || [];
122+
123+
for (const sensitiveParam of sensitiveParamsToRedact) {
124+
if (parsedUrl.searchParams.get(sensitiveParam)) {
125+
parsedUrl.searchParams.set(sensitiveParam, STR_REDACTED);
126+
}
131127
}
132-
}
133128

134-
const redactedQuery = searchParams.toString();
135-
path = `${pathname}?${redactedQuery}`;
129+
path = `${parsedUrl.pathname}${parsedUrl.search}`;
130+
} catch {
131+
// Ignore error, as the path was not a valid URL.
132+
}
136133
}
137134
const authPart = reqUrlObject.auth ? `${STR_REDACTED}:${STR_REDACTED}@` : '';
138135
return `${protocol}//${authPart}${host}${path}`;

experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ describe('Utility', () => {
211211
'http://localhost:8080/registers?AWSAccessKeyId=REDACTED'
212212
);
213213
});
214+
it('does not perform redaction if the provided path cannot be parsed', () => {
215+
const result = utils.getAbsoluteUrl(
216+
{ path: 'http://?AWSAccessKeyId=secret123' },
217+
{}
218+
);
219+
assert.strictEqual(
220+
result,
221+
'http://localhosthttp://?AWSAccessKeyId=secret123'
222+
);
223+
});
214224
});
215225

216226
describe('setSpanWithError()', () => {

0 commit comments

Comments
 (0)