Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
a8641c9
Bind every Compose port to 127.0.0.1
stphnwlsh Dec 4, 2026
7582189
Read the telemetry service.version from VersionInfo
stphnwlsh Dec 4, 2026
ffff1f9
Send security headers on every response
stphnwlsh Dec 4, 2026
975fc16
Run dotnet run in the Development environment
stphnwlsh Dec 5, 2026
d3f0203
Refuse to start a deployed copy with auth off
stphnwlsh Dec 5, 2026
c40cdad
Accept dotnet user-jwts tokens in Development
stphnwlsh Dec 5, 2026
a4bbb46
Add optional rate limiting
stphnwlsh Dec 5, 2026
d790692
Merge remote-tracking branch 'origin/main' into fix/compose-ports-loc…
stphnwlsh Dec 5, 2026
cc6ba8e
Merge branch 'fix/compose-ports-localhost' into feature/telemetry-ver…
stphnwlsh Dec 5, 2026
0221a98
Merge branch 'feature/telemetry-version-from-versioninfo' into featur…
stphnwlsh Dec 5, 2026
ab068c2
Merge branch 'feature/security-headers' into feature/development-envi…
stphnwlsh Dec 5, 2026
2661e0f
Merge branch 'feature/development-environment' into feature/refuse-de…
stphnwlsh Dec 5, 2026
bafe18e
Merge branch 'feature/refuse-deployed-without-auth' into feature/deve…
stphnwlsh Dec 5, 2026
4f8b11a
Merge branch 'feature/development-tokens' into feature/optional-rate-…
stphnwlsh Dec 5, 2026
2720055
Keep development tokens to the Development environment
stphnwlsh Dec 5, 2026
727a48c
Say what a deployed copy does without authentication
stphnwlsh Dec 5, 2026
fcca16f
Merge remote-tracking branch 'origin/main' into feature/optional-rate…
stphnwlsh Dec 5, 2026
9c09c3f
Refuse to start when an authority has no audience
stphnwlsh Dec 6, 2026
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
12 changes: 7 additions & 5 deletions .agents/skills/configuration-and-environments/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: configuration-and-environments
description: How settings are layered with appsettings files and environment variables, what the local and docker environments are, and how to add a new setting. Use when adding configuration, a connection string or environment-specific behaviour.
description: How settings are layered with appsettings files and environment variables, what the Development and docker environments are, and how to add a new setting. Use when adding configuration, a connection string or environment-specific behaviour.
metadata:
scope: shared
---
Expand All @@ -15,9 +15,9 @@ The Lambda template has no appsettings files or environments. Its startup builds

| Environment | Set by | Used for |
| --- | --- | --- |
| `local` | `launchSettings.json` (`DOTNET_ENVIRONMENT=local`) | `dotnet run` on a developer machine: console logs, sample data on |
| `docker` | `docker-compose.yml` and the Dockerfile | `docker compose up`: telemetry over OTLP through the collector to the Aspire dashboard, database in `/app/data`, sample data on |
| anything else | the host | Real deployments: base settings only, sample data off, HSTS on |
| `Development` | `launchSettings.json` (`DOTNET_ENVIRONMENT=Development`) | `dotnet run` on a developer machine: console logs, sample data on |
| `docker` | `docker-compose.yml` | `docker compose up`: telemetry over OTLP through the collector to the Aspire dashboard, database in `/app/data`, sample data on |
| anything else | the host, or `Production`, the image's default | Real deployments: base settings only, sample data off, HSTS on. The web templates refuse to start here without authentication unless `Authentication:AllowAnonymous` is true (`security-defaults`) |

Settings that exist today:

