Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1033,16 +1033,17 @@ All error responses include standardized error information:

### Common HTTP Status Codes

| Status | Meaning |
| ------ | -------------------------------------- |
| 200 | Success |
| 400 | Bad request (validation error) |
| 401 | Unauthorized (missing/invalid API key) |
| 403 | Forbidden (insufficient permissions) |
| 404 | Not found |
| 429 | Too many requests (rate limited) |
| 500 | Internal server error |
| 503 | Service unavailable |
| Status | Meaning |
| ------ | ----------------------------------------------------------- |
| 200 | Success |
| 400 | Bad request (validation error) |
| 401 | Unauthorized (missing/invalid API key) |
| 403 | Forbidden (insufficient permissions) |
| 404 | Not found |
| 429 | Too many requests (rate limited) or scan capacity exhausted |
| 500 | Internal server error |
| 503 | Service unavailable |
| 504 | Scan exceeded `api.scan_timeout_seconds` (deadline) |

## Configuration

Expand All @@ -1063,6 +1064,11 @@ CIBERWEBSCAN_API_AUTH_SERVER_SECRET=""
CIBERWEBSCAN_API_RATE_LIMIT_ENABLED=true
CIBERWEBSCAN_API_RATE_LIMIT_REQUESTS_PER_MINUTE=60

# Scan concurrency (admission, deadline, threadpool sizing)
CIBERWEBSCAN_API_MAX_CONCURRENT_SCANS=32
CIBERWEBSCAN_API_SCAN_TIMEOUT_SECONDS=300
CIBERWEBSCAN_API_SYNC_THREADPOOL_TOKENS=40

# Client identification (reverse proxies allowed to set X-Forwarded-For)
CIBERWEBSCAN_API_TRUSTED_PROXIES="127.0.0.1"

