Skip to content

Commit 25369df

Browse files
committed
feat(security): 添加 Helmet 安全 headers + gzip 响应压缩
1 parent 8def334 commit 25369df

6 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎.env.example‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ JWT_REFRESH_EXPIRES_IN=7d
3232
# 生产环境填写服务器公网地址,例如 https://oj.example.com
3333
BASE_URL=http://localhost:3000
3434

35+
# ---- CORS ----
36+
# 生产环境填写前端域名,例如 https://oj.example.com
37+
# 留空则默认允许所有来源(开发环境适用)
38+
CORS_ORIGIN=https://your-domain.com
39+
3540
# ---- Heng Judge Service ----
3641
HENG_BASE_URL=http://localhost:5000
3742
HENG_AK=your_access_key

‎docs/SECURITY.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,30 @@
194194
- 用户管理(create / import / delete):6 个
195195
- compete 游戏管理(create / delete):4 个
196196
- 需要登录的接口(提交/房间/status):3 个
197+
198+
---
199+
200+
## HTTP Security Headers (Helmet)
201+
202+
Production deployment includes Helmet middleware:
203+
- `X-Content-Type-Options: nosniff`
204+
- `X-Frame-Options: DENY`
205+
- `Strict-Transport-Security` (HSTS)
206+
- `X-XSS-Protection`
207+
208+
Configuration: `contentSecurityPolicy` and `crossOriginEmbedderPolicy` are disabled to support SPA and embedded media.
209+
210+
## Response Compression
211+
212+
gzip compression enabled via `compression` middleware.
213+
Typical savings: 60-80% for JSON responses (leaderboard, problem list, etc.).
214+
215+
## CORS Configuration
216+
217+
Restrict allowed origins in production via the `CORS_ORIGIN` environment variable:
218+
219+
```env
220+
CORS_ORIGIN=https://your-domain.com
221+
```
222+
223+
If unset, defaults to `*` (all origins). Always set this in production.

‎src/main.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { ValidationPipe } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { NestFactory } from '@nestjs/core';
44
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
5+
import compression from 'compression';
6+
import helmet from 'helmet';
57
import { Logger } from 'nestjs-pino';
68
import { AppModule } from './app.module';
79
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
@@ -13,6 +15,23 @@ async function bootstrap() {
1315
// Use nestjs-pino as the logger
1416
app.useLogger(app.get(Logger));
1517

18+
// gzip compression for large JSON responses (leaderboard, problem list, etc.)
19+
app.use(compression());
20+
21+
// Helmet security headers
22+
app.use(
23+
helmet({
24+
contentSecurityPolicy: false, // SPA doesn't need strict CSP
25+
crossOriginEmbedderPolicy: false, // allow embedding images etc.
26+
}),
27+
);
28+
29+
// CORS — restrict origin in production via CORS_ORIGIN env variable
30+
app.enableCors({
31+
origin: process.env.CORS_ORIGIN || '*',
32+
credentials: true,
33+
});
34+
1635
// Global validation pipe
1736
app.useGlobalPipes(
1837
new ValidationPipe({

‎src/modules/contest/contest.service.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ describe('ContestService', () => {
217217

218218
describe('create', () => {
219219
it('创建竞赛(无题目)', async () => {
220-
const created = { id: 1, ...contestFixture };
220+
const created = { ...contestFixture };
221221
contestRepo.create.mockReturnValue(created);
222222
contestRepo.save.mockResolvedValue(created);
223223

‎src/modules/tag/dto/create-tag.dto.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export class CreateTagDto {
99
@ApiPropertyOptional({ description: '父标签 ID(用于层级结构)' })
1010
@IsOptional()
1111
@IsInt()
12-
parentId?: number;
12+
parentId?: number | null;
1313
}

‎src/modules/user/dto/create-user.dto.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ export class CreateUserDto {
7272
@IsOptional()
7373
@IsString()
7474
@Length(1, 32)
75-
certifiedName?: string;
75+
certifiedName?: string | null;
7676

7777
@ApiPropertyOptional({ description: '昵称' })
7878
@IsOptional()
7979
@IsString()
8080
@Length(1, 32)
81-
nickname?: string;
81+
nickname?: string | null;
8282

8383
@ApiPropertyOptional({
8484
description: '性别',

0 commit comments

Comments
 (0)