aboutsummaryrefslogtreecommitdiff
path: root/test/util/paths-test.ts
blob: 4fff247cf44fe1f0e08eec48216665a22b92888c (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Runs on every OS in the CI matrix on purpose: `isPathWithin` delegates to `node:path`, so
// only a real Windows run proves the backslash and drive-letter-case cases the old check failed.

import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { tmpdir } from "node:os";
import { join, resolve, sep } from "node:path";
import { isPathWithin } from "../../src/util/paths";

const root = resolve(tmpdir());

describe("path containment", () => {
  test("a directory contains itself", () => {
    assert.equal(isPathWithin(root, root), true);
  });

  test("a direct child is contained", () => {
    assert.equal(isPathWithin(root, join(root, "script.js")), true);
  });

  test("a nested child is contained", () => {
    assert.equal(isPathWithin(root, join(root, "a", "b", "script.js")), true);
  });

  test("a native-separator path is contained", () => {
    assert.equal(isPathWithin(root, `${root}${sep}script.js`), true);
  });

  test("an unnormalised path that still lands inside is contained", () => {
    assert.equal(isPathWithin(root, join(root, "a", "..", "script.js")), true);
  });

  test("an unnormalised path that escapes the root is rejected", () => {
    assert.equal(isPathWithin(root, join(root, "..", "..", "evil.js")), false);
  });

  test("a sibling directory sharing the root's name prefix is rejected", () => {
    assert.equal(isPathWithin(root, `${root}-sibling${sep}script.js`), false);
  });

  test("the root's own parent is rejected", () => {
    assert.equal(isPathWithin(root, join(root, "..")), false);
  });

  test("the roots the run-script tool allows accept a file written into them", () => {
    for (const allowed of [tmpdir(), process.cwd()]) {
      assert.equal(isPathWithin(allowed, join(allowed, "bh-script.mjs")), true, allowed);
    }
  });
});