From 88cc4f07a87aa631baef7afbeb6e1b22ac8424fa Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:47:48 +0000 Subject: [PATCH 1/6] build(deps-dev): bump proxy from 1.0.2 to 2.1.1 Bumps [proxy](https://github.com/TooTallNate/proxy-agents/tree/HEAD/packages/proxy) from 1.0.2 to 2.1.1. - [Release notes](https://github.com/TooTallNate/proxy-agents/releases) - [Changelog](https://github.com/TooTallNate/proxy-agents/blob/main/packages/proxy/CHANGELOG.md) - [Commits](https://github.com/TooTallNate/proxy-agents/commits/proxy@2.1.1/packages/proxy) --- updated-dependencies: - dependency-name: proxy dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c85ec55102f..17c6a4ad2c1 100644 --- a/package.json +++ b/package.json @@ -114,7 +114,7 @@ "mitata": "^0.1.8", "node-fetch": "^3.3.2", "pre-commit": "^1.2.2", - "proxy": "^1.0.2", + "proxy": "^2.1.1", "proxyquire": "^2.1.3", "request": "^2.88.2", "snazzy": "^9.0.0", From f27b2e9aca92a18fd9256612b26647a57b6edd4a Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 25 Feb 2024 13:17:45 +0100 Subject: [PATCH 2/6] fix proxy test --- test/proxy.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/test/proxy.js b/test/proxy.js index e57babc8bc5..a2d0650b51d 100644 --- a/test/proxy.js +++ b/test/proxy.js @@ -4,7 +4,7 @@ const { tspl } = require('@matteo.collina/tspl') const { test } = require('node:test') const { Client, Pool } = require('..') const { createServer } = require('node:http') -const proxy = require('proxy') +const { createProxy } = require('proxy') test('connect through proxy', async (t) => { t = tspl(t, { plan: 3 }) @@ -50,8 +50,8 @@ test('connect through proxy with auth', async (t) => { const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - proxy.authenticate = function (req, fn) { - fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`) + proxy.authenticate = function (req, res) { + return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}` } server.on('request', (req, res) => { @@ -83,6 +83,40 @@ test('connect through proxy with auth', async (t) => { client.close() }) +test('connect through proxy with auth but invalid credentials', async (t) => { + t = tspl(t, { plan: 1 }) + + const server = await buildServer() + const proxy = await buildProxy() + + const serverUrl = `http://localhost:${server.address().port}` + const proxyUrl = `http://localhost:${proxy.address().port}` + + proxy.authenticate = function (req, res) { + return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:no-pass').toString('base64')}` + } + + server.on('request', (req, res) => { + t.fail('should not be called') + }) + + const client = new Client(proxyUrl) + + const response = await client.request({ + method: 'GET', + path: serverUrl + '/hello?foo=bar', + headers: { + 'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}` + } + }) + + t.strictEqual(response.statusCode, 407) + + server.close() + proxy.close() + client.close() +}) + test('connect through proxy (with pool)', async (t) => { t = tspl(t, { plan: 3 }) @@ -127,7 +161,7 @@ function buildServer () { function buildProxy () { return new Promise((resolve, reject) => { - const server = proxy(createServer()) + const server = createProxy(createServer()) server.listen(0, () => resolve(server)) }) } From 058e71952ebb17d9f8208d2beaa63e5e36c3564f Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 25 Feb 2024 13:31:23 +0100 Subject: [PATCH 3/6] fix proxy-agent --- test/proxy-agent.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/test/proxy-agent.js b/test/proxy-agent.js index 87129d01472..8f275c9505a 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -10,7 +10,7 @@ const ProxyAgent = require('../lib/proxy-agent') const Pool = require('../lib/pool') const { createServer } = require('node:http') const https = require('node:https') -const proxy = require('proxy') +const { createProxy } = require('proxy') test('should throw error when no uri is provided', (t) => { t = tspl(t, { plan: 2 }) @@ -84,12 +84,12 @@ test('use proxy agent to connect through proxy using Pool', async (t) => { if (++connectCount === 2) { t.ok(true, 'second connect should arrive while first is still inflight') resolveFirstConnect() - fn(null, true) + return true } else { await new Promise((resolve) => { resolveFirstConnect = resolve }) - fn(null, true) + return true } } @@ -161,9 +161,9 @@ test('use proxy-agent with auth', async (t) => { }) const parsedOrigin = new URL(serverUrl) - proxy.authenticate = function (req, fn) { + proxy.authenticate = function (req) { t.ok(true, 'authentication should be called') - fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`) + return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}` } proxy.on('connect', () => { t.ok(true, 'proxy should be called') @@ -205,9 +205,9 @@ test('use proxy-agent with token', async (t) => { }) const parsedOrigin = new URL(serverUrl) - proxy.authenticate = function (req, fn) { + proxy.authenticate = function (req) { t.ok(true, 'authentication should be called') - fn(null, req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}`) + return req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}` } proxy.on('connect', () => { t.ok(true, 'proxy should be called') @@ -418,7 +418,7 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async }) test('should throw when proxy does not return 200', async (t) => { - t = tspl(t, { plan: 2 }) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() @@ -426,8 +426,9 @@ test('should throw when proxy does not return 200', async (t) => { const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - proxy.authenticate = function (req, fn) { - fn(null, false) + proxy.authenticate = function (_req) { + t.ok(true, 'should call authenticate') + return false } const proxyAgent = new ProxyAgent(proxyUrl) @@ -446,15 +447,16 @@ test('should throw when proxy does not return 200', async (t) => { }) test('pass ProxyAgent proxy status code error when using fetch - #2161', async (t) => { - t = tspl(t, { plan: 1 }) + t = tspl(t, { plan: 2 }) const server = await buildServer() const proxy = await buildProxy() const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - proxy.authenticate = function (req, fn) { - fn(null, false) + proxy.authenticate = function (_req) { + t.ok(true, 'should call authenticate') + return false } const proxyAgent = new ProxyAgent(proxyUrl) @@ -700,8 +702,8 @@ function buildSSLServer () { function buildProxy (listener) { return new Promise((resolve) => { const server = listener - ? proxy(createServer(listener)) - : proxy(createServer()) + ? createProxy(createServer(listener)) + : createProxy(createServer()) server.listen(0, () => resolve(server)) }) } @@ -716,7 +718,7 @@ function buildSSLProxy () { } return new Promise((resolve) => { - const server = proxy(https.createServer(serverOptions)) + const server = createProxy(https.createServer(serverOptions)) server.listen(0, () => resolve(server)) }) } From f95b63f1edf55b322f0da01738e0282f86d72e3b Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 25 Feb 2024 13:34:22 +0100 Subject: [PATCH 4/6] fix docs --- docs/best-practices/proxy.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/best-practices/proxy.md b/docs/best-practices/proxy.md index bf102955cc8..60f3372cf75 100644 --- a/docs/best-practices/proxy.md +++ b/docs/best-practices/proxy.md @@ -17,7 +17,7 @@ If you proxy requires basic authentication, you can send it via the `proxy-autho ```js import { Client } from 'undici' import { createServer } from 'http' -import proxy from 'proxy' +import { createProxy } from 'proxy' const server = await buildServer() const proxyServer = await buildProxy() @@ -59,7 +59,7 @@ function buildServer () { function buildProxy () { return new Promise((resolve, reject) => { - const server = proxy(createServer()) + const server = createProxy(createServer()) server.listen(0, () => resolve(server)) }) } @@ -70,7 +70,7 @@ function buildProxy () { ```js import { Client } from 'undici' import { createServer } from 'http' -import proxy from 'proxy' +import { createProxy } from 'proxy' const server = await buildServer() const proxyServer = await buildProxy() @@ -78,8 +78,8 @@ const proxyServer = await buildProxy() const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxyServer.address().port}` -proxyServer.authenticate = function (req, fn) { - fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`) +proxyServer.authenticate = function (req) { + return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}` } server.on('request', (req, res) => { @@ -119,7 +119,7 @@ function buildServer () { function buildProxy () { return new Promise((resolve, reject) => { - const server = proxy(createServer()) + const server = createProxy(createServer()) server.listen(0, () => resolve(server)) }) } From 5fe088c9cab8572c7e3392cb0f2e66c6e6e5be0c Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 25 Feb 2024 13:36:46 +0100 Subject: [PATCH 5/6] fix --- test/proxy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/proxy.js b/test/proxy.js index a2d0650b51d..30d1290a6ff 100644 --- a/test/proxy.js +++ b/test/proxy.js @@ -50,7 +50,7 @@ test('connect through proxy with auth', async (t) => { const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - proxy.authenticate = function (req, res) { + proxy.authenticate = function (req) { return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}` } @@ -92,7 +92,7 @@ test('connect through proxy with auth but invalid credentials', async (t) => { const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - proxy.authenticate = function (req, res) { + proxy.authenticate = function (req) { return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:no-pass').toString('base64')}` } From 481b21a76728cbe3333da3c122404c2a8be1ef56 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 25 Feb 2024 13:41:17 +0100 Subject: [PATCH 6/6] fix --- test/proxy-agent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/proxy-agent.js b/test/proxy-agent.js index bd5a8c8912b..200f3066c6a 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -81,7 +81,7 @@ test('use proxy agent to connect through proxy using Pool', async (t) => { let resolveFirstConnect let connectCount = 0 - proxy.authenticate = async function (req, fn) { + proxy.authenticate = async function (req) { if (++connectCount === 2) { t.ok(true, 'second connect should arrive while first is still inflight') resolveFirstConnect() @@ -161,7 +161,7 @@ test('use proxy-agent to connect through proxy with basic auth in URL', async (t proxy.authenticate = function (req, fn) { t.ok(true, 'authentication should be called') - fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`) + return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}` } proxy.on('connect', () => { t.ok(true, 'proxy should be called')