All jobs

Add Input Validation to a Koa App

Yes. An agent validates the form fields and route parameters of your Koa app, answers 400 or 404 with the reason, adds a supertest case for each rule and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

In an app like the blog in koajs/examples, the agent reads blog/app.js: koa-logger, a render middleware from lib/render.js that maps .html views to the swig engine through koa-views, koa-body with its defaults, and four routes on @koa/router: GET / lists posts, GET /post/new shows the form, GET /post/:id shows one, POST /post creates one. The store is const posts = [] in memory. create takes ctx.request.body as it arrives, pushes it, sets created_at and id, and redirects; show does posts[ctx.params.id] and throws 404 only when the result is falsy. views/new.html posts a text input named title and a textarea named body; views/list.html prints posts.length. blog/test.js has three cases on mocha, should and supertest, and the root package.json runs every example's tests with mocha --harmony --reporter spec --require should */test.js and lints with ESLint 3 and eslint-config-koa; engines says Node 7.6 or later, .travis.yml is the only CI file, and there is no lockfile. The body-parsing example next door already shows the house style: if (!body.name) ctx.throw(400, '.name required').

Reading the handlers against the parser shows what gets through today. A form with an empty title or body creates a post that renders as a blank heading. A field sent twice, title=a&title=b, is parsed into an array, and a JSON body can make title an object; swig escapes the output, so neither is an injection, but both produce posts the list page cannot show sensibly. Any extra field in the body, id or created_at included, is stored as sent until the handler overwrites it. On the read side, ctx.params.id is a string indexed straight into the array: /post/length reads the array's length, a number, and /post/push reads a function, both truthy, so show renders a page with an empty title instead of a 404, while /post/-1 and /post/abc happen to 404 only because the lookup comes back empty.

The agent adds blog/lib/validate.js with no new dependency, because the repository's ESLint 3 configuration and Node floor predate the current releases of the usual schema libraries and the rules are few: validatePost(body) returns { value, errors }, where value holds only title and body as trimmed strings and errors names each field that is missing, not a string, or over a length the pull request proposes and a reviewer can change; parseId(param) accepts /^\d+$/ only and returns the number or nothing. In create, errors render views/new.html again with status 400, the submitted values and an errors list the template prints above the form, or { errors } as JSON when ctx.is('json'), matching how the request came in; the stored object is the whitelisted value. In show, a parameter parseId rejects, or an index at or past posts.length, goes through the existing ctx.throw(404, 'invalid post id').

blog/test.js gains one case per rule: a POST without a title gets 400 and the form with the error text; a POST with .type('form').send('title=a&title=b&body=c') gets 400; a POST with the two fields trimmed and stored shows on /post/0; and /post/length, /post/push, /post/abc and /post/-1 each get 404. The agent runs npm test, which executes all seventeen */test.js files, and npm run lint, and pastes both. Because the repository has no GitHub Actions workflow, nothing runs on the pull request itself, so the output in the description and the run's record are the evidence. koa-body keeps its defaults; the pull request notes jsonLimit and formLimit as the next knob if large bodies are a concern, as the body-parsing example sets jsonLimit: '1kb'.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add input validation to the blog app in this Koa repository. Read blog/app.js, blog/lib/render.js, blog/test.js, every template under blog/views, the root package.json, .eslintrc.yml and the body-parsing example first. Validate the POST /post body: title and body must be non-empty strings after trimming, with a length cap you propose, arrays and objects are rejected, and only those two fields are stored. Validate the :id route parameter as a non-negative integer within the posts array and answer 404 otherwise. On a bad body, re-render the form with the errors and status 400, or answer JSON errors for a JSON request. Add a test for each rule to blog/test.js, run npm test and npm run lint, and open a pull request with the 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

The pull request adds blog/lib/validate.js and changes blog/app.js, blog/views/new.html and blog/test.js. Its description lists each rule with the request that violates it and the response it now gets, and pastes the npm test run across every example and the npm run lint output.

No workflow runs on the pull request, since the repository carries only a Travis configuration, so a reviewer reads the pasted output and the run's record, which keeps the test run before and after the change. The diff to app.js is confined to the two handlers; the routes, the renderer and the logger are as they were.

Before merging, a reviewer sets the length caps the pull request proposes, decides whether a bad JSON request should get a problem-details body instead of a plain errors array, and whether the same validatePost should guard a future PUT. The in-memory store and the other examples are unchanged.

Run it on every new write endpoint

An automation on the Project with a github trigger on pull_request.opened and pull_request.synchronize and the prompt "if this pull request adds or changes a route that reads ctx.request.body or ctx.params, check that every field and parameter is validated before use and say what a malformed request would do" reviews each new endpoint as it arrives. The trigger adds the pull request after the prompt, and only work by an owner, member or collaborator fires it.

The same body sent to POST /o/{org}/sessions with a request_id that is a UUID v5 of the repository's full name and the route's path does the work for an endpoint that already exists. The key's webhook_url reports each run the key starts with the pull request in result; a run an automation starts appears in the Project's run list and has its own max_runs_per_hour cap.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Validation gaps are found by reading the parser and the handler together: which shapes koa-body can produce for a form field, what the array lookup does with a string that is not a number, which fields a client can smuggle into the stored object. The agent reads both in its own sandbox, sends the requests that expose each gap, and turns every one into a test before it changes the handler.

The test run across all seventeen examples and the lint output stay with the run and are quoted in the pull request, which matters here because no CI runs on the repository's pull requests. A team with several small Koa services gets the same reading per repository, each with its own list of rules and tests.

Frequently asked questions

Why not add a schema library?

The blog has two fields and one parameter, and the repository lints with ESLint 3 under a Node 7.6 floor, so a current schema library would need its own toolchain changes first. A forty-line validate.js with explicit rules keeps the pull request about validation. The description names the libraries to consider if the app grows.

What exactly was wrong with /post/:id?

posts[ctx.params.id] indexes the array with whatever string arrived. length and push are properties of every array, so /post/length and /post/push found something truthy and rendered an empty page instead of a 404. The fix accepts digits only and checks the index against posts.length.

Does a browser still see the form on a bad submit?

Yes. On errors create renders new.html again with status 400, the values the person typed and the list of errors, so nothing is lost. A request that sent JSON gets an errors array back as JSON instead, decided by ctx.is('json') on the request's content type.

Why is nothing running on the pull request?

The repository has a .travis.yml and no .github/workflows, so GitHub shows no checks. The agent runs npm test and npm run lint in its sandbox and pastes the output; adding a GitHub Actions workflow is a separate change the description points to.

More Koa jobs

The same job on other stacks

Keep reading