TopGit theo dõi paragonie/csp-builder trên GitHub trong nhóm Backend, đã đạt 540 sao. Build Content-Security-Policy headers from a JSON file (or build them programmatically)
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/paragonie/csp-builder là nguồn chính thức.
Có — paragonie/csp-builder phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/paragonie/csp-builder.
paragonie/csp-builder có trang demo không?
Dự án có trang chủ ở https://paragonie.com/projects. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
paragonie/csp-builder còn đang phát triển không?
Commit gần nhất trên paragonie/csp-builder là 2 tháng trước (theo timestamp GitHub). Repo có 39 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
paragonie/csp-builder dùng license gì?
paragonie/csp-builder phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc csp-builder có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về csp-builder.