Skip to content

Commit 2645cb1

Browse files
authoredOct 7, 2021
Enforce CSP (#551)
Switches our Content Security Policy from report-only mode to enforced mode. According to our internal dashboard, it looks like CSP violation numbers dropped very sharply on October 1, which is the day #540 landed. There do seem to be a few reports coming in as recently as October 5, but if so it is a very small number. Could be due to caching? Browser extensions injecting scripts/images etc. will also cause ongoing CSP violations, that's expected behavior. Also adds https://www.googletagmanager.com to the img-src directive, since https://developers.google.com/tag-manager/web/csp documents that this is needed, and in one page load I did actually see a violation here in local dev mode (but not consistently -- I can't reproduce it now). I guess analytics sometimes uses images for some reason. Fixes #517 Filed #550 to track the most important improvement, which we can't do until https://bugs.chromium.org/p/chromium/issues/detail?id=1253267 is fixed.
1 parent 26527ea commit 2645cb1

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed
 

‎packages/lit-dev-server/src/middleware/content-security-policy-middleware.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ export const contentSecurityPolicyMiddleware = (
121121

122122
// TODO(aomarks) We use some data: URLs for SVGs in docs.css. There's
123123
// probably a simpler way.
124+
//
124125
// The ytimg.com domain is needed for embedded YouTube videos.
125-
`img-src 'self' data: https://i.ytimg.com/`,
126+
//
127+
// The googletagmanager.com domain is needed for Google Analytics
128+
// (https://developers.google.com/tag-manager/web/csp).
129+
`img-src 'self' data: https://i.ytimg.com/ https://www.googletagmanager.com/`,
126130

127131
// Disallow any embeds, applets, etc. This would usually be covered by
128132
// `default-src: 'none'`, but we can't set that for the reason explained
@@ -201,9 +205,7 @@ export const contentSecurityPolicyMiddleware = (
201205
} else {
202206
policy = strictFallbackCsp;
203207
}
204-
// TODO(aomarks) Remove -Report-Only suffix when we are confident the
205-
// policy is working.
206-
ctx.set('Content-Security-Policy-Report-Only', policy);
208+
ctx.set('Content-Security-Policy', policy);
207209
return next();
208210
};
209211
};

0 commit comments

Comments
 (0)
Please sign in to comment.