Skip to content

Commit dffcb18

Browse files
chasenvdfdevdelucis
authored
chore(deps): Upgrading socket.io packages (#946)
BREAKING CHANGES: Previously the boardgame.io server accepted requests from any origin. After this change, you must set an `origins` argument when creating the server specifying the origins that are allowed to connect to your server. See the Server reference page for more details: https://boardgame.io/documentation/#/api/Server * upgrading socket.io server and client packages as well the koa-socket-2 package * Upgrades socketio types * Fix unit test * Adds origins param to the Server API and fix CORS issue * Use socket.io typing for origins server option * docs: Update origins documentation * feat: Log explanatory warning if origins option is undefined * docs: Fix link to MDN CORS documentation * chore(deps): deep update `ws` to fix ReDoS vulnerability * feat: Provide default origins configurations * docs: Document use of provided origins configurations * chore: Use origins config in examples server Co-authored-by: Vinny <[email protected]> Co-authored-by: vdf.dev <[email protected]> Co-authored-by: delucis <[email protected]>
1 parent dbba255 commit dffcb18

13 files changed

Lines changed: 415 additions & 271 deletions

File tree

docs/documentation/api/Server.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,45 @@ be configured to run on a separate port.
1616

1717
A config object with the following options:
1818

19-
1. `games` (_array_): a list of game implementations
19+
1. `games` (_array_) (required): a list of game implementations
2020
(each should be an object conforming to the [Game API](/api/Game.md)).
2121

22-
2. `db` (_object_): the [database connector](/storage).
22+
2. `origins` (_array_) (required): a list of allowed origins for
23+
[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS "Cross-Origin Resource Sharing").
24+
25+
The list can contain strings or regular expressions, matching the origins
26+
that are allowed to access the game server. For example, this could be
27+
`['https://example.com']` if that’s where your game is running. While
28+
developing locally you probably want to allow any page running on localhost
29+
to connect. boardgame.io provides default configurations to help with this:
30+
31+
```js
32+
const { Server, Origins } = require('boardgame.io/server');
33+
34+
Server({
35+
origins: [
36+
// Allow your game site to connect.
37+
'https://www.mygame.domain',
38+
// Allow localhost to connect, except when NODE_ENV is 'production'.
39+
Origins.LOCALHOST_IN_DEVELOPMENT
40+
],
41+
// ...
42+
});
43+
```
44+
45+
[cors]: https://github.com/expressjs/cors#configuration-options
46+
47+
3. `db` (_object_): the [database connector](/storage).
2348
If not provided, an in-memory implementation is used.
2449

25-
3. `transport` (_object_): the transport implementation.
50+
4. `transport` (_object_): the transport implementation.
2651
If not provided, socket.io is used.
2752

28-
4. `uuid` (_function_): an optional function that returns a unique identifier, used to create new game IDs and — if `generateCredentials` is not specified — player credentials. Defaults to [nanoid](https://www.npmjs.com/package/nanoid).
53+
5. `uuid` (_function_): an optional function that returns a unique identifier, used to create new game IDs and — if `generateCredentials` is not specified — player credentials. Defaults to [nanoid](https://www.npmjs.com/package/nanoid).
2954

30-
5. `generateCredentials` (_function_): an optional function that returns player credentials to store in the game metadata and validate against. If not specified, the `uuid` function will be used.
55+
6. `generateCredentials` (_function_): an optional function that returns player credentials to store in the game metadata and validate against. If not specified, the `uuid` function will be used.
3156

32-
6. `authenticateCredentials` (_function_): an optional function that tests if a player’s move is made with the correct credentials when using the default socket.io transport implementation.
57+
7. `authenticateCredentials` (_function_): an optional function that tests if a player’s move is made with the correct credentials when using the default socket.io transport implementation.
3358

3459
#### Returns
3560

docs/documentation/multiplayer.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,30 @@ a server at the location specified, which is discussed below.
208208
### Setting up the server
209209

210210
We’ll create a new file at `src/server.js` to write our server code.
211-
In order to run the game master on a Node server, we import the
212-
boardgame.io server module and provide it with our `TicTacToe` game object.
211+
212+
boardgame.io provides a server module that simplifies running the game
213+
master on a Node server. We import that module and configure it with our
214+
`TicTacToe` game object and a list of URL origins we want to allow to
215+
connect to the server. Later you would set `origins` with your game’s domain
216+
name, but for now we’ll import a default value that allows any locally served
217+
page to connect.
213218

214219
```js
215220
// src/server.js
216-
const { Server } = require('boardgame.io/server');
221+
const { Server, Origins } = require('boardgame.io/server');
217222
const { TicTacToe } = require('./Game');
218223
219-
const server = Server({ games: [TicTacToe] });
224+
const server = Server({
225+
games: [TicTacToe],
226+
origins: [Origins.LOCALHOST],
227+
});
220228
221229
server.run(8000);
222230
```
223231

232+
?> See [the Server reference page](api/Server.md) for more detail on
233+
the various configuration options.
234+
224235
Because `Game.js` is an ES module, we will use [esm](https://github.com/standard-things/esm)
225236
which enables us to use `import` statements in a Node environment:
226237

examples/react-web/server.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
* https://opensource.org/licenses/MIT.
77
*/
88

9-
import { Server } from 'boardgame.io/server';
9+
import { Server, Origins } from 'boardgame.io/server';
1010
import TicTacToe from './src/tic-tac-toe/game';
1111
import Chess from './src/chess/game';
1212

1313
const PORT = process.env.PORT || 8000;
14-
const server = Server({ games: [TicTacToe, Chess] });
14+
const server = Server({
15+
games: [TicTacToe, Chess],
16+
origins: [Origins.LOCALHOST],
17+
});
1518
server.run(PORT, () => {
16-
console.log(`Serving at: http://localhost:${PORT}/`);
19+
console.log(`Serving at: http://localhost:${PORT}`);
1720
});

0 commit comments

Comments
 (0)