Repository navigation
Expand file tree
/
Copy pathsubprocess.js
More file actions
146 lines (130 loc) · 5.26 KB
/
Copy pathsubprocess.js
File metadata and controls
146 lines (130 loc) · 5.26 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Running git and npm safely (#2571).
*
* Plugin sources, refs, pinned commits and package names come from quartz.config.yaml,
* quartz.lock.json and the command line. They are passed to git/npm as argument arrays, never
* through a shell, so `;`, `$(…)`, quotes and spaces are inert. Values that end up as positional
* arguments are also validated (or placed after `--`), because a value starting with `-` would
* otherwise be read as an option: `git fetch origin --upload-pack=<cmd>` runs <cmd> even without
* a shell.
*
* npm is `npm.cmd` on Windows, which Node only runs through a shell (CVE-2024-27980). There,
* every argument must pass a strict whitelist first.
*/
import { execFile, execFileSync } from "node:child_process"
import { promisify } from "node:util"
const execFileAsync = promisify(execFile)
export const GIT_TIMEOUT_MS = 120_000
export const NPM_TIMEOUT_MS = 600_000
const MAX_BUFFER = 64 * 1024 * 1024
/** A full or abbreviated commit SHA (as written to quartz.lock.json). */
export function assertCommit(commit) {
if (typeof commit !== "string" || !/^[0-9a-f]{7,64}$/i.test(commit)) {
throw new Error(`Invalid commit ${JSON.stringify(commit)}: expected a hexadecimal SHA`)
}
return commit
}
/**
* A branch or tag name. Follows `git check-ref-format` closely enough to reject everything that
* could be an option or isn't a ref: no leading `-`, no whitespace or control characters, none of
* `~ ^ : ? * [ \`, no `..`, `@{`, `//`, trailing `/`, `.` or `.lock`.
*/
export function assertRef(ref) {
const invalid =
typeof ref !== "string" ||
ref === "" ||
ref === "@" ||
ref.startsWith("-") ||
ref.startsWith("/") ||
/[\s\x00-\x1f\x7f~^:?*[\\]/.test(ref) ||
ref.includes("..") ||
ref.includes("@{") ||
ref.includes("//") ||
/(^|\/)\./.test(ref) ||
/[/.]$/.test(ref) ||
ref.endsWith(".lock")
if (invalid) throw new Error(`Invalid git ref ${JSON.stringify(ref)}`)
return ref
}
/** A repository URL or path for git (never an option). */
export function assertRepoUrl(url) {
if (typeof url !== "string" || url === "" || url.startsWith("-") || /[\x00-\x1f\x7f]/.test(url)) {
throw new Error(`Invalid repository ${JSON.stringify(url)}`)
}
return url
}
// no leading "-" (it would be read as an npm option), "." or "_"
const NPM_NAME = /^(?:@[a-z0-9~][a-z0-9._~-]*\/)?[a-z0-9~][a-z0-9._~-]*$/
// a version or range after the name: 1.2.3, ^1.2, >=1 <2, latest, npm:alias@1 is not accepted
const NPM_VERSION = /^[0-9A-Za-z.^~<>=*|+ -]+$/
/** An npm package spec: `name`, `@scope/name`, optionally `@<version or range or tag>`. */
export function assertNpmSpec(spec) {
if (typeof spec !== "string") throw new Error(`Invalid npm package ${JSON.stringify(spec)}`)
const at = spec.lastIndexOf("@")
const hasVersion = at > 0
const name = hasVersion ? spec.slice(0, at) : spec
const version = hasVersion ? spec.slice(at + 1) : undefined
if (
!NPM_NAME.test(name) ||
name.length > 214 ||
(version !== undefined && (version === "" || !NPM_VERSION.test(version)))
) {
throw new Error(`Invalid npm package ${JSON.stringify(spec)}`)
}
return spec
}
function options(opts = {}, timeout) {
return {
cwd: opts.cwd,
env: opts.env,
encoding: "utf8",
stdio: opts.stdio ?? "pipe",
timeout: opts.timeout ?? timeout,
maxBuffer: MAX_BUFFER,
windowsHide: true,
}
}
/** Run git synchronously; returns stdout ("" with stdio: "inherit"). */
export function git(args, opts) {
return execFileSync("git", args, options(opts, GIT_TIMEOUT_MS)) ?? ""
}
/** Run git; resolves to { stdout, stderr }. */
export function gitAsync(args, opts) {
const { stdio: _stdio, ...rest } = options(opts, GIT_TIMEOUT_MS)
return execFileAsync("git", args, rest)
}
// Characters allowed in an npm argument that must go through cmd.exe on Windows. Each argument
// is wrapped in double quotes, inside which spaces and < > | are literal; `"`, `%` and `!` (the
// characters cmd.exe still interprets there) are never allowed.
const WINDOWS_SHELL_SAFE = /^[A-Za-z0-9@._/:=+~^,<>|* -]+$/
export function npmCommand(args, platform = process.platform) {
if (platform !== "win32") return { file: "npm", args, shell: false }
const quoted = args.map((arg) => {
if (!WINDOWS_SHELL_SAFE.test(arg)) {
throw new Error(`Refusing to pass ${JSON.stringify(arg)} to npm through the Windows shell`)
}
return `"${arg}"`
})
return { file: "npm.cmd", args: quoted, shell: true }
}
/** Run npm synchronously; returns stdout ("" with stdio: "inherit"). */
export function npm(args, opts) {
const cmd = npmCommand(args)
return (
execFileSync(cmd.file, cmd.args, { ...options(opts, NPM_TIMEOUT_MS), shell: cmd.shell }) ?? ""
)
}
/** Run npm; resolves to { stdout, stderr }. */
export function npmAsync(args, opts) {
const cmd = npmCommand(args)
const { stdio: _stdio, ...rest } = options(opts, NPM_TIMEOUT_MS)
return execFileAsync(cmd.file, cmd.args, { ...rest, shell: cmd.shell })
}
/** A string literal for generated TypeScript/JavaScript source. */
export function jsString(value) {
return JSON.stringify(String(value))
}
/** Whether a name is a plain JavaScript identifier (safe to write into generated source). */
export function isIdentifier(name) {
return typeof name === "string" && /^[A-Za-z_$][\w$]*$/.test(name)
}