Skip to content

Commit

Permalink
Fix minor issues with convertor
Browse files Browse the repository at this point in the history
- Trim `'data:' true` etc. to `data: true`
- Remove empty keys
- Improve indentation
- Add YML header
- Add enabled key
  • Loading branch information
Firesphere authored Jun 29, 2023
1 parent b135761 commit 70ad819
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Helpers/CSPConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ public static function toYml($response, $return = false)
$cspHeader = $response->getHeader('content-security-policy') ?? $response->getHeader('content-security-policy-report-only');

$asArray = explode(';', $cspHeader);
$arrayHeader = [];
$arrayHeader = ['enabled' => true];

foreach ($asArray as $headerPart) {
if (empty($headerPart)) continue;
$parts = explode(' ', trim($headerPart));
$key = array_shift($parts);
$arrayHeader[$key] = [];
Expand All @@ -80,11 +82,12 @@ public static function toYml($response, $return = false)
continue;
}
$allowedKeys = array_merge(static::$non_url_defaults, static::$non_url_args[$key] ?? []);
foreach ($parts as $partkey => $part) {
$part = trim(trim($part, '"'), "'");
$part = str_replace('*', 'www', $part); // little hack for `https://*.google.com` etc.
if (!filter_var($part, FILTER_VALIDATE_URL) && $part !== 'none') {
foreach ($parts as $partkey => &$part) {
$part = trim(str_replace(['"', "'"], '', $part));
$asUrl = str_replace('*', 'www', $part); // little hack for `https://*.google.com` etc.
if (!filter_var($asUrl, FILTER_VALIDATE_URL) && $part !== 'none') {
if (in_array($part, $allowedKeys)) {
$part = rtrim($part, ':');
$arrayHeader[$key][$part] = true;
}
unset($parts[$partkey]);
Expand All @@ -93,7 +96,12 @@ public static function toYml($response, $return = false)
rsort($parts);
$arrayHeader[$key]['allow'] = $parts;
}
$yaml = Yaml::dump($arrayHeader);
$data = [
CSPBackend::class => [
'csp_config' => $arrayHeader
]
];
$yaml = Yaml::dump($data, 5, 2);
if ($return) {
return $yaml;
}
Expand Down

0 comments on commit 70ad819

Please sign in to comment.