Coder Social home page Coder Social logo

perl-http-cspheader's Introduction

NAME

HTTP::CSPHeader - manage dynamic content security policy headers

VERSION

version v0.3.4

SYNOPSIS

use HTTP::CSPheader;

my $csp = HTTP::CSPheader->new(
  policy => {
     "default-src" => q['self'],
     "script-src"  => q['self' cdn.example.com],
  },
  nonces_for => [qw/ script-src /],
);

...

use HTTP::Headers;

my $h = HTTP::Headers->new;

$csp->reset;

$csp->amend(
  "+script-src" => "https://captcha.example.com",
  "+style-src"  => "https://captcha.example.com",
);

my $nonce = $csp->nonce;
$h->header( 'Content-Security-Policy' => $csp->header );

my $body = ...

$body .= "<script nonce="${nonce}"> ... </script>";

DESCRIPTION

This module allows you to manage Content-Security-Policy (CSP) headers.

It supports dynamic changes to headers, for example, adding a source for a specific page, or managing a random nonce for inline scripts or styles.

It also supports caching, so that the header will only be regenerated if there is a change.

ATTRIBUTES

policy

This is a hash reference of policies. The keys a directives, and the values are sources.

There is no validation of these values.

nonces_for

This is an array reference of the directives to add a random "nonce" to when the "policy" is regenerated.

Note that the same nonce will be added to all of the directives, since using separate nonces does not improve security.

It is emply by default.

A single value will be coerced to an array.

This does not validate the values.

Note that if a directive allows 'unsafe-inline' then a nonce may cancel out that value.

nonce_seed_size

This is the size of the random seed data for the "nonce". It can be an integer between 16 and 256.

nonce

This is the random nonce that is added to directives in "nonces_for".

The nonce is a hex string based on a random 32-bit number, which is generated from Math::Random::ISAAC. The RNG is seeded by Crypt::URandom.

If you want to change how it is generated, you can override the _build_nonce method in a subclass.

Note that you should never make an assumption about the format of the nonce, as the source may change in future versions.

header

This is the value of the header, generated from the "policy".

This is a read-only accessor.

METHODS

reset

This resets any changes to the "policy" and clears the "nonce". It should be run at the start of each HTTP request.

If you never make use of the nonce, and never "amend" the headers, then you do not need to run this method.

amend

$csp->amend( $directive1 => $value1, $directive2 => $value2, ... );

This amends the "policy".

If the $directive starts with a + then the value will be appended to it. Otherwise the change will overwrite the value.

If the value is undef, then the directive will be deleted.

EXAMPLES

Mojolicious

You can use this with Mojolicious:

use HTTP::CSPHeader;

use feature 'state';

$self->hook(
  before_dispatch => sub ($c) {

    state $csp = HTTP::CSPHeader->new(
        policy => {
            'default-src' => q['self'],
            'script-src'  => q['self'],
        },
        nonces_for => 'script-src',
    );

    $csp->reset;

    $c->stash( csp_nonce => $csp->nonce );

    $c->res->headers->content_security_policy( $csp->header );
  }
);

and in your templates, you can use the following for inline scripts:

<script nonce="<%= $csp_nonce %>"> ... </script>

If you do not need the nonce, then you might consider using Mojolicious::Plugin::CSPHeader.

SUPPORT FOR OLDER PERL VERSIONS

Since v0.2.0, the this module requires Perl v5.14 or later.

If you need this module on Perl v5.10, please use one of the v0.1.x versions of this module. Significant bug or security fixes may be backported to those versions.

SEE ALSO

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy

HTTP::SecureHeaders

Plack::Middleware::CSP

SOURCE

The development version is on github at https://github.com/robrwo/perl-HTTP-CSPHeader and may be cloned from git://github.com/robrwo/perl-HTTP-CSPHeader.git

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/robrwo/perl-HTTP-CSPHeader/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

Robert Rothenberg [email protected]

COPYRIGHT AND LICENSE

This software is Copyright (c) 2022-2023 by Robert Rothenberg.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

perl-http-cspheader's People

Contributors

robrwo avatar

Watchers

 avatar

Forkers

pangyre

perl-http-cspheader's Issues

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.