CSP nonce-source for PHP
, (*1)
CSP (Content Security Policy) nonce-source library for PHP., (*2)
What is CSP nonce-source?
It is one of CSP 2 features to prevent XSS., (*3)
If you don't know, please see CSP for the web we have | Mozilla Security Blog., (*4)
Requirement
Installation
$ git clone https://github.com/kenjis/php-csp-nonce-source.git
$ cd php-csp-nonce-source
$ composer install
Usage
All you have to call is only Csp::sendHeader() and Csp::getNonce()., (*5)
Csp::sendHeader() sends CSP header., (*6)
Csp::getNonce() returns nonce value., (*7)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample of CSP nonce-source</title>
</head>
<body>
</body>
</html>
You can test it with PHP built-in web server., (*8)
$ php -S localhost:8000
And browse http://localhost:8000/., (*9)
You can see CSP violation report in csp-report.log file., (*10)
(Optional) Add other polices
You can add other polices using Csp::addPolicy()., (*11)
<?php
require __DIR__ . '/bootstrap.php';
Csp::addPolicy('default-src', 'self');
Csp::addPolicy('img-src', 'img.example.com');
Csp::sendHeader();
(Optional) Report Only
You can set Report Only Mode using Csp::setReportOnly()., (*12)
<?php
require __DIR__ . '/bootstrap.php';
Csp::addPolicy('default-src', 'self');
Csp::setReportOnly();
Csp::sendHeader();
You can see CSP violation report in csp-report.log file., (*13)
License
MIT License. See LICENSE.md., (*14)
Reference
- Content Security Policy Level 2 http://www.w3.org/TR/2014/WD-CSP2-20140703/
- Firefox Notes Version 31.0 https://www.mozilla.org/en-US/mobile/31.0/releasenotes/
- CSP for the web we have | Mozilla Security Blog https://blog.mozilla.org/security/2014/10/04/csp-for-the-web-we-have/