-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsp.php
29 lines (22 loc) · 870 Bytes
/
Csp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
namespace LimeSoda\LsSecurityHeaders\UserFunctions;
use Psr\Http\Message\ServerRequestInterface;
class Csp
{
public function generateNonce(string $_, array $conf, ServerRequestInterface $request): string
{
if ($conf['asAttribute'] === '1') {
$length = $conf['length'];
$policy = $conf['policy'];
} else {
$length = $request->getAttribute('currentContentObject')->cObjGetSingle($conf['length'], $conf['length.']);
$policy = $request->getAttribute('currentContentObject')->cObjGetSingle($conf['policy'], $conf['policy.']);
}
$nonce = bin2hex(random_bytes($length ?? 32));
$GLOBALS['LS_SECURITY_HEADERS']['CSP_NONCE'][$policy][] = $nonce;
if ($conf['asAttribute'] === '1') {
return " nonce=\"$nonce\"";
}
return $nonce;
}
}