From 818271c1c3b28464e10e8d87ddcb673b8bcb3e29 Mon Sep 17 00:00:00 2001
From: "Node.js GitHub Bot" <github-bot@iojs.org>
Date: Tue, 9 Aug 2022 18:55:40 -0400
Subject: [PATCH] deps: update undici to 5.8.2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

PR-URL: https://github.com/nodejs/node/pull/44187
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
---
 deps/undici/src/docs/api/MockAgent.md | 17 +++++++++++++++++
 deps/undici/src/index.js              |  7 ++++++-
 deps/undici/src/lib/core/request.js   |  3 ++-
 deps/undici/src/lib/core/util.js      | 17 ++++++++++++++---
 deps/undici/src/lib/fetch/webidl.js   | 15 +++++++++------
 deps/undici/src/package.json          |  2 +-
 deps/undici/undici.js                 | 22 +++++++++++++++-------
 7 files changed, 64 insertions(+), 19 deletions(-)

diff --git a/deps/undici/src/docs/api/MockAgent.md b/deps/undici/src/docs/api/MockAgent.md
index 5c8eda24937f..85ae69046e73 100644
--- a/deps/undici/src/docs/api/MockAgent.md
+++ b/deps/undici/src/docs/api/MockAgent.md
@@ -177,6 +177,23 @@ for await (const data of result2.body) {
   console.log('data', data.toString('utf8')) // data hello
 }
 ```
+#### Example - Mock different requests within the same file
+```js
+const { MockAgent, setGlobalDispatcher } = require('undici');
+const agent = new MockAgent();
+agent.disableNetConnect();
+setGlobalDispatcher(agent);
+describe('Test', () => {
+  it('200', async () => {
+    const mockAgent = agent.get('http://test.com');
+    // your test
+  });
+  it('200', async () => {
+    const mockAgent = agent.get('http://testing.com');
+    // your test
+  });
+});
+```
 
 #### Example - Mocked request with query body, headers and trailers
 
diff --git a/deps/undici/src/index.js b/deps/undici/src/index.js
index b2144c844ba7..8099f5a692f6 100644
--- a/deps/undici/src/index.js
+++ b/deps/undici/src/index.js
@@ -53,7 +53,12 @@ function makeDispatcher (fn) {
         throw new InvalidArgumentError('invalid opts.path')
       }
 
-      url = new URL(opts.path, util.parseOrigin(url))
+      let path = opts.path
+      if (!opts.path.startsWith('/')) {
+        path = `/${path}`
+      }
+
+      url = new URL(util.parseOrigin(url).origin + path)
     } else {
       if (!opts) {
         opts = typeof url === 'object' ? url : {}
diff --git a/deps/undici/src/lib/core/request.js b/deps/undici/src/lib/core/request.js
index 4dc2fcca0db6..b05a16d7a01d 100644
--- a/deps/undici/src/lib/core/request.js
+++ b/deps/undici/src/lib/core/request.js
@@ -297,7 +297,8 @@ function processHeader (request, key, val) {
   } else if (
     request.contentType === null &&
     key.length === 12 &&
-    key.toLowerCase() === 'content-type'
+    key.toLowerCase() === 'content-type' &&
+    headerCharRegex.exec(val) === null
   ) {
     request.contentType = val
     request.headers += `${key}: ${val}\r\n`
diff --git a/deps/undici/src/lib/core/util.js b/deps/undici/src/lib/core/util.js
index 635ef2e15f2a..5b0c5d1ef397 100644
--- a/deps/undici/src/lib/core/util.js
+++ b/deps/undici/src/lib/core/util.js
@@ -108,14 +108,25 @@ function parseURL (url) {
     const port = url.port != null
       ? url.port
       : (url.protocol === 'https:' ? 443 : 80)
-    const origin = url.origin != null
+    let origin = url.origin != null
       ? url.origin
       : `${url.protocol}//${url.hostname}:${port}`
-    const path = url.path != null
+    let path = url.path != null
       ? url.path
       : `${url.pathname || ''}${url.search || ''}`
 
-    url = new URL(path, origin)
+    if (origin.endsWith('/')) {
+      origin = origin.substring(0, origin.length - 1)
+    }
+
+    if (path && !path.startsWith('/')) {
+      path = `/${path}`
+    }
+    // new URL(path, origin) is unsafe when `path` contains an absolute URL
+    // From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
+    // If first parameter is a relative URL, second param is required, and will be used as the base URL.
+    // If first parameter is an absolute URL, a given second param will be ignored.
+    url = new URL(origin + path)
   }
 
   return url
