path-is-inside.js 858 B

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. var path = require("path");
  3. module.exports = function (thePath, potentialParent) {
  4. // For inside-directory checking, we want to allow trailing slashes, so normalize.
  5. thePath = stripTrailingSep(thePath);
  6. potentialParent = stripTrailingSep(potentialParent);
  7. // Node treats only Windows as case-insensitive in its path module; we follow those conventions.
  8. if (process.platform === "win32") {
  9. thePath = thePath.toLowerCase();
  10. potentialParent = potentialParent.toLowerCase();
  11. }
  12. return thePath.lastIndexOf(potentialParent, 0) === 0 &&
  13. (
  14. thePath[potentialParent.length] === path.sep ||
  15. thePath[potentialParent.length] === undefined
  16. );
  17. };
  18. function stripTrailingSep(thePath) {
  19. if (thePath[thePath.length - 1] === path.sep) {
  20. return thePath.slice(0, -1);
  21. }
  22. return thePath;
  23. }