Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { htmlRewritingStream } from '../../../../utils/index-file/html-rewriting-stream';
import { buildApplication } from '../../index';
import { OutputHashing } from '../../schema';
import {
APPLICATION_BUILDER_INFO,
BASE_OPTIONS,
describeBuilder,
lazyModuleFiles,
lazyModuleFnImport,
} from '../setup';

interface IndexHtmlInlineContent {
externalScripts: string[];
externalStylesheets: string[];
inlineScripts: string[];
inlineStyles: number;
inlineStyleAttributes: string[];
inlineEventHandlers: string[];
cspMetaTags: string[];
}

async function inspectIndexHtml(html: string): Promise<IndexHtmlInlineContent> {
const { rewriter, transformedContent } = await htmlRewritingStream(html);
const externalScripts: string[] = [];
const externalStylesheets: string[] = [];
const inlineScripts: string[] = [];
let inlineStyles = 0;
const inlineStyleAttributes: string[] = [];
const inlineEventHandlers: string[] = [];
const cspMetaTags: string[] = [];

rewriter.on('startTag', (tag) => {
if (tag.tagName === 'script') {
const src = tag.attrs.find((attr) => attr.name === 'src')?.value;
if (src) {
externalScripts.push(src);
} else {
const type = tag.attrs.find((attr) => attr.name === 'type')?.value;
inlineScripts.push(type ? `<script type="${type}">` : '<script>');
}
} else if (tag.tagName === 'link') {
const rel = tag.attrs
.find((attr) => attr.name === 'rel')
?.value.trim()
.toLowerCase();
const href = tag.attrs.find((attr) => attr.name === 'href')?.value;
if (rel === 'stylesheet' && href) {
externalStylesheets.push(href);
}
} else if (tag.tagName === 'style') {
inlineStyles++;
} else if (tag.tagName === 'meta') {
const httpEquiv = tag.attrs
.find((attr) => attr.name === 'http-equiv')
?.value.trim()
.toLowerCase();
if (
httpEquiv === 'content-security-policy' ||
httpEquiv === 'content-security-policy-report-only'
) {
cspMetaTags.push(httpEquiv);
}
}

for (const attr of tag.attrs) {
if (attr.name === 'style') {
inlineStyleAttributes.push(`${tag.tagName}[style]`);
} else if (/^on/i.test(attr.name)) {
inlineEventHandlers.push(`${tag.tagName}[${attr.name}]`);
}
}

rewriter.emitStartTag(tag);
});

await transformedContent();

return {
externalScripts,
externalStylesheets,
inlineScripts,
inlineStyles,
inlineStyleAttributes,
inlineEventHandlers,
cspMetaTags,
};
}

async function expectExternalOnlyIndexHtml(indexHtml: string): Promise<void> {
const inspected = await inspectIndexHtml(indexHtml);

expect(inspected.externalScripts.length)
.withContext('expected external script tags in index.html')
.toBeGreaterThan(0);
expect(inspected.externalStylesheets.length)
.withContext('expected external stylesheet links in index.html')
.toBeGreaterThan(0);
expect(inspected.inlineScripts).withContext('unexpected inline <script> tags').toEqual([]);
expect(inspected.inlineStyles).withContext('unexpected inline <style> tags').toBe(0);
expect(inspected.inlineStyleAttributes)
.withContext('unexpected inline style attributes')
.toEqual([]);
expect(inspected.inlineEventHandlers)
.withContext('unexpected inline event handler attributes')
.toEqual([]);
expect(inspected.cspMetaTags).withContext('unexpected CSP <meta> tags').toEqual([]);
}

describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
describe('Behavior: "CSP without inline scripts or styles"', () => {
beforeEach(async () => {
await harness.writeFile('src/styles.css', 'body { color: #000; }');
await harness.writeFiles(lazyModuleFiles);
await harness.writeFiles(lazyModuleFnImport);
});

it('should not emit inline scripts, inline styles, event handlers, or CSP meta tags in an optimized CSR build', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
outputHashing: OutputHashing.All,
optimization: {
scripts: true,
styles: {
minify: true,
inlineCritical: false,
},
fonts: false,
},
subresourceIntegrity: false,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();

await expectExternalOnlyIndexHtml(harness.readFile('dist/browser/index.html'));
});

it('should not emit inline scripts, inline styles, event handlers, or CSP meta tags when optimization is disabled', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
optimization: false,
subresourceIntegrity: false,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();

await expectExternalOnlyIndexHtml(harness.readFile('dist/browser/index.html'));
});
});
});