Giter Club home page Giter Club logo

bassinet's Introduction

Go Report Card

Bassinet

Bassinet is a set of 11 utility middlewares to help secure HTTP headers. It's based on the widely used helmet.js. Includes middleware functions for setting the following headers:

Usage

Initialize the middleware with the desired options —if any— and handle the returned error.

referrerPolicy, err := bassinet.ReferrerPolicy([]{
	bassinet.PolicyOrigin,
	bassinet.PolicyUnsafeURL,
})
if err != nil {
    // handle error
}

With ServeMux

To use bassinet with the builtin ServeMux you just wrap it with the initialized middleware.

mux := http.NewServeMux()
mux.HandleFunc("/", home)
srv := http.Server{
	Handler: referrerPolicy(mux)
}

As you might probably want to chain several of the middlewares it is recommended to use a composing function.

With justinas/alice

xssFilter, err := bassinet.XSSFilter()
if err != nil {
    // handle error
}
htsts, err := bassinet.StrictTransportSecurity(StrictTransportOption{
	maxAge:            60,
  excludeSubdomains: true,
})
if err != nil {
	// handle error
}

middleware := alice.New(xssFilter, htsts)

Response Headers

X-XSS-Protection

XSSFilter sets X-XSS-Protection header to 0 to prevent attackers from blocking legit code or inferring information. Read more. XSSFilter accepts no options.

xssFilter, err := bassinet.XSSFilter()
if err != nil {
	// Handle error
}

srv := http.Server{
	Handler: xssFilter(mux)
}

Strict-Transport-Security

StrictTransportSecurity sets Strict-Transport-Security so that browsers remember if HTTPS is available, to avoid insecure connection before redirect. Read more.

It accepts a bassinet.StrictTransportOptions struct to set the following directives:

  • maxAge: Time (in seconds) that the browser should remember if the site has HTTPS. Defaults to 180 days. int
  • excludeSubdomains: Optional. If set the browser will apply directive to subdomains. bool
  • preload: Optional. If set the browser will check the Preloading Strict Transport Security public list, enabling STS also on first load. bool
policies := bassinet.StrictTransportOptions{
	maxAge:            60 * 60 * 24 * 7, // recheck every week
	excludeSubdomains: true,
	preload: true,
}

sts, err := bassinet.StrictTransportSecurity(policies)
if err != nil {
	// Handle error
}

srv := http.Server{
	Handler: sts(mux)
}

X-Permitted-Cross-Domain-Policies

PermittedCrossDomainPolicies sets X-Permitted-Cross-Domain-Policies header to tell some user-agents (most notably Adobe products) your domain's policy for loading cross-domain content. Read more.

Accepts the following policies:

  • PCDPNone: No crossdomain.xml file is allowed.
  • PCDPMasterOnly: Only check crossdomain.xml in the root directory of the website.
  • PCDPByContentType: Only accept files with type text/x-cross-domain-policy.
  • PCDPAll: Allow any crossdomain.xml files.
permittedCrossDomainPolicies, err := bassinet.PermittedCrossDomainPolicies(bassinet.PCDPByContentType)
if err != nil {
	// Handle error
}

srv := http.Server{
	Handler: permittedCrossDomainPolicies(mux)
}

X-Download-Options

IeNoOpen sets X-Download-Options to noopen to prevent IE users to execute downloads in your site's context. Read more.

ieNoOpen := bassinet.IeNoOpen()

srv := http.Server{
	Handler: ieNoOpen(mux)
}

bassinet's People

Contributors

guillem-gelabert avatar

Watchers

 avatar

Forkers

forkkit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.