Coder Social home page Coder Social logo

reside-eng / fortifyjs Goto Github PK

View Code? Open in Web Editor NEW
0.0 10.0 1.0 10.72 MB

FortifyJS is a library focused on delivering security headers for web applications within the JavaScript ecosystem

License: MIT License

JavaScript 2.45% TypeScript 97.55%
security javascript web headers http content-security-policy cross-site-scripting clickjacking fortified

fortifyjs's Introduction

@side/fortifyjs

A platform-agnostic library for generating security headers for your web application.

NPM version License Build Status Coverage Status semantic-release Code Style

Getting Started

Modern web applications don't just need a helmet; they need a fortress. FortifyJS is just that, the walls to gate client and server-side requests in a world where attackers can manipulate the browser in many ways to break into your web application and steal information of trade secrets.

While helmet.js is useful for express applications, in a world of alternatives to express popping up and different ways of writing JavaScript applications on the rise, there needs to be an alternative that abstracts the production of valid security headers out of our modern applications. FortifyJS exists in this niche. FortifyJS is solely responsible for providing a representation that is useful in setting headers in consumer applications. FortifyJS takes control of the headers; you take over and implement them in your application.

FortifyJS also differs in that it seeks to provide a comprehensive set of security headers to form a default posture for any application.

Installation

Install through your package manager within an existing project:

yarn add @side/fortifyjs

How-to

FortifyJS exports one function: fortifyHeaders. FortifyJS takes an object literal with properties that match each supported security header. This function returns an object mapping ths string header name (e.g. X-Content-Type-Options) to a value (e.g. nosniff).

The current default configuration can be expressed like this:

import { fortifyHeaders, FortifySettings } from '@side/fortifyjs';

const headers = fortifyHeaders({
  xContentTypeOptions: {
    nosniff: true,
  },
  contentSecurityPolicy: {
    defaultSrc: ["'self'"],
    baseUri: ["'self'"],
    fontSrc: ["'self'", 'https:', 'data:'],
    frameAncestors: ["'self'"],
    imgSrc: ["'self'", "'data:'"],
    objectSrc: ["'none'"],
    scriptSrc: ["'self'"],
    scriptSrcAttr: ["'none'"],
    styleSrc: ["'self'", "'https:'", "'unsafe-inline'"],
    upgradeInsecureRequests: true,
  },
  crossOriginEmbedderPolicy: {
    requireCorp: true,
  },
  crossOriginOpenerPolicy: {
    sameOrigin: true,
  },
  crossOriginResourcePolicy: {
    sameOrigin: true,
  },
  expectCt: {
    maxAge: 0,
  },
  originAgentCluster: {
    enable: true,
  },
  referrerPolicy: {
    noReferrer: true,
  },
  strictTransportSecurity: {
    maxAge: 31536000,
  },
  xContentTypeOptions: {
    noSniff: true,
  },
  xDnsPrefetchControl: {
    off: true,
  },
  xDownloadOptions: {
    noopen: true,
  },
  xFrameOptions: {
    sameOrigin: true,
  },
  xPermittedCrossDomainPolicies: {
    none: true,
  },
});

/** @type {FortifySettings} */
console.log(headers);
/*
 * {
      'Content-Security-Policy':
        "default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests",
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin',
      'Cross-Origin-Resource-Policy': 'same-origin',
      'Expect-Ct': 'max-age=0',
      'Origin-Agent-Cluster': '?1',
      'Referrer-Policy': 'no-referrer',
      'Strict-Transport-Security': 'max-age=15552000',
      'X-Content-Type-Options': 'nosniff',
      'X-Dns-Prefetch-Control': 'off',
      'X-Download-Options': 'noopen',
      'X-Frame-Options': 'SAMEORIGIN',
      'X-Permitted-Cross-Domain-Policies': 'none',
    }
 */

You can choose to use these defaults by setting the fortify header to an empty object. This can be done globally:

import { fortifyHeaders, FortifySettings } from '@side/fortifyjs';

const headers = fortifyHeaders();

/** @type {FortifySettings} */
console.log(headers); // same as above

Or, you can choose to pull in individual header default postures:

