oatpp-authkit/include/oatpp-authkit/interceptor/SecurityHeadersInterceptor.hpp
Uwe Schuster 081e0b36dc v0.2.1: wrap clean-lift headers in namespace oatpp_authkit
The four clean-lift headers (SecurityHeadersInterceptor,
BodySizeLimitInterceptor, JsonErrorHandler, RateLimiter) were copied
verbatim in v0.1.0 and left in the global namespace — consumers that
adopt the library alongside existing same-named classes (e.g. fewo-webapp
during the #417 swap) would hit ODR clashes.

Wrap them in the same namespace the v0.2 auth seams use. Patch bump; no
API surface change beyond the qualifier.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 21:53:21 +02:00

43 lines
1.7 KiB
C++

#ifndef SecurityHeadersInterceptor_hpp
#define SecurityHeadersInterceptor_hpp
#include "oatpp/web/server/interceptor/ResponseInterceptor.hpp"
namespace oatpp_authkit {
/**
* @brief Response interceptor that adds standard security headers to all responses.
*
* Headers added:
* - X-Content-Type-Options: nosniff — prevents MIME type sniffing
* - X-Frame-Options: SAMEORIGIN — prevents clickjacking
* - Referrer-Policy: strict-origin-when-cross-origin — limits referrer leakage
* - Content-Security-Policy — restricts resource loading sources
*/
class SecurityHeadersInterceptor : public oatpp::web::server::interceptor::ResponseInterceptor {
public:
std::shared_ptr<OutgoingResponse> intercept(
const std::shared_ptr<IncomingRequest>& request,
const std::shared_ptr<OutgoingResponse>& response) override {
response->putHeader("X-Content-Type-Options", "nosniff");
response->putHeader("X-Frame-Options", "SAMEORIGIN");
response->putHeader("Referrer-Policy", "strict-origin-when-cross-origin");
response->putHeader("Content-Security-Policy",
"default-src 'self'; "
"script-src 'self' 'unsafe-inline' https://unpkg.com; "
"style-src 'self' 'unsafe-inline' https://unpkg.com; "
"img-src 'self' data: https:; "
"connect-src 'self' wss: ws:; "
"font-src 'self'; "
"frame-ancestors 'self'; "
"base-uri 'self'; "
"form-action 'self'");
response->putHeader("Strict-Transport-Security",
"max-age=63072000; includeSubDomains");
return response;
}
};
} // namespace oatpp_authkit
#endif