Skip to content

Security Headers ​

xp::securityHeaders() adds HTTP security headers to every response. These headers protect against common attacks like XSS, clickjacking, and MIME sniffing.

Basic usage ​

cpp
app.use(xp::securityHeaders());

That's it. By default it sets these headers on every response:

HeaderValueProtects against
X-Content-Type-OptionsnosniffMIME type sniffing
X-Frame-OptionsDENYClickjacking
Referrer-Policyno-referrerInformation leakage
X-XSS-Protection0Old XSS filter bugs

Custom options ​

cpp
xp::SecurityHeadersOptions opts;
opts.contentTypeOptions  = true;
opts.frameOptions        = true;
opts.frameOptionsValue   = "SAMEORIGIN"; // Allow iframes from same origin
opts.referrerPolicy      = true;
opts.referrerPolicyValue = "strict-origin-when-cross-origin";
opts.xssProtection       = true;
opts.hsts                = true;  // Enable HSTS (HTTPS only!)
opts.hstsValue           = "max-age=31536000; includeSubDomains";

app.use(xp::securityHeaders(opts));

Enable HSTS only on HTTPS

hsts (HTTP Strict Transport Security) tells browsers to always use HTTPS for your domain. Only enable it when your server is running with a valid TLS certificate — it cannot be easily undone.


Options reference ​

FieldDefaultDescription
contentTypeOptionstrueSet X-Content-Type-Options: nosniff
frameOptionstrueSet X-Frame-Options
frameOptionsValue"DENY"Value for X-Frame-Options
referrerPolicytrueSet Referrer-Policy
referrerPolicyValue"no-referrer"Value for Referrer-Policy
xssProtectiontrueSet X-XSS-Protection: 0
hstsfalseSet Strict-Transport-Security
hstsValue"max-age=15552000; includeSubDomains"Value for HSTS header

Built with ❤️ on top of Drogon, the world's fastest C++ HTTP framework.