import { fortifyHeaders, FortifySettings } from '@side/fortifyjs';

const headers = fortifyHeaders({
  contentSecurityPolicy: {},
});

/** @type {FortifySettings} */
console.log(headers); // same as above
/*
 * {
      'Content-Security-Policy':
        "default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests",
    }
 */

If you want to specify a custom header value for one, like Content-Security-Policy, but also want to include all of the default headers, then you can use the second parameter to fortifyHeaders to tell the header generation that you want to include the rest of the available headers:

import { fortifyHeaders, FortifySettings } from '@side/fortifyjs';

const headers = fortifyHeaders(
  {
    contentSecurityPolicy: {
      defaultSrc: ["'self'", '*.somedomain.com'],
    },
  },
  { useDefaults: true },
);

/** @type {FortifySettings} */
console.log(headers); // same as above
/*
 * {
      'Content-Security-Policy': "default-src 'self' *.somedomain.com",
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin',
      'Cross-Origin-Resource-Policy': 'same-origin',
      'Expect-Ct': 'max-age=0',
      'Origin-Agent-Cluster': '?1',
      'Referrer-Policy': 'no-referrer',
      'Strict-Transport-Security': 'max-age=15552000',
      'X-Content-Type-Options': 'nosniff',
      'X-Dns-Prefetch-Control': 'off',
      'X-Download-Options': 'noopen',
      'X-Frame-Options': 'SAMEORIGIN',
      'X-Permitted-Cross-Domain-Policies': 'none',
    }
 */

You can then use this object to integrate within the platform of your choice: next, fastify, etc.

Header Inclusion Strategy

The strategy for this problem is building a lib that can be consumed by packages that integrate with these different providers: next, fastify, etc.

Since this library is specifically for delivering the relevant headers to secure modern web applications, it not contain information about X-Powered-By. This header is typically removed completely. FortifyJS only provides ones that ought to be included, not excluded. This should be handled at the consumer-level in whichever platform you're integrating the security headers into.

Supported Headers

Header Name Default Value Details
Content-Security-Policy default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests Link
Cross-Origin-Embedder-Policy require-corp Link
Cross-Origin-Opener-Policy same-origin Link
Cross-Origin-Resource-Policy same-origin Link
Expect-Ct max-age=0 Link
Origin-Agent-Cluster ?1 Link
Referrer-Policy no-referrer Link
Strict-Transport-Security max-age=15552000 Link
X-Content-Type-Options nosniff Link
X-Dns-Prefetch-Control off Link
X-Download-Options noopen Link
X-Frame-Options SAMEORIGIN Link
X-Permitted-Cross-Domain-Policies none Link

Contributing Policy

The development team at Side is currently investigating the best expression of Codes of Conduct and Contributing Guidelines to fit our culture and values. FortifyJS is a Free and Open Source software solution that is licensed under MIT. If you desire to contribute to extend the functionality or address an issue, please maintain professional communication practices. That alone goes a long way toward effective collaboration and benefiting the community at large!

fortifyjs's People

Contributors

dependabot[bot] avatar mathieudi avatar offgriddev avatar prescottprue avatar renovate[bot] avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fortifyjs's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/release.yml
  • reside-eng/workflow-templates-library v1
.github/workflows/verify.yml
  • reside-eng/workflow-templates-library v1
npm
package.json
  • @commitlint/cli 19.5.0
  • @commitlint/config-conventional 19.5.0
  • @side/commitlint-config 1.1.0
  • @side/eslint-config-base 2.2.0
  • @side/eslint-config-jest 1.1.1
  • @side/prettier-config 1.1.0
  • @types/jest 29.5.13
  • @types/node 20.12.10
  • @typescript-eslint/eslint-plugin 7.8.0
  • @typescript-eslint/parser 7.8.0
  • eslint 8.57.1
  • eslint-config-prettier 9.1.0
  • husky 9.1.6
  • jest 29.7.0
  • lint-staged 15.2.10
  • prettier 3.3.3
  • rimraf 6.0.1
  • ts-jest 29.2.5
  • typescript 5.6.2
  • yarn 4.5.0

  • Check this box to trigger a request for Renovate to run again on this repository

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.