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 = 1000;
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
}
34
35
function trimValue(value: string | undefined, maxLength: number): string | undefined {
36
if (!value) return undefined;
37
return value.slice(0, maxLength);
38
}
39
40
export function getSecurityLogs(limit = 80): SecurityLogEntry[] {
41
return readSecurityLogs().slice(0, limit);
42
}
43
44
export function recordSecurityLog(req: Request, input: SecurityLogInput): void {
45
const actor = input.actor !== undefined ? input.actor : (req.session?.username || null);
46
const entry: SecurityLogEntry = {
47
id: randomUUID(),
48
type: input.type,
49
actor,
50
target: input.target ?? null,
51
action: input.action.slice(0, 120),
52
detail: trimValue(input.detail, 500),
53
ip: trimValue(req.ip, 80),
54
userAgent: trimValue(req.get('user-agent') || undefined, 240),
55
path: trimValue(input.path || req.originalUrl, 180),
56
createdAt: new Date().toISOString()
57
};
58
59
try {
60
writeSecurityLogs([entry, ...readSecurityLogs()]);
61
} catch (err) {
62
console.error('Error writing security log file:', err);
63
}
64
}
65