-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(server): fix header check for socket server #2077
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -677,25 +677,22 @@ class Server { | |
| const SocketServerImplementation = this.socketServerImplementation; | ||
| this.socketServer = new SocketServerImplementation(this); | ||
|
|
||
| this.socketServer.onConnection((connection) => { | ||
| this.socketServer.onConnection((connection, headers) => { | ||
| if (!connection) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| !this.checkHost(connection.headers) || | ||
| !this.checkOrigin(connection.headers) | ||
| ) { | ||
| if (headers && (!this.checkHost(headers) || !this.checkOrigin(headers))) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's too easy to accidentally skip the security check. Maybe changing it to
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra Good point. I think if users do not implement
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Loonride it should be no warning, we should drop connection if headers are not present
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @evilebottnawi If
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| this.sockWrite([connection], 'error', 'Invalid Host/Origin header'); | ||
|
|
||
| connection.close(); | ||
| this.socketServer.close(connection); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| this.sockets.push(connection); | ||
|
|
||
| connection.on('close', () => { | ||
| this.socketServer.onConnectionClose(connection, () => { | ||
| const idx = this.sockets.indexOf(connection); | ||
|
|
||
| if (idx >= 0) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,8 +57,14 @@ module.exports = class SockJSServer extends BaseServer { | |
| connection.close(); | ||
| } | ||
|
|
||
| // f should return the resulting connection | ||
| // f should return the resulting connection and, optionally, the connection headers | ||
| onConnection(f) { | ||
| this.socket.on('connection', f); | ||
| this.socket.on('connection', (connection) => { | ||
| f(connection, connection.headers); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use only
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @evilebottnawi Because |
||
| }); | ||
| } | ||
|
|
||
| onConnectionClose(connection, f) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @evilebottnawi As I say later, I want to not assume anything about the |
||
| connection.on('close', f); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`serverMode option with a bad host header results in an error 1`] = ` | ||
| Array [ | ||
| "open", | ||
| "{\\"type\\":\\"error\\",\\"data\\":\\"Invalid Host/Origin header\\"}", | ||
| "close", | ||
| ] | ||
| `; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
connection already has headers, i think we don't need extra argument