spath.js 611 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. /**
  3. * File Inclusion
  4. */
  5. function pathFilter(path) {
  6. if (typeof path !== 'string') return path;
  7. const pathSource = path;
  8. while (path.indexOf('%') !== -1) {
  9. try {
  10. path = decodeURIComponent(path);
  11. } catch (e) {
  12. if (process.env.NODE_ENV !== 'production') {
  13. // Not a PROD env, logging with a warning.
  14. this.ctx.coreLogger.warn('[egg-security:helper:spath] : decode file path %s failed.', path);
  15. }
  16. break;
  17. }
  18. }
  19. if (path.indexOf('..') !== -1 || path[0] === '/') {
  20. return null;
  21. }
  22. return pathSource;
  23. }
  24. module.exports = pathFilter;