Uncontrolled resource consumption

Limit concurrent work and resource use to reduce denial-of-service risk

Description

Requests can exhaust CPU, memory, file descriptors, or connections if the application allows unlimited work or fails to release resources afterward.

Potential impact

  • Slow responses, crashes or unavailable service
  • Excessive CPU, memory or other resource use that prevents normal operation

Remediation

  • Set limits for concurrent work and for the resources a single operation can consume. Delay or reject requests that exceed those limits.
  • On success and failure, close files and database or network connections and release the slot reserved for the operation.

Examples

The Spring controller limits concurrent downloads. The transfer itself must finish inside try. The excerpts omit the transfer, file-path validation, size and time limits, and imports.

Before

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) {
        // Download work would run without a concurrency limit
        File file = new File(fileName);
        // Actual download logic omitted
    }
}

After

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; // Concurrent-work limit
    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);
            // Actual download logic omitted
        } finally {
            activeConnections.decrementAndGet();
        }
        return ResponseEntity.ok("Download started");
    }
}

The counter allows up to 10 synchronous operations in one controller instance. finally releases the slot. Open files and connections still need to be closed. Manage asynchronous transfers and limits shared across instances separately.

Related CVEs

References