create-dp-koa 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/scripts/sync-template.mjs +21 -0
- package/template/src/app.ts +1 -2
- package/template/src/{framework/plugins → plugins}/registry.ts +2 -2
- package/template/src/plugins/weboffice/http/routes.ts +2 -2
- package/template/src/plugins/weboffice/index.ts +3 -3
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +3 -4
- package/template/src/utils/testDataInitializer.ts +1 -1
- package/template/tsconfig.json +6 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +0 -9
- package/template/src/annotations/decorators/DistributedTracing.ts +0 -9
- package/template/src/annotations/decorators/EnterprisePerformance.ts +0 -9
- package/template/src/annotations/decorators/PerformanceMonitor.ts +0 -32
- package/template/src/annotations/decorators/SecurityAudit.ts +0 -9
- package/template/src/annotations/index.ts +0 -50
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +0 -369
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +0 -288
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +0 -189
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +0 -101
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +0 -345
- package/template/src/annotations/processors/SwaggerProcessor.ts +0 -612
- package/template/src/annotations/processors/index.ts +0 -10
- package/template/src/examples/InterceptorExampleRunner.ts +0 -284
- package/template/src/examples/ServiceInterceptorExample.ts +0 -214
- package/template/src/examples/cacheExamples.ts +0 -155
- package/template/src/framework/decorator/controller.ts +0 -311
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +0 -100
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +0 -160
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +0 -45
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +0 -117
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +0 -95
- package/template/src/framework/decorator/processor/ProcessorManager.ts +0 -76
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +0 -126
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +0 -207
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +0 -99
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +0 -125
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +0 -128
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +0 -139
- package/template/src/framework/decorator/refactored/index.ts +0 -98
- package/template/src/framework/decorator/swagger.ts +0 -150
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +0 -375
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +0 -348
- package/template/src/framework/interceptors/index.ts +0 -19
- package/template/src/framework/plugins/types.ts +0 -15
- package/template/src/framework/types/ServiceResult.ts +0 -151
- package/template/src/framework/types/index.ts +0 -16
- package/template/src/framework/utils/CacheManager.ts +0 -430
- package/template/src/framework/utils/CacheService.ts +0 -248
- package/template/src/framework/utils/DtoValidator.ts +0 -164
- package/template/src/framework/utils/MigrationHelper.ts +0 -179
- package/template/src/framework/utils/MigrationManager.ts +0 -256
- package/template/src/framework/utils/NewRouter.ts +0 -207
- package/template/src/framework/utils/TransactionManager.ts +0 -172
- package/template/src/framework/utils/bootstrap.ts +0 -445
- package/template/src/framework/utils/cache.ts +0 -269
- package/template/src/framework/utils/databaseConfig.ts +0 -148
- package/template/src/framework/utils/db.ts +0 -39
- package/template/src/framework/utils/dbMonitor.ts +0 -106
- package/template/src/framework/utils/function.ts +0 -61
- package/template/src/framework/utils/gracefulShutdown.ts +0 -131
- package/template/src/framework/utils/logger.ts +0 -388
- package/template/src/framework/utils/metrics.ts +0 -182
- package/template/src/framework/utils/router.ts +0 -417
- package/template/src/framework/utils/swagger.ts +0 -184
- package/template/src/framework/utils/testDb.ts +0 -19
- package/template/src/framework/utils/token.ts +0 -23
- package/template/src/framework/utils/transform.ts +0 -17
- package/template/src/libs/aokEmailSender.ts +0 -42
- package/template/src/libs/captcha.ts +0 -37
- package/template/src/libs/cos.ts +0 -45
- package/template/src/libs/mCache.ts +0 -7
- package/template/src/libs/serviceValidate.ts +0 -3
- package/template/src/libs/tecentSms.ts +0 -51
- package/template/src/middlewares/a.middleware.ts +0 -6
- package/template/src/middlewares/error.middleware.ts +0 -14
- package/template/src/middlewares/logging.middleware.ts +0 -187
- package/template/src/middlewares/static.middleware.ts +0 -79
- package/template/src/middlewares/swagger.middleware.ts +0 -70
- package/template/src/middlewares/token.middleware.ts +0 -32
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +0 -172
- package/template/src/migrations/index.ts +0 -6
- package/template/src/repository/base/BaseRepository.ts +0 -124
- package/template/src/repository/interfaces/IBaseRepository.ts +0 -67
- package/template/src/service/base.service.ts +0 -116
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 企业级安全审计注解处理器
|
|
3
|
-
* 提供安全事件记录、审计日志和合规性检查
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { Context } from 'koa';
|
|
7
|
-
import { AnnotationProcessor } from '@src/framework/decorator/processor/AnnotationProcessor';
|
|
8
|
-
import { logger } from '@src/framework/utils/logger';
|
|
9
|
-
|
|
10
|
-
export interface SecurityAuditConfig {
|
|
11
|
-
enableAudit?: boolean;
|
|
12
|
-
logLevel?: 'info' | 'warn' | 'error';
|
|
13
|
-
includeRequestData?: boolean;
|
|
14
|
-
includeResponseData?: boolean;
|
|
15
|
-
sensitiveFields?: string[];
|
|
16
|
-
complianceMode?: 'GDPR' | 'SOX' | 'HIPAA' | 'PCI-DSS';
|
|
17
|
-
retentionDays?: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface SecurityEvent {
|
|
21
|
-
timestamp: string;
|
|
22
|
-
userId?: string;
|
|
23
|
-
sessionId?: string;
|
|
24
|
-
ipAddress: string;
|
|
25
|
-
userAgent: string;
|
|
26
|
-
method: string;
|
|
27
|
-
url: string;
|
|
28
|
-
controller: string;
|
|
29
|
-
action: string;
|
|
30
|
-
requestData?: any;
|
|
31
|
-
responseData?: any;
|
|
32
|
-
securityLevel: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
|
|
33
|
-
eventType: 'AUTHENTICATION' | 'AUTHORIZATION' | 'DATA_ACCESS' | 'DATA_MODIFICATION' | 'SYSTEM_ACCESS';
|
|
34
|
-
riskScore: number;
|
|
35
|
-
complianceFlags: string[];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class SecurityAuditProcessor implements AnnotationProcessor {
|
|
39
|
-
readonly name = 'SecurityAudit';
|
|
40
|
-
readonly priority = 2; // 高优先级
|
|
41
|
-
|
|
42
|
-
private auditLog: SecurityEvent[] = [];
|
|
43
|
-
private complianceRules: Map<string, any> = new Map();
|
|
44
|
-
|
|
45
|
-
constructor() {
|
|
46
|
-
this.initializeComplianceRules();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async process(
|
|
50
|
-
ctx: Context,
|
|
51
|
-
controller: any,
|
|
52
|
-
methodName: string,
|
|
53
|
-
annotationData: SecurityAuditConfig,
|
|
54
|
-
callParams: any[]
|
|
55
|
-
): Promise<boolean> {
|
|
56
|
-
if (!annotationData?.enableAudit) return true;
|
|
57
|
-
|
|
58
|
-
const config = annotationData;
|
|
59
|
-
|
|
60
|
-
// 创建安全事件
|
|
61
|
-
const securityEvent: SecurityEvent = {
|
|
62
|
-
timestamp: new Date().toISOString(),
|
|
63
|
-
userId: ctx.state.user?.id,
|
|
64
|
-
sessionId: ctx.state.sessionId,
|
|
65
|
-
ipAddress: ctx.ip || ctx.request.ip,
|
|
66
|
-
userAgent: ctx.get('User-Agent') || '',
|
|
67
|
-
method: ctx.method,
|
|
68
|
-
url: ctx.url,
|
|
69
|
-
controller: controller.constructor.name,
|
|
70
|
-
action: methodName,
|
|
71
|
-
securityLevel: this.determineSecurityLevel(controller, methodName),
|
|
72
|
-
eventType: this.determineEventType(controller, methodName),
|
|
73
|
-
riskScore: this.calculateRiskScore(ctx, controller, methodName),
|
|
74
|
-
complianceFlags: this.checkCompliance(ctx, config)
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// 添加请求数据(如果配置允许)
|
|
78
|
-
if (config.includeRequestData) {
|
|
79
|
-
securityEvent.requestData = this.sanitizeData(ctx.request.body, config.sensitiveFields);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// 存储事件
|
|
83
|
-
this.auditLog.push(securityEvent);
|
|
84
|
-
|
|
85
|
-
// 记录审计日志
|
|
86
|
-
this.logSecurityEvent(securityEvent, config.logLevel || 'info');
|
|
87
|
-
|
|
88
|
-
// 检查高风险操作
|
|
89
|
-
if (securityEvent.riskScore > 7) {
|
|
90
|
-
this.handleHighRiskEvent(securityEvent);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async postProcess(
|
|
97
|
-
ctx: Context,
|
|
98
|
-
controller: any,
|
|
99
|
-
methodName: string,
|
|
100
|
-
response: any
|
|
101
|
-
): Promise<void> {
|
|
102
|
-
// 更新最后的安全事件
|
|
103
|
-
const lastEvent = this.auditLog[this.auditLog.length - 1];
|
|
104
|
-
if (lastEvent && lastEvent.controller === controller.constructor.name && lastEvent.action === methodName) {
|
|
105
|
-
lastEvent.responseData = this.sanitizeData(response, []);
|
|
106
|
-
|
|
107
|
-
// 检查响应中的敏感数据泄露
|
|
108
|
-
this.checkDataLeakage(lastEvent);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private determineSecurityLevel(controller: any, methodName: string): SecurityEvent['securityLevel'] {
|
|
113
|
-
const controllerName = controller.constructor.name.toLowerCase();
|
|
114
|
-
const method = methodName.toLowerCase();
|
|
115
|
-
|
|
116
|
-
// 高风险操作
|
|
117
|
-
if (method.includes('delete') || method.includes('remove') || method.includes('destroy')) {
|
|
118
|
-
return 'CRITICAL';
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 中等风险操作
|
|
122
|
-
if (method.includes('update') || method.includes('modify') || method.includes('change')) {
|
|
123
|
-
return 'HIGH';
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// 低风险操作
|
|
127
|
-
if (method.includes('get') || method.includes('list') || method.includes('find')) {
|
|
128
|
-
return 'LOW';
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return 'MEDIUM';
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
private determineEventType(controller: any, methodName: string): SecurityEvent['eventType'] {
|
|
135
|
-
const controllerName = controller.constructor.name.toLowerCase();
|
|
136
|
-
const method = methodName.toLowerCase();
|
|
137
|
-
|
|
138
|
-
if (controllerName.includes('auth') || controllerName.includes('login')) {
|
|
139
|
-
return 'AUTHENTICATION';
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (method.includes('create') || method.includes('update') || method.includes('delete')) {
|
|
143
|
-
return 'DATA_MODIFICATION';
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (method.includes('get') || method.includes('list') || method.includes('find')) {
|
|
147
|
-
return 'DATA_ACCESS';
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return 'SYSTEM_ACCESS';
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private calculateRiskScore(ctx: Context, controller: any, methodName: string): number {
|
|
154
|
-
let score = 0;
|
|
155
|
-
|
|
156
|
-
// 基于HTTP方法
|
|
157
|
-
switch (ctx.method) {
|
|
158
|
-
case 'DELETE': score += 4; break;
|
|
159
|
-
case 'PUT': score += 3; break;
|
|
160
|
-
case 'POST': score += 2; break;
|
|
161
|
-
case 'GET': score += 1; break;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// 基于用户权限
|
|
165
|
-
if (!ctx.state.user) score += 3;
|
|
166
|
-
if (!ctx.state.user?.isAdmin) score += 1;
|
|
167
|
-
|
|
168
|
-
// 基于IP地址
|
|
169
|
-
if (this.isSuspiciousIP(ctx.ip)) score += 2;
|
|
170
|
-
|
|
171
|
-
// 基于时间
|
|
172
|
-
if (this.isOffHours()) score += 1;
|
|
173
|
-
|
|
174
|
-
return Math.min(score, 10);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
private checkCompliance(ctx: Context, config: SecurityAuditConfig): string[] {
|
|
178
|
-
const flags: string[] = [];
|
|
179
|
-
|
|
180
|
-
if (config.complianceMode) {
|
|
181
|
-
const rules = this.complianceRules.get(config.complianceMode);
|
|
182
|
-
if (rules) {
|
|
183
|
-
// GDPR 检查
|
|
184
|
-
if (config.complianceMode === 'GDPR') {
|
|
185
|
-
if (this.containsPersonalData(ctx.request.body)) {
|
|
186
|
-
flags.push('GDPR_PERSONAL_DATA');
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// SOX 检查
|
|
191
|
-
if (config.complianceMode === 'SOX') {
|
|
192
|
-
if (this.isFinancialData(ctx.request.body)) {
|
|
193
|
-
flags.push('SOX_FINANCIAL_DATA');
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return flags;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
private sanitizeData(data: any, sensitiveFields: string[] = []): any {
|
|
203
|
-
if (!data || typeof data !== 'object') return data;
|
|
204
|
-
|
|
205
|
-
const sanitized = { ...data };
|
|
206
|
-
|
|
207
|
-
// 移除敏感字段
|
|
208
|
-
sensitiveFields.forEach(field => {
|
|
209
|
-
if (sanitized[field]) {
|
|
210
|
-
sanitized[field] = '[REDACTED]';
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// 移除密码字段
|
|
215
|
-
if (sanitized.password) {
|
|
216
|
-
sanitized.password = '[REDACTED]';
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return sanitized;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
private logSecurityEvent(event: SecurityEvent, level: string): void {
|
|
223
|
-
const logData = {
|
|
224
|
-
securityEvent: event,
|
|
225
|
-
message: `Security audit: ${event.eventType} - ${event.controller}.${event.action}`,
|
|
226
|
-
riskScore: event.riskScore,
|
|
227
|
-
complianceFlags: event.complianceFlags
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
switch (level) {
|
|
231
|
-
case 'error':
|
|
232
|
-
logger.error(`[SecurityAudit] ${logData.message}`, undefined, logData);
|
|
233
|
-
break;
|
|
234
|
-
case 'warn':
|
|
235
|
-
logger.warn(`[SecurityAudit] ${logData.message}`, logData);
|
|
236
|
-
break;
|
|
237
|
-
default:
|
|
238
|
-
logger.info(`[SecurityAudit] ${logData.message}`, logData);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private handleHighRiskEvent(event: SecurityEvent): void {
|
|
243
|
-
logger.error(`[SecurityAudit] 高风险安全事件`, undefined, {
|
|
244
|
-
event,
|
|
245
|
-
alert: 'HIGH_RISK_SECURITY_EVENT',
|
|
246
|
-
timestamp: new Date().toISOString()
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
// 这里可以集成外部安全系统
|
|
250
|
-
// 例如:发送到 SIEM 系统、触发告警等
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
private checkDataLeakage(event: SecurityEvent): void {
|
|
254
|
-
if (event.responseData && typeof event.responseData === 'object') {
|
|
255
|
-
const responseStr = JSON.stringify(event.responseData);
|
|
256
|
-
|
|
257
|
-
// 检查是否包含敏感信息
|
|
258
|
-
const sensitivePatterns = [
|
|
259
|
-
/password/i,
|
|
260
|
-
/ssn/i,
|
|
261
|
-
/credit.*card/i,
|
|
262
|
-
/social.*security/i
|
|
263
|
-
];
|
|
264
|
-
|
|
265
|
-
sensitivePatterns.forEach(pattern => {
|
|
266
|
-
if (pattern.test(responseStr)) {
|
|
267
|
-
logger.error(`[SecurityAudit] 潜在数据泄露`, undefined, {
|
|
268
|
-
event,
|
|
269
|
-
pattern: pattern.toString(),
|
|
270
|
-
alert: 'POTENTIAL_DATA_LEAKAGE'
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
private initializeComplianceRules(): void {
|
|
278
|
-
this.complianceRules.set('GDPR', {
|
|
279
|
-
personalDataFields: ['email', 'phone', 'address', 'name', 'ssn'],
|
|
280
|
-
retentionPeriod: 365
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
this.complianceRules.set('SOX', {
|
|
284
|
-
financialDataFields: ['amount', 'transaction', 'payment', 'invoice'],
|
|
285
|
-
auditTrail: true
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
this.complianceRules.set('HIPAA', {
|
|
289
|
-
healthDataFields: ['medical', 'health', 'diagnosis', 'treatment'],
|
|
290
|
-
encryptionRequired: true
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
private containsPersonalData(data: any): boolean {
|
|
295
|
-
const personalFields = ['email', 'phone', 'address', 'name', 'ssn'];
|
|
296
|
-
return personalFields.some(field =>
|
|
297
|
-
data && typeof data === 'object' && data[field]
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
private isFinancialData(data: any): boolean {
|
|
302
|
-
const financialFields = ['amount', 'transaction', 'payment', 'invoice'];
|
|
303
|
-
return financialFields.some(field =>
|
|
304
|
-
data && typeof data === 'object' && data[field]
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private isSuspiciousIP(ip: string): boolean {
|
|
309
|
-
// 简单的可疑IP检查
|
|
310
|
-
const suspiciousRanges = ['192.168.', '10.', '172.'];
|
|
311
|
-
return suspiciousRanges.some(range => ip.startsWith(range));
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
private isOffHours(): boolean {
|
|
315
|
-
const hour = new Date().getHours();
|
|
316
|
-
return hour < 6 || hour > 22;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* 获取审计日志
|
|
321
|
-
*/
|
|
322
|
-
getAuditLog(filter?: Partial<SecurityEvent>): SecurityEvent[] {
|
|
323
|
-
if (!filter) return [...this.auditLog];
|
|
324
|
-
|
|
325
|
-
return this.auditLog.filter(event => {
|
|
326
|
-
return Object.keys(filter).every(key =>
|
|
327
|
-
event[key as keyof SecurityEvent] === filter[key as keyof SecurityEvent]
|
|
328
|
-
);
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* 清理过期日志
|
|
334
|
-
*/
|
|
335
|
-
cleanupExpiredLogs(retentionDays: number = 90): void {
|
|
336
|
-
const cutoffDate = new Date();
|
|
337
|
-
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
|
|
338
|
-
|
|
339
|
-
this.auditLog = this.auditLog.filter(event =>
|
|
340
|
-
new Date(event.timestamp) > cutoffDate
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
logger.info(`[SecurityAudit] 清理过期日志完成,保留 ${this.auditLog.length} 条记录`);
|
|
344
|
-
}
|
|
345
|
-
}
|