Skip to content

Commit aa2a8e8

Browse files
Daniel-KhodabakhshAnujRNair
authored andcommitted
Add meta tag if it doesn't exist (#13)
Add meta tag if it doesn't exist
1 parent f729964 commit aa2a8e8

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

plugin.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,26 @@ class CspHtmlWebpackPlugin {
9393
decodeEntities: false
9494
});
9595

96+
let metaTag = $('meta[http-equiv="Content-Security-Policy"]');
97+
9698
// if not enabled, remove the empty tag
9799
if (!this.isEnabled(htmlPluginData)) {
98-
$('meta[http-equiv="Content-Security-Policy"]').remove();
100+
metaTag.remove();
99101

100102
// eslint-disable-next-line no-param-reassign
101103
htmlPluginData.html = $.html();
102104

103105
return compileCb(null, htmlPluginData);
104106
}
105107

108+
// Add element if it doesn't exist.
109+
if (!metaTag.length) {
110+
metaTag = cheerio.load('<meta http-equiv="Content-Security-Policy">')(
111+
'meta'
112+
);
113+
metaTag.appendTo($('head'));
114+
}
115+
106116
const policyObj = JSON.parse(JSON.stringify(this.policy));
107117

108118
const inlineSrc = $('script:not([src])')
@@ -120,10 +130,7 @@ class CspHtmlWebpackPlugin {
120130
inlineStyle
121131
);
122132

123-
$('meta[http-equiv="Content-Security-Policy"]').attr(
124-
'content',
125-
this.buildPolicy(policyObj)
126-
);
133+
metaTag.attr('content', this.buildPolicy(policyObj));
127134

128135
// eslint-disable-next-line no-param-reassign
129136
htmlPluginData.html = $.html();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<meta name="author" content="Slack">
5+
<title>Slack CSP HTML Webpack Plugin Tests</title>
6+
</head>
7+
<body>
8+
Body
9+
</body>
10+
</html>

spec/plugin.spec.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,85 @@ describe('CspHtmlWebpackPlugin', () => {
286286
);
287287
});
288288

289+
it('adds meta tag with completed policy when no meta tag is specified', done => {
290+
const webpackConfig = {
291+
entry: path.join(__dirname, 'fixtures/index.js'),
292+
output: {
293+
path: OUTPUT_DIR,
294+
filename: 'index.bundle.js'
295+
},
296+
plugins: [
297+
new HtmlWebpackPlugin({
298+
filename: path.join(OUTPUT_DIR, 'index.html'),
299+
template: path.join(__dirname, 'fixtures', 'with-no-meta-tag.html'),
300+
inject: 'body'
301+
}),
302+
new CspHtmlWebpackPlugin({
303+
'base-uri': ["'self'", 'https://slack.com'],
304+
'object-src': ["'self'"],
305+
'script-src': ["'self'"],
306+
'style-src': ["'self'"]
307+
})
308+
]
309+
};
310+
311+
testCspHtmlWebpackPlugin(
312+
webpackConfig,
313+
'index.html',
314+
(cspPolicy, _, doneFn) => {
315+
const expected =
316+
"base-uri 'self' https://slack.com;" +
317+
" object-src 'self';" +
318+
" script-src 'self';" +
319+
" style-src 'self'";
320+
321+
expect(cspPolicy).toEqual(expected);
322+
323+
doneFn();
324+
},
325+
done
326+
);
327+
});
328+
329+
it('adds meta tag with completed policy when no template is specified', done => {
330+
const webpackConfig = {
331+
entry: path.join(__dirname, 'fixtures/index.js'),
332+
output: {
333+
path: OUTPUT_DIR,
334+
filename: 'index.bundle.js'
335+
},
336+
plugins: [
337+
new HtmlWebpackPlugin({
338+
filename: path.join(OUTPUT_DIR, 'index.html'),
339+
inject: 'body'
340+
}),
341+
new CspHtmlWebpackPlugin({
342+
'base-uri': ["'self'", 'https://slack.com'],
343+
'object-src': ["'self'"],
344+
'script-src': ["'self'"],
345+
'style-src': ["'self'"]
346+
})
347+
]
348+
};
349+
350+
testCspHtmlWebpackPlugin(
351+
webpackConfig,
352+
'index.html',
353+
(cspPolicy, _, doneFn) => {
354+
const expected =
355+
"base-uri 'self' https://slack.com;" +
356+
" object-src 'self';" +
357+
" script-src 'self';" +
358+
" style-src 'self'";
359+
360+
expect(cspPolicy).toEqual(expected);
361+
362+
doneFn();
363+
},
364+
done
365+
);
366+
});
367+
289368
it('throws an error if an invalid hashing method is used', () => {
290369
expect(() => {
291370
// eslint-disable-next-line no-new

0 commit comments

Comments
 (0)