Expand All @@ -26,6 +26,8 @@ Settings that exist today:
| `ConnectionStrings:MovieReviews` | SQLite connection string. `Data Source=Name;Mode=Memory;Cache=Shared` keeps everything in memory. |
| `Database:SeedSampleData` | Seed sample data into an empty database (`sample-data`). |
| `Authentication:Authority`, `Authentication:Audience` | Only in a solution created with `--include-auth`: turn on token authentication (`optional-token-auth`). MCP also has `Authentication:Scopes`, published in its protected resource metadata. |
| `Authentication:AllowAnonymous` | Web templates only: let a deployed environment run without authentication on purpose (`security-defaults`). Off by default. |
| `RateLimiting:PermitLimit`, `RateLimiting:Window` | Web templates, only in a solution created with `--include-rate-limiting`: requests each client IP address may make in each window. `0`, the default, leaves the limiter off (`security-defaults`). |
| `Serilog:*` | Logging (`serilog`). |

## Steps
Expand All @@ -39,7 +41,7 @@ Settings that exist today:
## Don't

- Put secrets, passwords or tokens in any `appsettings*.json` file. Use environment variables or user secrets.
- Check `IsEnvironment("local")` for behaviour that should be a setting. Add a setting instead.
- Check `IsDevelopment()` or `IsEnvironment("docker")` for behaviour that should be a setting. Add a setting instead.
- Read `Environment.GetEnvironmentVariable` directly. Go through `IConfiguration`.

## Check
Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/docker-and-compose/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ One multi-stage `Dockerfile` does everything the pipeline needs:
| `publish` | Publishes the Presentation project |
| `run` | The runtime image: non-root `$APP_UID`, `WORKDIR /app`, data in `/app/data`, a `HEALTHCHECK` on `/health/live` |

`docker-compose.yml` runs the app with `DOTNET_ENVIRONMENT=docker`, the `movie-reviews-data` volume a pinned OpenTelemetry Collector that receives the app's OTLP logs, traces and metrics, configured by `otel-collector.yaml`, and a pinned Aspire dashboard the collector forwards them to (`telemetry`). The dashboard UI and the collector's OTLP ports are published on the template's own ports (below), so a `dotnet run` can send to the collector too. The dashboard runs with `ASPIRE_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS` and the collector's OTLP ports take anything sent to them, on purpose: the compose file is for a developer's machine, with nothing to sign in to. Keep it that way rather than adding sign-in, and don't offer the compose file for a shared or deployed environment. Images are built by CI but not pushed anywhere; the template ships the Dockerfile so users can.
`docker-compose.yml` runs the app with `DOTNET_ENVIRONMENT=docker`, the `movie-reviews-data` volume a pinned OpenTelemetry Collector that receives the app's OTLP logs, traces and metrics, configured by `otel-collector.yaml`, and a pinned Aspire dashboard the collector forwards them to (`telemetry`). The dashboard UI and the collector's OTLP ports are published on the template's own ports (below), so a `dotnet run` can send to the collector too. The dashboard runs with `ASPIRE_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS` and the collector's OTLP ports take anything sent to them, on purpose: the compose file is for a developer's machine, with nothing to sign in to. That's why every published port binds to `127.0.0.1`, as in `"127.0.0.1:<host port>:18888"`, so nobody else on the same network can reach the app, the dashboard or the collector. Keep it that way rather than adding sign-in, and don't offer the compose file for a shared or deployed environment. Images are built by CI but not pushed anywhere; the template ships the Dockerfile so users can.

The Lambda template's `run` stage is the AWS Lambda .NET base image instead. It runs as user `65534` from `/var/task`, keeps its database in `/tmp`, and has no `HEALTHCHECK`. Its compose file runs the collector and the Aspire dashboard as well. Compose maps the runtime interface emulator to 7350, so the function can be invoked with a `POST` to `/2015-03-31/functions/function/invocations`.

Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/openapi-and-scalar/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The OpenAPI document is generated at runtime by `Microsoft.AspNetCore.OpenApi` a
| Transformer | Adds |
| --- | --- |
| `DocumentInfoTransformer` | Title (with the environment name), description, contact, licence, external docs and the tag descriptions |
| `BearerSecurityTransformer` | Only in a solution with authentication, and only when `Authentication:Authority` is set: the `Bearer` security scheme, a security requirement and a `401` response on every operation that needs a token |
| `BearerSecurityTransformer` | Only in a solution with authentication, and only when `Authentication:Authority` or `dotnet user-jwts` is set up: the `Bearer` security scheme, a security requirement and a `401` response on every operation that needs a token |

