# Virtual patching and safe mode (SHIELD_PATCHES=1)

When a flaw is found in your site (by you, or by Praecise Scan), you can shut it here within the minute and fix the code afterwards. Your server asks Shield about a request before it serves it; a rule that matches makes the answer "refuse" or "challenge". Nothing is deployed.

Off unless the operator runs Shield with SHIELD_PATCHES=1; every call below answers 404 otherwise.

## Ask about a request (from your server)
`POST /patches/check` with `{ "site", "secret", "request": { "method", "path", "params"?, "headers"?, "body"?, "size"?, "agent"?, "signedIn"? } }` answers `{ "decision": "allow" | "challenge" | "refuse", "rule"?, "reason" }`. Pass what you want looked at: Shield looks at no more than the first 8192 characters of any value. Do not pass secrets: leave out the Authorization header and cookies.

## Place a rule
`POST /patches?site=` with your site secret as a bearer:
`{ "reason": "CVE-... in /export", "action": "block" | "challenge", "expiresInMinutes": 1440, "match": { ... }, "testRefused": { request }, "testPass": { request } }`.
- `match` names at least one of: `routes` (exact path, `*` for one segment, a trailing `/**` for everything below), `methods`, `param` { name?, pattern, ignoreCase? }, `header` { name, pattern }, `body` { pattern }, `overBytes`, `agents` (signed agents by name). All the conditions named must hold.
- Patterns are regular expressions of at most 200 characters. A repeated group (`(ab)+`) and a back-reference are refused, because they can run for very long.
- **The gate.** The rule is placed only if `testRefused` is stopped by this rule, `testPass` is not stopped by it, and `testPass` still passes with every rule and safe-mode entry already in force. Otherwise the answer is 422 `gate_failed` with the reason and nothing is placed. `POST /patches/test` runs the gate without placing.
- Every rule expires: 1 minute to 30 days. At most 100 at a time. It takes effect on the next `/patches/check`.
- `GET /patches?site=` lists the rules with their owner, reason, expiry and hits; `DELETE /patches/<id>?site=` removes one.

## Let Praecise Scan place patches
`POST /patches/tokens?site=` (site secret) gives a patch token, shown once. A token can place rules and remove the ones it placed, and nothing else (not safe mode, not your other rules). Give it to Scan with the site's Scan settings; `DELETE /patches/tokens/<id>?site=` revokes it and takes down what it placed. Rules placed with a token carry the owner `scan:<id>`.

## Safe mode
`PUT /safe-mode?site=` with `{ "reason", "mode": "signin" | "readonly", "routes": ["/admin/**"], "expiresInMinutes": 240 }`: with "signin", a request to those routes from someone not signed in (you pass `signedIn`) gets "challenge"; with "readonly", anything but GET, HEAD and OPTIONS gets "refuse". It keeps the rest of the service up while you fix. Expires in 1 minute to 7 days. `DELETE /safe-mode?site=&id=` lifts one (no id: all).

## How patches apply
- Shield decides; your server enforces, by calling `/patches/check` on each request.
- A pattern rule is a stop-gap that holds the line while you fix the code; the passing test is one request you chose.
- Shield reads what your server passes it, up to 8192 characters of a value.

## How Shield decides

- One touch, never a puzzle: a person passes with a single touch.
- Shield makes automation slow, costly and visible; agents that sign their requests get their own identity, budget and rules.
- The quiet check reads what a browser reports about itself, and the token is confirmed on your own server.
- The agent owner check is a signature over an Ethereum-style address.
- Rules are applied at the page and at your server; edge rules and certificates are not part of this service.
- Counts are kept by the Shield service.
