Skip to content

Commit d48e0d2

Browse files
authored
fix: prevent potentially null base log urls from being used (#20)
* fix: prevent potentially null baseUrls from being used * test: fixing a test that was asserting against a false positive
1 parent 0d41c7a commit d48e0d2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/node/__tests__/index.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ describe('#metrics', () => {
355355
})
356356
.get('/v1/')
357357
.basicAuth({ user: apiKey })
358-
.reply(200, {
358+
.reply(401, {
359359
error: 'APIKEY_NOTFOUNDD',
360360
message: "We couldn't find your API key",
361361
suggestion:
@@ -382,7 +382,7 @@ describe('#metrics', () => {
382382
.get('/test')
383383
.expect(200)
384384
.expect(res => {
385-
expect(getCache().getKey('baseUrl')).toBeUndefined();
385+
expect(getCache().getKey('baseUrl')).toBeNull();
386386

387387
// `x-documentation-url` header should not be present since we couldn't get the base URL!
388388
expect(Object.keys(res.headers)).not.toContain('x-documentation-url');

packages/node/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ async function getProjectBaseUrl(encodedApiKey) {
5757
'User-Agent': `${pkg.name}/${pkg.version}`,
5858
},
5959
})
60-
.then(res => res.json())
60+
.then(res => {
61+
if (res.status >= 400 && res.status <= 599) {
62+
throw res;
63+
}
64+
65+
return res.json();
66+
})
6167
.then(project => {
6268
baseUrl = project.baseUrl;
6369

@@ -96,7 +102,7 @@ module.exports.metrics = (apiKey, group, options = {}) => {
96102
const startedDateTime = new Date();
97103
const logId = uuidv4();
98104

99-
if (baseLogUrl !== undefined) {
105+
if (baseLogUrl !== undefined && typeof baseLogUrl === 'string') {
100106
res.setHeader('x-documentation-url', `${baseLogUrl}/logs/${logId}`);
101107
}
102108

0 commit comments

Comments
 (0)