From 6d56a110ca883f5e8dcbb10fada70d4858c918f4 Mon Sep 17 00:00:00 2001
From: Dmitry Gozman <dgozman@gmail.com>
Date: Mon, 8 Feb 2021 12:07:45 -0800
Subject: [PATCH] feat(proxy): throw when socks proxy is used with auth (#5358)

---
 src/server/browserContext.ts      |  4 ++++
 test/browsercontext-proxy.spec.ts | 16 ++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/src/server/browserContext.ts b/src/server/browserContext.ts
index 5d6201ea4a2c8..cbc03f4dabbc6 100644
--- a/src/server/browserContext.ts
+++ b/src/server/browserContext.ts
@@ -462,6 +462,10 @@ export function normalizeProxySettings(proxy: types.ProxySettings): types.ProxyS
   } catch (e) {
     url = new URL('http://' + server);
   }
+  if (url.protocol === 'socks4:' && (proxy.username || proxy.password))
+    throw new Error(`Socks4 proxy protocol does not support authentication`);
+  if (url.protocol === 'socks5:' && (proxy.username || proxy.password))
+    throw new Error(`Browser does not support socks5 proxy authentication`);
   server = url.protocol + '//' + url.host;
   if (bypass)
     bypass = bypass.split(',').map(t => t.trim()).join(',');
diff --git a/test/browsercontext-proxy.spec.ts b/test/browsercontext-proxy.spec.ts
index 8fe95a3c52608..ee968c1654567 100644
--- a/test/browsercontext-proxy.spec.ts
+++ b/test/browsercontext-proxy.spec.ts
@@ -103,6 +103,22 @@ it('should work with IP:PORT notion', async ({contextFactory, contextOptions, se
   await browser.close();
 });
 
+it('should throw for socks5 authentication', async ({contextFactory, contextOptions}) => {
+  const error = await contextFactory({
+    ...contextOptions,
+    proxy: { server: `socks5://localhost:1234`, username: 'user', password: 'secret' }
+  }).catch(e => e);
+  expect(error.message).toContain('Browser does not support socks5 proxy authentication');
+});
+
+it('should throw for socks4 authentication', async ({contextFactory, contextOptions}) => {
+  const error = await contextFactory({
+    ...contextOptions,
+    proxy: { server: `socks4://localhost:1234`, username: 'user', password: 'secret' }
+  }).catch(e => e);
+  expect(error.message).toContain('Socks4 proxy protocol does not support authentication');
+});
+
 it('should authenticate', async ({contextFactory, contextOptions, server}) => {
   server.setRoute('/target.html', async (req, res) => {
     const auth = req.headers['proxy-authorization'];