|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import * as fs from "fs/promises" |
| 3 | +import path from "path" |
| 4 | +import { Instruction } from "../../src/session/instruction" |
| 5 | +import { Instance } from "../../src/project/instance" |
| 6 | +import { tmpdir } from "../fixture/fixture" |
| 7 | + |
| 8 | +describe("Instruction.systemPaths symlink containment", () => { |
| 9 | + test("symlink escaping rules directory is excluded", async () => { |
| 10 | + // Create a temp dir with a rules directory containing: |
| 11 | + // - normal.md (regular file, should be included) |
| 12 | + // - evil.md -> /etc/hosts (symlink escape, should be excluded) |
| 13 | + await using homeTmp = await tmpdir({ |
| 14 | + init: async (dir) => { |
| 15 | + const rulesDir = path.join(dir, ".opencode", "rules") |
| 16 | + await fs.mkdir(rulesDir, { recursive: true }) |
| 17 | + await Bun.write(path.join(rulesDir, "normal.md"), "# Normal Rules") |
| 18 | + |
| 19 | + // Create a symlink that escapes the rules directory |
| 20 | + try { |
| 21 | + await fs.symlink("/etc/hosts", path.join(rulesDir, "evil.md")) |
| 22 | + } catch { |
| 23 | + // If we can't create symlinks (permission issue), skip this part |
| 24 | + } |
| 25 | + }, |
| 26 | + }) |
| 27 | + await using projectTmp = await tmpdir() |
| 28 | + |
| 29 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 30 | + process.env.OPENCODE_TEST_HOME = homeTmp.path |
| 31 | + |
| 32 | + try { |
| 33 | + await Instance.provide({ |
| 34 | + directory: projectTmp.path, |
| 35 | + fn: async () => { |
| 36 | + const paths = await Instruction.systemPaths() |
| 37 | + const allPaths = Array.from(paths) |
| 38 | + |
| 39 | + // Normal file should be included |
| 40 | + expect(allPaths.some((p) => p.endsWith("normal.md"))).toBe(true) |
| 41 | + |
| 42 | + // Symlink escape should be excluded |
| 43 | + expect(allPaths.some((p) => p.endsWith("evil.md"))).toBe(false) |
| 44 | + }, |
| 45 | + }) |
| 46 | + } finally { |
| 47 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + test("symlink within rules directory is allowed", async () => { |
| 52 | + // A symlink that points to another file INSIDE the rules dir should be fine |
| 53 | + await using homeTmp = await tmpdir({ |
| 54 | + init: async (dir) => { |
| 55 | + const rulesDir = path.join(dir, ".opencode", "rules") |
| 56 | + await fs.mkdir(rulesDir, { recursive: true }) |
| 57 | + await Bun.write(path.join(rulesDir, "original.md"), "# Original Rules") |
| 58 | + |
| 59 | + try { |
| 60 | + await fs.symlink( |
| 61 | + path.join(rulesDir, "original.md"), |
| 62 | + path.join(rulesDir, "alias.md"), |
| 63 | + ) |
| 64 | + } catch { |
| 65 | + // If symlinks not supported, test is meaningless |
| 66 | + } |
| 67 | + }, |
| 68 | + }) |
| 69 | + await using projectTmp = await tmpdir() |
| 70 | + |
| 71 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 72 | + process.env.OPENCODE_TEST_HOME = homeTmp.path |
| 73 | + |
| 74 | + try { |
| 75 | + await Instance.provide({ |
| 76 | + directory: projectTmp.path, |
| 77 | + fn: async () => { |
| 78 | + const paths = await Instruction.systemPaths() |
| 79 | + const allPaths = Array.from(paths) |
| 80 | + |
| 81 | + // Both original and alias should be included (alias resolves within rules dir) |
| 82 | + expect(allPaths.some((p) => p.endsWith("original.md"))).toBe(true) |
| 83 | + expect(allPaths.some((p) => p.endsWith("alias.md"))).toBe(true) |
| 84 | + }, |
| 85 | + }) |
| 86 | + } finally { |
| 87 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + test("broken symlink is skipped gracefully", async () => { |
| 92 | + await using homeTmp = await tmpdir({ |
| 93 | + init: async (dir) => { |
| 94 | + const rulesDir = path.join(dir, ".opencode", "rules") |
| 95 | + await fs.mkdir(rulesDir, { recursive: true }) |
| 96 | + await Bun.write(path.join(rulesDir, "valid.md"), "# Valid Rules") |
| 97 | + |
| 98 | + try { |
| 99 | + // Symlink to a non-existent target |
| 100 | + await fs.symlink("/nonexistent/path/to/nowhere.md", path.join(rulesDir, "broken.md")) |
| 101 | + } catch { |
| 102 | + // If symlinks not supported, test is meaningless |
| 103 | + } |
| 104 | + }, |
| 105 | + }) |
| 106 | + await using projectTmp = await tmpdir() |
| 107 | + |
| 108 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 109 | + process.env.OPENCODE_TEST_HOME = homeTmp.path |
| 110 | + |
| 111 | + try { |
| 112 | + await Instance.provide({ |
| 113 | + directory: projectTmp.path, |
| 114 | + fn: async () => { |
| 115 | + // Should not throw |
| 116 | + const paths = await Instruction.systemPaths() |
| 117 | + const allPaths = Array.from(paths) |
| 118 | + |
| 119 | + // Valid file should be included |
| 120 | + expect(allPaths.some((p) => p.endsWith("valid.md"))).toBe(true) |
| 121 | + |
| 122 | + // Broken symlink should be excluded (realpath fails) |
| 123 | + expect(allPaths.some((p) => p.endsWith("broken.md"))).toBe(false) |
| 124 | + }, |
| 125 | + }) |
| 126 | + } finally { |
| 127 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + test("project rules symlink escape is also blocked", async () => { |
| 132 | + await using homeTmp = await tmpdir() |
| 133 | + await using projectTmp = await tmpdir({ |
| 134 | + init: async (dir) => { |
| 135 | + const rulesDir = path.join(dir, ".opencode", "rules") |
| 136 | + await fs.mkdir(rulesDir, { recursive: true }) |
| 137 | + await Bun.write(path.join(rulesDir, "legit.md"), "# Legit Project Rules") |
| 138 | + |
| 139 | + try { |
| 140 | + await fs.symlink("/etc/passwd", path.join(rulesDir, "sneaky.md")) |
| 141 | + } catch { |
| 142 | + // If symlinks not supported |
| 143 | + } |
| 144 | + }, |
| 145 | + }) |
| 146 | + |
| 147 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 148 | + process.env.OPENCODE_TEST_HOME = homeTmp.path |
| 149 | + |
| 150 | + try { |
| 151 | + await Instance.provide({ |
| 152 | + directory: projectTmp.path, |
| 153 | + fn: async () => { |
| 154 | + const paths = await Instruction.systemPaths() |
| 155 | + const allPaths = Array.from(paths) |
| 156 | + |
| 157 | + // Legit file should be included |
| 158 | + expect(allPaths.some((p) => p.endsWith("legit.md"))).toBe(true) |
| 159 | + |
| 160 | + // Symlink escape should be excluded |
| 161 | + expect(allPaths.some((p) => p.endsWith("sneaky.md"))).toBe(false) |
| 162 | + }, |
| 163 | + }) |
| 164 | + } finally { |
| 165 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 166 | + } |
| 167 | + }) |
| 168 | +}) |
0 commit comments