fix(install): describe automatic setup and guard truncated downloads (#1902)
* fix(install): describe automatic setup and guard truncated downloads
The help text said the installer only installs the CLI and that
`rea setup` must be run separately, but the installer starts
`rea setup` interactively whenever a terminal is available. Describe
that behaviour and the --no-setup/--no-prompt opt-out.
Wrap the installer body in `main` and call it on the last line, so a
truncated `curl | bash` download fails to parse instead of executing a
partial script. Behaviour is otherwise unchanged.
Add an installer scenario that runs a copy truncated right after the
npm install line and requires that it prints nothing and never invokes
npm; it fails against the previous script.
Validation: bash -n, shellcheck 0.10.0, installScript boundary tests.
* fix(install): require the closing token before running main
Wrapping the installer in main() kept a truncated download from running
a partial body, but the last line was a bare `main "$@"`. If the piped
download stopped right after `main` (or `main `), bash saw a complete
command at EOF and ran main with no positional parameters. Under the
documented `curl ... | bash -s -- --dry-run` (or --version / --no-setup)
path that silently dropped the caller's flags, so a dry run could resolve
the latest release, run npm, and start setup.
The call now sits in a brace group:
{
main "$@"
exit $?
}
Bash does not run the group until it has read the closing brace, so
every truncation inside it is a syntax error and nothing executes; a
truncation before the group leaves only function definitions. A comment
in install.sh records why the bare call is not enough.
Validation: a new boundary test pipes install.sh to `bash -s --
--dry-run` truncated at every byte offset from the end of main() to EOF
and asserts that npm and the rea binary are never invoked; offsets
inside the group must exit non-zero with a bash parse error and no
stdout, offsets before it must print nothing, and the complete group
must finish the dry run. Reproduced the original bug by cutting the old
script after `main`: the dry run invoked npm install. `bash -n` and
shellcheck 0.11.0 are clean.
* fix(install): wrap the whole script in the truncation guard
The previous guard only wrapped the final `main "$@"` invocation, so the
preamble streamed on the `curl ... | bash` path before any incomplete
compound command was open. A download cut off right after the bytes
`set` reached EOF with a complete, argument-less `set`, which prints every
shell variable, so inherited tokens could reach stdout.
The brace group now opens on line 2, directly after the shebang, and
closes on the last line, enclosing `set -euo pipefail`, the constants, the
functions and the final `main "$@"` / `exit $?`. Bash must therefore read
the whole file before running anything; any earlier cut is a syntax error
at EOF. `set -euo pipefail` still applies inside the group (checked
manually for `false`, an unbound variable, and a failing function body),
and shellcheck 0.11.0 and `bash -n` are clean.
The truncation test now pipes every prefix of install.sh, from byte 0 to
EOF (about 6,100 cuts, run 16 at a time with a minimal environment that
includes a sentinel variable), into `bash -s -- --dry-run`. Before the
group opens it must exit quietly; inside the group it must fail with a
syntax error; in every case stdout stays empty and neither npm nor rea is
invoked. Only the complete script performs the dry run.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { execFile, spawn } from "node:child_process";
|
||||
import {
|
||||
chmod,
|
||||
mkdir,
|
||||
@@ -172,6 +172,99 @@ describe("curl installer scenarios", { timeout: 20_000 }, () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("truncated installer download", { timeout: 20_000 }, () => {
|
||||
it("runs nothing from a download truncated after the npm install line", async () => {
|
||||
const fixture = await createFixture();
|
||||
const source = await readFile(join(process.cwd(), "install.sh"), "utf8");
|
||||
const installLine = source.indexOf('npm "${install_args[@]}" ||');
|
||||
expect(installLine).toBeGreaterThan(0);
|
||||
const truncated = join(fixture.home, "..", "install-truncated.sh");
|
||||
await writeFile(
|
||||
truncated,
|
||||
source.slice(0, source.indexOf("\n", installLine) + 1),
|
||||
);
|
||||
const failure: unknown = await runInstaller(
|
||||
fixture,
|
||||
["--version", "0.3.0", "--no-setup"],
|
||||
{},
|
||||
truncated,
|
||||
).then(
|
||||
() => undefined,
|
||||
(error: unknown) => error,
|
||||
);
|
||||
expect(failure).toBeInstanceOf(Error);
|
||||
expect(getProcessOutput(failure, "stdout")).toBe("");
|
||||
expect(getProcessOutput(failure, "stderr")).toContain("syntax error");
|
||||
await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe(
|
||||
"installer download truncated at any byte",
|
||||
{
|
||||
timeout: 180_000,
|
||||
},
|
||||
() => {
|
||||
// Every prefix of the script, from the empty download to the full file, as
|
||||
// `curl ... | bash -s -- --dry-run` would see it. Covers the preamble (a
|
||||
// cut right after `set` would print every shell variable), the body, and
|
||||
// the final invocation (a cut right after `main` would drop the flags).
|
||||
it("runs nothing and prints nothing until the closing brace arrives", async () => {
|
||||
const source = await readFile(join(process.cwd(), "install.sh"));
|
||||
expect(source.subarray(0, 14).toString()).toBe("#!/bin/bash\n{\n");
|
||||
const groupOpen = source.indexOf("\n{\n") + 2;
|
||||
const groupEnd = source.lastIndexOf("\n}\n") + 2;
|
||||
expect(groupEnd).toBe(source.length - 1);
|
||||
expect(source.subarray(groupOpen, groupEnd).toString()).toContain(
|
||||
"set -euo pipefail",
|
||||
);
|
||||
const fixture = await createFixture();
|
||||
const offsets = Array.from(
|
||||
{ length: source.length + 1 },
|
||||
(_, offset) => offset,
|
||||
);
|
||||
const failures: string[] = [];
|
||||
const check = async (offset: number): Promise<void> => {
|
||||
const result = await runPipedInstaller(
|
||||
fixture,
|
||||
source.subarray(0, offset),
|
||||
["--dry-run"],
|
||||
{ SECRET_TOKEN: "inherited-secret" },
|
||||
);
|
||||
const context = `truncated at byte ${offset} after ${JSON.stringify(
|
||||
source.subarray(Math.max(0, offset - 24), offset).toString(),
|
||||
)}`;
|
||||
if (offset >= groupEnd) {
|
||||
if (result.code !== 0 || !result.stdout.includes("no changes made"))
|
||||
failures.push(`${context}: complete script did not dry-run`);
|
||||
return;
|
||||
}
|
||||
if (result.stdout !== "")
|
||||
failures.push(`${context}: stdout ${JSON.stringify(result.stdout)}`);
|
||||
if (offset >= groupOpen) {
|
||||
if (result.code === 0) failures.push(`${context}: exit code 0`);
|
||||
if (!/syntax error|unexpected EOF/u.test(result.stderr))
|
||||
failures.push(
|
||||
`${context}: stderr ${JSON.stringify(result.stderr)}`,
|
||||
);
|
||||
} else if (result.code !== 0 || result.stderr !== "")
|
||||
failures.push(`${context}: preamble did not exit quietly`);
|
||||
};
|
||||
for (let index = 0; index < offsets.length; index += 16)
|
||||
await Promise.all(offsets.slice(index, index + 16).map(check));
|
||||
expect(failures).toEqual([]);
|
||||
await expect(readFile(fixture.npmLog, "utf8")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
await expect(readFile(fixture.reaLog, "utf8")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
describe("Linux user-prefix installer", { timeout: 20_000 }, () => {
|
||||
it("prints the executed arguments as a reusable command with shell metacharacters", async () => {
|
||||
const fixture = await createFixture();
|
||||
@@ -408,8 +501,9 @@ const runInstaller = async (
|
||||
fixture: InstallerFixture,
|
||||
args: readonly string[] = [],
|
||||
overrides: Readonly<Record<string, string>> = {},
|
||||
script = join(process.cwd(), "install.sh"),
|
||||
): Promise<{ readonly stdout: string; readonly stderr: string }> =>
|
||||
execFileAsync("/bin/bash", [join(process.cwd(), "install.sh"), ...args], {
|
||||
execFileAsync("/bin/bash", [script, ...args], {
|
||||
env: {
|
||||
...process.env,
|
||||
HOME: fixture.home,
|
||||
@@ -423,5 +517,46 @@ const runInstaller = async (
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Run the installer as `curl ... | bash -s -- ARGS` does: source on stdin.
|
||||
* The environment is minimal and does not inherit the test runner's.
|
||||
*/
|
||||
const runPipedInstaller = async (
|
||||
fixture: InstallerFixture,
|
||||
source: Uint8Array,
|
||||
args: readonly string[],
|
||||
overrides: Readonly<Record<string, string>> = {},
|
||||
): Promise<{
|
||||
readonly code: number | null;
|
||||
readonly stdout: string;
|
||||
readonly stderr: string;
|
||||
}> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const child = spawn("/bin/bash", ["-s", "--", ...args], {
|
||||
env: {
|
||||
HOME: fixture.home,
|
||||
TMPDIR: fixture.temporary,
|
||||
PATH: `${fixture.bin}:/usr/bin:/bin`,
|
||||
FAKE_NPM_LOG: fixture.npmLog,
|
||||
FAKE_REA_LOG: fixture.reaLog,
|
||||
FAKE_REA_SOURCE: join(fixture.bin, "rea-source"),
|
||||
FAKE_NPM_PREFIX: join(fixture.home, ".npm-global"),
|
||||
...overrides,
|
||||
},
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
});
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.setEncoding("utf8").on("data", (chunk: string) => {
|
||||
stdout += chunk;
|
||||
});
|
||||
child.stderr.setEncoding("utf8").on("data", (chunk: string) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
child.on("error", reject);
|
||||
child.on("close", (code) => resolve({ code, stdout, stderr }));
|
||||
child.stdin.end(source);
|
||||
});
|
||||
|
||||
const shellQuote = (value: string): string =>
|
||||
`'${value.replaceAll("'", `'"'"'`)}'`;
|
||||
|
||||
Reference in New Issue
Block a user