Scalar is set up in `ProgramExtensions.ConfigureApplication` with the title, C# `HttpClient` as the default code sample, and its developer tools, agent, MCP and telemetry features turned off.

Expand Down
8 changes: 6 additions & 2 deletions .agents/skills/optional-token-auth/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ metadata:

## The rule

Authentication is a choice made when the solution is created. With `--include-auth`, `--authority` and usually `--audience`, every request needs a valid JWT bearer token from that authority (an OAuth access token in the MCP server), and those options fill in `Authentication:Authority` and `Authentication:Audience`. Without `--include-auth` there's no authentication code at all: no settings, no package, no policy and nothing asking for authorization. If `AuthenticationSettings` isn't in the solution, it was created without `--include-auth`; see "Adding it later".
Authentication is a choice made when the solution is created. With `--include-auth`, `--authority` and `--audience`, every request needs a valid JWT bearer token from that authority (an OAuth access token in the MCP server), and those options fill in `Authentication:Authority` and `Authentication:Audience`. Without `--include-auth` there's no authentication code at all: no settings, no package, no policy and nothing asking for authorization. If `AuthenticationSettings` isn't in the solution, it was created without `--include-auth`; see "Adding it later".

With `--include-auth`, endpoints always ask for authorization. An empty `Authentication:Authority` still runs everything open, because the default policy then lets every request through, which is how the tests run unless they set an authority. Only health checks and the API reference (where a template has them) are anonymous. The version endpoint is protected like everything else; telemetry carries `service.version` for anyone who needs to know what's deployed. The Lambda template has no token authentication; API Gateway or the caller's IAM role does that job.

Settings are read through `AuthenticationSettings.FromConfiguration` (`Enabled` is true when the authority isn't empty).
Settings are read through `AuthenticationSettings.FromConfiguration`. `Enabled` is true when the authority isn't empty or `dotnet user-jwts` is set up.

For a token on a developer's machine, `dotnet user-jwts create --project src/Presentation` writes `Authentication:Schemes:Bearer` (issuer and audiences) into `appsettings.Development.json` and the signing key into user secrets. When that issuer is there, `UsesDevelopmentTokens` is true and wins over the authority: the JWT bearer handler gets no authority and reads the issuer, audiences and key from that section by itself. Both files only load in `Development`, and `DeploymentGuard` refuses to start with that issuer set in any other environment, so deployed copies keep using the authority. `DevelopmentTokenTests` sets the same keys in memory and signs a token with them.

Open is only fine on a developer's machine. In the web templates, with or without `--include-auth`, `DeploymentGuard` stops a deployed environment (anything but `Development` and `docker`) starting without authentication unless `Authentication:AllowAnonymous` is `true`, and checks that a set authority is an `https` URL and comes with an audience (`security-defaults`).

## Steps

Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/presentation-integration-tests/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:

## The rule

`tests/Presentation.Tests.Integration` makes real HTTP requests to the whole application in memory. `WebApiApplication` (a `WebApplicationFactory<Program>`) starts the app with the `local` environment, its own shared-cache in-memory SQLite database and the sample data on, so tests never touch a file or each other.
`tests/Presentation.Tests.Integration` makes real HTTP requests to the whole application in memory. `WebApiApplication` (a `WebApplicationFactory<Program>`) starts the app with the `Development` environment, its own shared-cache in-memory SQLite database and the sample data on, so tests never touch a file or each other.

Each test class creates one `WebApiApplication` in its constructor and disposes it. The constructor takes three optional arguments:

Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/sample-data/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ There are two kinds of data, and they never mix.
| Sample data | `src/Infrastructure/Databases/MovieReviews/Extensions/MovieReviewsDbContextExtensions.cs` (`AddData`) | Local runs, Docker and the presentation integration tests | 15 authors, 50 movies, 150 reviews from Bogus with a fixed seed |
| Test data | `tests/Infrastructure.Tests.Integration/Databases/MovieReviews/Extensions/MovieReviewsCollectionData.cs` (`AddTestData`) | Repository tests | A handful of hand-written rows named "One", "Two", "Three" so assertions can find them |

Sample data is only added when `Database:SeedSampleData` is `true` (on in `appsettings.local.json` and `appsettings.docker.json`, off in `appsettings.json`; Lambda turns it on only in `docker-compose.yml`) and the database is empty. A deployed copy never starts with fake data.
Sample data is only added when `Database:SeedSampleData` is `true` (on in `appsettings.Development.json` and `appsettings.docker.json`, off in `appsettings.json`; Lambda turns it on only in `docker-compose.yml`) and the database is empty. A deployed copy never starts with fake data.

## Steps

Expand Down
71 changes: 71 additions & 0 deletions .agents/skills/security-defaults/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
name: security-defaults
description: What this Web API does to be safe by default - the startup guard for deployed environments, the security response headers, optional rate limiting and where the other protections live. Use when adding a response header, turning on or tuning rate limiting, changing security middleware, or asked whether the app is safe to deploy.
metadata:
scope: presentation
---

# Security defaults (Web API)

## The rule

The app ships safe by default, and each protection lives in one place in `src/Presentation/Security/`, switched on in the Security region of `ProgramExtensions`. Keep that shape: a new protection gets its own small class there and a line in that region, never a check scattered across endpoints.

| Protection | Where | Default |
| --- | --- | --- |
| Refuse to start deployed and open | `DeploymentGuard` | On; `Authentication:AllowAnonymous` opts out |
| Security response headers | `SecurityHeaders` | Always on |
| Rate limiting per client IP address | `RateLimiting/` | Only with `--include-rate-limiting`, and off until `RateLimiting:PermitLimit` is set |
| HSTS and HTTPS redirection | Security region | HSTS outside local runs |
| Compose ports on `127.0.0.1` | `docker-compose.yml` (`docker-and-compose`) | Always |

## Deployment guard

`DeploymentGuard.EnsureSafeToStart` runs first in the startup, before any service is added. `Development` (`dotnet run`) and `docker` (Docker Compose) are a developer's machine. Every other environment name, including `Production` (the image's default), `uat` or anything a team invents, counts as deployed, so a new name fails closed.

