JailFS.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.JailFS = void 0;
  4. const NodeFS_1 = require("./NodeFS");
  5. const ProxiedFS_1 = require("./ProxiedFS");
  6. const path_1 = require("./path");
  7. const JAIL_ROOT = path_1.PortablePath.root;
  8. class JailFS extends ProxiedFS_1.ProxiedFS {
  9. constructor(target, { baseFs = new NodeFS_1.NodeFS() } = {}) {
  10. super(path_1.ppath);
  11. this.target = this.pathUtils.resolve(path_1.PortablePath.root, target);
  12. this.baseFs = baseFs;
  13. }
  14. getRealPath() {
  15. return this.pathUtils.resolve(this.baseFs.getRealPath(), this.pathUtils.relative(path_1.PortablePath.root, this.target));
  16. }
  17. getTarget() {
  18. return this.target;
  19. }
  20. getBaseFs() {
  21. return this.baseFs;
  22. }
  23. mapToBase(p) {
  24. const normalized = this.pathUtils.normalize(p);
  25. if (this.pathUtils.isAbsolute(p))
  26. return this.pathUtils.resolve(this.target, this.pathUtils.relative(JAIL_ROOT, p));
  27. if (normalized.match(/^\.\.\/?/))
  28. throw new Error(`Resolving this path (${p}) would escape the jail`);
  29. return this.pathUtils.resolve(this.target, p);
  30. }
  31. mapFromBase(p) {
  32. return this.pathUtils.resolve(JAIL_ROOT, this.pathUtils.relative(this.target, p));
  33. }
  34. }
  35. exports.JailFS = JailFS;