Open History to see the last commit
1import { lstat, readFile, realpath, writeFile } from 'node:fs/promises'
2import { isAbsolute, join, relative, sep } from 'node:path'
3
4const WORKFLOWS_DENY = '.github/workflows'
5
6function toPosix(p: string) {
7 return p.split(sep).join('/')
8}
9
10function escapesRoot(rootReal: string, absolute: string) {
11 const rel = relative(rootReal, absolute)
12 return rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel)
13}
14
15function isEnoent(error: unknown) {
16 return (
17 typeof error === 'object' &&
18 error !== null &&
19 'code' in error &&
20 error.code === 'ENOENT'
21 )
22}
23
24function splitRelPath(relPath: string) {
25 const parts = relPath.replace(/\\/g, '/').split('/')
26 const out = []
27 for (const part of parts) {
28 if (part.length === 0 || part === '.') continue
29 if (part === '..') {
30 throw new Error(`path escapes worktree root: ${relPath}`)
31 }
32 out.push(part)
33 }
34 return out
35}
36
37async function resolveUnderRoot(
38 root: string,
39 relPath: string,
40 forWrite: boolean,
41) {
42 const rootReal = await realpath(root)
43 const parts = splitRelPath(relPath)
44 let current = rootReal
45 for (let i = 0; i < parts.length; i++) {
46 const part = parts[i]
47 if (part === undefined) continue
48 const next = join(current, part)
49 const isLast = i === parts.length - 1
50 let st
51 try {
52 st = await lstat(next)
53 } catch (error) {
54 if (!isEnoent(error)) throw error
55 const rest = parts.slice(i)
56 current = join(current, ...rest)
57 if (escapesRoot(rootReal, current)) {
58 throw new Error(`path escapes worktree root: ${relPath}`)
59 }
60 return { absolute: current, posix: toPosix(relative(rootReal, current)) }
61 }
62 if (st.isSymbolicLink()) {
63 if (forWrite && isLast) {
64 throw new Error(`path escapes worktree root: ${relPath}`)
65 }
66 const real = await realpath(next)
67 if (escapesRoot(rootReal, real)) {
68 throw new Error(`path escapes worktree root: ${relPath}`)
69 }
70 current = real
71 continue
72 }
73 current = await realpath(next)
74 if (escapesRoot(rootReal, current)) {
75 throw new Error(`path escapes worktree root: ${relPath}`)
76 }
77 }
78 return { absolute: current, posix: toPosix(relative(rootReal, current)) }
79}
80
81/**
82 * Read a UTF-8 file under a worktree root.
83 *
84 * Resolves each existing path component. Throws if the path leaves `root`
85 * or follows a symlink out of `root`.
86 */
87export async function readWorktreeFile(root: string, relPath: string) {
88 const resolved = await resolveUnderRoot(root, relPath, false)
89 return readFile(resolved.absolute, 'utf8')
90}
91
92/**
93 * Write a UTF-8 file under a worktree root.
94 *
95 * Same escape check as `readWorktreeFile`. Refuses a symlink as the final
96 * component. Also refuses `.github/workflows` after resolution.
97 *
98 * @param root worktree root directory
99 * @param relPath path relative to `root`
100 * @param contents UTF-8 file contents
101 */
102export async function writeWorktreeFile(
103 root: string,
104 relPath: string,
105 contents: string,
106) {
107 const resolved = await resolveUnderRoot(root, relPath, true)
108 const isDenied =
109 resolved.posix === WORKFLOWS_DENY ||
110 resolved.posix.startsWith(`${WORKFLOWS_DENY}/`)
111 if (isDenied) {
112 throw new Error(
113 `refusing to write ${relPath}: ${WORKFLOWS_DENY} is on the deny list`,
114 )
115 }
116 await writeFile(resolved.absolute, contents, 'utf8')
117}