-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathauto-csp.ts
More file actions
138 lines (117 loc) · 4.16 KB
/
auto-csp.ts
File metadata and controls
138 lines (117 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import assert from 'node:assert/strict';
import { setTimeout } from 'node:timers/promises';
import { getGlobalVariable } from '../../utils/env';
import { expectFileToMatch, writeFile, writeMultipleFiles } from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { execAndWaitForOutputToMatch, ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { executeBrowserTest } from '../../utils/puppeteer';
const CSP_META_TAG = /<meta http-equiv="Content-Security-Policy"/;
export default async function () {
assert(
getGlobalVariable('argv')['esbuild'],
'This test should not be called in the Webpack suite.',
);
// Add global css to trigger critical css inlining
await writeFile('src/styles.css', `body { color: green }`);
// Turn on auto-CSP
await updateJsonFile('angular.json', (json) => {
const build = json['projects']['test-project']['architect']['build'];
build.options = {
...build.options,
security: { autoCsp: true },
};
});
await writeMultipleFiles({
'serve.js': `
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'dist/test-project/browser')));
app.listen(PORT, () => {
console.log('Node Express server listening on ' + PORT);
});
`,
'public/script1.js': `
const externalScriptCreated = 1337;
console.warn('First External Script: ' + inlineScriptBodyCreated);
`,
'public/script2.js': `console.warn('Second External Script: ' + externalScriptCreated);`,
'src/index.html': `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="/">
<script>
const inlineScriptHeadCreated = 1339;
console.warn("Inline Script Head");
</script>
</head>
<body>
<app-root></app-root>
<script>
const inlineScriptBodyCreated = 1338;
console.warn("Inline Script Body: " + inlineScriptHeadCreated);
</script>
<script src='script1.js'></script>
<script src='script2.js'></script>
</body>
</html>
`,
});
async function spawnServer(): Promise<number> {
const port = await findFreePort();
await execAndWaitForOutputToMatch('node', ['serve.js'], /Node Express server listening on/, {
...process.env,
'PORT': String(port),
});
return port;
}
await ng('build');
// Make sure the output files have auto-CSP as a result of `ng build`
await expectFileToMatch('dist/test-project/browser/index.html', CSP_META_TAG);
// Make sure if contains the critical CSS inlining CSP code.
await expectFileToMatch('dist/test-project/browser/index.html', 'ngCspMedia');
// Make sure that our e2e tests run to confirm that our angular project runs.
const port = await spawnServer();
await executeBrowserTest({
baseUrl: `http://localhost:${port}/`,
checkFn: async (page) => {
const warnMessages: string[] = [];
page.on('console', (msg) => {
if (msg.type() === 'warn') {
warnMessages.push(msg.text());
}
});
// Reload to ensure we capture messages from the start if needed,
// although executeBrowserTest already navigated.
await page.reload();
// Wait for the expected number of warnings
let retries = 50;
while (warnMessages.length < 4 && retries > 0) {
await setTimeout(100);
retries--;
}
assert.strictEqual(
warnMessages.length,
4,
`Expected 4 console warnings, but got ${warnMessages.length}:\n${warnMessages.join('\n')}`,
);
const expectedMessages = [
'Inline Script Head',
'Inline Script Body: 1339',
'First External Script: 1338',
'Second External Script: 1337',
];
for (let i = 0; i < expectedMessages.length; i++) {
if (!warnMessages[i].includes(expectedMessages[i])) {
assert.fail(
`Expected warning ${i} to include '${expectedMessages[i]}', but got '${warnMessages[i]}'`,
);
}
}
},
});
}