OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Scrub turbo scripts before rendering body
Oliver Günther997ed8576c34
diff --git a/frontend/src/turbo/helpers.ts b/frontend/src/turbo/helpers.tsindex 5922eb37f44..84845167533 100644--- a/frontend/src/turbo/helpers.ts+++ b/frontend/src/turbo/helpers.ts@@ -8,4 +8,19 @@ export namespace TurboHelpers {export function hideProgressBar() {Turbo.session.adapter.formSubmissionFinished();}++ export function scrubScriptElements(element:HTMLElement|DocumentFragment) {+ const cspNonce = document.getElementsByName('csp-nonce')[0]?.getAttribute('content') || '';++ element+ .querySelectorAll('script')+ .forEach((script) => {+ const nonce = script.getAttribute('nonce');++ if (!(nonce && nonce === cspNonce)) {+ console.warn('Removing script element %O because it does not match our nonce', script);+ script.remove();+ }+ });+ }}diff --git a/frontend/src/turbo/turbo-event-listeners.ts b/frontend/src/turbo/turbo-event-listeners.tsindex b102187836c..0d514296967 100644--- a/frontend/src/turbo/turbo-event-listeners.ts+++ b/frontend/src/turbo/turbo-event-listeners.ts@@ -1,3 +1,6 @@+import { TurboHelpers } from './helpers';+import { StreamElement } from '@hotwired/turbo';+export function addTurboEventListeners() {// Close the primer dialog when the form inside has been submitted with a success response.//@@ -25,4 +28,23 @@ export function addTurboEventListeners() {headers['Turbo-Referrer'] = window.location.href;headers['X-Turbo-Nonce'] = document.getElementsByName('csp-nonce')[0]?.getAttribute('content') || '';});++ // Turbo adds nonces to all scripts, even though we want to explicitly pass nonces+ // https://github.com/hotwired/turbo/issues/294#issuecomment-2633216052+ // We remove them manually as a workaround+ // in Handle Turbo Drive page loads (full reloads)+ document.addEventListener('turbo:before-render', (event) => {+ TurboHelpers.scrubScriptElements(event.detail.newBody);+ });++ // in Turbo Streams (partial updates)+ document.addEventListener('turbo:before-stream-render', (event) => {+ const content = (event.target as StreamElement).templateContent;+ TurboHelpers.scrubScriptElements(content);+ });++ // in Turbo Frames (when they load new content)+ document.addEventListener('turbo:frame-render', (event) => {+ TurboHelpers.scrubScriptElements(event.target as HTMLElement);+ });}diff --git a/frontend/src/typings/shims.d.ts b/frontend/src/typings/shims.d.tsindex 6a151a4ae8f..71b55b4ebf7 100644--- a/frontend/src/typings/shims.d.ts+++ b/frontend/src/typings/shims.d.ts@@ -45,6 +45,7 @@ declare module '@hotwired/turbo' {export interface StreamElement {templateElement:HTMLTemplateElement;+ templateContent:DocumentFragment;}export function start():void;