Skip to content
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

fix(server): Listen only to 127.0.0.1 by default #2977

Merged
merged 12 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(cli): Tweak --host parameter: be even more explicit about defaults
  • Loading branch information
eirslett committed May 2, 2021
commit af7b24df893d176caa4f009cd9c43e7ab14a1a26
11 changes: 10 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ export async function preview(
)

const options = config.server || {}
const hostname = options.host || '127.0.0.1'
let hostname: string
if (options.host === undefined) {
eirslett marked this conversation as resolved.
Show resolved Hide resolved
// Use a secure default
hostname = '127.0.0.1'
} else if (options.host === true) {
// The user probably passed --host in the CLI, without arguments
hostname = '0.0.0.0'
} else {
hostname = options.host as string
}
Copy link
Member

@nihalgonsalves nihalgonsalves May 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is reused in both files, let's extract it to a function:

const getHostname(hostnameOption: string | boolean | undefined): string {
  if (typeof hostnameOption === 'string') {
    return hostnameOption
  }

  // `--host` without parameters
  if (hostnameOption === true) {
    return '0.0.0.0'
  }

  // secure loopback-only default
  return '127.0.0.1'
}

const protocol = options.https ? 'https' : 'http'
const logger = config.logger
const base = config.base
Expand Down
14 changes: 12 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { ssrRewriteStacktrace } from '../ssr/ssrStacktrace'
import { createMissingImporterRegisterFn } from '../optimizer/registerMissing'

export interface ServerOptions {
host?: string
host?: string | boolean
port?: number
/**
* Enable TLS + HTTP/2.
Expand Down Expand Up @@ -531,7 +531,17 @@ async function startServer(

const options = server.config.server || {}
let port = inlinePort || options.port || 3000
const hostname = options.host || '127.0.0.1'
let hostname: string
if (options.host === undefined) {
// Use a secure default
hostname = '127.0.0.1'
} else if (options.host === true) {
// probably passed --host in the CLI, without arguments
hostname = '0.0.0.0'
} else {
hostname = options.host as string
Comment on lines +541 to +542
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is this also the case if options.host is set to false?
Cause I see that host is of type string | boolean | undefined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patak-js as I mentioned here, what is with the options.host being false case?

image

The as string just hides the TypeScript safeness here ⚠️

}

const protocol = options.https ? 'https' : 'http'
const info = server.config.logger.info
const base = server.config.base
Expand Down