Skip to content

fix(devtools-proxy-agent): properly forward errors for Socks5 requests #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2024
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
27 changes: 27 additions & 0 deletions packages/devtools-proxy-support/src/socks5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,33 @@ describe('createSocks5Tunnel', function () {
expect(await response.text()).to.equal('OK /hello');
});

it('properly handles forwarding failures', async function () {
tunnel = await setupSocks5Tunnel(
{
useEnvironmentVariableProxies: true,
env: {
MONGODB_PROXY: `http://foo:[email protected]:1`,
},
},
{},
'mongodb://'
);
if (!tunnel) {
// regular conditional instead of assertion so that TS can follow it
expect.fail('failed to create Socks5 tunnel');
}

try {
const fetch = createFetch({
proxy: `socks5://@127.0.0.1:${tunnel.config.proxyPort}`,
});
await fetch('http://example.com/hello');
expect.fail('missed exception');
} catch (err: any) {
expect(err.message).to.include('Socket closed');
}
});

context('with a non-HTTP target', function () {
let netServer: Server;
beforeEach(async function () {
Expand Down
33 changes: 23 additions & 10 deletions packages/devtools-proxy-support/src/socks5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,28 @@ export async function connectThroughAgent({
}): Promise<Duplex> {
const channel = await new Promise<Duplex | undefined>((resolve, reject) => {
const req = createFakeHttpClientRequest(dstAddr, dstPort, overrideProtocol);
req.onSocket = (sock) => {
if (sock) resolve(sock);
req.on('error', reject);
const done = (
error: Error | null | undefined,
sock: Duplex | undefined
) => {
req.off('error', reject);
if (error) reject(error);
else if (sock) resolve(sock);
else
reject(
new Error(
'Received neither error object nor socket from agent.createSocket()'
)
);
};

// err isn't actually optional but not part of the Node.js typings
req.onSocket = (
sock: Duplex | undefined,
err?: Error | null | undefined
) => {
done(err, sock);
};
agent.createSocket(
req,
Expand All @@ -107,14 +127,7 @@ export async function connectThroughAgent({
// Ideally, we would always be using this callback for retrieving the `sock`
// instance. However, agent-base does not call the callback at all if
// the agent resolved to another agent (as is the case for e.g. `ProxyAgent`).
if (err) reject(err);
else if (sock) resolve(sock);
else
reject(
new Error(
'Received neither error object nor socket from agent.createSocket()'
)
);
done(err, sock);
}
);
});
Expand Down
Loading