Skip to content

Commit 07da97d

Browse files
fix: resourceUrl.startsWith error, proxy agent should only handle strings (#91)
1 parent 27abfcf commit 07da97d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/ProxyFetch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class PatchedHttpsProxyAgent extends HttpsProxyAgent {
4646
* @returns {http.Agent} a http.Agent for basic auth proxy
4747
*/
4848
function proxyAgent (resourceUrl, proxyAuthOptions) {
49+
if (typeof resourceUrl !== 'string') {
50+
throw new codes.ERROR_PROXY_FETCH_INITIALIZATION_TYPE({ sdkDetails: { resourceUrl }, messageValues: 'resourceUrl must be of type string' })
51+
}
52+
4953
const { proxyUrl, username, password, rejectUnauthorized = true } = proxyAuthOptions
5054
const proxyOpts = urlToHttpOptions(proxyUrl)
5155

src/SDKErrors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ module.exports = {
4949

5050
// Define your error codes with the wrapper
5151
E('ERROR_PROXY_FETCH_INITIALIZATION', 'Proxy fetch initialization error(s). Missing arguments: %s')
52+
E('ERROR_PROXY_FETCH_INITIALIZATION_TYPE', 'Proxy fetch initialization error(s). Arguments are the wrong type: %s')

test/proxy.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ describe('http proxy', () => {
7878
expect(response.ok).toEqual(false)
7979
expect(response.status).toEqual(502)
8080
})
81+
82+
test('failure due to passing URL object as resourceUrl', async () => {
83+
// connect to non-existent server port
84+
const testUrl = new URL(`${protocol}://localhost:${portNotInUse}/mirror/?foo=bar`)
85+
86+
const proxyUrl = proxyServer.url
87+
const proxyFetch = new ProxyFetch({ proxyUrl, rejectUnauthorized: false })
88+
89+
const err = new codes.ERROR_PROXY_FETCH_INITIALIZATION_TYPE({
90+
sdkDetails: {},
91+
messageValues: 'resourceUrl must be of type string'
92+
})
93+
await expect(proxyFetch.fetch(testUrl)).rejects.toThrow(err)
94+
})
8195
})
8296

8397
describe('basic auth', () => {

0 commit comments

Comments
 (0)