Skip to content

Commit 66e023a

Browse files
authored
Fix dev server rejecting non-localhost hosts when using FX_PROFILER_HOST (#5889)
1 parent e743aec commit 66e023a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ If you'd like to use [profiler.firefox.com](https://profiler.firefox.com) via UR
9696
FX_PROFILER_HOST="0.0.0.0" yarn start
9797
```
9898

99-
You'll probably also want to add your non-localhost domains to the `allowedHosts` property in `server.js`.
99+
When using `FX_PROFILER_HOST="0.0.0.0"`, any hostname is allowed so you can access the profiler from other devices on your network. If you want to expose only a specific hostname instead, set `FX_PROFILER_HOST` to that hostname directly and it will be added to the allowed hosts automatically.
100100

101101
## Finding something to work on
102102

scripts/lib/dev-server.mjs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ const EXTRA_HEADERS = {
2626
// Allowed hosts for dev server
2727
const ALLOWED_HOSTS = ['localhost', '.app.github.dev'];
2828

29-
function isHostAllowed(hostHeader) {
29+
function isHostAllowed(hostHeader, boundHost) {
3030
if (!hostHeader) {
3131
return false;
3232
}
3333

34-
// Extract hostname without port
34+
// When binding to all interfaces, allow any host.
35+
if (boundHost === '0.0.0.0' || boundHost === '::' || boundHost === '::0') {
36+
return true;
37+
}
38+
3539
const hostname = hostHeader.split(':')[0];
40+
const allowedHosts = [...ALLOWED_HOSTS, boundHost];
3641

37-
// Check exact match or suffix match for wildcard patterns
38-
return ALLOWED_HOSTS.some((allowedHost) => {
42+
return allowedHosts.some((allowedHost) => {
3943
if (allowedHost.startsWith('.')) {
4044
// Wildcard pattern like '.app.github.dev'
4145
return hostname.endsWith(allowedHost);
@@ -75,7 +79,7 @@ export async function startDevServer(buildConfig, options = {}) {
7579
// Create HTTP server
7680
const server = http.createServer((req, res) => {
7781
// Validate Host header
78-
if (!isHostAllowed(req.headers.host)) {
82+
if (!isHostAllowed(req.headers.host, host)) {
7983
res.writeHead(403, { 'Content-Type': 'text/plain' });
8084
res.end('Invalid Host header');
8185
return;
@@ -86,7 +90,10 @@ export async function startDevServer(buildConfig, options = {}) {
8690
port: esbuildServerPort,
8791
path: req.url,
8892
method: req.method,
89-
headers: req.headers,
93+
headers: {
94+
...req.headers,
95+
host: hostname + ':' + esbuildServerPort,
96+
},
9097
};
9198

9299
// Forward each incoming request to esbuild

0 commit comments

Comments
 (0)