Public Source Viewer
비나래아카이브 개발자 포털
실제 서비스 구조를 살펴볼 수 있는 공개용 코드 뷰어입니다. 인증, 세션, 외부 연동, 토큰, 관리자 식별 등 보안상 민감한 구현은 파일 단위 또는 줄 단위로 검열됩니다.
src/services/security-log.service.ts
공개 가능
1
import fs from 'fs';
2
import path from 'path';
3
import { randomUUID } from 'crypto';
4
import { Request } from 'express';
5
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
6
import { SecurityLogEntry } from '../types/models';
7
8
type SecurityLogInput = Pick<SecurityLogEntry, 'type' | 'action'> & {
9
actor?: string | null;
10
target?: string | null;
11
detail?: string;
12
path?: string;
13
};
14
15
const MAX_SECURITY_LOGS = 5000;
16
17
function readSecurityLogs(): SecurityLogEntry[] {
18
try {
19
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
20
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
21
if (!raw.trim()) return [];
22
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
23
return Array.isArray(logs) ? logs : [];
24
} catch (err) {
25
console.error('Error reading security log file:', err);
26
return [];
27
}
28
}
29
30
function writeSecurityLogs(logs: SecurityLogEntry[]): void {
31
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
32
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
33
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
34
[SECURITY REDACTED] 민감한 설정/인증/토큰 관련 코드입니다.
35
}
36
37
function trimValue(value: string | undefined, maxLength: number): string | undefined {
38
if (!value) return undefined;
39
return value.slice(0, maxLength);
40
}
41
42
export function getSecurityLogs(limit = 80): SecurityLogEntry[] {
43
return readSecurityLogs().slice(0, limit);
44
}
45
46
export function recordSecurityLog(req: Request, input: SecurityLogInput): void {
47
const actor = input.actor !== undefined ? input.actor : (req.session?.username || null);
48
const entry: SecurityLogEntry = {
49
id: randomUUID(),
50
type: input.type,
51
actor,
52
target: input.target ?? null,
53
action: input.action.slice(0, 120),
54
detail: trimValue(input.detail, 500),
55
ip: trimValue(req.ip, 80),
56
userAgent: trimValue(req.get('user-agent') || undefined, 240),
57
path: trimValue(input.path || req.originalUrl, 180),
58
createdAt: new Date().toISOString()
59
};
60
61
try {
62
writeSecurityLogs([entry, ...readSecurityLogs()]);
63
} catch (err) {
64
console.error('Error writing security log file:', err);
65
}
66
}
67