Skip to content

Commit

Permalink
Merge pull request #315 from humhub/fix/314-snippet-js-nonce
Browse files Browse the repository at this point in the history
Add nonce attribute to all HTML snippets automatically
  • Loading branch information
luke- authored Dec 12, 2023
2 parents 8e0ee0e + b506dc0 commit 457b9a1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions controllers/AbstractCustomContainerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use humhub\components\access\StrictAccess;
use humhub\modules\admin\permissions\ManageModules;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\custom_pages\helpers\Html;
use humhub\modules\custom_pages\models\ContainerPage;
use humhub\modules\custom_pages\models\ContainerSnippet;
use humhub\modules\custom_pages\models\CustomContentContainer;
Expand Down Expand Up @@ -116,8 +117,6 @@ public function renderTemplate($page, $editMode = null)
$canEdit = PagePermission::canEdit();
$editMode = ($editMode || Yii::$app->request->get('editMode')) && $canEdit;

$html = '';

if(!$canEdit && TemplateCache::exists($templateInstance)) {
$html = TemplateCache::get($templateInstance);
} else {
Expand All @@ -126,7 +125,8 @@ public function renderTemplate($page, $editMode = null)
TemplateCache::set($templateInstance, $html);
}
}
return $html;

return Html::applyScriptNonce($html);
}

/**
Expand All @@ -148,4 +148,4 @@ public function isCanEdit() {
return $this->_canEdit;
}

}
}
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
Unreleased
-------------------------
- Fix #312: Highlight the top menu entry if the current URL matches the Target Url of a "Link" custom page
- Enh #314: Add nonce attribute to all JavaScript tags in snippet templates and HTML snippets automatically

1.9.5 (November 16, 2023)
-------------------------
Expand Down
5 changes: 5 additions & 0 deletions models/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace humhub\modules\custom_pages\models;

use humhub\modules\custom_pages\helpers\Html;
use humhub\modules\custom_pages\helpers\Url;
use humhub\modules\custom_pages\models\forms\SettingsForm;
use humhub\modules\custom_pages\modules\template\models\Template;
Expand Down Expand Up @@ -111,6 +112,10 @@ public function getAllowedTemplateSelection()
*/
public function getPageContent()
{
if ($this->type == HtmlType::ID) {
return Html::applyScriptNonce($this->page_content);
}

return $this->page_content;
}

Expand Down

0 comments on commit 457b9a1

Please sign in to comment.