methodnoallow.js 573 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. const methods = require('methods');
  3. const METHODS_NOT_ALLOWED = [ 'trace', 'track' ];
  4. const safeHttpMethodsMap = {};
  5. for (const method of methods) {
  6. if (!METHODS_NOT_ALLOWED.includes(method)) {
  7. safeHttpMethodsMap[method.toUpperCase()] = true;
  8. }
  9. }
  10. // https://www.owasp.org/index.php/Cross_Site_Tracing
  11. // http://jsperf.com/find-by-map-with-find-by-array
  12. module.exports = () => {
  13. return function notAllow(ctx, next) {
  14. // ctx.method is upper case
  15. if (!safeHttpMethodsMap[ctx.method]) {
  16. ctx.throw(405);
  17. }
  18. return next();
  19. };
  20. };