# Add Input Validation to a Socket.IO Chat

Yes. An agent checks each event payload's type and size before it is stored or broadcast, refuses bad input through the acknowledgement, and opens a PR.

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

## What the agent does

The agent starts with what the server accepts and where it goes, because validation has to sit before the first side effect. In a chat laid out like socketio/chat-example, `index.js` forks one worker per CPU with `node:cluster`, opens `chat.db` through `sqlite`, creates a `messages` table with a `client_offset TEXT UNIQUE` column, and builds the `Server` with `connectionStateRecovery` on and the cluster adapter. The `chat message` handler takes `msg`, `clientOffset` and `callback`, inserts `msg` and `clientOffset` straight into the table, and on success broadcasts `msg` with the row id. Nothing checks that `msg` is a string, that it is non-empty, or how long it is; a `clientOffset` of any type and any length reaches the unique index. The `catch` around the insert handles the constraint error and ignores every other one with a comment, and the client in `index.html` sends with `ackTimeout` 10000 and `retries` 3, so a message the server never acknowledges is sent again.

The agent then writes the checks in the handler, ahead of the insert. `msg` must be a string, trimmed, non-empty and under a length cap held as one constant; `clientOffset` must be a string in the `socketid-counter` shape the client builds. A failing check calls `callback` with an error object and returns, which matters with this client: an unacknowledged bad message would be retried three times, while an acknowledged refusal stops at once. The empty `catch` branch becomes a `callback` with an error too, so a failed insert is no longer silent, and the stored `msg` is the trimmed string rather than the raw one.

The client gets the matching change. `index.html` adds `maxlength` on the input equal to the server's cap, so the common case never leaves the browser, and the `emit` passes a callback that reads the error object and shows it in the list instead of dropping it. The recovery query that replays rows after `serverOffset` is unchanged; it already reads only what the insert stored.

In its sandbox the agent runs `npm install` and starts the server with `PORT` set, then writes a short script with `socket.io-client` that connects, sends a valid message and asserts the broadcast, sends a number, an empty string and a string past the cap and asserts the error callback for each, and sends a duplicate `clientOffset` and asserts the existing idempotent acknowledgement still works. It adds that script as `npm test` with Node's built-in `node:test`, so the repository's one `start` script gains a second.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add input validation to this Socket.IO chat server. Read index.js, index.html and package.json first. In the chat message handler, before the insert, require msg to be a non-empty trimmed string under a length cap held as one constant and clientOffset to be a string in the socketid-counter form the client sends; on failure call the acknowledgement callback with an error object and return, since the client retries unacknowledged messages. Replace the empty catch with a callback error. Store the trimmed message. In index.html add maxlength matching the cap and an emit callback that shows the server's error. Do not change the table, the recovery query or the cluster setup. Run npm install, start the server, and add a node:test script using socket.io-client that proves a valid message broadcasts, a number, an empty string and an oversized string are refused through the callback, and a repeated clientOffset is still acknowledged. Wire it as npm test and open a pull request quoting its output.",
  "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

What comes back is a pull request that changes `index.js` (the checks, the constant, the callback errors), `index.html` (`maxlength` and the emit callback) and `package.json` (`socket.io-client` as a dev dependency and a `test` script), and adds one test file. The description quotes the `node --test` output with each case named: the valid broadcast, the three refusals and the duplicate offset.

The run's record keeps the server's startup lines from each cluster worker and the test's client log, so a reviewer can see which worker answered and that the refusals came back through the acknowledgement rather than as a dropped connection.

Before merging, a reviewer sets the length cap to what the chat should allow, decides whether a refused message should be logged on the server, and considers whether `clientOffset` should also be bounded in the database schema, which the pull request leaves alone.

## Run it on every Linear issue labelled abuse

A team running several realtime services makes this a rule. In the Project for each repository, an automation with the prompt above and a `linear` trigger on `linear.issue.labeled` for the label `abuse` starts a run when someone files "a client sent a two-megabyte message and the broadcast froze every tab". The trigger adds the issue after the prompt, so the run reads which event and which payload the issue names and writes the checks for that handler.

Each run opens its own pull request on its own branch and links the issue. A handler that already validates gets a description saying so and a pull request that only closes the gap the issue found, such as a missing type check on a second argument.

## Why run it on Rush?

The failure mode is quiet: a non-string `msg` goes into SQLite as whatever the driver makes of it, an oversized one is broadcast to every tab, and an unacknowledged refusal is retried three times by the client's own settings. Reading the client and the server together is what makes the fix right, and on a laptop that means two files, a running server and a hand-written client. In a Rush sandbox the agent starts the server, drives it with a real Socket.IO client and reads each acknowledgement.

The test output stays with the run and the pull request quotes it, so the reviewer sees the error object the server returned for the oversized message rather than a sentence saying it would. Across several services the job runs in parallel, one sandbox and one pull request each.

## Frequently asked questions

### Why answer through the callback instead of just dropping the message?

Because the client in `index.html` is built with `ackTimeout` and `retries`: a message with no acknowledgement is sent again up to three times. Calling the callback with an error stops the retries and lets the page show why.

### Does this change connection state recovery?

No. The `connectionStateRecovery` option and the replay query after `serverOffset` are untouched. Only what is allowed into the `messages` table changes, so a recovered client replays only messages that passed the checks.

### What about the duplicate clientOffset case?

It keeps working as before: a repeated offset hits the unique index, the constraint error is caught and the callback is called so the client stops retrying. The new test asserts it so the validation did not break the idempotency the table was designed for.

### Is maxlength on the input enough on its own?

No. It stops the common case in the browser, but any client can open a socket and send what it likes. The server check is the one that counts; the input attribute only saves a round trip.

## More Socket.IO jobs

- [Add JWT Authentication to a Socket.IO Server](https://getrush.ai/run/add-jwt-authentication/socket-io/)
- [Show Online Users in a Socket.IO Chat](https://getrush.ai/run/add-online-users/socket-io/)
- [Add Message History Pagination to a Socket.IO Chat](https://getrush.ai/run/add-pagination/socket-io/)

## The same job on other stacks

- [Add Request Validation to a Vapor API](https://getrush.ai/run/add-input-validation/vapor/)
- [Add Input Validation to an Actix Web App from a Linear Issue](https://getrush.ai/run/add-input-validation/actix/)
- [Add Input Validation to an aiohttp App](https://getrush.ai/run/add-input-validation/aiohttp/)

## 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/)