- A deployed environment without authentication throws at startup, unless `Authentication:AllowAnonymous` is `true`. The message names the setting.
- In a solution with authentication, a set `Authentication:Authority` must be an absolute `https` URL and come with an `Authentication:Audience`, in every environment. Without an audience every token would be rejected, so the app stops at startup and says so.
- In a solution with authentication, `dotnet user-jwts` development tokens (`Authentication:Schemes:Bearer:ValidIssuer`) set up outside `Development` throw at startup, because they replace the authority.

Don't add environment names to `IsLocal` to get a deployment past the guard; set `Authentication:AllowAnonymous` there instead, so the choice is visible in that environment's settings. `tests/Presentation.Tests.Integration/Security/DeploymentGuardTests.cs` starts the app in each kind of environment.

## Security headers

`UseSecurityHeaders` adds three headers to every response, errors and health checks included:

| Header | Value | Why |
| --- | --- | --- |
| `X-Content-Type-Options` | `nosniff` | Browsers use the declared content type instead of guessing |
| `X-Frame-Options` | `DENY` | No other site can show the app in a frame |
| `Referrer-Policy` | `no-referrer` | Links out of the app don't leak its URLs |

There's no `Content-Security-Policy` on purpose. Scalar is served from the same app and needs scripts, styles and frames of its own, so a strict policy would break it. The headers are set in `Response.OnStarting`, because the exception handler clears headers set before it runs.

## Rate limiting

A solution created with `--include-rate-limiting` has a fixed window limiter in `src/Presentation/RateLimiting/`, wired in by `ProgramExtensions`. It's the one protection outside `Security/`, because the whole folder is left out without the toggle. It stays off until `RateLimiting:PermitLimit` is above `0`:

``` json
"RateLimiting": {
"PermitLimit": 100,
"Window": "00:01:00"
}
```

