From 8157f2719062345e3e70512d10beace743f51721 Mon Sep 17 00:00:00 2001 From: Zack Moore Date: Thu, 6 Mar 2025 16:29:19 -0500 Subject: [PATCH 1/5] working on adding cspNonce --- .../src/modifiers/hds-code-editor.ts | 37 ++++++++++++++++++- showcase/app/components/shw/flex/index.gts | 2 +- showcase/app/components/shw/grid/index.gts | 2 +- .../app/components/shw/placeholder/index.gts | 2 +- showcase/app/index.html | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/components/src/modifiers/hds-code-editor.ts b/packages/components/src/modifiers/hds-code-editor.ts index 272b65758f7..44d164d97dd 100644 --- a/packages/components/src/modifiers/hds-code-editor.ts +++ b/packages/components/src/modifiers/hds-code-editor.ts @@ -39,6 +39,7 @@ export interface HdsCodeEditorSignature { ariaDescribedBy?: string; ariaLabel?: string; ariaLabelledBy?: string; + cspNonce?: string; hasLineWrapping?: boolean; language?: HdsCodeEditorLanguages; value?: string; @@ -55,6 +56,27 @@ async function defineStreamLanguage(streamParser: StreamParserType) { return StreamLanguage.define(streamParser); } +function getCSPNonceFromMeta(): string | undefined { + const meta = document.querySelector( + 'meta[http-equiv="Content-Security-Policy"]' + ); + + if (meta === null) { + return undefined; + } + + const content = meta.getAttribute('content'); + + if (content === null) { + return undefined; + } + + // searches for either "style-src" or "script-src" followed by anything until a token like 'nonce-' + const match = content.match(/(?:style-src|script-src)[^;]*'nonce-([^']+)'/); + + return match ? match[1] : undefined; +} + const LOADER_HEIGHT = '164px'; const LANGUAGES: Record< @@ -276,7 +298,7 @@ export default class HdsCodeEditorModifier extends Modifier { + async ({ cspNonce, language, hasLineWrapping }) => { const [ { keymap, @@ -337,6 +359,13 @@ export default class HdsCodeEditorModifier extends Modifier ) => { try { const { EditorState } = await import('@codemirror/state'); const extensions = await this._buildExtensionsTask.perform({ + cspNonce, language, hasLineWrapping: hasLineWrapping ?? false, }); @@ -395,6 +426,7 @@ export default class HdsCodeEditorModifier extends Modifier { {{@label}} {{/if}} {{yield (hash Label=ShwLabel)}} -
+
{{yield (hash Item=ShwFlexItem)}}
diff --git a/showcase/app/components/shw/grid/index.gts b/showcase/app/components/shw/grid/index.gts index be627d411f0..fd07f9809cc 100644 --- a/showcase/app/components/shw/grid/index.gts +++ b/showcase/app/components/shw/grid/index.gts @@ -65,7 +65,7 @@ export default class ShwGrid extends Component { {{@label}} {{/if}} {{yield (hash Label=ShwLabel)}} -
+
{{yield (hash Item=ShwGridItem)}}
diff --git a/showcase/app/components/shw/placeholder/index.gts b/showcase/app/components/shw/placeholder/index.gts index 5518d0ab80d..c4a1f0f9246 100644 --- a/showcase/app/components/shw/placeholder/index.gts +++ b/showcase/app/components/shw/placeholder/index.gts @@ -80,7 +80,7 @@ export default class ShwPlaceholder extends Component { }