Images not loaded due to security headers restrictions
zainfathoni opened this issue ยท 17 comments
Is it related to your PWA implementation in #705 combined with our new security policies, Mas @mazipan?
It seems that the error started from this deploy preview
https://deploy-preview-705--wargabantuwarga.netlify.app
I'm also trying another approach by adding res.cloudinary.com
to the content security policy header on #719 PR.
Let's reopen this issue until it's confirmed to be resolved.
I think this is due to service worker. The images work just fine before.
Apparently so, I tried accessing the deploy preview on https://deploy-preview-719--wargabantuwarga.netlify.app/ once the PWA kicks in, it's failing again.
Revert it first, I will recheck the implementation later when have time.
Shall we remove the service worker first until we can figure out a way to add it without messing up CSP?
Since we already added service worker, we should also modify our service worker to become a self-destructing service worker, just in case people already have it installed:
self.addEventListener('install', () => {
self.skipWaiting();
});
self.addEventListener('activate', () => {
self.registration.unregister()
.then(() => {
return self.clients.matchAll();
})
.then((clients) => {
clients.forEach(client => client.navigate(client.url))
});
});
Mas @mazipan, where did the workbox added the service-worker.js
file?
According to this guide, we need to replace the service worker file content with that code snippet so the browser would uninstall the previously installed service worker.
I'm still experiencing the issue on my local since the service worker is still kicking in.
I'm outside and accessing from my phone (so can't investigate dev console) but +1 having the same behaviour as @zainfathoni on https://deploy-preview-719--wargabantuwarga.netlify.app/
- initially images load fine
- on 2nd/3rd reload once the service worker has taken control (as indicated by "add to home screen" menu) images don't load
It's generated by next-pwa.
Relevant resources for further investigation in the future.
Next-pwa uses Workbox under the hood, so this may be relevant: https://www.npmjs.com/package/cloudinary-workbox-plugin
More on SW cross origin caching behaviour:
I am new with CSP stuff, but I think this might be a clue https://qubyte.codes/blog/content-security-policy-and-service-workers.
TLDR; SW is not using image-src
policy but using connect-src
policy instead. Since we don't have connect-src
it fallback to default-src
wich is only limited to self
/ origin of the domain itself. So, it refuses to "connect"
Mas @zainfathoni tried to add Cloudinary url to script-src
(at #719 ) but ofc it doesn't work, we need to add to connect-src
.
So I tried to add res.cloudinary.com to connect-src
and deploy to my personal netlify account for preview (yes, I'm dumb. brb making a Draft PR for deploy preview). And it works (and it cache cloudinary img assets)
https://hardcore-noyce-3b9626.netlify.app/
Update:
it works https://deploy-preview-762--wargabantuwarga.netlify.app.
need review #762