UNPKG

711 BJavaScriptView Raw
1'use strict';
2
3var debug = require('debug')('plugin:header-clean');
4const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/g
5
6// strips bad characters from header values
7module.exports.init = function(config, logger, stats) {
8 return {
9 onrequest: function(req, res, next) {
10 Object.keys(req.headers).forEach(function(key) {
11 var val = req.headers[key]
12 if (typeof val == 'string') {
13 var newVal = val.replace(headerCharRegex, '')
14 if (debug.enabled && val.match(headerCharRegex)) {
15 debug('Cleaned header %s. Old: %s. New: %s', key, val, newVal);
16 }
17 req.headers[key] = newVal
18 }
19 });
20 next();
21 },
22 };
23}
\No newline at end of file