-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace portfinder with custom implementation and fix security p…
…roblem (#4384)
- Loading branch information
1 parent
c9b6433
commit eea50f3
Showing
8 changed files
with
201 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
"use strict"; | ||
|
||
/* | ||
* Based on the packages get-port https://www.npmjs.com/package/get-port | ||
* and portfinder https://www.npmjs.com/package/portfinder | ||
* The code structure is similar to get-port, but it searches | ||
* ports deterministically like portfinder | ||
*/ | ||
const net = require("net"); | ||
const os = require("os"); | ||
|
||
const minPort = 1024; | ||
const maxPort = 65_535; | ||
|
||
/** | ||
* @return {Set<string|undefined>} | ||
*/ | ||
const getLocalHosts = () => { | ||
const interfaces = os.networkInterfaces(); | ||
|
||
// Add undefined value for createServer function to use default host, | ||
// and default IPv4 host in case createServer defaults to IPv6. | ||
// eslint-disable-next-line no-undefined | ||
const results = new Set([undefined, "0.0.0.0"]); | ||
|
||
for (const _interface of Object.values(interfaces)) { | ||
if (_interface) { | ||
for (const config of _interface) { | ||
results.add(config.address); | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
/** | ||
* @param {number} basePort | ||
* @param {string | undefined} host | ||
* @return {Promise<number>} | ||
*/ | ||
const checkAvailablePort = (basePort, host) => | ||
new Promise((resolve, reject) => { | ||
const server = net.createServer(); | ||
server.unref(); | ||
server.on("error", reject); | ||
|
||
server.listen(basePort, host, () => { | ||
// Next line should return AdressInfo because we're calling it after listen() and before close() | ||
const { port } = /** @type {import("net").AddressInfo} */ ( | ||
server.address() | ||
); | ||
server.close(() => { | ||
resolve(port); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* @param {number} port | ||
* @param {Set<string|undefined>} hosts | ||
* @return {Promise<number>} | ||
*/ | ||
const getAvailablePort = async (port, hosts) => { | ||
/** | ||
* Errors that mean that host is not available. | ||
* @type {Set<string | undefined>} | ||
*/ | ||
const nonExistentInterfaceErrors = new Set(["EADDRNOTAVAIL", "EINVAL"]); | ||
/* Check if the post is available on every local host name */ | ||
for (const host of hosts) { | ||
try { | ||
await checkAvailablePort(port, host); // eslint-disable-line no-await-in-loop | ||
} catch (error) { | ||
/* We throw an error only if the interface exists */ | ||
if ( | ||
!nonExistentInterfaceErrors.has( | ||
/** @type {NodeJS.ErrnoException} */ (error).code | ||
) | ||
) { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
return port; | ||
}; | ||
|
||
/** | ||
* @param {number} basePort | ||
* @return {Promise<number>} | ||
*/ | ||
async function getPorts(basePort) { | ||
if (basePort < minPort || basePort > maxPort) { | ||
throw new Error(`Port number must lie between ${minPort} and ${maxPort}`); | ||
} | ||
|
||
let port = basePort; | ||
const hosts = getLocalHosts(); | ||
/** @type {Set<string | undefined>} */ | ||
const portUnavailableErrors = new Set(["EADDRINUSE", "EACCES"]); | ||
while (port <= maxPort) { | ||
try { | ||
const availablePort = await getAvailablePort(port, hosts); // eslint-disable-line no-await-in-loop | ||
return availablePort; | ||
} catch (error) { | ||
/* Try next port if port is busy; throw for any other error */ | ||
if ( | ||
!portUnavailableErrors.has( | ||
/** @type {NodeJS.ErrnoException} */ (error).code | ||
) | ||
) { | ||
throw error; | ||
} | ||
port += 1; | ||
} | ||
} | ||
|
||
throw new Error("No available ports found"); | ||
} | ||
|
||
module.exports = getPorts; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
const net = require("net"); | ||
const util = require("util"); | ||
const getPort = require("../../lib/getPort"); | ||
|
||
it("it should bind to the preferred port", async () => { | ||
const preferredPort = 8080; | ||
const port = await getPort(8080); | ||
expect(port).toBe(preferredPort); | ||
}); | ||
|
||
it("should pick the next port if the preferred port is unavailable", async () => { | ||
const preferredPort = 8345; | ||
const server = net.createServer(); | ||
server.unref(); | ||
await util.promisify(server.listen.bind(server))(preferredPort); | ||
const port = await getPort(preferredPort); | ||
expect(port).toBe(preferredPort + 1); | ||
}); | ||
|
||
it("should reject privileged ports", async () => { | ||
try { | ||
await getPort(80); | ||
} catch (e) { | ||
expect(e.message).toBeDefined(); | ||
} | ||
}); | ||
|
||
it("should reject too high port numbers", async () => { | ||
try { | ||
await getPort(65536); | ||
} catch (e) { | ||
expect(e.message).toBeDefined(); | ||
} | ||
}); |
Oops, something went wrong.