Last active
August 28, 2018 22:58
-
-
Save neotreat/231053ca222ef723ec44a4952be384cc to your computer and use it in GitHub Desktop.
Add HTTP Security Headers to Cloudfront with Lambda@Edge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
exports.handler = (event, context, callback) => { | |
const response = event.Records[0].cf.response; | |
const headers = response.headers; | |
// Add security headers | |
const securityHeaders = [ | |
[{ | |
'value': 'max-age=31536000', | |
'key': 'Strict-Transport-Security' | |
}], | |
[{ | |
'value': 'deny', | |
'key': 'X-Frame-Options' | |
}], | |
[{ | |
'value': '1; mode=block', | |
'key': 'X-XSS-Protection' | |
}], | |
[{ | |
'value': 'nosniff', | |
'key': 'X-Content-Type-Options' | |
}], | |
[{ | |
'value': 'strict-origin-when-cross-origin', | |
'key': 'Referrer-Policy' | |
}] | |
]; | |
// Add all headers of the array to the response object in the correct format | |
for(let header of securityHeaders) { | |
headers[header[0].key.toLowerCase()] = header; | |
} | |
callback(null, response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment