1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
// app/api/rfq-attachments/download/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { readFile, access, constants, stat } from 'fs/promises';
import { join, normalize, resolve } from 'path';
import db from '@/db/db';
import { bRfqAttachmentRevisions, vendorResponseAttachmentsB } from '@/db/schema';
import { eq } from 'drizzle-orm';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { createFileDownloadLog } from '@/lib/file-download-log/service';
import rateLimit from '@/lib/rate-limit';
import { z } from 'zod';
import { getRequestInfo } from '@/lib/network/get-client-ip';
// 허용된 파일 확장자
const ALLOWED_EXTENSIONS = new Set([
'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx',
'txt', 'csv', 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'svg',
'dwg', 'dxf', 'zip', 'rar', '7z'
]);
// 최대 파일 크기 (50MB)
const MAX_FILE_SIZE = 50 * 1024 * 1024;
// 다운로드 요청 검증 스키마
const downloadRequestSchema = z.object({
path: z.string().min(1, 'File path is required'),
type: z.enum(['client', 'vendor']).optional(),
revisionId: z.string().optional(),
responseFileId: z.string().optional(),
});
// 파일 정보 타입
interface FileRecord {
id: number;
fileName: string;
originalFileName?: string;
filePath: string;
fileSize: number;
fileType?: string;
}
// 강화된 파일 경로 검증 함수
function validateFilePath(filePath: string): boolean {
// null, undefined, 빈 문자열 체크
if (!filePath || typeof filePath !== 'string') {
return false;
}
// 위험한 패턴 체크
const dangerousPatterns = [
/\.\./, // 상위 디렉토리 접근
/\/\//, // 이중 슬래시
/[<>:"'|?*]/, // 특수문자
/[\x00-\x1f]/, // 제어문자
/\\+/ // 백슬래시
];
if (dangerousPatterns.some(pattern => pattern.test(filePath))) {
return false;
}
// 시스템 파일 접근 방지
const dangerousPaths = ['/etc', '/proc', '/sys', '/var', '/usr', '/root', '/home'];
for (const dangerousPath of dangerousPaths) {
if (filePath.toLowerCase().startsWith(dangerousPath)) {
return false;
}
}
return true;
}
// 파일 확장자 검증
function validateFileExtension(fileName: string): boolean {
const extension = fileName.split('.').pop()?.toLowerCase() || '';
return ALLOWED_EXTENSIONS.has(extension);
}
// 안전한 파일명 생성
function sanitizeFileName(fileName: string): string {
return fileName
.replace(/[^\w\s.-]/g, '_') // 안전하지 않은 문자 제거
.replace(/\s+/g, '_') // 공백을 언더스코어로
.substring(0, 255); // 파일명 길이 제한
}
export async function GET(request: NextRequest) {
const startTime = Date.now();
const requestInfo = getRequestInfo(request);
let fileRecord: FileRecord | null = null;
try {
// Rate limiting 체크
const limiterResult = await rateLimit(request);
if (!limiterResult.success) {
console.warn('🚨 Rate limit 초과:', {
ip: requestInfo.ip,
userAgent: requestInfo.userAgent
});
return NextResponse.json(
{ error: "Too many requests" },
{ status: 429 }
);
}
// 세션 확인
const session = await getServerSession(authOptions);
if (!session?.user) {
console.warn('🚨 인증되지 않은 다운로드 시도:', {
ip: requestInfo.ip,
userAgent: requestInfo.userAgent,
path: request.nextUrl.searchParams.get("path")
});
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 401 }
);
}
// 파라미터 검증
const searchParams = {
path: request.nextUrl.searchParams.get("path"),
type: request.nextUrl.searchParams.get("type"),
revisionId: request.nextUrl.searchParams.get("revisionId"),
responseFileId: request.nextUrl.searchParams.get("responseFileId"),
};
const validatedParams = downloadRequestSchema.parse(searchParams);
const { path, type, revisionId, responseFileId } = validatedParams;
// 파일 경로 보안 검증
if (!validateFilePath(path)) {
console.warn(`🚨 의심스러운 파일 경로 접근 시도: ${path}`, {
userId: session.user.id,
ip: requestInfo.ip,
userAgent: requestInfo.userAgent
});
return NextResponse.json(
{ error: "Invalid file path" },
{ status: 400 }
);
}
// 경로 정규화
const normalizedPath = normalize(path.replace(/^\/+/, ""));
// DB에서 파일 정보 조회
let dbRecord: FileRecord | null = null;
if (type === "client" && revisionId) {
// 발주처 첨부파일 리비전
const [record] = await db
.select({
id: bRfqAttachmentRevisions.id,
fileName: bRfqAttachmentRevisions.fileName,
originalFileName: bRfqAttachmentRevisions.originalFileName,
filePath: bRfqAttachmentRevisions.filePath,
fileSize: bRfqAttachmentRevisions.fileSize,
fileType: bRfqAttachmentRevisions.fileType,
})
.from(bRfqAttachmentRevisions)
.where(eq(bRfqAttachmentRevisions.id, Number(revisionId)));
dbRecord = record;
} else if (type === "vendor" && responseFileId) {
// 벤더 응답 파일
const [record] = await db
.select({
id: vendorResponseAttachmentsB.id,
fileName: vendorResponseAttachmentsB.fileName,
originalFileName: vendorResponseAttachmentsB.originalFileName,
filePath: vendorResponseAttachmentsB.filePath,
fileSize: vendorResponseAttachmentsB.fileSize,
fileType: vendorResponseAttachmentsB.fileType,
})
.from(vendorResponseAttachmentsB)
.where(eq(vendorResponseAttachmentsB.id, Number(responseFileId)));
dbRecord = record;
} else {
// filePath로 직접 검색 (fallback) - 정규화된 경로로 검색
const [clientRecord] = await db
.select({
id: bRfqAttachmentRevisions.id,
fileName: bRfqAttachmentRevisions.fileName,
originalFileName: bRfqAttachmentRevisions.originalFileName,
filePath: bRfqAttachmentRevisions.filePath,
fileSize: bRfqAttachmentRevisions.fileSize,
fileType: bRfqAttachmentRevisions.fileType,
})
.from(bRfqAttachmentRevisions)
.where(eq(bRfqAttachmentRevisions.filePath, normalizedPath));
if (clientRecord) {
dbRecord = clientRecord;
} else {
// 벤더 파일에서도 검색
const [vendorRecord] = await db
.select({
id: vendorResponseAttachmentsB.id,
fileName: vendorResponseAttachmentsB.fileName,
originalFileName: vendorResponseAttachmentsB.originalFileName,
filePath: vendorResponseAttachmentsB.filePath,
fileSize: vendorResponseAttachmentsB.fileSize,
fileType: vendorResponseAttachmentsB.fileType,
})
.from(vendorResponseAttachmentsB)
.where(eq(vendorResponseAttachmentsB.filePath, normalizedPath));
dbRecord = vendorRecord;
}
}
// DB에서 파일 정보를 찾지 못한 경우
if (!dbRecord) {
console.warn("⚠️ DB에서 파일 정보를 찾지 못함:", {
path,
normalizedPath,
userId: session.user.id,
ip: requestInfo.ip
});
return NextResponse.json(
{ error: "File not found in database" },
{ status: 404 }
);
}
fileRecord = dbRecord;
// 파일명 설정
const fileName = dbRecord.originalFileName || dbRecord.fileName;
// 파일 확장자 검증
if (!validateFileExtension(fileName)) {
console.warn(`🚨 허용되지 않은 파일 타입 다운로드 시도: ${fileName}`, {
userId: session.user.id,
ip: requestInfo.ip
});
// 실패 로그 기록
await createFileDownloadLog({
fileId: dbRecord.id,
success: false,
errorMessage: 'File type not allowed',
requestId: requestInfo.requestId,
fileInfo: {
fileName,
filePath: path,
fileSize: 0,
}
});
return NextResponse.json(
{ error: "File type not allowed" },
{ status: 403 }
);
}
// 안전한 파일 경로 구성
const allowedDirs = ["public", "uploads", "storage"];
let actualPath: string | null = null;
let baseDir: string | null = null;
// 각 허용된 디렉터리에서 파일 찾기
for (const dir of allowedDirs) {
baseDir = resolve(process.cwd(), dir);
const testPath = resolve(baseDir, normalizedPath);
// 경로 탐색 공격 방지 - 허용된 디렉터리 외부 접근 차단
if (!testPath.startsWith(baseDir)) {
continue;
}
try {
await access(testPath, constants.R_OK);
actualPath = testPath;
console.log("✅ 파일 발견:", testPath);
break;
} catch (err) {
// 조용히 다음 디렉터리 시도
}
}
if (!actualPath || !baseDir) {
console.error("❌ 모든 경로에서 파일을 찾을 수 없음:", {
normalizedPath,
userId: session.user.id,
requestedPath: path
});
// 실패 로그 기록
await createFileDownloadLog({
fileId: dbRecord.id,
success: false,
errorMessage: 'File not found on server',
requestId: requestInfo.requestId,
fileInfo: {
fileName,
filePath: path,
fileSize: dbRecord.fileSize || 0,
}
});
return NextResponse.json(
{ error: "File not found on server" },
{ status: 404 }
);
}
// 파일 크기 확인
const stats = await stat(actualPath);
if (stats.size > MAX_FILE_SIZE) {
console.warn(`🚨 파일 크기 초과: ${fileName} (${stats.size} bytes)`, {
userId: session.user.id,
ip: requestInfo.ip
});
// 실패 로그 기록
await createFileDownloadLog({
fileId: dbRecord.id,
success: false,
errorMessage: 'File too large',
requestId: requestInfo.requestId,
fileInfo: {
fileName,
filePath: path,
fileSize: stats.size,
}
});
return NextResponse.json(
{ error: "File too large" },
{ status: 413 }
);
}
// 파일 읽기
const fileBuffer = await readFile(actualPath);
// MIME 타입 결정
const fileExtension = fileName.split('.').pop()?.toLowerCase() || '';
let contentType = dbRecord.fileType || 'application/octet-stream';
// 확장자에 따른 MIME 타입 매핑 (fallback)
if (!contentType || contentType === 'application/octet-stream') {
const mimeTypes: Record<string, string> = {
'pdf': 'application/pdf',
'doc': 'application/msword',
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xls': 'application/vnd.ms-excel',
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'ppt': 'application/vnd.ms-powerpoint',
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'txt': 'text/plain; charset=utf-8',
'csv': 'text/csv; charset=utf-8',
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'bmp': 'image/bmp',
'svg': 'image/svg+xml',
'dwg': 'application/acad',
'dxf': 'application/dxf',
'zip': 'application/zip',
'rar': 'application/x-rar-compressed',
'7z': 'application/x-7z-compressed',
};
contentType = mimeTypes[fileExtension] || 'application/octet-stream';
}
// 안전한 파일명 생성
const safeFileName = sanitizeFileName(fileName);
// 보안 헤더와 다운로드용 헤더 설정
const headers = new Headers();
headers.set('Content-Type', contentType);
headers.set('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(safeFileName)}`);
headers.set('Content-Length', fileBuffer.length.toString());
// 보안 헤더
headers.set('Cache-Control', 'no-cache, no-store, must-revalidate');
headers.set('Pragma', 'no-cache');
headers.set('Expires', '0');
headers.set('X-Content-Type-Options', 'nosniff');
headers.set('X-Frame-Options', 'DENY');
headers.set('X-XSS-Protection', '1; mode=block');
headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
// 성공 로그 기록
await createFileDownloadLog({
fileId: dbRecord.id,
success: true,
requestId: requestInfo.requestId,
fileInfo: {
fileName: safeFileName,
filePath: path,
fileSize: fileBuffer.length,
}
});
console.log("✅ 파일 다운로드 성공:", {
fileName: safeFileName,
contentType,
size: fileBuffer.length,
actualPath,
userId: session.user.id,
ip: requestInfo.ip,
downloadDurationMs: Date.now() - startTime
});
return new NextResponse(fileBuffer, {
status: 200,
headers,
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error('❌ RFQ 첨부파일 다운로드 오류:', {
error: errorMessage,
userId: (await getServerSession(authOptions))?.user?.id,
ip: requestInfo.ip,
path: request.nextUrl.searchParams.get("path"),
downloadDurationMs: Date.now() - startTime
});
// 에러 로그 기록
if (fileRecord?.id) {
try {
await createFileDownloadLog({
fileId: fileRecord.id,
success: false,
errorMessage,
requestId: requestInfo.requestId,
fileInfo: {
fileName: fileRecord.fileName || 'unknown',
filePath: request.nextUrl.searchParams.get("path") || '',
fileSize: fileRecord.fileSize || 0,
}
});
} catch (logError) {
console.error('로그 기록 실패:', logError);
}
}
// Zod 검증 에러 처리
if (error instanceof z.ZodError) {
return NextResponse.json(
{
error: 'Invalid request parameters',
details: error.errors.map(e => e.message).join(', ')
},
{ status: 400 }
);
}
// 에러 정보 최소화 (정보 노출 방지)
return NextResponse.json(
{
error: 'Internal server error',
details: process.env.NODE_ENV === 'development' ? errorMessage : undefined
},
{ status: 500 }
);
}
}
|