ServiceWorkerとupgrade-insecure-requestsで、httpsで提供されていないMixed Contentなリソースでもロードできた。
- レスポンスヘッダで Content-Security-Policy: upgrade-insecure-requests を指定
- SWでのonfetchで、fetchしてみて失敗したやつは、same originにある自作アプリ(https)にhttpでこのurlをかわりにとってくるように依頼
- 自作アプリはurlパラメータで受け取ったurlを代わりに取得し、レスポンスヘッダどボディをそのままかえす
- SWは自作アプリからのresponseをevent.respondWithに設定する
Cookieやヘッダの扱いに問題はあるにせよ、とりあえず動いた。 セキュリティ的にはdowngradeしてて問題がありそうなので、自作アプリでしっかり対策しないとまずいかも
ServiceWorkerの実装こんなかんじ
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request.clone()) // とりあえず取ってみる
.then(function(response) {
console.log("fetch sucecss : " + event.request.url);
console.log(response);
return response;
}).catch(function(error) {
console.log("proxy request : " + event.request.url);
// 自作アプリにかわりに取ってきてもらう
return fetch("/myproxy_request?url=" + encodeURIComponent(event.request.url), { credentials: 'include' })
.then(function(response){
console.log("proxy fetch sucecss : " + event.request.url);
console.log(response);
return response;
});
})
);
});