// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 namespace Progmasoft.Progmaweb.Api.Security; /// Adds the security headers of a JSON API to every response. /// /// The API serves no documents, so its content security policy allows nothing, it may not be framed, and its /// responses are never cached. /// /// The next middleware in the pipeline. internal sealed class SecurityHeadersMiddleware(RequestDelegate next) { /// Sets the headers and continues the pipeline. /// The current request. /// A task that completes when the rest of the pipeline has run. public async Task InvokeAsync(HttpContext context) { IHeaderDictionary headers = context.Response.Headers; headers["Content-Security-Policy"] = "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'"; headers["Referrer-Policy"] = "no-referrer"; headers["X-Content-Type-Options"] = "nosniff"; headers["X-Frame-Options"] = "DENY"; headers.Append("Permissions-Policy", "camera=(), microphone=(), geolocation=()"); headers["Cache-Control"] = "no-store"; await next(context); } }