ApacheやNginxでX-XSS-ProtectionやX-Frame-Optionsをレスポンスヘッダーに追加できるように、
AWSマネージドサービスのCloudFront(CDN)でもLambda@Edgeを使えばリクエストヘッダーやレスポンスヘッダーをカスタマイズして、
Webアプリのセキュリティレベル上げることができる。
設定するに当たって注意すること
・Lambda@Edgeはバージニア北部リージョンでしか動かない
・現時点(2018/09/14)ではNode.js 6.10しか対応していない
・Lambda実行ロールの[信頼関係]項目を編集してサービスプリンシパルにedgelambda.amazonaws.comとlambda.amazonaws.comを追加する必要がある
使用する関数は以下
'use strict';
exports.handler = (event, context, callback) => {
//Get contents of response
const response = event.Records[0].cf.response;
const headers = response.headers;
//Set new headers
headers['strict-transport-security'] = [{key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubdomains; preload'}];
headers['content-security-policy'] = [{key: 'Content-Security-Policy', value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'"}];
headers['x-content-type-options'] = [{key: 'X-Content-Type-Options', value: 'nosniff'}];
headers['x-frame-options'] = [{key: 'X-Frame-Options', value: 'DENY'}];
headers['x-xss-protection'] = [{key: 'X-XSS-Protection', value: '1; mode=block'}];
headers['referrer-policy'] = [{key: 'Referrer-Policy', value: 'same-origin'}];
//Return modified response
callback(null, response);
};
※大規模なWebアプリを作っているなどの際にはCSPの設定には注意。
引用・参考
https://aws.amazon.com/jp/blogs/networking-and-content-delivery/adding-http-security-headers-using-lambdaedge-and-amazon-cloudfront/
https://qiita.com/peg_73_/items/d5eafc06925d9a66f3de
トップへ