Skip to content

Instantly share code, notes, and snippets.

@lesleh
Created July 9, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesleh/85b0012a7c14f10b94300b7215334ad4 to your computer and use it in GitHub Desktop.
Save lesleh/85b0012a7c14f10b94300b7215334ad4 to your computer and use it in GitHub Desktop.
Lambda@Edge security headers
'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