diff --git a/deps/undici/src/lib/fetch/webidl.js b/deps/undici/src/lib/fetch/webidl.js
index 293199e8f9b4..430e657e275a 100644
--- a/deps/undici/src/lib/fetch/webidl.js
+++ b/deps/undici/src/lib/fetch/webidl.js
@@ -388,10 +388,6 @@ webidl.converters.DOMString = function (V, opts = {}) {
   return String(V)
 }
 
-// Check for 0 or more characters outside of the latin1 range.
-// eslint-disable-next-line no-control-regex
-const isLatin1 = /^[\u0000-\u00ff]{0,}$/
-
 // https://webidl.spec.whatwg.org/#es-ByteString
 webidl.converters.ByteString = function (V) {
   // 1. Let x be ? ToString(V).
@@ -400,8 +396,15 @@ webidl.converters.ByteString = function (V) {
 
   // 2. If the value of any element of x is greater than
   //    255, then throw a TypeError.
-  if (!isLatin1.test(x)) {
-    throw new TypeError('Argument is not a ByteString')
+  for (let index = 0; index < x.length; index++) {
+    const charCode = x.charCodeAt(index)
+
+    if (charCode > 255) {
+      throw new TypeError(
+        'Cannot convert argument to a ByteString because the character at' +
+        `index ${index} has a value of ${charCode} which is greater than 255.`
+      )
+    }
   }
 
   // 3. Return an IDL ByteString value whose length is the
diff --git a/deps/undici/src/package.json b/deps/undici/src/package.json
index 4ac0eb8863d1..0b9d4f713e4d 100644
--- a/deps/undici/src/package.json
+++ b/deps/undici/src/package.json
@@ -1,6 +1,6 @@
 {
   "name": "undici",
-  "version": "5.8.1",
+  "version": "5.8.2",
   "description": "An HTTP/1.1 client, written from scratch for Node.js",
   "homepage": "https://undici.nodejs.org",
   "bugs": {
diff --git a/deps/undici/undici.js b/deps/undici/undici.js
index 123b51b4d306..a4536d4a530e 100644
--- a/deps/undici/undici.js
+++ b/deps/undici/undici.js
@@ -737,9 +737,15 @@ var require_util = __commonJS({
       }
       if (!(url instanceof URL)) {
         const port = url.port != null ? url.port : url.protocol === "https:" ? 443 : 80;
-        const origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`;
-        const path = url.path != null ? url.path : `${url.pathname || ""}${url.search || ""}`;
-        url = new URL(path, origin);
+        let origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`;
+        let path = url.path != null ? url.path : `${url.pathname || ""}${url.search || ""}`;
+        if (origin.endsWith("/")) {
+          origin = origin.substring(0, origin.length - 1);
+        }
+        if (path && !path.startsWith("/")) {
+          path = `/${path}`;
+        }
+        url = new URL(origin + path);
       }
       return url;
     }
@@ -1281,11 +1287,13 @@ var require_webidl = __commonJS({
       }
       return String(V);
     };
-    var isLatin1 = /^[\u0000-\u00ff]{0,}$/;
     webidl.converters.ByteString = function(V) {
       const x = webidl.converters.DOMString(V);
-      if (!isLatin1.test(x)) {
-        throw new TypeError("Argument is not a ByteString");
+      for (let index = 0; index < x.length; index++) {
+        const charCode = x.charCodeAt(index);
+        if (charCode > 255) {
+          throw new TypeError(`Cannot convert argument to a ByteString because the character atindex ${index} has a value of ${charCode} which is greater than 255.`);
+        }
       }
       return x;
     };
@@ -2580,7 +2588,7 @@ var require_request = __commonJS({
         if (!Number.isFinite(request.contentLength)) {
           throw new InvalidArgumentError("invalid content-length header");
         }
-      } else if (request.contentType === null && key.length === 12 && key.toLowerCase() === "content-type") {
+      } else if (request.contentType === null && key.length === 12 && key.toLowerCase() === "content-type" && headerCharRegex.exec(val) === null) {
         request.contentType = val;
         request.headers += `${key}: ${val}\r
 `;
