333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
 
const escapeMap = {
  '"': '"',
  '<': '&lt;',
  '>': '&gt;',
  '\'': '&#x27;',
};
 
module.exports = function surl(val) {
 
  // Just get the converted the protocalWhiteList in `Set` mode,
  // Avoid conversions in `foreach`
  const protocolWhiteListSet = this.app.config.security._protocolWhiteListSet;
 
  if (typeof val !== 'string') return val;
 
  // only test on absolute path
  if (val[0] !== '/') {
    const arr = val.split('://', 2);
    const protocol = arr.length > 1 ? arr[0].toLowerCase() : '';
    if (protocol === '' || !protocolWhiteListSet.has(protocol)) {
      if (this.app.config.env === 'local') {
        this.ctx.coreLogger.warn('[egg-security:surl] url: %j, protocol: %j, ' +
          'protocol is empty or not in white list, convert to empty string', val, protocol);
      }
      return '';
    }
  }
 
  return val.replace(/["'<>]/g, function(ch) {
    return escapeMap[ch];
  });
};