Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ export interface ServerConfig {
};
/**
* Configure CORS for the dev server or preview server.
* - true: enable CORS with default options.
* - false: disable CORS.
* - object: enable CORS with the specified options.
* - true: enable CORS with default options (allow all origins, not recommended).
* - false: disable CORS.
* @default false
* @link https://github.com/expressjs/cors
*/
Expand Down
23 changes: 17 additions & 6 deletions website/docs/en/config/server/cors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

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.

- `true`:Enable CORS with default options.
- `false`:Disable CORS.
- `object`:Enable CORS with the specified options.
- `true`:Enable CORS with default options (allow all origins, not recommended).
- `false`:Disable CORS.

:::tip
Although `cors` can be set to `true`, we recommend setting a specified `origin` option to prevent untrusted origins from accessing your dev server.
:::warning
Using `cors: true` or `cors.origin: '*'` exposes your dev server to all origins, potentially compromising your source code security. It is recommended to use [origin](#origin) option to specify an allowlist of trusted origins instead.
:::

## Example

- Enable CORS:
- Enable CORS for a specific origin:

```ts title="rsbuild.config.ts"
export default {
Expand All @@ -41,7 +41,7 @@ export default {
};
```

- Disable CORS (default behavior):
- Disable CORS:

```ts title="rsbuild.config.ts"
export default {
Expand All @@ -51,6 +51,17 @@ export default {
};
```

- Enable CORS for all origins (not recommended):

```ts title="rsbuild.config.ts"
export default {
server: {
// Equivalent to `{ origin: '*' }`
cors: true,
},
};
```

## Options

The `cors` option can be an object, which is the same as the [cors](https://github.com/expressjs/cors) middleware options.
Expand Down
23 changes: 17 additions & 6 deletions website/docs/zh/config/server/cors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

为开发服务器和预览服务器配置 [CORS](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS) 选项,基于 [cors](https://github.com/expressjs/cors) 中间件实现。

- `true`:启用 CORS 并使用默认选项。
- `false`:禁用 CORS。
- `object`:启用 CORS 并使用指定的选项。
- `true`:启用 CORS 并使用默认选项(允许所有 origin,不推荐)。
- `false`:禁用 CORS。

:::tip
虽然 `cors` 可以设置为 `true`,但我们建议设置一个指定的 `origin` 选项,以防止不受信任的 origin 访问你的开发服务器
:::warning
使用 `cors: true` 或 `cors.origin: '*'` 会使您的开发服务器对所有 origin 开放,这可能会危及源代码的安全性,建议使用 [origin](#origin) 选项指定一个受信任 origins 的白名单
:::

## 示例

- 启用 CORS:
- 为特定 origin 启用 CORS:

```ts title="rsbuild.config.ts"
export default {
Expand All @@ -41,7 +41,7 @@ export default {
};
```

禁用 CORS(默认行为)
- 禁用 CORS:

```ts title="rsbuild.config.ts"
export default {
Expand All @@ -51,6 +51,17 @@ export default {
};
```

- 为所有 origin 启用 CORS(不推荐):

```ts title="rsbuild.config.ts"
export default {
server: {
// 等价于 `{ origin: '*' }`
cors: true,
},
};
```

## 选项

`cors` 选项可以是一个对象,与 [cors](https://github.com/expressjs/cors) 中间件的选项相同。
Expand Down
Loading