Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Last active March 26, 2016 05:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuroyoro/ab92faa955dca4ef7267 to your computer and use it in GitHub Desktop.
Save yuroyoro/ab92faa955dca4ef7267 to your computer and use it in GitHub Desktop.

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;
          });
      })
  );
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment