# Redact Tokens and Passwords from Winston Logs in an Express API

Yes. An agent finds where your Express API logs a reset token or password through Winston, adds a redaction format and request-line scrubbing, and opens a PR.

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

## What the agent does

The agent traces every line that reaches the logger before writing a format. In an Express API laid out like hagopj13/node-express-boilerplate, `src/config/logger.js` builds one Winston logger (`winston` at `^3.2.1` in `package.json`) with `enumerateErrorFormat`, which copies an error's `stack` into `message`, then `splat` and a `printf` of `level: message`, to a single console transport. `src/config/morgan.js` streams Morgan's request line into it with the format `:method :url :status - :response-time ms`, and for responses of 400 and up appends `message: :message` from `res.locals.errorMessage`, which `src/middlewares/error.js` sets to the error's message. `src/routes/v1/auth.route.js` serves `POST /reset-password` and `POST /verify-email`, and `src/validations/auth.validation.js` requires their `token` in the query string. So every password reset and every email verification writes the one-time JWT into the log line as part of `:url`, and in production the line carries `:remote-addr` too.

It then follows the error path. `errorHandler` logs the whole error object in development, `src/index.js` logs an uncaught exception or unhandled rejection the same way, and `enumerateErrorFormat` prints whatever the error's stack and properties hold, so an error that carries request data, a failed request to the SMTP server configured in `src/config/config.js` with its credentials attached, or an `ApiError` built from a message that quotes input, would print it as is. The agent lists each of these sites in the pull request with the line that produces it, which is the map a reviewer needs to believe the fix is complete.

The fix has two parts. A `redact` format in `src/config/logger.js`, written with `winston.format` like the existing one, walks `info.message` and any object fields and replaces the value of keys named `password`, `token`, `refreshToken`, `authorization` and `Authorization` with `[REDACTED]`, and rewrites a `token=` query parameter inside any string to the same marker; the key list lives in one exported constant so a reviewer can extend it. The format sits first in the `combine` chain, before `enumerateErrorFormat` turns a stack into a message, so an error's properties are scrubbed before they are stringified. In `src/config/morgan.js` a custom `url` token strips `token` from the query string before the line is built, which keeps the path and the other parameters readable.

Tests go under `tests/unit`, next to `tests/unit/middlewares/error.test.js`, with Jest 26 and the `-i` serial run `package.json` defines. One test writes a message containing `token=abc` and an error whose `message` holds a password through the logger with a transport that captures its output, and asserts neither value appears; another drives `app` with Supertest through `POST /v1/auth/reset-password?token=…` with `NODE_ENV` forced so Morgan is mounted, since `src/app.js` skips it in `test`, and asserts the captured request line has the path and `[REDACTED]` but not the token. It runs `yarn lint` and `yarn test`, the two steps `.travis.yml` runs, against a MongoDB it installs in the sandbox, and opens the pull request with the output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Redact secrets from this Express API's Winston logs. Read package.json, src/config/logger.js, src/config/morgan.js, src/config/config.js, src/app.js, src/index.js, src/middlewares/error.js, src/routes/v1/auth.route.js, src/validations/auth.validation.js, src/controllers/auth.controller.js, src/services/auth.service.js, src/services/token.service.js, tests/unit/middlewares/error.test.js, tests/utils/setupTestDB.js, jest.config.js and .travis.yml first. List in the pull request every place a password, JWT or reset token can reach the logger, with the file and line. Add a redact format in src/config/logger.js, placed first in the combine chain, that replaces the values of password, token, refreshToken and authorization keys in message objects and error properties with [REDACTED] and rewrites token= query parameters inside strings; keep the key list in one exported constant. Add a Morgan url token in src/config/morgan.js that strips the token query parameter from the request line. Write Jest tests with a capturing transport for the format and a Supertest request to POST /v1/auth/reset-password with a token in the query that asserts the logged line is scrubbed. Run yarn lint and yarn test, 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

What comes back is a pull request that changes `src/config/logger.js` and `src/config/morgan.js` and adds two test files under `tests/unit`. The description holds the list of logging sites with their lines, a before-and-after of the request line for a reset-password call, and the Jest and ESLint output.

The run's record keeps the test run and a sample of the scrubbed output, so a reviewer sees `[REDACTED]` where the token was rather than reads that it would be.

Before merging, a reviewer reads the key list and adds any field their own routes use for a secret, which the pull request names as the one decision it could not make from the code alone, and decides whether the production line should keep `:remote-addr` at all.

## Run it on every alert on a secret in logs

The signal is usually a log search that matches a JWT pattern, or a scanner that flags a line. In the Project for the repository, an automation with the prompt above and a `webhook` trigger takes that alert from a Grafana contact point, which can send the trigger's bearer secret in an `Authorization` header and an `Idempotency-Key` made from the alert's fingerprint to the trigger's URL, so a re-fired alert does not start a second run. The trigger adds the payload after the prompt, so the run reads which line matched. A scanner that cannot set that header posts to a handler on your server, which forwards the alert the same way.

A team with several Express services on the same logger sends this body once per repository from its own server, each with its repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started and opens no second pull request.

## Why run it on Rush?

The hard part is not the format, it is the list: every path by which a secret can reach a log line, including the ones that only show on an error. The agent reads the routes, the validators, the error converter and the logger as one data path and writes that list down with lines, then proves the fix against it with a real request through the app in its own sandbox, and the record keeps the scrubbed output.

A company with several services on the same boilerplate runs this once per repository, each in its own sandbox with its own MongoDB and its own pull request, and reviews the same two-file diff and the same test in each.

## Frequently asked questions

### Why is the token in the URL in the first place?

Because the reset and verification links in the email carry it as a query parameter, and `auth.validation.js` reads it from `query`. Moving it to the body would change the email links and the client; the pull request leaves that as a follow-up and scrubs the line, which is the change that stops the leak today.

### Will the redaction slow down logging?

The format walks the fields of each log entry once and runs one regular expression over strings. Request lines are short, and the agent includes the timing from the test run so a reviewer can see the cost rather than assume it.

### Does this cover errors with the stack trace?

Yes. The redact format runs before `enumerateErrorFormat`, so an error's own properties are scrubbed while they are still properties, and the stack string is scrubbed as a string. A `token=` parameter quoted inside an error message is replaced before the message is printed.

### Why keep the key list in a constant?

Because the list is the policy and the format is the mechanism. A reviewer who adds a new secret field to a route edits one exported array, and the test that asserts each key is scrubbed reads the same array, so the two cannot drift.

## More Winston jobs

- [Add Log Rotation to a Winston Logger](https://getrush.ai/run/add-log-rotation/winston/)
- [Add a Request ID to Every Winston Log Line in an Express App](https://getrush.ai/run/add-request-id-logging/winston/)
- [Migrate From Winston to Pino in a Node.js Service](https://getrush.ai/run/migrate-winston-to-pino/winston/)

## More jobs like this: Fix a bug from an alert

- [Add a Fallback Model to an AI SDK Chat Route](https://getrush.ai/run/add-model-fallbacks/ai-sdk/)
- [Add Query Timeouts to a Drizzle and postgres.js Setup in Next.js](https://getrush.ai/run/add-query-timeouts/drizzle/)
- [Add Query Timeouts to a sqlc Project](https://getrush.ai/run/add-query-timeouts/sqlc/)

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