hsts.js 581 B

123456789101112131415161718192021
  1. 'use strict';
  2. const utils = require('../utils');
  3. // Set Strict-Transport-Security header
  4. module.exports = options => {
  5. return async function hsts(ctx, next) {
  6. await next();
  7. const opts = utils.merge(options, ctx.securityOptions.hsts);
  8. if (utils.checkIfIgnore(opts, ctx)) return;
  9. let val = 'max-age=' + opts.maxAge;
  10. // If opts.includeSubdomains is defined,
  11. // the rule is also valid for all the sub domains of the website
  12. if (opts.includeSubdomains) {
  13. val += '; includeSubdomains';
  14. }
  15. ctx.set('strict-transport-security', val);
  16. };
  17. };