Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .changeset/few-beers-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@evervault/sdk": patch
---

Correct logic for parsing domains from URLs supplied to the http request function: add fallback support for path alongisde pathname.

Fix bug in parsing raw strings supplied to request to URLs by correcting object keys.
59 changes: 30 additions & 29 deletions lib/utils/httpsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ const certificateUtil = (evClient) => {
};
};

/**
*
* @param {Parameters<typeof import('node:https').request>} args
* @returns {{ domain: string, path: string }}
*/
function getDomainAndPathFromArgs(args) {
if (typeof args[0] === 'string') {
const parsedUrl = new URL(args[0]);
return { domain: parsedUrl.host, path: parsedUrl.pathname };
}

if (args[0] instanceof URL) {
return { domain: args[0].host, path: args[0].pathname };
}

let domain, path;
for (const arg of args) {
if (arg instanceof Object) {
domain = domain ?? arg.hostname ?? arg.host;
path = path ?? arg.pathname ?? arg.path;
}
}
return {
domain,
path,
};
}

/**
* @param {string} apiKey
* @param {string} tunnelHostname
Expand All @@ -56,41 +84,13 @@ const overloadHttpsModule = (
evClient,
originalRequest
) => {
/**
*
* @param {Parameters<typeof import('node:https').request>} args
* @returns {{ host: string, path?: string }}
*/
function getDomainAndPathFromArgs(args) {
if (typeof args[0] === 'string') {
const parsedUrl = new URL(args[0]);
return { host: parsedUrl.host, path: parsedUrl.pathname };
}

if (args[0] instanceof URL) {
return { host: args[0].host, path: args[0].pathname };
}

let domain, path;
for (const arg of args) {
if (arg instanceof Object) {
domain = domain ?? arg.hostname ?? arg.host;
path = path ?? arg.pathname;
}
}
return {
domain,
path,
};
}

/**
* @param {Parameters<typeof import('node:https').request>} args
* @returns {ReturnType<typeof import('node:https').request>}
*/
function wrapMethodRequest(...args) {
const { domain, path } = getDomainAndPathFromArgs(args);
const shouldProxy = domainFilter(domain, path);
const shouldProxy = !!domain && domainFilter(domain, path);
if (
debugRequests &&
!EVERVAULT_DOMAINS.some((evervault_domain) =>
Expand Down Expand Up @@ -147,4 +147,5 @@ const httpsRelayAgent = (
module.exports = {
overloadHttpsModule,
httpsRelayAgent,
getDomainAndPathFromArgs,
};
18 changes: 18 additions & 0 deletions tests/httpHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ const { expect } = require('chai');
const https = require('https');
const fs = require('fs');

describe('getDomainAndPathFromArgs', () => {
[
[new URL('https://a.com'), 'a.com', '/'],
[new URL('https://a.com/b/c'), 'a.com', '/b/c'],
['https://a.com', 'a.com', '/'],
['https://a.com/b/c', 'a.com', '/b/c'],
[{ hostname: 'a.com', pathname: '/' }, 'a.com', '/'],
[{ host: 'a.com', path: '/' }, 'a.com', '/'],
[{ yayForJavascript: 1 }, undefined, undefined],
].forEach(([url, expectedDomain, expectedPath], i) => {
it(`works for ${JSON.stringify(url)} (${i})`, () => {
const result = httpsHelper.getDomainAndPathFromArgs([url]);
expect(result.domain).to.equal(expectedDomain);
expect(result.path).to.equal(expectedPath);
});
});
});

describe('overload https requests', () => {
const apiKey = 'test-api-key';
const tunnelHostname = 'relay.evervault.com';
Expand Down
Loading