# Add Bandit Security Checks to a Python Project's CI

Yes. An agent turns on the flake8-bandit rules in your existing Ruff config, triages every finding, fixes the real ones, scopes the test-only noise and opens a PR.

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

## What the agent does

The agent reads how the project lints itself before adding a security scanner, because the right answer depends on what is already there. In a library like encode/httpx, `pyproject.toml` configures Ruff with `select = ["E", "F", "I", "B", "PIE"]`, ignores `B904` and `B028`, and sets per-file ignores for `__init__.py`; `requirements.txt` pins `ruff==0.12.11` and `mypy==1.17.1` with a comment explaining that tooling is pinned and dependencies are not; `scripts/check` runs `ruff format --diff`, `mypy` and `ruff check` over `httpx tests`, and `.github/workflows/test-suite.yml` runs `scripts/check` on Python 3.9 through 3.13 before the tests and a coverage gate of 100 percent. Bandit's checks already exist inside Ruff as the `S` rule family, so the agent adds `"S"` to `select` rather than installing a second scanner with its own config, pins and CI step, and notes the choice in the pull request.

Turning the rules on produces a long list, and the agent triages it by reading each kind of finding at its source. Almost all of it is `S101`, `assert` statements in the test suite, which is how pytest tests are written; the library itself has a handful too, such as `assert scheme.lower() == "digest"` in `_auth.py` guarding a method that is only ever called with a Digest header, `assert isinstance(response.stream, SyncByteStream)` in `_client.py` and the `assert connect is UNSET` lines in `_config.py` that enforce how `Timeout` is constructed. `S105` and `S106` fire on hardcoded passwords, every one of them a test fixture like `httpx.BasicAuth(username="user", password="pass")` in `tests/test_auth.py` and `tests/client/test_auth.py`, plus one `if password == "-"` in `_main.py` that is the CLI's convention for prompting. `S301` fires on `pickle.loads` in `tests/models/test_requests.py` and `test_responses.py`, which round-trip a `Request` and a `Response` to prove they are picklable. One finding is real: `S324` on `hashlib.sha1(s).hexdigest()[:16]` in `_get_client_nonce` in `_auth.py`.

The fixes follow the triage. The client nonce is not a security hash, it is a source of per-request variety mixed from the nonce count, the server nonce, the time and eight random bytes, so the agent passes `usedforsecurity=False`, which Python 3.9 and later accept and which `requires-python = ">=3.9"` allows, and Ruff stops reporting it. The `password == "-"` sentinel in `_main.py` gets a `# noqa: S105` with the reason on the line. For the test suite the agent adds a `per-file-ignores` entry for `"tests/*"` covering `S101`, `S105`, `S106` and `S301`, because asserts, fixture credentials and pickling round-trips are what tests are for; for the library's own asserts it adds `S101` to the global `ignore` list with a comment, since they are internal invariants and converting them to raised exceptions would be a behaviour change this pull request should not make.

Then it proves the change the way the project checks itself. It runs `scripts/install`, `scripts/check`, which now includes the `S` rules in `ruff check httpx tests`, `scripts/test`, and `scripts/coverage`, because the one line it changed in `_auth.py` sits on a path the Digest tests cover and the gate is `--fail-under=100`. The pull request shows the `ruff check` output before and after, so a reviewer sees the rules go from a wall of findings to a clean run with each exception explained where it lives.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Bandit security checks to this Python project's CI. Read pyproject.toml, requirements.txt, scripts/check, scripts/lint, scripts/test, scripts/coverage and .github/workflows/test-suite.yml first. If the project already uses Ruff, enable its S rule family in the existing config instead of adding bandit as a separate tool; otherwise add bandit with a config file and a CI step. Run the rules, read every category of finding at its source, fix the real ones in library code (an insecure hash used for non-security purposes gets usedforsecurity=False), scope test-only findings such as asserts, fixture credentials and pickle round-trips to the tests directory with per-file ignores, and justify any global ignore in a comment. Run the project's own check, test and coverage scripts so coverage stays at its gate, and open a pull request with the finding counts by rule before and after, each fix, and each ignore with its reason.",
  "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 changing `pyproject.toml` (the `S` family in `select`, `S101` in `ignore` with a comment, and a `per-file-ignores` entry for `tests/*`), one line in `httpx/_auth.py` and one `noqa` with its reason in `httpx/_main.py`. The description has a table of findings by rule before and after, and a sentence per rule saying whether it was fixed, scoped or ignored and why.

