paragonie/csp-builder — a backend project — sits at 540 GitHub stars in the Backend space. Build Content-Security-Policy headers from a JSON file (or build them programmatically)
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Easily integrate Content-Security-Policy headers into your web application, either
from a JSON configuration file, or programatically.
CSP Builder was created by Paragon Initiative Enterprises
as part of our effort to encourage better application security practices.
Check out our other open source projects too.
There's also a CSP middleware available that uses this library.
Installing
First, get Composer, then run:
composer require paragonie/csp-builder
Build a Content Security Policy header from a JSON configuration file
<?php
use ParagonIE\CSPBuilder\CSPBuilder;
$csp = CSPBuilder::fromFile('/path/to/source.json');
$csp->sendCSPHeader();
You can also load the configuration from a JSON string, like so:
<?php
use ParagonIE\CSPBuilder\CSPBuilder;
$configuration = file_get_contents('/path/to/source.json');
if (!is_string($configuration)) {
throw new Error('Could not read configuration file!');
}
$csp = CSPBuilder::fromData($configuration);
$csp->sendCSPHeader();
Finally, you can just pass an array to the first argument of the constructor:
<?php
use ParagonIE\CSPBuilder\CSPBuilder;
$configuration = file_get_contents('/path/to/source.json');
if (!is_string($configuration)) {
throw new Error('Could not read configuration file!');
}
$decoded = json_decode($configuration, true);
if (!is_array($decoded)) {
throw new Error('Could not parse configuration!');
}
$csp = new CSPBuilder($decoded);
$csp->sendCSPHeader();
<?php
use ParagonIE\CSPBuilder\CSPBuilder;
$csp = CSPBuilder::fromFile('/path/to/source.json');
// Let's add a nonce for inline JS
$nonce = $csp->nonce('script-src');
$body .= "<script nonce={$nonce}>";
$body .= $desiredJavascriptCode;
$body .= "</script>";
// Let's add a hash to the CSP header for $someScript
$hash = $csp->hash('script-src', $someScript, 'sha256');
// Add a new source domain to the whitelist
$csp->addSource('image', 'https://ytimg.com');
// Set the Report URI
$csp->setReportUri('https://example.com/csp_report.php');
// Let's turn on HTTPS enforcement
$csp->addDirective('upgrade-insecure-requests', true);
$csp->sendCSPHeader();
Note that many of these methods can be chained together:
Instead of invoking sendCSPHeader(), you can instead inject the headers into
your PSR-7 message object by calling it like so:
/**
* $yourMessageHere is an instance of an object that implements
* \Psr\Http\Message\MessageInterface
*
* Typically, this will be a Response object that implements
* \Psr\Http\Message\ResponseInterface
*
* @ref https://github.com/guzzle/psr7/blob/master/src/Response.php
*/
$csp->injectCSPHeader($yourMessageHere);
Save a CSP header for configuring Apache/nginx
Instead of calling sendCSPHeader() on every request, you can build the CSP once
and save it to a snippet for including in your server configuration:
If your company uses this library in their products or services, you may be
interested in purchasing a support contract from Paragon Initiative Enterprises.
How active is development on paragonie/csp-builder?
The most recent commit recorded on paragonie/csp-builder was 3 months ago, based on the GitHub push timestamp. The repository has 39 forks — one of the better signals of community interest.
Is paragonie/csp-builder open source?
Yes — paragonie/csp-builder ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/paragonie/csp-builder.
What license does paragonie/csp-builder use?
paragonie/csp-builder is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
What topics is paragonie/csp-builder associated with?
The project maintains a homepage at https://paragonie.com/projects. The README tab on this page also usually contains screenshots and a quickstart.
Where do I read more about paragonie/csp-builder?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/paragonie/csp-builder is the definitive source.
Read full README in the tab above.
Curious whether csp-builder is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about csp-builder.