Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

Commit 136f90b

Browse files
authored
fix: allow unformatted originalURL input (#1100)
Fixes: googleapis/nodejs-logging-winston#607 Allows users to input invalid or badly formatted `originalUrl` values. As it did previously.
1 parent 1eb1086 commit 136f90b

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

src/http-request.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ export function makeHttpRequestData(
7070
responseSize,
7171
latency;
7272
// Format request properties
73-
if (req.url) {
74-
requestUrl = req.url;
75-
const url = new URL(requestUrl);
76-
protocol = url.protocol;
77-
}
73+
if (req.url) requestUrl = req.url;
7874
// OriginalURL overwrites inferred url
79-
if ('originalUrl' in req && req.originalUrl) {
80-
requestUrl = req.originalUrl;
81-
const url = new URL(requestUrl);
82-
protocol = url.protocol;
75+
if ('originalUrl' in req && req.originalUrl) requestUrl = req.originalUrl;
76+
// Format protocol from valid URL
77+
if (requestUrl) {
78+
try {
79+
const url = new URL(requestUrl);
80+
protocol = url.protocol;
81+
} catch (e) {
82+
// Library should not panic
83+
}
8384
}
8485
req.method ? (requestMethod = req.method) : null;
8586
if (req.headers && req.headers['user-agent']) {

test/http-request.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ describe('http-request', () => {
3333
assert.strictEqual(cloudReq.requestUrl, 'http://google.com/');
3434
assert.strictEqual(cloudReq.requestMethod, 'GET');
3535
});
36+
it('should not panic on invalid URL', () => {
37+
const req = {
38+
method: 'GET',
39+
originalUrl: 'invalid/url/',
40+
} as ServerRequest;
41+
const cloudReq = makeHttpRequestData(req);
42+
assert.strictEqual(cloudReq.protocol, undefined);
43+
assert.strictEqual(cloudReq.requestUrl, 'invalid/url/');
44+
assert.strictEqual(cloudReq.requestMethod, 'GET');
45+
});
3646
it('should infer as many request values as possible', () => {
3747
const req = {
3848
method: 'GET',

0 commit comments

Comments
 (0)