GithubHelp home page GithubHelp logo

qurio-za / helmet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from helmetjs/helmet

0.0 12.0 0.0 236 KB

Collection of middleware to implement various security headers for Express / Connect

Home Page: http://andyet.net

License: MIT License

JavaScript 100.00%

helmet's Introduction

Helmet

Dependency Status

Helmet is a series of middlewares for Express/Connect apps that implement various security headers to make your app more secure.

Included middleware

  • csp (Content Security Policy)
  • hsts (HTTP Strict Transport Security)
  • xframe (X-Frame-Options)
  • iexss (X-XSS-Protection for IE8+)
  • ienoopen (X-Download-Options for IE8+)
  • contentTypeOptions (X-Content-Type-Options)
  • cacheControl (Cache-Control)
  • hidePoweredBy (remove X-Powered-By)

Installation

npm install helmet

Basic usage

var helmet = require('helmet');

To use a particular middleware application-wide, just use it:

app.use(helmet.csp());
app.use(helmet.xframe('deny'));
app.use(helmet.contentTypeOptions());

If you're using Express, make sure these middlewares are listed before app.router.

If you just want to use the default-level policies, all you need to do is:

helmet.defaults(app);

Don't want all the defaults?

helmet.defaults(app, { xframe: false });
app.use(helmet.xframe('sameorigin'));

Content Security Policy

The Content Security Policy (W3C Draft) is pretty much required reading if you want to do anything with CSP.

Browser Support

Currently there is CSP support in Firefox and experimental support in Chrome. Both X-Content-Security-Policy and X-WebKit-CSP headers are set by Helmet.

There are two different ways to build CSP policies with Helmet.

Using policy()

policy() eats a JSON blob (including the output of its own toJSON() function) to create a policy. By default helmet has a defaultPolicy that looks like;

Content-Security-Policy: default-src 'self'

To override this and create a new policy you could do something like

policy = {
  defaultPolicy: {
    'default-src': ["'self'"],
    'img-src': ['static.andyet.net','*.cdn.example.com'],
  }
}

helmet.csp.policy(policy);

Using add()

The same thing could be accomplished using add() since the defaultPolicy default-src is already 'self':

helmet.csp.add('img-src', ['static.andyet.net', '*.cdn.example.com']);

Reporting Violations

CSP can report violations back to a specified URL. You can either set the report-uri using policy() or add() or use the reportTo() helper function.

helmet.csp.reportTo('http://example.com/csp');

HTTP Strict Transport Security

draft-ietf-websec-strict-transport-sec-04

This middleware adds the Strict-Transport-Security header to the response.

Basic Usage

To use the default header of Strict-Transport-Security: maxAge=15768000:

helmet.hsts();

To adjust other values for maxAge and to include subdomains:

helmet.hsts(1234567, true);  // hsts(maxAge, includeSubdomains)

X-FRAME-OPTIONS

xFrame is a lot more straight forward than CSP. It has three modes. DENY, SAMEORIGIN, ALLOW-FROM. If your app does not need to be framed (and most don't) you can use the default DENY.

Browser Support

  • IE8+
  • Opera 10.50+
  • Safari 4+
  • Chrome 4.1.249.1042+
  • Firefox 3.6.9 (or earlier with NoScript)

Here is an example for both SAMEORIGIN and ALLOW-FROM:

helmet.xframe('sameorigin');
helmet.xframe('allow-from', 'http://example.com');

X-XSS-PROTECTION

The following example sets the X-XSS-PROTECTION: 1; mode=block header:

helmet.iexss();

X-Download-Options

Sets the X-Download-Options header to noopen to prevent IE users from executing downloads in your site's context. For more, see this MSDN blog post.

app.use(helmet.ienoopen())

X-Content-Type-Options

The following example sets the X-Content-Type-Options header to its only and default option, nosniff:

helmet.contentTypeOptions();

Cache-Control

The following example sets the Cache-Control header to no-store, no-cache. This is not configurable at this time.

helmet.cacheControl();

Hide X-Powered-By

This middleware will remove the X-Powered-By header if it is set.

helmet.hidePoweredBy()

Note: if you're using Express, you can skip Helmet's middleware if you want:

app.disable('x-powered-by')

To Be Implemented

  • Warn when self, unsafe-inline or unsafe-eval are not single quoted
  • Warn when unsafe-inline or unsafe-eval are used
  • Caching of generated CSP headers
  • Device to capture and parse reported CSP violations

githalytics.com alpha

helmet's People

Contributors

evanhahn avatar hatchan avatar jbuck avatar jeffharrell avatar mattjay avatar renehamburger avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.