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
41 changes: 41 additions & 0 deletions .agents/skills/security-defaults/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
name: security-defaults
description: What this MCP server does to be safe by default - the security response headers, and where the other protections live. Use when adding a response header, changing security middleware, or asked whether the app is safe to deploy.
metadata:
scope: presentation
---

# Security defaults (MCP server)

## 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 `WebApplicationExtensions.ConfigureApplication`. 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 |
| --- | --- | --- |
| Security response headers | `SecurityHeaders` | Always on |
| HSTS and HTTPS redirection | Security region | HSTS outside local runs |
| Compose ports on `127.0.0.1` | `docker-compose.yml` (`docker-and-compose`) | Always |

## 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. The headers are set in `Response.OnStarting`, because the exception handler clears headers set before it runs.

## 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:7320/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.
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ These are for this MCP server's presentation layer. The other templates have the
| `api-conventions` | Name a tool, set its annotations, shape a result or change an error message |
| `json-serialization` | Add a result model or change how JSON looks |
| `presentation-integration-tests` | Write a test that talks to the server through an MCP client |
| `security-defaults` | Add a response header or other protection, or check what the app does to stay safe |
| `mcp-inspector` | Try the server by hand as a client sees it |
<!--#if (false) -->

Expand Down
3 changes: 3 additions & 0 deletions src/Presentation/Extensions/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace CleanModelContextProtocol.Presentation.Extensions;
#if includeAuth
using Presentation.Authentication;
#endif
using Presentation.Security;
using Serilog;

[ExcludeFromCodeCoverage]
Expand All @@ -24,6 +25,8 @@ public static WebApplication ConfigureApplication(this WebApplication app)

#region Security

_ = app.UseSecurityHeaders();

// HSTS tells browsers to only use HTTPS from then on, so it's left off for local runs.
if (!app.Environment.IsDevelopment() && !app.Environment.IsEnvironment("local"))
{
Expand Down
34 changes: 34 additions & 0 deletions src/Presentation/Security/SecurityHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace CleanModelContextProtocol.Presentation.Security;

using Microsoft.AspNetCore.Builder;

/// <summary>
/// The response headers every response carries, errors included. No Content-Security-Policy, so the browser tools keep
/// working.
/// </summary>
public static class SecurityHeaders
{
/// <summary>
/// Adds <c>X-Content-Type-Options: nosniff</c>, <c>X-Frame-Options: DENY</c> and <c>Referrer-Policy: no-referrer</c>
/// to every response.
/// </summary>
/// <param name="app">The application to add the headers to.</param>
/// <returns>The application, for chaining.</returns>
public static IApplicationBuilder UseSecurityHeaders(this IApplicationBuilder app)
{
return app.Use(async (context, next) =>
{
// Set as the response starts, because the exception handler clears headers set earlier.
context.Response.OnStarting(() =>
{
var headers = context.Response.Headers;
headers.XContentTypeOptions = "nosniff";
headers.XFrameOptions = "DENY";
headers["Referrer-Policy"] = "no-referrer";
return Task.CompletedTask;
});

await next(context);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace CleanModelContextProtocol.Presentation.Tests.Integration.Security;

using System.Threading.Tasks;
using Shouldly;
using Xunit;

public class SecurityHeadersTests : IDisposable
{
private readonly ModelContextProtocolApplication application = new();

[Theory]
[InlineData("/health/live")]
[InlineData("/not-a-real-path")]
public async Task Response_ShouldCarry_SecurityHeaders(string path)
{
// Arrange
using var client = this.application.CreateClient();

// Act
using var response = await client.GetAsync(path);

// Assert
response.Headers.GetValues("X-Content-Type-Options").ShouldBe(["nosniff"]);
response.Headers.GetValues("X-Frame-Options").ShouldBe(["DENY"]);
response.Headers.GetValues("Referrer-Policy").ShouldBe(["no-referrer"]);
}

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.application.Dispose();
}
}
}
Loading