How to fix Unrestricted Resource Consumption
in ServiceStack
Executive Summary
ServiceStack's default flexibility is a goldmine for resource exhaustion if you aren't enforcing strict boundaries. Without explicit constraints on request sizes, memory allocation, and execution timeouts, an attacker can trigger Out of Memory (OOM) conditions or CPU pegging with a single malformed payload. To harden the stack, you must move away from automatic DTO buffering and enforce global constraints.
The Vulnerable Pattern
[Route("/upload-logs", "POST")] public class UploadLogs : IReturn{ // VULNERABLE: Automatically buffers entire payload into a byte array in RAM public byte[] Data { get; set; } }
public class LogsService : Service { public object Any(UploadLogs request) { // If an attacker sends a 2GB payload, the CLR will likely throw OOM here File.WriteAllBytes(“/tmp/logs.dat”, request.Data); return true; } }
The Secure Implementation
To mitigate Unrestricted Resource Consumption, the fix implements three layers of defense: 1. Configuration Hardening: Setting 'MaxRequestSize' in the AppHost prevents the web server from accepting massive payloads before they reach your logic. 2. Memory Management: Removing large 'byte[]' or 'string' properties from DTOs prevents ServiceStack's deserializer from attempting to buffer the entire request into the Managed Heap. 3. Streaming: Utilizing 'IRequest.Files' and 'Stream.CopyTo' ensures that even if a file is 1MB, the application only uses a small buffer (typically 80KB) during the transfer, preventing RAM exhaustion.
public override void Configure(Container container) { // 1. Enforce Global Request Size Limits (e.g., 1MB) this.Config.MaxRequestSize = 1024 * 1024; }[Route(“/upload-logs”, “POST”)] public class UploadLogs : IReturn
{ } public class LogsService : Service { public object Any(UploadLogs request) { // 2. Use Stream-based processing to keep memory footprint constant var file = Request.Files.FirstOrDefault(); if (file == null || file.ContentLength > 1024 * 1024) throw HttpError.BadRequest(“Payload exceeds security policy.”);
using (var inputStream = file.InputStream) using (var outputStream = File.Create("/tmp/logs.dat")) { inputStream.CopyTo(outputStream); } return true; }
}
Your ServiceStack API
might be exposed to Unrestricted Resource Consumption
74% of ServiceStack apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.