# Add Input Validation to an aiohttp App

Yes. An agent validates path and form input in your aiohttp handlers so bad ids and missing fields answer 400 or 404 instead of a 500 page, with tests, in a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-input-validation/aiohttp/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads each handler and traces what happens to a bad value, because in a small aiohttp app the gaps are specific. In the polls app under `demos/polls` of aio-libs/aiohttp-demos, `routes.py` registers `GET /`, `GET /poll/{question_id}`, `GET /poll/{question_id}/results` and `POST /poll/{question_id}/vote`. The `poll` view in `views.py` passes `request.match_info["question_id"]` straight into `select(Question).where(Question.id == question_id)` as a string, so `/poll/abc` reaches asyncpg, which refuses it, and the error middleware in `middlewares.py` renders the 500 template. `results` and `vote` call `int()` on the same value, so `/poll/abc/results` raises `ValueError` and is a 500 too. `vote` reads `choice` from the form and answers `HTTPBadRequest` when it is missing or not an integer, which is right, but `db.vote` then does `result.votes += 1` before the `if not result` check, so an unknown choice id raises `AttributeError` on `None` and is a 500 rather than the `RecordNotFound` the code meant to raise, and nothing checks that the choice belongs to the question in the URL.

The agent fixes the routes first: `{question_id:\d+}` in each pattern, so a non-numeric id is a 404 from the router before any handler runs, and the three handlers convert with `int()` safely. It writes `aiohttpdemo_polls/validation.py` with a small form schema built on `trafaret`, which the app already installs through `trafaret_config` for `config/polls.yaml`, rather than adding pydantic: `choice` must be an integer, and a helper reads the form, applies the schema and raises `HTTPBadRequest` with a plain message on failure. `db.vote` gets the `None` check before the increment and a `where` clause that requires `Choice.question_id` to match, so a choice from another poll is a 404 with the same `RecordNotFound` path. The 400 is left as a plain text response, since `setup_middlewares` only overrides 404 and 500 and the agent changes no template.

Tests go in `tests/test_integration.py` beside the existing four, using the same `cli` and `tables_and_data` fixtures from `conftest.py`, which create a test database from `config/polls_test.yaml`: `/poll/abc` and `/poll/abc/results` answer 404, `/poll/999` answers 404 with the not-found template, a vote with no `choice` answers 400, a vote with `choice=abc` answers 400, a vote for a choice id that does not exist answers 404 rather than 500, and a vote for a choice of another question answers 404 and leaves the vote count unchanged. The existing `test_vote` still passes.

The agent runs the suite the way the repository's CI does: `make install` at the root, a Postgres it starts in its sandbox with the `postgres` superuser the fixtures connect as, then `pytest demos/polls` with the `pytest.ini` settings, which turn warnings into errors, and `flake8 demos` from the root `lint` target. It pastes the test and lint output into the pull request, with the status each request answered before and after the change.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add input validation to the polls app under demos/polls. Read aiohttpdemo_polls/routes.py, views.py, db.py, middlewares.py, utils.py, settings.py, templates/detail.html, tests/conftest.py, tests/test_integration.py, pytest.ini, requirements-dev.txt, config/polls_test.yaml and the root Makefile first. Constrain question_id to digits in every route so a non-numeric id is a 404 from the router, validate the vote form with a trafaret schema (trafaret is already installed through trafaret_config) so a missing or non-integer choice answers 400, fix db.vote so an unknown choice id raises RecordNotFound before the increment, and require the choice to belong to the question in the URL. Add tests beside the existing ones using the same fixtures for each of those cases, keep test_vote passing, run make install, start Postgres, run pytest demos/polls and flake8 demos in your sandbox, and open a pull request with the output and the before-and-after status codes. Change no template.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `aiohttpdemo_polls/validation.py` and changes `routes.py`, `views.py`, `db.py` and `tests/test_integration.py`. Its description shows the pytest output with the new tests passing under the repository's warnings-as-errors setting, the flake8 result, and a table of requests with the status each answered before and after: the non-numeric ids, the missing and malformed `choice`, the unknown choice id, and the choice from another question.

The templates, the error middleware, the configuration schema in `utils.py` and the index and results pages are unchanged. The 500 page now appears only for a real failure, and a probe or a crawler that hits `/poll/abc` no longer writes a traceback into the log through `request.protocol.logger.exception`.

Before merging, a reviewer decides whether the 400 should render a template like the 404 and 500 do, and whether the same `trafaret` helper should validate the other demos in the repository, which the agent leaves alone.

## Run it on every handler that reads request input

An automation in the Project with a `github` trigger on `pull_request.opened` starts a read-only run that reads the diff and comments when a new handler reads `match_info`, `request.post()` or `request.json()` without a check beside it, naming the status a bad value would produce today. The automation's prompt describes what a validated handler looks like in this app; Rush appends the pull request event after it, and the run can comment but not push.

A developer who wants the fix written rather than described sends this page's body with the pull request URL in `resources`, as `https://github.com/your-org/your-app/pull/<n>`, so the run works on that branch and pushes the change to it.

## Why run it on Rush?

Finding these gaps means reading how each value travels from the URL or form into the database layer and what each library does with a bad one: asyncpg refusing a string, `int()` raising, an attribute read on `None` before the check that should have caught it. The agent does that reading, then proves each case in its own sandbox with the real database the fixtures expect, so the pull request lists the status codes it saw rather than the ones it hopes for.

The run keeps the commands and the pytest output, including the first run that shows the 500s, so a reviewer can see each case was wrong before it was right. Several apps can be checked at once, each in its own sandbox with its own database.

## Frequently asked questions

### Why trafaret instead of pydantic or marshmallow?

The app already depends on `trafaret` through `trafaret_config`, which validates `config/polls.yaml` with the schema in `utils.py`. Using it for the vote form adds no package and keeps one validation style in the codebase. A team that already uses pydantic elsewhere would get that instead.

### Why put the digit check in the route rather than the handler?

aiohttp's router matches `{question_id:\d+}` before any handler runs, so a non-numeric id is a 404 from the framework with no code to write per view, and the three handlers can convert with `int()` knowing it cannot fail. The handlers still check that the id exists in the database, which is where the 404 for an unknown question comes from.

### Was the unknown-choice 500 a real bug?

Yes. `db.vote` increments `result.votes` before checking whether `result` is `None`, so the `RecordNotFound` branch below it could never run for a missing choice; the request died with `AttributeError` and the error middleware rendered the 500 page. The pull request moves the check above the increment and adds the test that would have caught it.

### Does the test suite need Postgres?

Yes. `conftest.py` creates a database from `config/polls_test.yaml` using the `postgres` superuser on localhost, the same way the repository's CI service does. The agent starts one in its sandbox before running `pytest demos/polls`, and `pytest.ini` turns warnings into errors, so the new tests pass under the same strictness as the old ones.

## More aiohttp jobs

- [Add OpenTelemetry Tracing to an aiohttp App](https://getrush.ai/run/add-opentelemetry/aiohttp/)
- [Add Pagination to an aiohttp App](https://getrush.ai/run/add-pagination/aiohttp/)
- [Add Prometheus Metrics to an aiohttp App](https://getrush.ai/run/add-prometheus-metrics/aiohttp/)

## The same job on other stacks

- [Add Request Validation to an API Gateway in AWS CDK](https://getrush.ai/run/add-input-validation/aws-cdk/)
- [Add Input Validation to an Axum API](https://getrush.ai/run/add-input-validation/axum/)
- [Validate Request Bodies in a Cloudflare Worker](https://getrush.ai/run/add-input-validation/cloudflare-workers/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
