Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ The ports below are the defaults of the clients.
| Ray | host `127.0.0.1`, port `8000` in `ray.php` | [Ray](docs/ray.md) |
| HTTP dumps | any HTTP request, e.g. `http://127.0.0.1:8000/webhook` | [HTTP dumps](docs/http-dump.md) |
| OpenTelemetry (OTLP) | `OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318` | [OpenTelemetry](docs/opentelemetry.md) |
| CSP and Reporting API | `report-uri http://127.0.0.1:8000/csp/my-app` in the policy | [CSP reports](docs/csp.md) |
| Profiles | `trap()::profile()`, `POST /api/profiler/*`, a watched directory | [Profiling](docs/profiling.md), [receiving](docs/profiles.md), [SPX](docs/spx.md) |
| Xdebug step debugging | `--xdebug`, `xdebug.client_port=9003`, the IDE moved to `9004` | [Xdebug debugger](docs/xdebug.md) |
| Xdebug function traces | `POST` with `X-Buggregator-Event: xdebug-trace`, a watched directory | [Xdebug traces](docs/xdebug-traces.md) |
Expand Down Expand Up @@ -232,7 +233,8 @@ All the options and variables: [CLI](docs/cli.md) and [Configuration](docs/confi
- [Functions](docs/functions.md)
- [Profiling code](docs/profiling.md) and [receiving profiles](docs/profiles.md), [SPX](docs/spx.md)
- Integrations: [var-dumper](docs/var-dumper.md), [Monolog](docs/monolog.md), [Sentry](docs/sentry.md),
[SMTP](docs/smtp.md), [Ray](docs/ray.md), [HTTP dumps](docs/http-dump.md), [OpenTelemetry](docs/opentelemetry.md)
[SMTP](docs/smtp.md), [Ray](docs/ray.md), [HTTP dumps](docs/http-dump.md), [OpenTelemetry](docs/opentelemetry.md),
[CSP reports](docs/csp.md)
- [Senders](docs/senders.md)
- [Web UI](docs/web-ui.md)
- [MCP server](docs/mcp.md)
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Ray](ray.md) - payloads of Ray clients.
- [HTTP dumps](http-dump.md) - any other HTTP request.
- [OpenTelemetry](opentelemetry.md) - traces over OTLP/HTTP.
- [CSP reports](csp.md) - Content Security Policy violations and Reporting API reports of browsers.
- [Receiving profiles](profiles.md) - XHProf, Xdebug, Speedscope, pprof and SPX over HTTP or from a directory.
- [SPX](spx.md) - SPX full reports: directory, HTTP and profiling from code.
- [Xdebug traces](xdebug-traces.md) - Xdebug function traces over HTTP or from a directory: the call tree.
Expand Down
2 changes: 1 addition & 1 deletion docs/agent-skills.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The agent loads the skill when it debugs or profiles code at runtime, reads what

The references of the skill cover dumps and logs, profiling and Xdebug function traces, step debugging with the
[Xdebug debugger](xdebug.md), long-running workers, pointing Sentry, Monolog, Ray, a mailer or OpenTelemetry at Trap,
and the server setup.
fixing a Content Security Policy with the [CSP reports](csp.md) of a Report-Only policy, and the server setup.

## Install with Composer

Expand Down
2 changes: 2 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ page of the [web UI](web-ui.md), Sentry pages included:
trace with database, cache and HTTP spans, logs and trace metrics;
- an [OpenTelemetry](opentelemetry.md) trace across two services, with logs and metrics of each type;
- a Sentry error with logs of a second project, `billing`;
- [CSP reports](csp.md) of each format: a `report-uri` violation, and a Reporting API batch of a violation and a
deprecation;
- an image as binary data.

The times of the logs, traces and metrics are those of the run, so the period filters of the UI show them.
Expand Down
127 changes: 127 additions & 0 deletions docs/csp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# CSP reports

