Concepts 🚀 Project 🧮 Labs
📚 Learn / Security / Input validation

Input validation

Never trust user input. Pydantic models enforce length, type, and regex at the boundary.

Code example

class NotebookInstallRequest(BaseModel):
    package: str = Field(
        ..., min_length=1, max_length=100,
        pattern=r'^[a-zA-Z0-9_\-\.\[\]>=<~ ;,]+$'
    )
    timeout_seconds: int = Field(default=180, ge=10, le=600)

How it works

Every byte of user input is potentially hostile. Pydantic models act as a bouncer at the API door: they reject anything that doesn't match the schema before the route handler ever sees it. The notebook install endpoint uses a regex pattern to allow only valid pip package specifiers — no shell metacharacters, no path traversal, no command injection. Length limits (max_length=100) prevent memory-exhaustion attacks. Numeric bounds (ge=10, le=600) prevent timeout=0 or timeout=999999.

🔍 Where this is used in OpenBenchML Every POST endpoint uses Pydantic models with explicit bounds. app/routes/notebook.py has the strictest schemas.