Skip to content
Closed
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
13 changes: 13 additions & 0 deletions src/render/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ function injectAEMHtmlHeadEntries(daCtx, headNode, headHtmlStr) {
const { org, site, orgSiteInPath } = daCtx;
const aemHeadHtmlTree = fromHtml(headHtmlStr, { fragment: true });

// the pipeline moves this meta to a response header and rewrites the placeholder
// nonce; drop both.
aemHeadHtmlTree.children = aemHeadHtmlTree.children.filter(
(node) => !(node.type === 'element'
&& node.tagName === 'meta'
&& node.properties?.['move-to-http-header'] !== undefined
&& typeof node.properties?.content === 'string'
&& node.properties.content.includes("'nonce-aem'")),
);
Comment on lines +38 to +44

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the check look for nonce-aem ?

selectAll('[nonce=aem]', aemHeadHtmlTree).forEach((n) => {
delete n.properties.nonce;
});

// TODO: reuse fixUrlsWhenLocalDev from aemCtx.js instead of duplicating.
if (orgSiteInPath) {
const headScriptsAndLinks = selectAll(
Expand Down
24 changes: 24 additions & 0 deletions test/render/compose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,28 @@ describe('render compose', () => {
assert.ok(!html.includes('urn:adobe:aue'));
assert.ok(!html.includes('universal-editor-service'));
});
it('drops a CSP meta the pipeline would have moved to a header', async () => {
const cspHead = '<meta http-equiv="Content-Security-Policy"'
+ ' content="script-src \'nonce-aem\' \'strict-dynamic\';"'
+ ' move-to-http-header="true" />'
+ '<script nonce="aem" src="/scripts/aem.js" type="module"></script>';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the nonce="aem" also be removed?


const tree = await composeHtml(daCtx, aemCtx, '<div><p>content</p></div>', cspHead);
const html = serializeHtml(tree);

assert.ok(!html.includes('Content-Security-Policy'));
assert.ok(!html.includes('move-to-http-header'));
assert.ok(!html.includes('nonce="aem"'));
// the rest of head.html is untouched
assert.ok(html.includes('src="/scripts/aem.js"'));
});

it('keeps a CSP meta the pipeline would have left in place', async () => {
const cspHead = '<meta http-equiv="Content-Security-Policy" content="object-src \'none\';" />';

const tree = await composeHtml(daCtx, aemCtx, '<div><p>content</p></div>', cspHead);
const html = serializeHtml(tree);

assert.ok(html.includes('Content-Security-Policy'));
});
});