Documentation
¶
Overview ¶
Package secure is an HTTP middleware for Go that handles adding security headers to HTTP responses, and accompanying security checks.
package main
import (
"net/http"
"github.com/kenshaw/secure"
)
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
func main() {
secureMiddleware := secure.New(
secure.AllowedHosts("www.example.com", "sub.example.com"),
secure.SSLRedirect(true),
})
app := secureMiddleware.Handler(myHandler)
http.ListenAndServe("127.0.0.1:3000", app)
}
Index ¶
- func DefaultBadHostHandler(w http.ResponseWriter, r *http.Request)
- type Error
- type Middleware
- type Option
- func AllowedHosts(allowedHosts ...string) Option
- func BadHostHandler(badHostHandler http.HandlerFunc) Option
- func BrowserXSSFilter(browserXSSFilter bool) Option
- func ContentSecurityPolicy(contentSecurityPolicy string) Option
- func ContentTypeNosniff(contentTypeNosniff bool) Option
- func CustomBrowserXSSValue(customBrowserXSSValue string) Option
- func CustomFrameOptionsValue(customFrameOptionsValue string) Option
- func DevEnvironment(isDevEnvironment bool) Option
- func ForceSTSHeader(forceSTSHeader bool) Option
- func FrameDeny(frameDeny bool) Option
- func HostsProxyHeaders(hostsProxyHeaders ...string) Option
- func ReferrerPolicy(referrerPolicy string) Option
- func SSLForwardedProxyHeaders(m map[string]string) Option
- func SSLHost(sslHost string) Option
- func SSLRedirect(sslRedirect bool) Option
- func SSLTemporaryRedirect(sslTemporaryRedirect bool) Option
- func STSIncludeSubdomains(stsIncludeSubdomains bool) Option
- func STSPreload(stsPreload bool) Option
- func STSSeconds(stsSeconds int64) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultBadHostHandler ¶
func DefaultBadHostHandler(w http.ResponseWriter, r *http.Request)
DefaultBadHostHandler is the default bad host http handler.
Types ¶
type Error ¶
type Error string
Error is a secure error.
type Middleware ¶
type Middleware struct {
// AllowedHosts is a list of fully qualified domain names that are allowed.
// When empty, allows any host.
AllowedHosts []string
// HostsProxyHeaders is a set of header keys that may hold a proxied
// hostname value for the request.
HostsProxyHeaders []string
// If SSLRedirect is set to true, then only allow https requests.
SSLRedirect bool
// If SSLTemporaryRedirect is true, the a 302 will be used while
// redirecting.
SSLTemporaryRedirect bool
// SSLHost is the host name that is used to redirect http requests to
// https. If not set, indicates to use the same host.
SSLHost string
// SSLForwardedProxyHeaders is the set of header keys with associated
// values that would indicate a valid https request. This is used when
// proxying requests from behind another webserver (ie, nginx, apache,
// etc).
//
// &secure.Middleware{
// SSLForwardedProxyHeaders: map[string]string{
// "X-Forwarded-Proto": "https",
// },
// }
//
SSLForwardedProxyHeaders map[string]string
// STSSeconds is the max-age of the Strict-Transport-Security header.
// Header will not be included if STSSeconds = 0.
STSSeconds int64
// When STSIncludeSubdomains is true, `includeSubdomains` will be appended to
// the Strict-Transport-Security header.
STSIncludeSubdomains bool
// When STSPreload is true, the `preload` flag will be appended to the
// Strict-Transport-Security header.
STSPreload bool
// When ForceSTSHeader is true, the STS header will be added even when the
// connection is HTTP.
ForceSTSHeader bool
// When FrameDeny is true, adds the X-Frame-Options header with the value
// of `DENY`.
FrameDeny bool
// CustomFrameOptionsValue allows the X-Frame-Options header value to be
// set with a custom value. Overrides the FrameDeny option.
CustomFrameOptionsValue string
// If ContentTypeNosniff is true, adds the X-Content-Type-Options header
// with the value `nosniff`.
ContentTypeNosniff bool
// If BrowserXSSFilter is true, adds the X-XSS-Protection header with the
// value `1; mode=block`.
BrowserXSSFilter bool
// CustomBrowserXSSValue allows the X-XSS-Protection header value to be set
// with a custom value. This overrides the BrowserXSSFilter option.
CustomBrowserXSSValue string
// ContentSecurityPolicy allows the Content-Security-Policy header value to
// be set with a custom value.
ContentSecurityPolicy string
// ReferrerPolicy configures which the browser referrer policy.
ReferrerPolicy string
// BadHostHandler is the bad host handler.
BadHostHandler http.HandlerFunc
// When DevEnvironment is true, disables the AllowedHosts, SSL, and STS
// checks.
//
// This should be toggled only when testing / developing, and is necessary
// when testing sites configured only for https from a http based
// connection.
//
// If you would like your development environment to mimic production with
// complete Host blocking, SSL redirects, and STS headers, leave this as
// false.
DevEnvironment bool
}
Middleware that sets basic security headers and provides simple security checks for http servers.
func New ¶
func New(opts ...Option) *Middleware
New constructs a new secure Middleware instance with the supplied options.
func (*Middleware) Handler ¶
func (s *Middleware) Handler(h http.Handler) http.Handler
Handler implements the http.HandlerFunc for integration with the standard net/http lib.
func (*Middleware) HandlerFuncWithNext ¶
func (s *Middleware) HandlerFuncWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
HandlerFuncWithNext is a special implementation for Negroni, but could be used elsewhere.
func (*Middleware) Process ¶
func (s *Middleware) Process(w http.ResponseWriter, r *http.Request) error
Process runs the actual checks and returns an error if the middleware chain should stop.
type Option ¶
type Option func(*Middleware)
Option is a secure Middleware option.
func AllowedHosts ¶
AllowedHosts is an option to set the allowed hosts.
func BadHostHandler ¶
func BadHostHandler(badHostHandler http.HandlerFunc) Option
BadHostHandler is an option to set the bad host handler.
func BrowserXSSFilter ¶
BrowserXSSFilter is an option to set browser xss filter.
func ContentSecurityPolicy ¶
ContentSecurityPolicy is an option to set the content security policy.
func ContentTypeNosniff ¶
ContentTypeNosniff is an option to set content type to NOSNIFF.
func CustomBrowserXSSValue ¶
CustomBrowserXSSValue is an option to set custom browser xss value.
func CustomFrameOptionsValue ¶
CustomFrameOptionsValue is an option to set custom frame options value.
func DevEnvironment ¶
DevEnvironment is an option to set toggle development environment options.
func ForceSTSHeader ¶
ForceSTSHeader is an option to force STS header.
func HostsProxyHeaders ¶
HostsProxyHeaders is an option to set the host proxy headers.
func ReferrerPolicy ¶
ReferrerPolicy is an option to set the referrer policy.
func SSLForwardedProxyHeaders ¶
SSLForwardedProxyHeaders is an option to set the SSL forwarded proxy headers.
func SSLRedirect ¶
SSLRedirect is an option to toggle ssl redirect.
func SSLTemporaryRedirect ¶
SSLTemporaryRedirect is an option to set the SSL temporary redirect.
func STSIncludeSubdomains ¶
STSIncludeSubdomains is an option to set STS include subdomains.
func STSPreload ¶
STSPreload is an option to set STS preload.
func STSSeconds ¶
STSSeconds is an option to set the STS seconds.
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
chi
command
examples/chi/main.go
|
examples/chi/main.go |
|
echo
command
examples/echo/main.go
|
examples/echo/main.go |
|
gin
command
examples/gin/main.go
|
examples/gin/main.go |
|
goji
command
examples/goji/main.go
|
examples/goji/main.go |
|
iris
command
examples/iris/main.go
|
examples/iris/main.go |
|
negroni
command
examples/negroni/main.go
|
examples/negroni/main.go |
|
redirect
command
examples/redirect/main.go
|
examples/redirect/main.go |
|
std
command
examples/std/main.go
|
examples/std/main.go |