Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added csp nonce support #47

Merged
merged 2 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/BassetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BassetManager
private array $loaded;
private string $basePath;
private string $cachebusting;
private string|null $nonce;
private bool $dev = false;

public CacheMap $cacheMap;
Expand All @@ -40,6 +41,7 @@ public function __construct()
$this->cachebusting = '?'.substr(md5(base_path('composer.lock')), 0, 12);
$this->basePath = (string) Str::of(config('backpack.basset.path'))->finish('/');
$this->dev = config('backpack.basset.dev_mode', false);
$this->nonce = config('backpack.basset.nonce', null);

$this->cacheMap = new CacheMap($this->disk, $this->basePath);
$this->loader = new LoadingTime();
Expand Down Expand Up @@ -110,12 +112,10 @@ public function echoFile(string $path, array $attributes = []): void
*/
public function echoCss(string $path, array $attributes = []): void
{
$args = '';
foreach ($attributes as $key => $value) {
$args .= " $key".($value === true || empty($value) ? '' : "=\"$value\"");
}
$href = asset($path.$this->cachebusting);
$args = $this->prepareAttributes($attributes);

echo '<link href="'.asset($path.$this->cachebusting).'"'.$args.' rel="stylesheet" type="text/css" />'.PHP_EOL;
echo '<link href="'.$href.'"'.$args.' rel="stylesheet" type="text/css" />'.PHP_EOL;
}

/**
Expand All @@ -126,12 +126,30 @@ public function echoCss(string $path, array $attributes = []): void
*/
public function echoJs(string $path, array $attributes = []): void
{
$src = asset($path.$this->cachebusting);
$args = $this->prepareAttributes($attributes);

echo '<script src="'.$src.'"'.$args.'></script>'.PHP_EOL;
}

/**
* Prepares attributes to be added to the script/style dom element.
*
* @param array $attributes
* @return string
*/
private function prepareAttributes(array $attributes = []): string
{
if ($this->nonce) {
$attributes['nonce'] ??= $this->nonce;
}

$args = '';
foreach ($attributes as $key => $value) {
$args .= " $key".($value === true || empty($value) ? '' : "=\"$value\"");
}

echo '<script src="'.asset($path.$this->cachebusting).'"'.$args.'></script>'.PHP_EOL;
return $args;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/config/backpack/basset.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
'view_paths' => [
resource_path('views'),
],

// content security policy nonce
'nonce' => null,
];