Api Scraping in Aspnet
How Api Scraping Manifests in Aspnet
API scraping is the automated harvesting of data from an API, often to build large datasets without permission. In ASP.NET, this threat arises when endpoints are overly permissive: missing authentication, lacking rate limits, or exposing too much data.
Common patterns in ASP.NET applications:
- No authentication: Controllers or actions without
[Authorize]are publicly accessible. Example: aCustomersControllerreturning all records lets anyone download the entire database. - No rate limiting: ASP.NET Core (pre-7) has no built-in throttling. Without custom middleware, attackers can flood endpoints to enumerate resources or paginate through results.
- Excessive data exposure: Returning entity models (e.g.,
IEnumerable<Customer>) often includes sensitive fields. OData endpoints with unrestricted$select/$expandenable bulk extraction. - Predictable IDs: Sequential numeric IDs in routes (e.g.,
/api/customers/1) allow enumeration when object-level authorization is missing (BOLA/IDOR). - Permissive CORS:
AllowAnyOriginlets malicious sites invoke the API from a user's browser, stealing authenticated data.
A vulnerable example:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IEnumerable<Product> Get() => _context.Products.ToList();
}
This returns all products without auth, pagination, or rate limiting. An attacker can scrape the entire catalog in one request if small, or paginate if large. Sensitive fields like CostPrice are exposed.
Aspnet-Specific Detection
Detecting API scraping in ASP.NET combines manual code review with dynamic scanning. Review for missing [Authorize], absent rate limiting, and overexposed DTOs. Dynamic testing reveals runtime issues.
middleBrick's black-box scan simulates an attacker and checks:
- Authentication bypass: Unauthenticated requests to endpoints; a 200 response with data indicates a critical flaw.
- Rate limiting absence: Sends a burst of requests (e.g., 100 in 10 seconds). If all succeed, no throttling exists.
- Data exposure: Analyzes responses for excessive data, full entity graphs, PII, or missing pagination. Tests OData
$expandfor related data. - OpenAPI spec mismatch: Resolves
$refand compares declared security with actual behavior. Discrepancies (e.g., spec says secure but data is public) are flagged.
Scan via middleBrick's web dashboard, CLI, or CI/CD integration. Submit the API URL or OpenAPI spec. Scans take 5–15 seconds, returning an A–F score with per-category breakdowns and ASP.NET-specific remediation steps.
Example CLI usage:
middlebrick scan https://api.example.com --output json
The JSON output pinpoints failing endpoints, easing mapping to code.
Aspnet-Specific Remediation
Remediate API scraping in ASP.NET using these built-in features:
1. Authentication: Apply [Authorize] to controllers/actions. Use [AllowAnonymous] only for safe endpoints.
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class OrdersController : ControllerBase { ... }
2. Rate limiting: In ASP.NET Core 7+, use AddRateLimiter. For older versions, use AspNetCoreRateLimit.
builder.Services.AddRateLimiter(options => { ... });
app.UseRateLimiter();
3. DTOs: Return data via DTOs that exclude sensitive fields. Never expose entity models directly.
public class CustomerDto { public int Id; public string Name; }
[HttpGet]
public IEnumerable<CustomerDto> Get() => _context.Customers.Select(c => new CustomerDto { Id = c.Id, Name = c.Name }).ToList();
4. Pagination: Implement skip/take or page/pageSize. Return metadata.
[HttpGet]
public PaginatedResult<CustomerDto> Get(int page, int pageSize) { var query = _context.Customers; var total = query.Count(); var items = query.Skip((page-1)*pageSize).Take(pageSize).Select(c => new CustomerDto { Id = c.Id, Name = c.Name }).ToList(); return new PaginatedResult<CustomerDto> { Items = items, Total = total, Page = page, PageSize = pageSize }; }
5. CORS: Restrict origins with AddCors. Avoid AllowAnyOrigin.
builder.Services.AddCors(o => o.AddPolicy("Secure", b => b.WithOrigins("https://app.example.com").AllowAnyMethod().AllowAnyHeader()));
app.UseCors("Secure");
6. OData: Limit query options and set MaxTop.
config.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
Finally, integrate middleBrick into CI/CD (e.g., GitHub Action) to fail builds on score drops.