제어되지 않은 자원 소비

동시 작업과 자원 사용량을 제한하여 서비스 거부 방지

설명

요청이 소모하는 CPU, 메모리, 파일이나 연결 수에 한도가 없거나 사용 후 자원을 반환하지 않으면 다른 요청을 처리할 자원이 부족해질 수 있습니다.

잠재적 영향

  • 서비스 거부: 시스템이 속도가 느려지거나, 충돌하거나, 사용자 접근이 제한될 수 있습니다.
  • 리소스 오버로드: CPU, 메모리 등 제한된 자원 소비 증가로 시스템이 마비될 수 있습니다.

해결 방법

  • 동시에 실행할 작업 수와 작업 하나에 허용할 자원량을 정하세요. 한도를 넘는 요청은 지연시키거나 거절하세요.
  • 작업이 성공하거나 실패해도 사용한 파일과 데이터베이스·네트워크 연결을 닫고, 확보한 작업 슬롯을 반환하세요.

예시

Spring 컨트롤러에서 동시에 실행할 다운로드 수를 제한하는 예시입니다. 실제 전송은 try 안에서 끝나야 합니다. 전송 코드, 파일 경로 검증, 전송 크기·시간 제한, 필요한 import는 생략했습니다.

변경 전

java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileDownloadController {
    @GetMapping("/download")
    public void downloadFile(@RequestParam String fileName) {
        // 대규모 파일 다운로드 메소드를 호출하지만 모든 연결에 무제한 리소스를 사용
        File file = new File(fileName);
        // 실제 파일 다운로드 로직
    }
}

변경 후

java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileDownloadController {
    private static final int MAX_CONNECTIONS = 10; // 최대 연결 수를 제한
    private AtomicInteger activeConnections = new AtomicInteger(0);

    @GetMapping("/download")
    public ResponseEntity<String> downloadFile(@RequestParam String fileName) {
        if (activeConnections.incrementAndGet() > MAX_CONNECTIONS) {
            activeConnections.decrementAndGet();
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("Too Many Requests");
        }

        try {
            File file = new File(fileName);
            // 실제 파일 다운로드 로직
        } finally {
            activeConnections.decrementAndGet();
        }
        return ResponseEntity.ok("Download started");
    }
}

이 카운터는 컨트롤러 인스턴스 하나에서 동기 작업을 최대 10개까지 허용합니다. finally는 슬롯을 반환하며, 열린 파일이나 연결은 별도로 닫아야 합니다. 비동기 전송이나 여러 인스턴스가 공유하는 한도는 별도로 관리하세요.

관련 CVE

참조