amp-iframeにおけるallow-same-originの取り扱いと、Let's Encryptで証明書にAlternative Nameが追加できない件

公開:2017-09-10 10:01
更新:2020-02-15 04:37
カテゴリ:AMP,静的ブログジェネレータ,Lets Encrypt,HTML,IFRAME,iframe origin policy

はじめに

amp-iframeの「amp-iframe origin policy」についての対応の続き。

AMPにおける「Origin of <amp-iframe> must not be equal to container 」エラーとIframe origin policy 、さらには<iframe>のsandboxについて

おさらい

amp-iframeは下記ドキュメントによれば、allow-same-originを指定するときはamp-iframesrc別オリジンでなければならないとこのことである。

amphtml/amp-iframe-origin-policy.md at master · ampproject/amphtml

理由としては

とのことであった。

amp-iframeの場合、既定でiframesandboxを空文字列指定した時と同じ強い制約がかかる。sandbox属性にトークンを指定することで、必要な権限をつけていく。

iframeallow-same-originを指定した場合、同一オリジンおよび異なるオリジンであっても動作には支障ない。amp-iframeの場合は上述の理由によって異なるオリジンが強制されるため、対応が必要となる。

allow-same-origin

具体的にはamp-iframeのみ別オリジンとなるようにsrc属性を書き換えることが必要になってくる。もしくはiframeamp-iframeに関係なく別オリジンとするかとか。

allow-same-originは強力な権限なのでそもそもつけるべきではない。ホストするページと交信することもないのであればallow-scriptsにとどめておくことが望ましい。もちろんスクリプトの実行の必要のないページはallow-scriptsすら指定する必要はないが。。

今起きている問題

で、今起きていることでちょっと悩ましいのは、 amp-iframeでホストされるページが、同一オリジンでありかつwindow.localStorageを使用するものである。localStorageでホストページとデータのやり取りをするわけではなく、ホストされるページはもともと独立して動作するものである。直接ページにアクセスすれば動作に支障ないものの、amp-iframeでホストしようとすると問題が発生するのである。amp-iframeにはallow-scriptを設定していて、スクリプトの実行はできるのだが、windowへのアクセスは制限がかかってしまう。

対応するにはamp-iframesandbox属性にallow-same-originを追加する必要がある。iframeであればこれで問題は解消するが、amp-iframeでは同一オリジンでallow-same-originは指定できないため、なにも表示されなくなってしまう。

これを解消するにはホストされるコンテンツを別オリジンでサーブする必要がある。オリジンが異なればよいので、例えば「www.xxx.com」でサーブしている場合、DNSに同じIPで「alter.xxx.com」を定義し、WWWサーバ側でホスト名を追加すれば比較的簡単にできるかなと考えた。つまりamp-iframesrcを見て、同一オリジンであれば「alter.xxx.com」に書き換えるのである。これで解決するはずだ。

Let's EncryptでなぜかAlternative Nameが追加できない

私のWWWサーバーはhttps onlyなので、新しいホスト名をAlternative Nameとして証明書に追加しなくてはならない。証明書はLet's Encryptで取得しているので、DNSに「alter.sfpgmr.net」を追加し名前が引けることを確認したのち 以下のコマンドを実行した。

./certbot/certbot-auto certonly --standalone -d www.sfpgmr.net -d github.sfpgmr.net -d alter.sfpgmr.
net
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Cert not yet due for renewal

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/www.sfpgmr.net-0003.conf)

What would you like to do?
-------------------------------------------------------------------------------
1: Keep the existing certificate for now
2: Renew & replace the cert (limit ~5 per 7 days)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Renewing an existing certificate
Performing the following challenges:
tls-sni-01 challenge for www.sfpgmr.net
tls-sni-01 challenge for github.sfpgmr.net
tls-sni-01 challenge for alter.sfpgmr.net
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/xxxx/xxx/xxxxx/yyyy.pem
   Your key file has been saved at:
   /etc/xxxx/xxx/xxxxx/zzzzz.pem
   Your cert will expire on 2017-12-08. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

一応エラーなく追加できたのだが、実際にalter.sfpgmr.netでSSL Testを実行すると、「Alternative names Missmatch」となってしまうのである。

Alternative names    blog.sfpgmr.net github.sfpgmr.net www.sfpgmr.net   MISMATCH

上を見るにAlternative Nameに「alter.sfpgmr.net」が追加されていないようである。原因は今のところ不明であるが、何となく「Renew & replace the cert」できるのは「limit ~5 per 7 days」ということらしく、この制限に引っかかったのかな?と思っている。

とりあえず来週また証明書を更新してみることにする。ああ、謎だ。。

2017/9/10 22:00 追記

一度証明書を保存しているフォルダを消して再作成したら、登録できていた。原因は何なのだろう。一体。。
とりあえずはamp-iframeの問題はこれで解消できた。これで次に進めるな。。