Created
July 9, 2019 21:05
-
-
Save lesleh/85b0012a7c14f10b94300b7215334ad4 to your computer and use it in GitHub Desktop.
Lambda@Edge security headers
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'; | |
function addHeader(headers, key, value) { | |
if (!headers[key.toLowerCase()]) { | |
headers[key.toLowerCase()] = [{ | |
key, | |
value, | |
}]; | |
} | |
} | |
exports.handler = (event, context, callback) => { | |
const { response } = event.Records[0].cf; | |
const { headers } = response; | |
addHeader(headers, 'Strict-Transport-Security', 'max-age=15552000'); | |
addHeader(headers, 'X-Frame-Options', 'SAMEORIGIN'); | |
addHeader(headers, 'X-XSS-Protection', '1; mode=block'); | |
addHeader(headers, 'X-Content-Type-Options', 'nosniff'); | |
addHeader(headers, 'Feature-Policy', "camera 'none'; geolocation 'none'; microphone 'none'"); | |
addHeader(headers, 'Referrer-Policy', 'same-origin'); | |
addHeader(headers, 'Content-Security-Policy', 'upgrade-insecure-requests'); | |
return callback(null, response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment