Skip to content
AI-assisted, verified against source

Security Headers

New in 3.6.0

Security Headers middleware was added in version 3.6.0.

Configurable security headers middleware to protect against common web vulnerabilities. The middleware adds HTTP security headers to all responses.

Overview

json
json
{
  "SecurityHeaders": {
    "Enabled": false,
    "XContentTypeOptions": "nosniff",
    "XFrameOptions": "DENY",
    "ReferrerPolicy": "strict-origin-when-cross-origin",
    "ContentSecurityPolicy": null,
    "PermissionsPolicy": null,
    "CrossOriginOpenerPolicy": null,
    "CrossOriginEmbedderPolicy": null,
    "CrossOriginResourcePolicy": null
  }
}

Settings Reference

SettingTypeDefaultDescription
EnabledboolfalseEnable security headers middleware. When enabled, configured headers are added to all HTTP responses.
XContentTypeOptionsstring"nosniff"Prevents browsers from MIME-sniffing a response away from the declared content-type. Set to null to not include this header.
XFrameOptionsstring"DENY"Controls whether the browser should allow the page to be rendered in a frame. Values: "DENY", "SAMEORIGIN". Set to null to not include.
ReferrerPolicystring"strict-origin-when-cross-origin"Controls how much referrer information should be included with requests. Set to null to not include.
ContentSecurityPolicystringnullDefines approved sources of content that the browser may load. Helps prevent XSS and code injection attacks.
PermissionsPolicystringnullControls which browser features and APIs can be used.
CrossOriginOpenerPolicystringnullControls how your document is shared with cross-origin popups.
CrossOriginEmbedderPolicystringnullPrevents a document from loading cross-origin resources that don't explicitly grant permission.
CrossOriginResourcePolicystringnullIndicates how the resource should be shared cross-origin.

X-Content-Type-Options

Prevents browsers from MIME-sniffing a response away from the declared content-type, reducing exposure to drive-by download attacks.

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "XContentTypeOptions": "nosniff"
  }
}

The recommended value is "nosniff".

X-Frame-Options

Controls whether the browser should allow the page to be rendered in a <frame>, <iframe>, <embed> or <object>. Prevents clickjacking attacks.

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "XFrameOptions": "DENY"
  }
}
ValueDescription
DENYNever allow the page to be framed
SAMEORIGINAllow framing from the same origin only

WARNING

This header is skipped if Antiforgery is enabled, as Antiforgery already sets X-Frame-Options: SAMEORIGIN by default via its SuppressXFrameOptionsHeader setting.

Referrer-Policy

Controls how much referrer information should be included with requests made from your site.

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "ReferrerPolicy": "strict-origin-when-cross-origin"
  }
}
ValueDescription
no-referrerNever send referrer information
no-referrer-when-downgradeSend full URL for same-security requests, nothing for downgrades
originSend only the origin (scheme, host, port)
origin-when-cross-originSend full URL for same-origin, origin only for cross-origin
same-originSend full URL for same-origin, nothing for cross-origin
strict-originSend origin for same-security, nothing for downgrades
strict-origin-when-cross-originSend full URL for same-origin, origin for cross-origin same-security
unsafe-urlAlways send full URL (not recommended)

Content-Security-Policy

Defines approved sources of content that the browser may load. This is the primary browser-side defense against XSS, clickjacking, and other code injection attacks.

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "ContentSecurityPolicy": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'"
  }
}

TIP

CSP should be configured based on your specific application needs. Start with a restrictive policy and loosen as needed.

Common directives:

  • default-src - Fallback for other directives
  • script-src - Valid sources for JavaScript
  • style-src - Valid sources for stylesheets
  • img-src - Valid sources for images
  • connect-src - Valid sources for fetch, WebSocket, etc.
  • font-src - Valid sources for fonts
  • frame-src - Valid sources for frames

Reference: MDN Content-Security-Policy

Permissions-Policy

Controls which browser features and APIs can be used by your application and any embedded content.

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "PermissionsPolicy": "geolocation=(), microphone=(), camera=()"
  }
}

This example disables geolocation, microphone, and camera access entirely.

To allow features only from the same origin:

json
json
{
  "SecurityHeaders": {
    "PermissionsPolicy": "geolocation=(self), microphone=(self)"
  }
}

Reference: MDN Permissions-Policy

Cross-Origin Policies

Cross-Origin-Opener-Policy

Controls how your document is shared with cross-origin popups.

json
json
{
  "SecurityHeaders": {
    "CrossOriginOpenerPolicy": "same-origin"
  }
}
ValueDescription
unsafe-noneDefault browser behavior
same-origin-allow-popupsIsolate from cross-origin, allow popups
same-originFull isolation from cross-origin documents

Cross-Origin-Embedder-Policy

Prevents a document from loading cross-origin resources that don't explicitly grant permission.

json
json
{
  "SecurityHeaders": {
    "CrossOriginEmbedderPolicy": "require-corp"
  }
}
ValueDescription
unsafe-noneDefault browser behavior
require-corpRequire CORP or CORS for cross-origin resources
credentiallessLoad cross-origin resources without credentials

TIP

require-corp along with CrossOriginOpenerPolicy: same-origin enables access to SharedArrayBuffer and high-resolution timers.

Cross-Origin-Resource-Policy

Indicates how the resource should be shared cross-origin.

json
json
{
  "SecurityHeaders": {
    "CrossOriginResourcePolicy": "same-origin"
  }
}
ValueDescription
same-siteOnly same-site requests allowed
same-originOnly same-origin requests allowed
cross-originAny origin can load the resource

Example Configurations

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "XContentTypeOptions": "nosniff",
    "XFrameOptions": "DENY",
    "ReferrerPolicy": "strict-origin-when-cross-origin"
  }
}

API-Only Application

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "XContentTypeOptions": "nosniff",
    "XFrameOptions": "DENY",
    "ReferrerPolicy": "no-referrer",
    "ContentSecurityPolicy": "default-src 'none'; frame-ancestors 'none'"
  }
}

Full Protection with CSP

json
json
{
  "SecurityHeaders": {
    "Enabled": true,
    "XContentTypeOptions": "nosniff",
    "XFrameOptions": "SAMEORIGIN",
    "ReferrerPolicy": "strict-origin-when-cross-origin",
    "ContentSecurityPolicy": "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self'",
    "PermissionsPolicy": "geolocation=(), microphone=(), camera=()",
    "CrossOriginOpenerPolicy": "same-origin",
    "CrossOriginEmbedderPolicy": "require-corp",
    "CrossOriginResourcePolicy": "same-origin"
  }
}

Next Steps

Comments