Skip to content

Commit a41040e

Browse files
committed
fix #4110: support custom non-IP host values
1 parent dfe0e1c commit a41040e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252

5353
Some libraries have many files and also use the same legal comment text in all files. Previously esbuild would copy each legal comment to the output file. Starting with this release, legal comments duplicated across separate files will now be grouped in the output file by unique comment content.
5454

55+
* Allow a custom host with the development server ([#4110](https://github.com/evanw/esbuild/issues/4110))
56+
57+
With this release, you can now use a custom non-IP `host` with esbuild's local development server (either with `--serve=` for the CLI or with the `serve()` call for the API). This was previously possible, but was intentionally broken in [version 0.25.0](https://github.com/evanw/esbuild/releases/v0.25.0) to fix a security issue. This change adds the functionality back except that it's now opt-in and only for a single domain name that you provide.
58+
59+
For example, if you add a mapping in your `/etc/hosts` file from `local.example.com` to `127.0.0.1` and then use `esbuild --serve=local.example.com:8000`, you will now be able to visit http://local.example.com:8000/ in your browser and successfully connect to esbuild's development server (doing that would previously have been blocked by the browser). This should also work with HTTPS if it's enabled (see esbuild's documentation for how to do that).
60+
5561
* Add a limit to CSS nesting expansion ([#4114](https://github.com/evanw/esbuild/issues/4114))
5662

5763
With this release, esbuild will now fail with an error if there is too much CSS nesting expansion. This can happen when nested CSS is converted to CSS without nesting for older browsers as expanding CSS nesting is inherently exponential due to the resulting combinatorial explosion. The expansion limit is currently hard-coded and cannot be changed, but is extremely unlikely to trigger for real code. It exists to prevent esbuild from using too much time and/or memory. Here's an example:

pkg/api/serve_other.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,20 @@ func (ctx *internalContext) Serve(serveOptions ServeOptions) (ServeResult, error
774774
var listener net.Listener
775775
network := "tcp4"
776776
host := "0.0.0.0"
777+
hostIsIP := true
777778
if serveOptions.Host != "" {
778779
host = serveOptions.Host
780+
ip := net.ParseIP(host)
779781

780782
// Only use "tcp4" if this is an IPv4 address, otherwise use "tcp"
781-
if ip := net.ParseIP(host); ip == nil || ip.To4() == nil {
783+
if ip == nil || ip.To4() == nil {
782784
network = "tcp"
783785
}
786+
787+
// Remember whether the host is a valid IP address or not
788+
if ip == nil {
789+
hostIsIP = false
790+
}
784791
}
785792

786793
// Pick the port
@@ -833,6 +840,14 @@ func (ctx *internalContext) Serve(serveOptions ServeOptions) (ServeResult, error
833840
result.Hosts = append(result.Hosts, boundHost)
834841
}
835842

843+
// If the host isn't a valid IP address, add it to the list of allowed hosts.
844+
// For example, mapping "local.example.com" to "127.0.0.1" in "/etc/hosts"
845+
// and then using "--serve=local.example.com:8000" should make it possible to
846+
// successfully visit "http://local.example.com:8000/" in a browser.
847+
if !hostIsIP {
848+
result.Hosts = append(result.Hosts, host)
849+
}
850+
836851
// HTTPS-related files should be absolute paths
837852
isHTTPS := serveOptions.Keyfile != "" && serveOptions.Certfile != ""
838853
if isHTTPS {

0 commit comments

Comments
 (0)