Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
✨ support for nonce in style element
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Meilick <[email protected]>
  • Loading branch information
bnomei committed Feb 8, 2020
1 parent ff9f05a commit 95d0c32
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
12 changes: 11 additions & 1 deletion classes/Bnomei/Srcset.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ public function applyRatio(string $text, array $data = []): string
);
$ratio = $srcfile->ratio();

$text = '<style>.'.A::get($data, 'ratio').'[data-ratio="'.$ratio.'"]{padding-bottom:'.$ratio.'%;}</style>' . $text;
$nonceAttr = '';
if ($nonce = $this->option('nonce')) {
$nonceAttr = ' nonce="'.$nonce.'"';
}
$text = '<style'.$nonceAttr.'>.'.A::get($data, 'ratio').'[data-ratio="'.$ratio.'"]{padding-bottom:'.$ratio.'%;}</style>' . $text;

if (A::get($data, 'caption')) {
$text = str_replace(
Expand Down Expand Up @@ -286,6 +290,11 @@ public function html(): string
*/
public static function defaultOptions(): array
{
$nonce = option('bnomei.srcset.nonce');
if (is_callable($nonce)) {
$nonce = $nonce();
}

return [
'debug' => option('debug'),
'lazy' => option('bnomei.srcset.lazy'),
Expand All @@ -294,6 +303,7 @@ public static function defaultOptions(): array
'figure' => option('bnomei.srcset.figure') === true,
'ratio' => option('bnomei.srcset.ratio'),
'quality' => intval(option('thumbs.quality', 80)),
'nonce' => $nonce,
];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-srcset",
"type": "kirby-plugin",
"version": "3.1.5",
"version": "3.2.0",
"license": "MIT",
"description": "Kirby 3 Plugin for creating lazyloading image srcset",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
'autosizes' => 'auto',
'figure' => true,
'ratio' => 'lazysrcset-ratio',
'nonce' => function() {
$nonce = site()->nonce();
if (is_a($nonce, \Kirby\Cms\Field::class)) {
$nonce = $nonce->isNotEmpty() ? $nonce->value() : null;
}
return $nonce;
},
],
'snippets' => [
'editor/srcset' => __DIR__ . '/snippets/srcset.php',
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ Each figure element will be prefixed with a style element defining the ratio. Fo
<style>.lazysrcset-ratio[data-ratio="56.25"]{padding-bottom:56.25%;}</style><figure><div data-ratio="56.25" class="lazysrcset-ratio"><!--- image tag with srcset --></div><figcaption>with caption</figcaption></figure>
```

**with nonce**

You can set a custom nonce using the options or install the [security headers plugin](https://github.com/bnomei/kirby3-security-headers).
```html
<style nonce="A-NONCE-HERE">...
```
## Settings
| bnomei.srcset. | Default | Description |
Expand All @@ -229,6 +237,7 @@ Each figure element will be prefixed with a style element defining the ratio. Fo
| autosizes | `auto` | bool or string. related to `data-sizes` attribute |
| figure | `true` | bool. wrap image in figure or not |
| ratio | `lazysrcset-ratio` | bool or string. adds data-ratio to parent of `img` |
| nonce | `null` | null, string or callback. default nonce for style element |
## FAQ
Expand Down
33 changes: 33 additions & 0 deletions tests/SrcsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@ public function testOption()
$this->assertEquals('test2000.png', $srcset->option('value'));
}

public function testNonce()
{
// csp plugin or callback could provide this
$srcset = new Srcset([
'value' => 'test2000.png',
'parent' => page('home'),
'nonce' => '',
]);
$this->assertRegExp(
'/^<style>/',
$srcset->html()
);
$srcset = new Srcset([
'value' => 'test2000.png',
'parent' => page('home'),
'nonce' => null,
]);
$this->assertRegExp(
'/^<style>/',
$srcset->html()
);
$srcset = new Srcset([
'value' => 'test2000.png',
'parent' => page('home'),
'nonce' => '@NONCE@',
]);
$this->assertRegExp(
'/^<style nonce="@NONCE@">/',
$srcset->html()
);

}

public function testImageFromData()
{
$srcset = new Srcset([
Expand Down

0 comments on commit 95d0c32

Please sign in to comment.