Skip to content

Wrong SockJS Host validation with Reverse Proxy #13627

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

Closed
mistic100 opened this issue Feb 8, 2019 · 4 comments · Fixed by #13838
Closed

Wrong SockJS Host validation with Reverse Proxy #13627

mistic100 opened this issue Feb 8, 2019 · 4 comments · Fixed by #13838
Milestone

Comments

@mistic100
Copy link

🐞 Bug report

Angular CLI version : 7.2.3

Command (mark with an x)

- [ ] new
- [ ] build
- [x] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Is this a regression?

I don't know

Description

When using "ng serve" behind a reverse proxy on a sub-path, the SockJS Server throws 400 errors because it fails to validate the Host/Origin header.

🔬 Minimal Reproduction

I want to have my application served on my workstation at http://dev.local/common-demo

  1. Create a new Angular CLI project
  2. Configure the serve task in angular.json with
            "host": "localhost",
            "port": 9038,
            "publicHost": "dev.local/common-demo/sockjs-node",
            "baseHref": "/common-demo/",
            "servePath": "/"
  1. Configure you workstation reverse proxy with (here Apache is used)
ProxyPass /common-demo http://localhost:9038
ProxyPassReverse /common-demo http://localhost:9038

🔥 Exception or Error

Chrome console shows :

WebSocket connection to 'ws://dev.local/common-demo/sockjs-node/287/kbmqffv3/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

Invalid Host/Origin header

[WDS] Disconnected!

Notes

If I set disableHostCheck to true, it works, but I don't want to.

If I set publicHost to dev.local the app tries to connect to ws://dev.local/sockjs-node which is not valid as it ignores the baseHref.

The servePath needs to be / as the app is served at the root of localhost:9038 (by default servePath inherits from baseHref but the sub-path is handled by the Reverse Proxy in my setup).

@ngbot ngbot bot added this to the needsTriage milestone Feb 8, 2019
@filipesilva filipesilva added type: bug/fix help wanted freq1: low Only reported by a handful of users who observe it rarely severity3: broken labels Feb 14, 2019
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Feb 14, 2019
@EdwinChua
Copy link

EdwinChua commented Feb 19, 2019

I also faced this problem. @mistic100 This might help:

Brief description of project setup:

  • Windows IIS to set up Reverse Proxy
  • Matches URL to rewrite to specific port on localhost
    • i.e. localhost:83/4200 redirects to localhost:4200

Command: ng serve --public-host=localhost:83

SockJs tries to connect via: http://localhost:83/sockjs-node/info?t=1550556432730

If I try the command: ng serve --public-host=localhost:83/4200
SockJs tries to connect via: http://localhost:83/4200/info?t=1550556778792

This worked! (Kinda)

Out of desperation, I also tried: ng serve --public-host=localhost:83/4200/sockjs-node, which yielded this error:

sockjs.js:1683 WebSocket connection to 'ws://localhost:83/4200/sockjs-node/642/4rpx323d/websocket' failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

After some searching, I stumbled across this issue. Turns out I had to manually enable the websocket protocol in IIS.

TLDR;

  1. Set the --public-host || --publicHost option
  2. Append the /sockjs-node to your desired URL
  • eg. ng serve --public-host=localhost:83/4200/sockjs-node

localhost can be replaced with an IP address*

* There seems to be a problem with change detection. Using this approach, the default page localhost:4200 reloads and displays the new updates, while the redirected one, localhost:83/4200 refreshes, but shows the old data. Am trying to figure out if this is an IIS issue or a browser caching one. IIS caches the files requested for about 30sec. Disabling it fixed the problem.

Question to the experts working on angular-cli:
Is 2. intended for applications not served at the root of the domain?

@clydin
Copy link
Member

clydin commented Feb 19, 2019

@EdwinChua No, you shouldn't have to do that. It appears that the Webpack-dev-server drops the sockjs-node segment of the path if a custom path is provided. The CLI should be able to fix up the path in this case.

@EdwinChua
Copy link

@clydin Is that a bug I should report then? Or should I be doing something differently?

clydin added a commit to clydin/angular-cli that referenced this issue Mar 6, 2019
…th public host option

If a custom path is provided the webpack dev server client drops the sockjs-node segment. This adds it back so that behavior is consistent when using a custom URL path.

Fixes angular#13627
clydin added a commit to clydin/angular-cli that referenced this issue Mar 7, 2019
…th public host option

If a custom path is provided the webpack dev server client drops the sockjs-node segment. This adds it back so that behavior is consistent when using a custom URL path.

Fixes angular#13627
clydin added a commit to clydin/angular-cli that referenced this issue Mar 29, 2019
…th public host option

If a custom path is provided the webpack dev server client drops the sockjs-node segment. This adds it back so that behavior is consistent when using a custom URL path.

Fixes angular#13627
mgechev pushed a commit that referenced this issue Mar 29, 2019
…th public host option

If a custom path is provided the webpack dev server client drops the sockjs-node segment. This adds it back so that behavior is consistent when using a custom URL path.

Fixes #13627
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants