Skip to content

Commit 306528d

Browse files
authored
fix!: disable server.cors by default for security reasons (#4399)
1 parent fcf6f35 commit 306528d

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

e2e/cases/server/cors/index.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { build, dev } from '@e2e/helper';
22
import { expect, test } from '@playwright/test';
33

4-
test('should include CORS headers by default for dev server', async ({
4+
test('should include CORS headers for dev server if `cors` is `true`', async ({
55
page,
66
request,
77
}) => {
88
const rsbuild = await dev({
99
cwd: __dirname,
1010
page,
11+
rsbuildConfig: {
12+
server: {
13+
cors: true,
14+
},
15+
},
1116
});
1217

1318
const response = await request.get(`http://127.0.0.1:${rsbuild.port}`);
@@ -16,13 +21,18 @@ test('should include CORS headers by default for dev server', async ({
1621
await rsbuild.close();
1722
});
1823

19-
test('should include CORS headers by default for preview server', async ({
24+
test('should include CORS headers for preview server if `cors` is `true`', async ({
2025
page,
2126
request,
2227
}) => {
2328
const rsbuild = await build({
2429
cwd: __dirname,
2530
page,
31+
rsbuildConfig: {
32+
server: {
33+
cors: true,
34+
},
35+
},
2636
});
2737

2838
const response = await request.get(`http://127.0.0.1:${rsbuild.port}`);

packages/core/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const getDefaultServerConfig = (): NormalizedServerConfig => ({
8585
compress: true,
8686
printUrls: true,
8787
strictPort: false,
88-
cors: true,
88+
cors: false,
8989
});
9090

9191
let swcHelpersPath: string;

packages/core/src/types/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export interface ServerConfig {
394394
* - true: enable CORS with default options.
395395
* - false: disable CORS.
396396
* - object: enable CORS with the specified options.
397-
* @default true
397+
* @default false
398398
* @link https://github.com/expressjs/cors
399399
*/
400400
cors?: boolean | cors.CorsOptions;

packages/core/tests/__snapshots__/environments.test.ts.snap

+9-9
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ exports[`environment config > should normalize environment config correctly 1`]
112112
"server": {
113113
"base": "/",
114114
"compress": true,
115-
"cors": true,
115+
"cors": false,
116116
"host": "0.0.0.0",
117117
"htmlFallback": "index",
118118
"open": false,
@@ -254,7 +254,7 @@ exports[`environment config > should normalize environment config correctly 2`]
254254
"server": {
255255
"base": "/",
256256
"compress": true,
257-
"cors": true,
257+
"cors": false,
258258
"host": "0.0.0.0",
259259
"htmlFallback": "index",
260260
"open": false,
@@ -396,7 +396,7 @@ exports[`environment config > should print environment config when inspect confi
396396
"server": {
397397
"base": "/",
398398
"compress": true,
399-
"cors": true,
399+
"cors": false,
400400
"host": "0.0.0.0",
401401
"htmlFallback": "index",
402402
"open": false,
@@ -534,7 +534,7 @@ exports[`environment config > should print environment config when inspect confi
534534
"server": {
535535
"base": "/",
536536
"compress": true,
537-
"cors": true,
537+
"cors": false,
538538
"host": "0.0.0.0",
539539
"htmlFallback": "index",
540540
"open": false,
@@ -692,7 +692,7 @@ exports[`environment config > should support modify environment config by api.mo
692692
"server": {
693693
"base": "/",
694694
"compress": true,
695-
"cors": true,
695+
"cors": false,
696696
"host": "0.0.0.0",
697697
"htmlFallback": "index",
698698
"open": false,
@@ -831,7 +831,7 @@ exports[`environment config > should support modify environment config by api.mo
831831
"server": {
832832
"base": "/",
833833
"compress": true,
834-
"cors": true,
834+
"cors": false,
835835
"host": "0.0.0.0",
836836
"htmlFallback": "index",
837837
"open": false,
@@ -970,7 +970,7 @@ exports[`environment config > should support modify environment config by api.mo
970970
"server": {
971971
"base": "/",
972972
"compress": true,
973-
"cors": true,
973+
"cors": false,
974974
"host": "0.0.0.0",
975975
"htmlFallback": "index",
976976
"open": false,
@@ -1111,7 +1111,7 @@ exports[`environment config > should support modify single environment config by
11111111
"server": {
11121112
"base": "/",
11131113
"compress": true,
1114-
"cors": true,
1114+
"cors": false,
11151115
"host": "0.0.0.0",
11161116
"htmlFallback": "index",
11171117
"open": false,
@@ -1250,7 +1250,7 @@ exports[`environment config > should support modify single environment config by
12501250
"server": {
12511251
"base": "/",
12521252
"compress": true,
1253-
"cors": true,
1253+
"cors": false,
12541254
"host": "0.0.0.0",
12551255
"htmlFallback": "index",
12561256
"open": false,

website/docs/en/config/server/cors.mdx

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# server.cors
22

33
- **Type:** `boolean | import('cors').CorsOptions`
4-
- **Default:** `true`
4+
- **Default:** `false`
55
- **Version:** `>= 1.1.11`
66

77
Configure [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) options for the dev server or preview server, based on the [cors](https://github.com/expressjs/cors) middleware.
@@ -10,14 +10,25 @@ Configure [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) options
1010
- `false`:Disable CORS.
1111
- `object`:Enable CORS with the specified options.
1212

13+
:::tip
14+
Although `cors` can be set to `true`, we recommend setting a specified `origin` option to prevent untrusted origins from accessing your dev server.
15+
:::
16+
1317
## Example
1418

1519
Only enable CORS for the dev server:
1620

1721
```ts
22+
const isDev = process.env.NODE_ENV === 'development';
23+
1824
export default {
1925
server: {
20-
cors: process.env.NODE_ENV === 'development',
26+
cors: isDev
27+
? {
28+
// Configures the `Access-Control-Allow-Origin` CORS response header
29+
origin: 'https://example.com',
30+
}
31+
: false,
2132
},
2233
};
2334
```

website/docs/zh/config/server/cors.mdx

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# server.cors
22

33
- **类型:** `boolean | import('cors').CorsOptions`
4-
- **默认值:** `true`
4+
- **默认值:** `false`
55
- **版本:** `>= 1.1.11`
66

77
为开发服务器和预览服务器配置 [CORS](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS) 选项,基于 [cors](https://github.com/expressjs/cors) 中间件实现。
@@ -10,14 +10,25 @@
1010
- `false`:禁用 CORS。
1111
- `object`:启用 CORS 并使用指定的选项。
1212

13+
:::tip
14+
虽然 `cors` 可以设置为 `true`,但我们建议设置一个指定的 `origin` 选项,以防止不受信任的 origin 访问你的开发服务器。
15+
:::
16+
1317
## 示例
1418

1519
仅为开发服务器启用 CORS:
1620

1721
```ts
22+
const isDev = process.env.NODE_ENV === 'development';
23+
1824
export default {
1925
server: {
20-
cors: process.env.NODE_ENV === 'development',
26+
cors: isDev
27+
? {
28+
// 配置 `Access-Control-Allow-Origin` CORS 响应头
29+
origin: 'https://example.com',
30+
}
31+
: false,
2132
},
2233
};
2334
```

0 commit comments

Comments
 (0)