Trap receives the security reports browsers send: Content Security Policy violations and the other reports of the
Reporting API, such as `deprecation`, `intervention`, `crash` and `network-error`. Point a policy of your
application at Trap, open its pages, and every resource the policy would block shows up in the console, on the
CSP reports page of the [web UI](web-ui.md) and in the [MCP tools](mcp.md#csp-reports).

Your application only sends a header: there is no client to install. Use `Content-Security-Policy-Report-Only` while
you debug a policy: the browser reports what the policy would block and blocks nothing.

## Receive reports

Send the reports to `/csp` or `/csp/{project}` on an HTTP port of Trap, `8000` by default:

```text
http://127.0.0.1:8000/csp/my-app
```

Trap takes a report from a `POST`:

- with the `application/csp-report` content type, of the `report-uri` directive, to any path;
- with the `application/reports+json` content type, of the `report-to` directive, to any path;
- to `/csp` or `/csp/{project}` with any content type: a JSON list is read as `reports+json`, an object as
`csp-report`;
- marked `csp` with the `X-Buggregator-Event` header or the user of Basic authorization, to any path with any
content type.

So an existing report URL of your application works too when its host and port point at Trap. A request marked for
another kind of event, with the `X-Buggregator-Event` header or the user of Basic authorization, is left to it.

The project of the reports is the `{project}` of the path, else the password of the `csp` user of Basic
authorization (`http://csp:my-app@127.0.0.1:8000/`), else the `X-Buggregator-Project` header, else `default`.
A browser sends no credentials from the userinfo of a report URL and adds no headers of yours, so for browsers the
project works only as `/csp/{project}`. Basic authorization and the header are for a proxy or another client that
forwards the reports.

Trap answers a report with `204` and an empty body, a body that is not a report with `400`, and a body larger than
1 MiB with `413`. Any `OPTIONS` request to `/csp` or `/csp/...` gets a CORS preflight answer for any origin:
Chrome sends Reporting API reports to another origin only after one.

## Set up the policy

The samples send the policy only in development: take it out of the application when you are done.

### `report-uri`

`report-uri` sends a `csp-report` right away, one request per violation, from HTTP and HTTPS pages alike.

```php
if (getenv('APP_ENV') === 'dev') {
header("Content-Security-Policy-Report-Only: default-src 'self'; report-uri http://127.0.0.1:8000/csp/my-app");
}
```

### `report-to` and `Reporting-Endpoints`

`report-to` names an endpoint of the `Reporting-Endpoints` header and sends `reports+json`. The endpoint named
`default` also gets the other reports of the page: deprecations, interventions, crashes.

```php
if (getenv('APP_ENV') === 'dev') {
header('Reporting-Endpoints: trap="https://127.0.0.1:8000/csp/my-app", default="https://127.0.0.1:8000/csp/my-app"');
header("Content-Security-Policy-Report-Only: default-src 'self'; report-to trap; report-uri https://127.0.0.1:8000/csp/my-app");
}
```

- Chrome sends Reporting API reports only from an HTTPS page to an HTTPS endpoint, so the endpoint is `https://`:
Trap takes TLS on the same port, and the browser must trust its certificate, see [TLS](tls.md#certificate).
- A browser that knows `report-to` ignores `report-uri` in the same policy; keep both for the other browsers.
- The browser batches the reports and sends them with a delay of up to a minute. Keep the page open meanwhile.
- Send the reports of another origin to `/csp` or `/csp/{project}`: only these paths answer the preflight.

`network-error` reports come from Network Error Logging, which a `NEL` header turns on. It names a group of the
older `Report-To` header: Chrome takes the endpoints of Network Error Logging from it, not from `Reporting-Endpoints`.

```php
header('Report-To: {"group": "trap", "max_age": 3600, "endpoints": [{"url": "https://127.0.0.1:8000/csp/my-app"}]}');
header('NEL: {"report_to": "trap", "max_age": 3600, "failure_fraction": 1.0}');
```

The console and the MCP text show the failure of a `network-error` report (its `type`, such as `tcp.refused`), the
phase, the HTTP status, the elapsed time and the server IP, and the reason of a `crash` report.

### Laravel

[spatie/laravel-csp](https://github.com/spatie/laravel-csp) adds `report-uri` from `report_uri` of
`config/csp.php`, which reads `CSP_REPORT_URI`. Set it in the `.env` of development:

```dotenv
CSP_REPORT_URI=http://127.0.0.1:8000/csp/my-app
```

Put the policy you debug into the report-only one: `report_only_presets` in version 3 of the package,
`report_only_policy` in version 2.

### Symfony

[NelmioSecurityBundle](https://github.com/nelmio/NelmioSecurityBundle) sends the `report` policy as
`Content-Security-Policy-Report-Only`:

```yaml
# config/packages/nelmio_security.yaml
when@dev:
nelmio_security:
csp:
report:
default-src: ['self']
report-uri: ['http://127.0.0.1:8000/csp/my-app']
```

## HTTPS pages

An HTTPS page sends no report to an `http://` URL of a host other than `127.0.0.1` or `localhost`: the browser
blocks it as mixed content. When Trap runs on another host, for example in Docker, report to `https://` on its
port, with a certificate the browser trusts: see [TLS](tls.md#certificate).

## What Trap shows

The console prints a block per report: what was blocked or reported, the report type, the page, the directive, what
was blocked, the disposition (`enforce`: blocked; `report`: Report-Only, not blocked), the source file with its line
and column, the code sample, the message of a report of another type, the policy and the user agent.

One request is one event of the `csp` type: a `report-uri` request carries one report, a `report-to` batch may
carry several. The web UI lists the events on its CSP reports page; the [MCP tools](mcp.md#csp-reports) list,
filter and read them.

`vendor/bin/trap test` sends a report of each format.
3 changes: 2 additions & 1 deletion docs/http-dump.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ which posts to `/_debug/bar`. That request is dumped and answered with `200`.

## Notes

- The requests of Sentry, Ray, the profilers and OpenTelemetry go to their modules and are not dumped.
- The requests of Sentry, Ray, the profilers, OpenTelemetry and [CSP reports](csp.md) go to their modules and are
not dumped.
- When the [web UI](web-ui.md) runs on the same port, it answers `/`, the paths of its files, e.g. `/favicon.ico`,
its `/api/...` routes, e.g. `/api/events`, and a browser opening a page of the UI, e.g. `/smtp`: see
[Port](web-ui.md#port).
Expand Down
22 changes: 19 additions & 3 deletions docs/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Other clients take the same URL with the `http` (Streamable HTTP) transport type

## Tools

The tools read the events Trap has caught, the Sentry and OpenTelemetry [traces](#traces) it has received, Sentry
[user feedback](#user-feedback), and the call trees of [Xdebug traces](#xdebug-traces). The events are the ones the [web UI](web-ui.md) shows, kept with
The tools read the events Trap has caught, [CSP reports](#csp-reports) among them, the Sentry and OpenTelemetry
[traces](#traces) it has received, Sentry [user feedback](#user-feedback), and the call trees of
[Xdebug traces](#xdebug-traces). The events are the ones the [web UI](web-ui.md) shows, kept with
`--mcp` alone too. An event deleted in the UI is gone for the agent. An event
the agent deletes is gone from the UI on its next load; a page already open keeps showing it.

Expand Down Expand Up @@ -63,7 +64,7 @@ the agent deletes is gone from the UI on its next load; a page already open keep

| Argument | Matches |
| --------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `type` | One event type or a list: `var-dump`, `smtp`, `monolog`, `ray`, `sentry`, `profiler`, `http-dump`, `xdebug-trace` |
| `type` | One event type or a list: `var-dump`, `smtp`, `monolog`, `ray`, `sentry`, `profiler`, `http-dump`, `xdebug-trace`, `csp` |
| `project` | One project or a list; events sent without a project belong to `default` |
| `from` | Events caught at this time or later: ISO 8601 (UTC without an offset) or a duration back from now: `30s`, `5m`, `2h`, `1d`, `1w` |
| `to` | Events caught at this time or earlier, in the same forms |
Expand Down Expand Up @@ -119,6 +120,7 @@ seconds. Trap catches events and answers other requests meanwhile, and a call st
| `http-dump` | `method`, `path`, `host` |
| `ray` | `payload_type`, `exception`, `label`, `color` |
| `profiler` | `name` (the application or the file), `format` |
| `csp` | `report_type`, `directive`, `blocked`, `disposition`, `page`, `source_file` (of each report of the request) |

An `xdebug-trace` event has no fields: its summary starts with the URL or the name of the trace, so `search` finds it.
The `list_events` description lists the fields too. Examples:
Expand Down Expand Up @@ -342,6 +344,20 @@ dropped. Clearing the Sentry pages of the web UI drops their traces for the tool
ID of that error for `get_event`, null when Trap no longer has it or the feedback is about no error.
- `limit` is 20 by default, 100 at most; `total` counts all the matching entries.

### CSP reports

The browser [security reports](csp.md) are `csp` events, one per request of the browser, which may carry several
reports. The summary is the first report and its page, as `script-src-elem blocked https://cdn.example.com/x.js on
https://localhost:8080/checkout (+1 more)`. `get_event` gives a block per report: the title, then the report type,
the page, the directive, what was blocked, the disposition (`enforce`, blocked; `report`, Report-Only, not blocked),
the source file with its line and column, the code sample, the message and ID of a report of another type, the
failure, phase, status, elapsed time and server IP of a `network-error` report, the reason of a `crash`, the
policy and the user agent. A field of the report the browser left empty is left out.

- The CSP violations of the last 5 minutes: `{"type": "csp", "from": "5m", "where": {"report_type": "csp-violation"},
"include": "text"}`.
- What a page has blocked: `{"type": "csp", "where": {"page": "*/checkout*", "disposition": "enforce"}}`.

### Profiles

The [profiles](profiles.md) are `profiler` events. `get_event` gives one as the console prints it; these tools let
Expand Down
11 changes: 6 additions & 5 deletions docs/web-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ On a shared port the UI takes only its own requests, and the others stay [HTTP d
such endpoint.

Open the UI from Trap itself: it sends no CORS headers, so a frontend served from another origin, such as a dev
server of buggregator/frontend, can't call its API. An `OPTIONS` request is an HTTP dump like any other.
server of buggregator/frontend, can't call its API. An `OPTIONS` request is an HTTP dump like any other, but one to
`/csp` or `/csp/...`: it gets the CORS preflight answer [CSP reports](csp.md) need.

The UI asks for no password or token, the [MCP token](mcp.md#security) included: anyone who reaches its port sees
the caught events. Trap warns at startup when the port listens on a host other than a loopback one.
Expand All @@ -43,8 +44,8 @@ vendor/bin/trap --ui=8080 -s

## What it shows

Dumps, Monolog logs, Sentry events, emails, Ray payloads, HTTP dumps, profiles and
[Xdebug traces](xdebug-traces.md) with their call tree.
Dumps, Monolog logs, Sentry events, emails, Ray payloads, HTTP dumps, profiles,
[Xdebug traces](xdebug-traces.md) with their call tree, and [CSP reports](csp.md) on the CSP reports page.

The UI keeps the last 200 events; `/trap/events/@maxEvents` in [`trap.xml`](configuration.md#trapxml) changes it.
A pinned event is pushed out last: only when all the kept events are pinned. A clear keeps the pinned events when
Expand All @@ -59,8 +60,8 @@ sessions and steps through the ones Trap drives, and its setup guide shows the d

The UI has pages for features of the Buggregator Server that Trap has not: metrics, the mock server, updates.
They show no data, and the menu of the mock server stays while HTTP dumps are on: hide it in Settings › Menu.
Other features of the server stay hidden, as Trap answers 404 to their endpoints: the Broadcast and CSP reports
pages, the Crons and Queues tabs of the Sentry section, the
Other features of the server stay hidden, as Trap answers 404 to their endpoints: the Broadcast page, the Crons
and Queues tabs of the Sentry section, the
«Send to inbox» button of an email (Settings › Email relay says the server can't send email), the other versions
of an email and its invite card, sign-in and the audit log. A Ray pause shows its state in the tab whose button
released it; another tab keeps its buttons.
Expand Down
58 changes: 58 additions & 0 deletions resources/payloads/csp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[
{
"content_type": "application/csp-report",
"body": {
"csp-report": {
"document-uri": "http://localhost:8080/checkout",
"referrer": "http://localhost:8080/cart",
"violated-directive": "script-src-elem",
"effective-directive": "script-src-elem",
"original-policy": "default-src 'self'; script-src 'self' 'nonce-r4nd0m'; report-uri http://127.0.0.1:8000/csp",
"disposition": "report",
"blocked-uri": "https://cdn.payments.example/widget.js",
"line-number": 42,
"column-number": 9,
"source-file": "http://localhost:8080/checkout",
"status-code": 200,
"script-sample": ""
}
}
},
{
"content_type": "application/reports+json",
"body": [
{
"age": 1530,
"body": {
"blockedURL": "inline",
"columnNumber": 17,
"disposition": "enforce",
"documentURL": "http://localhost:8080/profile",
"effectiveDirective": "script-src-elem",
"lineNumber": 88,
"originalPolicy": "script-src 'self'; report-to trap",
"referrer": "",
"sample": "document.getElementById('avatar').onclick",
"sourceFile": "http://localhost:8080/profile",
"statusCode": 200
},
"type": "csp-violation",
"url": "http://localhost:8080/profile",
"user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
},
{
"age": 920,
"body": {
"columnNumber": 5,
"id": "UnloadHandler",
"lineNumber": 12,
"message": "Unload event listeners are deprecated and will be removed.",
"sourceFile": "http://localhost:8080/assets/app.js"
},
"type": "deprecation",
"url": "http://localhost:8080/profile",
"user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
}
]
}
]
Loading
Loading