angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Invalid Host header after updating to 1.0.1

Open SanderElias opened this issue 9 years ago • 49 comments

- [x ] bug report -> please search issues before submitting
- [ ] feature request

Versions.

1.0.1

Repro steps.

update to 1.0.1 ng serve browser will read: Invalid Host header, and do nothing

The log given by the failure.

none

Desired functionality.

serve my application.

Mention any other details that might be useful.

Downgrading to 1.0.0 fixed the issue. (BTW, running on ubuntu and debian, same issue)

SanderElias avatar Apr 25 '17 05:04 SanderElias

same here, runs on debian

MichalBu avatar Apr 25 '17 07:04 MichalBu

same here on OSX.

AlanCrevon avatar Apr 25 '17 07:04 AlanCrevon

Maybe it related to? https://github.com/webpack/webpack-dev-server/issues/882

kolkov avatar Apr 25 '17 11:04 kolkov

It's indeed due to a security fix added lately to webpack-dev-server. You must start with ng serve --host your.ip.addr.ess

amiram avatar Apr 25 '17 12:04 amiram

@amiram I had to add some trickery to my hosts file, and serve from a name, but it is indeed the curlpit. Closing as it's not a CLI issue!

SanderElias avatar Apr 25 '17 12:04 SanderElias

@SanderElias there is a new option in webpack disableHostCheck that will also solve it. Can I set it if I'm using angular cli?

amiram avatar Apr 25 '17 12:04 amiram

@amiram Nope, it's not exported to cmd-line yet afaik. Seems even impossible from webpack cmd-line itself for now.

SanderElias avatar Apr 25 '17 14:04 SanderElias

In the details of this Webpack security update, there's an important note:

The Host header of the request have to match the listening adress or the host provided in the public option.

It means what it means. If you were using just a webpack-dev-server and localhost hostname or 127.0.0.1 to develop/test an app in your browser, you should be fine.

But if you were using it behind a hostname (e.g. myapp.com) while the NG CLI or Webpack dev server served your app from the default address, and you proxied requests to it from, let's say, Nginx, then the whole HTTP header forwarding path must be matched. I left an example in the previously linked issue - https://github.com/webpack/webpack-dev-server/issues/882#issuecomment-297555924

flackjap avatar Apr 26 '17 22:04 flackjap

Seems like specifying --public <hostname> option for webpack-dev-server works for people behind proxies. Would it be possible to add it to ng serve?

dinvlad avatar Apr 27 '17 02:04 dinvlad

I have multitenant system .SO in 1.0.0 version when I was adding subdomain It was working. But now It is giving me Invalid host header. localhost(success) asd.localhost(Invalid host header) How to allow any subdomain?

vivekamin avatar Apr 28 '17 07:04 vivekamin

@vivekamin edit your hosts file is the easiest. The dev server is not meant to be used in production. This a secutiry measure taken by the webpack team. There is little no nothing the CLI team can do, execpt from exposing the switched spoken about above!

SanderElias avatar Apr 28 '17 07:04 SanderElias

@SanderElias which hosts file? Can you please elaborate. Is it nginx hosts files? How to do It in development environment ?

vivekamin avatar Apr 28 '17 07:04 vivekamin

/etc/hosts if you're on Linux. That's inconvenient though, if the host is always migrating, like in some cloud environments.. Besides, there could be DNS complications depending on the network setup.

Adding --public seems like a better option - no hosts need to be edited, and security is uncompromised.

dinvlad avatar Apr 28 '17 08:04 dinvlad

This is a problem for me too. I use two separate IDEs and applications, one for Angular and one for the web services. The Angular app will deploy to S3 and the services to Elastic Beanstalk, and I simulate this locally with myapp.lvh.me:4200 and myservices.lvh.me:4300 was working great until I upgraded to Angular 4 and latest angular-cli

AaronLangley avatar Apr 28 '17 08:04 AaronLangley

@AaronLangley Degrade to Angular-CLI 1.0.0 which has Angular V4, will solve problem. I am still figuring out the way for development purspose.

vivekamin avatar Apr 28 '17 08:04 vivekamin

@dinvlad I am developing multitenant Sys .So subdomain may change whatever. How can I put a regex for same in hosts files. I am confused.

vivekamin avatar Apr 28 '17 09:04 vivekamin

@vivekamin - maybe a child object/section in .angular-cli.json that maps to webpack.config.js ?

Was just looking at this SO post for reactjs http://stackoverflow.com/questions/43619644/i-am-getting-an-invalid-host-header-message-when-running-my-react-app-in-a-we

AaronLangley avatar Apr 28 '17 09:04 AaronLangley

I'm reopening this issue, because it's seems a major inconvienece to developers that the needed switches are not available in the CLI. To mitegate this issue, the ng server command needs to be able to port the --pubic <hostname> flag, and perhaps even the --disableHostCheck (although that last one really needs a severe warning, perhaps even a confirmation every time!)

SanderElias avatar Apr 28 '17 13:04 SanderElias

Until this gets resolved you can just change the following line in node_modules/webpack-dev-server/lib/Server.js (line 402):

if(this.disableHostCheck) return true;

change to

return true;

flackjap avatar Apr 28 '17 13:04 flackjap

Or you can just give disableHostCheck : false, in webpackDevServer config in serve.js file in tasks folder of cli. node_modules/@angular/cli

vivekamin avatar Apr 28 '17 13:04 vivekamin