The run's record keeps `scripts/check`, `scripts/test` and `scripts/coverage` from the sandbox, so a reviewer sees Ruff pass with the new rules, the Digest tests still cover the changed line, and the coverage gate hold.

Before merging, a reviewer reads the two source changes, decides whether the library's internal asserts should stay ignored or become explicit exceptions in a later change, and checks that `scripts/check` is still the only lint command CI runs, so nothing new needs pinning.

## Run it on every repository without a security linter

A maintainer hands this off from the terminal: one `POST /o/{org}/sessions` with this body and the repository in `resources`. A team with many Python services sends one request per repository, each with a `request_id` that is a UUID v5 of the repository path so a retried call returns the run it already started, and the key's `webhook_url` reports each run as it finishes with its pull request.

Each run reads its own repository's lint setup and finding list, so a project without Ruff gets a Bandit install and CI step instead, and the triage is done against that code, not a template.

## Why run it on Rush?

The work in adopting a security linter is not the config line, it is the first run: a long list in which almost everything is a test assert and one or two lines deserve a look. The agent does that reading in its own sandbox, with the project's own pinned Ruff and its own check scripts, and arrives with the list already sorted into fixed, scoped and ignored, each with a reason a reviewer can disagree with.

Because it runs the real `scripts/check`, `scripts/test` and `scripts/coverage`, the pull request is green in the same way CI will be green. Nothing is claimed about the findings that the run's record does not show.

## Frequently asked questions

### Why Ruff's S rules instead of installing bandit?

Because the project already pins and runs Ruff in `scripts/check`, and Ruff ships Bandit's checks as the `S` family. One line in `pyproject.toml` adds them to the same command CI already runs, with no new dependency to pin. A project that does not use Ruff gets bandit itself and a CI step.

### Does this change any behaviour of the library?

One line: the Digest client nonce is now computed with `hashlib.sha1(s, usedforsecurity=False)`. The output is identical; the flag only tells the interpreter the hash is not used for security, which is true of a nonce mixer. Everything else is lint configuration and comments.

### Why ignore asserts in the library instead of fixing them?

The library's asserts guard internal invariants, like a method that is only called with a Digest header. Replacing them with raised exceptions changes behaviour and needs its own review, so this pull request keeps them, ignores `S101` with a comment, and leaves that decision to the maintainers.

### Will the coverage gate still pass?

Yes. The only changed library line is in the Digest nonce path, which the existing Digest auth tests exercise, and the agent runs `scripts/coverage` with its `--fail-under=100` in the sandbox to show it.

### What about the hardcoded passwords Ruff reports?

They are test fixtures such as `password="pass"` in the auth tests, plus the CLI's `"-"` sentinel that means prompt for a password. The test ones are scoped to `tests/*`; the sentinel gets a `noqa` with its reason on the line, so the rule still watches the rest of the library.

## More Python jobs

- [Add Benchmarks That Catch Performance Regressions in a Python Library](https://getrush.ai/run/add-benchmarks-to-ci/python/)
- [Add Test Coverage Reporting to Python CI](https://getrush.ai/run/add-coverage-to-ci/python/)
- [Fail CI on Sphinx Warnings and Broken Links for a Python Library](https://getrush.ai/run/add-docs-build-check/python/)

## More jobs like this: Hand off a long task

- [Add Blog Categories to a Wagtail Site](https://getrush.ai/run/add-blog-categories/wagtail/)
- [Add a Blog to a Starlight Docs Site](https://getrush.ai/run/add-blog/starlight/)
- [Add a Buildifier Check to CI for a Bazel Repository](https://getrush.ai/run/add-buildifier-to-ci/bazel/)

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