From f94960f28db6740744a085c6f38b42f6a381d9dd Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 3 Aug 2023 20:54:58 +0800 Subject: [PATCH 1/5] fix: if host is specified check whether it is valid --- packages/vite/src/node/utils.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 27a619947c0b0d..65272d6075f2ff 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1,6 +1,7 @@ import fs from 'node:fs' import os from 'node:os' import path from 'node:path' +import net from 'node:net' import { exec } from 'node:child_process' import { createHash } from 'node:crypto' import { URL, URLSearchParams, fileURLToPath } from 'node:url' @@ -865,6 +866,12 @@ export async function resolveHostname( // If passed --host in the CLI without arguments host = undefined // undefined typically means 0.0.0.0 or :: (listen on all IPs) } else { + // check if the host is valid + if (!net.isIP(optionsHost)) { + throw new Error( + `The address passed to --host is not a valid IP address: ${optionsHost}`, + ) + } host = optionsHost } From c624cc094df8b32e8fa257b3dabeda682ee86900 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 3 Aug 2023 21:09:08 +0800 Subject: [PATCH 2/5] fix: update if statement --- packages/vite/src/node/utils.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 65272d6075f2ff..eed2a2b85a5372 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -867,7 +867,11 @@ export async function resolveHostname( host = undefined // undefined typically means 0.0.0.0 or :: (listen on all IPs) } else { // check if the host is valid - if (!net.isIP(optionsHost)) { + if ( + optionsHost !== 'localhost' && + !wildcardHosts.has(optionsHost) && + !net.isIP(optionsHost) + ) { throw new Error( `The address passed to --host is not a valid IP address: ${optionsHost}`, ) From 263c3b14fc0963c62a531b50a2b7aa9f66567890 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 15 Aug 2023 22:07:56 +0800 Subject: [PATCH 3/5] fix: add type parameter --- packages/vite/src/node/cli.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index df858f403f80c5..7e6e4a748d3867 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -102,6 +102,16 @@ function cleanOptions( return ret } +/*** + * host may be a number(like 0), should convert to string + */ +const convertHost = (v: any) => { + if (typeof v === 'number') { + return String(v) + } + return v +} + cli .option('-c, --config ', `[string] use specified config file`) .option('--base ', `[string] public base path (default: /)`) @@ -116,7 +126,7 @@ cli .command('[root]', 'start dev server') // default command .alias('serve') // the command is called 'serve' in Vite's API .alias('dev') // alias to align with the script name - .option('--host [host]', `[string] specify hostname`) + .option('--host [host]', `[string] specify hostname`, { type: [convertHost] }) .option('--port ', `[number] specify port`) .option('--https', `[boolean] use TLS + HTTP/2`) .option('--open [path]', `[boolean | string] open browser on startup`) @@ -306,7 +316,7 @@ cli // preview cli .command('preview [root]', 'locally preview production build') - .option('--host [host]', `[string] specify hostname`) + .option('--host [host]', `[string] specify hostname`, { type: [convertHost] }) .option('--port ', `[number] specify port`) .option('--strictPort', `[boolean] exit if specified port is already in use`) .option('--https', `[boolean] use TLS + HTTP/2`) From 081274a6a0b157b8ca90d7d542bb2a55d4c97cef Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 16 Aug 2023 11:16:16 +0800 Subject: [PATCH 4/5] chore: remove unnecessary if statement --- packages/vite/src/node/utils.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index eed2a2b85a5372..27a619947c0b0d 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1,7 +1,6 @@ import fs from 'node:fs' import os from 'node:os' import path from 'node:path' -import net from 'node:net' import { exec } from 'node:child_process' import { createHash } from 'node:crypto' import { URL, URLSearchParams, fileURLToPath } from 'node:url' @@ -866,16 +865,6 @@ export async function resolveHostname( // If passed --host in the CLI without arguments host = undefined // undefined typically means 0.0.0.0 or :: (listen on all IPs) } else { - // check if the host is valid - if ( - optionsHost !== 'localhost' && - !wildcardHosts.has(optionsHost) && - !net.isIP(optionsHost) - ) { - throw new Error( - `The address passed to --host is not a valid IP address: ${optionsHost}`, - ) - } host = optionsHost } From 0cbdf514de0f8a3326c551da3990f4b1be64a010 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 22 Aug 2023 15:52:47 +0800 Subject: [PATCH 5/5] chore: update comment --- packages/vite/src/node/cli.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index 7e6e4a748d3867..f6d3c9b6d6fd15 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -102,8 +102,8 @@ function cleanOptions( return ret } -/*** - * host may be a number(like 0), should convert to string +/** + * host may be a number (like 0), should convert to string */ const convertHost = (v: any) => { if (typeof v === 'number') {