Skip to content

Commit 149543a

Browse files
authored
Merge pull request #31 from BeAPI/feature/http-headers
add http-headers default
2 parents 676f027 + 5704d63 commit 149543a

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"config": {
1515
"optimize-autoloader": true,
1616
"preferred-install": { "*": "dist" },
17-
"sort-packages": true
17+
"sort-packages": true,
18+
"allow-plugins": {
19+
"phpro/grumphp-shim": true,
20+
"dealerdirect/phpcodesniffer-composer-installer": true
21+
}
1822
},
1923
"require-dev": {
2024
"dealerdirect/phpcodesniffer-composer-installer": "v0.7.1",

default-http-headers.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/*
3+
Plugin Name: BeAPI Default - HTTP Headers
4+
Version: 1.0.0
5+
Plugin URI: https://beapi.fr
6+
Description: Add custom http headers (CP, CSP, ....)
7+
Author: Be API
8+
Author URI: https://beapi.fr
9+
10+
----
11+
12+
Copyright 2024 Be API Technical team ([email protected])
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation; either version 2 of the License, or
17+
(at your option) any later version.
18+
19+
This program is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program; if not, write to the Free Software
26+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27+
28+
*/
29+
30+
namespace BEAPI\Plugin_Defaults\Http_Header;
31+
32+
if ( ! defined( 'ABSPATH' ) ) {
33+
die( 'Cannot access pages directly.' );
34+
}
35+
36+
/**
37+
* Add custom behavior for headers
38+
*
39+
* @param array $headers
40+
*
41+
* @return array
42+
*
43+
* @author Alexandre Sadowski
44+
*
45+
*/
46+
function wp_headers( array $headers ): array {
47+
if ( is_admin() ) {
48+
return $headers;
49+
}
50+
51+
$csp = get_csp_headers();
52+
53+
if ( defined( 'CSP_REPORT_ONLY' ) && defined( 'WP_SENTRY_SECURITY_HEADER_ENDPOINT' ) ) {
54+
$csp['report-uri'] = WP_SENTRY_SECURITY_HEADER_ENDPOINT;
55+
$csp['report-to'] = 'csp-endpoint';
56+
}
57+
58+
/**
59+
* We rely on the CSP_REPORT_ONLY .env value to decide whether we apply the CSP or just report the errors
60+
*/
61+
$csp_header = defined( 'CSP_REPORT_ONLY' ) && CSP_REPORT_ONLY ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy';
62+
$csp_headers_array[ $csp_header ] = get_prepare_csp( $csp );
63+
64+
$custom_headers_array = [];
65+
66+
/**$custom_headers_array = [
67+
* 'X-Content-Type-Options' => 'nosniff',
68+
* 'X-Frame-Options' => 'SAMEORIGIN',
69+
* 'X-XSS-Protection' => '1; mode=block',
70+
* 'Referrer-Policy' => 'no-referrer-when-downgrade',
71+
* 'Permissions-Policy' => 'accelerometer=(), geolocation=(), fullscreen=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), display-capture=()',
72+
* ];**/
73+
74+
return wp_parse_args( $csp_headers_array, $custom_headers_array );
75+
}
76+
77+
add_filter( 'wp_headers', __NAMESPACE__ . '\\wp_headers', 99 );
78+
79+
/**
80+
*
81+
* Prepare CSP attribute values
82+
*
83+
* @param array $csp
84+
*
85+
* @return string
86+
*/
87+
function get_prepare_csp( array $csp ): string {
88+
$csp_values = '';
89+
90+
if ( empty( $csp ) ) {
91+
return $csp_values;
92+
}
93+
94+
// Loop and not implode to add both key and value
95+
foreach ( $csp as $key => $value ) {
96+
if ( empty( $value ) ) {
97+
continue;
98+
}
99+
$csp_values .= $key . ' ' . $value . '; ';
100+
}
101+
102+
// Remove last space
103+
return trim( $csp_values );
104+
}
105+
106+
/**
107+
* Generate CSP headers array
108+
*
109+
* @return array
110+
* @author Alexandre Sadowski
111+
*/
112+
function get_csp_headers(): array {
113+
$csp = [
114+
'default-src' => '\'self\'',
115+
'script-src' => '\'self\'',
116+
'style-src' => '\'self\'',
117+
'img-src' => '\'self\'',
118+
'font-src' => '\'self\'',
119+
'connect-src' => '\'self\'',
120+
'frame-src' => '\'self\'',
121+
'manifest-src' => '\'self\'',
122+
'worker-src' => '\'self\'',
123+
'object-src' => '\'none\'',
124+
];
125+
126+
//if ( 'production' === WP_ENV ) {
127+
//$csp = [];
128+
//}
129+
130+
return apply_filters( 'csp_headers', $csp );
131+
}

0 commit comments

Comments
 (0)