FileDummy Logo
FileDummy
Bảo Mật & Toàn Vẹn Tệp Tin

Quét Virus Tệp Tải Lên: Tích Hợp ClamAV Antivirus Với Node.js

Tích hợp công cụ quét mã độc ClamAV vào pipeline upload file của Node.js. Bảo vệ ứng dụng khỏi các mã độc với ví dụ code clamscan thực tế.

20 tháng 9, 202611 phút đọc1,420 lượt xem
nodejssecurityclamavantivirusfile-upload

Permitting users to upload arbitrary documents and archives creates an acute security liability. ClamAV is an open-source antivirus engine capable of detecting viruses, trojans, malicious macros, and zip bombs in real-time.

ClamAV Deployment Architecture

The recommended production architecture runs clamd (the ClamAV daemon) as a standalone container or local Unix socket service, communicating with your Node.js application via TCP port 3310 or Unix IPC.

Code
[User Upload] --> [Node.js API Server] --(INSTREAM via TCP 3310)--> [ClamAV Daemon (clamd)]
                        |
                        +--> If Clean: Persist to S3/R2
                        +--> If Infected: Quarantine & Log Security Alert
Verified Test Asset.pdf

Download a verified clean 10MB PDF sample to test your ClamAV scanning daemon without false positives.

Download Clean 10MB PDF →

Integrating with Node.js via clamscan

Terminal
npm install clamscan
TypeScript
import NodeClam from 'clamscan';

let clamScanInstance: NodeClam | null = null;

async function getClamScanner(): Promise<NodeClam> {
  if (clamScanInstance) return clamScanInstance;

  clamScanInstance = await new NodeClam().init({
    clamdscan: {
      host: process.env.CLAMAV_HOST || '127.0.0.1',
      port: Number(process.env.CLAMAV_PORT) || 3310,
      timeout: 60000,
    },
    preference: 'clamdscan',
  });

  return clamScanInstance;
}

export async function scanUploadedBuffer(fileBuffer: Buffer): Promise<{ isInfected: boolean; viruses: string[] }> {
  const scanner = await getClamScanner();
  const { isInfected, viruses } = await scanner.scanBuffer(fileBuffer);

  if (isInfected) {
    console.warn('MALWARE DETECTED IN UPLOAD:', viruses);
  }

  return { isInfected, viruses };
}

Handling Scans Asynchronously

Scanning large files takes 1–5 seconds. For files over 50MB:

  1. Save the file with status PENDING_SCAN in object storage.
  2. Publish a scan event to a background job queue (e.g. BullMQ / AWS SQS).
  3. Update file status to ACTIVE or REJECTED once ClamAV completes its evaluation.
NDL

Nguyễn Đại Long

Tác Giả

Backend Lead • Chuyên gia Kiến trúc Hệ thống Phân tán & Lưu trữ Đám mây

Hơn 4 năm kinh nghiệm thiết kế các hệ thống xử lý tệp tải lên thông lượng lớn, tối ưu hóa cơ sở dữ liệu và hạ tầng phân tán Cloudflare R2 / AWS S3. Người sáng lập FileDummy và Mạng lưới Hệ sinh thái NDL.

Bài viết này có hữu ích không?

Bấm Thích để ủng hộ tác giả và giúp bài viết lan tỏa tới cộng đồng lập trình viên.

Thảo Luận Kỹ Thuật & Đóng Góp Ý Kiến (0)

Chia sẻ kết quả benchmark, phản hồi các trường hợp biên hoặc đặt câu hỏi chuyên môn.

0/3000

Chưa có bình luận nào. Hãy là lập trình viên đầu tiên bắt đầu cuộc thảo luận!

Câu Hỏi Thường Gặp (FAQ)

Is ClamAV good enough for production use?

ClamAV is adequate for common malware and macro-based threats. For enterprise-grade protection, combine with a commercial engine (VirusTotal API, OPSWAT) for multi-engine scanning. ClamAV's signature database updates multiple times daily via freshclam.

How slow does ClamAV make the upload process?

ClamAV scans at roughly 5–10MB per second per CPU core. A 10MB file takes 1–2 seconds. For large files, run the scan asynchronously and delay file access until the scan completes.

What is a zip bomb and does ClamAV detect it?

A zip bomb is a tiny archive that expands to a massive size (42KB → 4.5PB), designed to exhaust memory or disk. ClamAV detects known zip bomb patterns. Always set maxFileSize and maxScanSize limits in clamd.conf.

Bài Viết Liên Quan