Helmet
Helmet middleware provides protection against cross-site scripting (XSS) attack, content type sniffing, clickjacking, insecure connection and other code injection attacks.
Installation
- go get -u github.com/gofiber/helmet
Signature
- helmet.New(config ...Config) func(*Ctx)
Config
Property | Type | Description | Default |
---|---|---|---|
Filter | func(*fiber.Ctx) bool | Defines a function to skip middleware | nil |
XSSProtection | string | XSSProtection provides protection against cross-site scripting attack (XSS) by setting the X-XSS-Protection header. | 1; mode=block” |
ContentTypeNosniff | string | ContentTypeNosniff provides protection against overriding Content-Type header by setting the X-Content-Type-Options header. | “nosniff” |
XFrameOptions | string | XFrameOptions can be used to indicate whether or not a browser should be allowed to render a page in a , or . Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites.provides protection against clickjacking. Possible values: SAMEORIGIN, DENY, ALLOW-FROM uri | “SAMEORIGIN” |
HSTSMaxAge | int | HSTSMaxAge sets the Strict-Transport-Security header to indicate how long (in seconds) browsers should remember that this site is only to be accessed using HTTPS. This reduces your exposure to some SSL-stripping man-in-the-middle (MITM) attacks. | </td></tr><tr><td>HSTSExcludeSubdomains</td><td><code>bool</code></td><td>HSTSExcludeSubdomains won't include subdomains tag in the <code>Strict Transport Security</code> header, excluding all subdomains from security policy. It has no effect unless HSTSMaxAge is set to a non-zero value.</td><td> |
ContentSecurityPolicy | string | ContentSecurityPolicy sets the Content-Security-Policy header providing security against cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context | </td></tr><tr><td>CSPReportOnly</td><td><code>bool</code></td><td></td><td> |
HSTSPreloadEnabled | bool | </td></tr><tr><td>ReferrerPolicy</td><td><code>string</code></td><td></td><td> |
Example
- package main
- import (
- "github.com/gofiber/fiber"
- "github.com/gofiber/helmet"
- )
- func main() {
- app := fiber.New()
- app.Use(helmet.New())
- app.Get("/", func(c *fiber.Ctx) {
- c.Send("Welcome!")
- })
- app.Listen(3000)
- // curl -I http://localhost:3000
- }