Expand Down Expand Up @@ -1093,6 +1099,32 @@ The API includes rate limiting to prevent abuse:
- **Per-client counter**: each client identity has its own bucket (see
[Client identification](#client-identification-and-trusted-proxies) below)

### Scan concurrency

Scan endpoints (`POST /api/analyze`, `/api/attack`, `/api/scrape`,
`/api/scrape/batch`, `/api/quick`) are long, blocking operations. They run
under a separate, per-process admission control that is distinct from the
per-client rate limiter:

- **Admission limit**: at most `api.max_concurrent_scans` scans execute at
once per API process. Excess requests are rejected immediately with
**429** and a `Retry-After` header instead of queueing indefinitely. The
limit is per process — with `--workers N` the effective capacity is
`N × max_concurrent_scans`.
- **Deadline** (`api.scan_timeout_seconds`, default `null` = no deadline):
when set, the client is answered with **504** once the deadline elapses.
The blocking work cannot be cancelled mid-flight; it keeps running server
side until its own bounded end, and the admission slot stays held until
the worker thread truly finishes (capacity tracks real resource usage,
not the client's willingness to wait). Scans therefore never accumulate
unbounded behind a dead request.
- **Sizing**: scan work runs on a dedicated thread pool
(`api.sync_threadpool_tokens`) so scans cannot crowd out synchronous
dependencies (like auth) or streaming responses on the shared AnyIO
threadpool.

See [CONFIGURATION.md](CONFIGURATION.md) for the full option reference.

### Client identification and trusted proxies

Clients are identified by their **connection peer address**. The
Expand Down
51 changes: 49 additions & 2 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Examples that work out of the box:
- `CIBERWEBSCAN_ANALYSIS_CVE_NVD_API_KEY` → `analysis.cve.nvd_api_key`
- `CIBERWEBSCAN_CACHE_MAX_SIZE_MB` → `cache.max_size_mb`
- `CIBERWEBSCAN_API_RATE_LIMIT_REQUESTS_PER_MINUTE` → `api.rate_limit.requests_per_minute`
- `CIBERWEBSCAN_API_MAX_CONCURRENT_SCANS` → `api.max_concurrent_scans` (scan admission limit per process)
- `CIBERWEBSCAN_API_SCAN_TIMEOUT_SECONDS` → `api.scan_timeout_seconds` (scan deadline; must be >= 1 when set)
- `CIBERWEBSCAN_API_SYNC_THREADPOOL_TOKENS` → `api.sync_threadpool_tokens`
- `CIBERWEBSCAN_API_TRUSTED_PROXIES` → `api.trusted_proxies` (comma-separated IPs/CIDRs)
- `CIBERWEBSCAN_API_CORS_ORIGINS` → `api.cors_origins`
- `CIBERWEBSCAN_API_CORS_ALLOW_CREDENTIALS` → `api.cors_allow_credentials`
Expand Down Expand Up @@ -632,6 +635,9 @@ Configure the FastAPI server, authentication, and CORS settings.
"api": {
"host": "0.0.0.0",
"port": 8000,
"max_concurrent_scans": 32,
"scan_timeout_seconds": null,
"sync_threadpool_tokens": 40,
"auth": {
"api_keys": ["your-api-key-1", "your-api-key-2"]
},
Expand All @@ -655,6 +661,9 @@ Configure the FastAPI server, authentication, and CORS settings.
| ------------------------------------ | --------: | ------------------------------------------------ |
| `api.host` | `0.0.0.0` | API server bind address |
| `api.port` | `8000` | API server port (1-65535) |
| `api.max_concurrent_scans` | `32` | Max scans in flight per process (1-256) |
| `api.scan_timeout_seconds` | `null` | Scan deadline in seconds (1-3600, `null` = off) |
| `api.sync_threadpool_tokens` | `40` | Dedicated scan threadpool size (1-512) |
| `api.auth.api_keys` | `[]` | List of valid API keys for authentication |
| `api.auth.server_secret` | `""` | Secret for HMAC log obfuscation only (see below) |
| `api.rate_limit.enabled` | `true` | Rate limiting enabled by default |
Expand Down Expand Up @@ -737,6 +746,41 @@ api:

See also: [API.md — Authentication](API.md#authentication-1).

#### Scan concurrency

The scan endpoints (`/api/analyze`, `/api/attack`, `/api/scrape`,
`/api/scrape/batch`, `/api/quick`) run blocking, non-cancellable work. Three
fields control how much of it a single API process will accept:

- **`api.max_concurrent_scans`** (default `32`, range 1-256): admission limit
for in-flight scans. When the limit is reached, new scan requests are
rejected immediately with **429** and a `Retry-After` header instead of
queueing behind an unbounded backlog. The limit is **per process** — with
`--workers N` the effective capacity is `N × max_concurrent_scans`.
Changing it requires a restart (the controller is built at startup).
- **`api.scan_timeout_seconds`** (default `null` = no deadline, range
1-3600): when set, a scan that exceeds the deadline is answered with
**504** while the blocking work keeps running server side until its own
bounded end (it cannot be cancelled mid-flight). The admission slot is
held until the worker thread truly finishes, so abandoned scans cannot
accumulate and free capacity they still consume. Read per request —
changes apply without a restart.
- **`api.sync_threadpool_tokens`** (default `40`, range 1-512): size of the
dedicated thread pool for scan work, so scans cannot crowd out synchronous
dependencies (like authentication) or streaming responses on the shared
AnyIO threadpool. Applied at startup.

```yaml
api:
max_concurrent_scans: 32
scan_timeout_seconds: 300 # optional deadline; omit (null) to disable
sync_threadpool_tokens: 40
```

These limits are independent of the per-client HTTP rate limiter
(`api.rate_limit.*`): the rate limiter bounds request frequency per client,
while admission control bounds concurrent scan work per process.

#### CORS Security

CORS (Cross-Origin Resource Sharing) controls which domains can access your API from a browser. The defaults are **secure by default**:
Expand Down Expand Up @@ -1014,10 +1058,13 @@ configuration object in place, so every `get_config()` consumer sees them immedi
- **Applied at next use (no restart):** `api.auth.api_keys` (API authentication is checked per
request), `api.auth.server_secret` (log obfuscation only, read per request — the auto-generated
default stays stable until the process restarts), `api.trusted_proxies` (client identity for
rate limiting and auth logs is resolved per request), and `download.require_same_user` (checked on
rate limiting and auth logs is resolved per request), `api.scan_timeout_seconds` (scan deadline
is read per request), and `download.require_same_user` (checked on
every `GET /api/download/{token}`), HTTP client settings used when a client is created for a new
operation, and scraping/analysis/attack defaults read per scan.
- **Require a restart:** `api.cors.*`, `api.rate_limit.*`, `download.cleanup_interval_seconds`,
- **Require a restart:** `api.cors.*`, `api.rate_limit.*`, `api.max_concurrent_scans` and
`api.sync_threadpool_tokens` (the admission controller and the scan threadpool are sized at
startup), `download.cleanup_interval_seconds`,
`logging.*` handler configuration, and the API server `host`/`port` — these are captured once at
startup (app creation, scheduler start, logging setup, server launch).

Expand Down
13 changes: 12 additions & 1 deletion src/ciberwebscan/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

import anyio.to_thread
from fastapi import FastAPI, Request
from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from ciberwebscan import __description__, __version__
from ciberwebscan.api.concurrency import configure as configure_scan_concurrency
from ciberwebscan.api.middleware import (
add_rate_limiting_middleware,
add_request_logging_middleware,
Expand Down Expand Up @@ -44,8 +46,17 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""Handle application startup and shutdown."""
logger.info("Starting CiberWebScan API")

# Start cleanup scheduler
# Explicit sizing of the shared AnyIO threadpool (BUG-013): the
# framework default of 40 tokens becomes configuration, and the scan
# admission controller/limiter are built from the current config so
# the effective limits are fixed and logged at startup.
app_config = get_config()
anyio.to_thread.current_default_thread_limiter().total_tokens = (
app_config.api.sync_threadpool_tokens
)
configure_scan_concurrency()

# Start cleanup scheduler
if app_config.download.enabled:
scheduler = get_scheduler()
scheduler.start()
Expand Down
Loading
Loading