- Every request counts against the caller's IP address. Past the limit the app answers `429 Too Many Requests` with a `Retry-After` header.
- `/health` is never limited, so a busy client can't make the app look down to its host.
- The limiter runs before authentication, so a flood of calls without a token is limited too.
- Behind a reverse proxy or load balancer every request comes from the proxy's address, so all clients share one window. Set `ASPNETCORE_FORWARDEDHEADERS_ENABLED=true` there, which makes the app read the client's address from the proxy's `X-Forwarded-For` header before any of its own middleware runs, or limit at the proxy instead.

`tests/Presentation.Tests.Integration/RateLimiting/RateLimitingTests.cs` checks the 429, the health exemption and that nothing is limited by default. To try it by hand, set a `PermitLimit` of `2` and make three calls inside a minute: the third gets the 429.

## Steps

1. Add a header in `SecurityHeaders`, not in an endpoint or a filter, so every response gets it.
2. Add it to the theory in `tests/Presentation.Tests.Integration/Security/SecurityHeadersTests.cs`, which checks a healthy response and a 404.
3. Check it by hand: `curl -sI http://localhost:7340/health/live`.

## Check

- [ ] The header is set in `SecurityHeaders` and tested for a success and an error response.
- [ ] No `Content-Security-Policy` unless the browser tools have been checked with it.
4 changes: 2 additions & 2 deletions .agents/skills/serilog/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Serilog is the logger. Application and Infrastructure code log through `Microsof

| Environment | Sink | Where to look |
| --- | --- | --- |
| `local` | Console, with the output template in `appsettings.local.json` | The terminal |
| `Development` | Console, with the output template in `appsettings.Development.json` | The terminal |
| `docker` | Console, and OTLP through the collector to the Aspire dashboard | The Aspire dashboard, on the port `docs/RUN.md` lists, or `docker compose logs` |
| Lambda, anywhere | Console, as compact JSON, and OTLP when the endpoint is set | CloudWatch Logs, or `docker compose logs` for the emulator, and the Aspire dashboard in Docker Compose |
| Any, when `OTEL_EXPORTER_OTLP_ENDPOINT` is set | Also OTLP, through `Serilog.Sinks.OpenTelemetry` | Whatever receives the OTLP (`telemetry`) |
Expand All @@ -29,7 +29,7 @@ Every event carries these properties:
| `AssemblyVersion`, `AssemblyInformationalVersion` | Added in code at startup |
| Anything pushed with `LogContext` | `FromLogContext` |

Levels are set in configuration, never in code: `Information` by default and `Debug` in `local`, with `Information` for `Microsoft`, `Warning` for `Microsoft.AspNetCore` and `Microsoft.EntityFrameworkCore.Database.Command` (a line per SQL statement buries everything else, and EF Core's meter already counts them), and `Error` for `System`. The built-in `Logging` section is set to `None` so nothing is written twice.
Levels are set in configuration, never in code: `Information` by default and `Debug` in `Development`, with `Information` for `Microsoft`, `Warning` for `Microsoft.AspNetCore` and `Microsoft.EntityFrameworkCore.Database.Command` (a line per SQL statement buries everything else, and EF Core's meter already counts them), and `Error` for `System`. The built-in `Logging` section is set to `None` so nothing is written twice.

Three things log a request, and each says something the others don't:

Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/telemetry/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Each layer publishes under its own name, and only the presentation layer's start
| --- | --- | --- |
| Application | `ApplicationTelemetry.Name` activity source: a span per use case, named after the request, tagged with `application.request.name` and `application.request.outcome` | `LoggingBehaviour`, with `System.Diagnostics` only, so Application stays free of telemetry packages |
| Infrastructure | `Microsoft.EntityFrameworkCore` meter: queries, saves, active contexts, compiled query cache | Built into EF Core; startup subscribes to it by name |
| Presentation | `service.name`, `service.version` and `deployment.environment.name` on everything, plus its own instrumentation | `TelemetrySettings` and the startup's Telemetry region |
| Presentation | `service.name`, `service.version` and `deployment.environment.name` on everything, plus its own instrumentation | `TelemetrySettings`, which takes `service.version` from `VersionInfo`, and the startup's Telemetry region |

What each presentation layer adds:

Expand Down
Loading
Loading