mjackson / mach

HTTP for JavaScript
810 stars 41 forks source link

Server crashes if sent request without host header #74

Open appsforartists opened 8 years ago

appsforartists commented 8 years ago

If I send a request without a Host header attached, the whole server crashes.

var hostParts = host.split(":", 2);
                      ^
TypeError: Cannot read property 'split' of undefined
    at createLocation (node_modules/mach/lib/utils/createConnection.js:47:23)

It looks like I'm behind a proxy that probes the host with host-less requests before sending the actual request.

I don't know HTTP well enough to know what the behavior should be if Host is unspecified. Maybe it should serve from the default hostname or maybe it should respond with a blank page and/or error code, but it certainly shouldn't crash the server.

Here's the test I used to see what the client was sending:

var http = require("http");
var server = http.createServer(
  function (req, res) {
    console.log(JSON.stringify(req.headers));
    res.end();
  }
);
server.listen(8080);

and the result:

{
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…"
} 

{
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…"
} 

{
  "host": "x.x.x.x:8080",
  "scheme": "http",
  "cache-control": "max-age=0",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  "upgrade-insecure-requests": "1",
  "user-agent": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Mobile Safari/537.36",
  "accept-encoding": "gzip, deflate, sdch",
  "accept-language": "en-US,en;q=0.8",
  "cookie": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…",
  "connection": "Keep-Alive",
  "x-proxy-stuff": "…",
  "x-proxy-stuff": "…"
}