Skip to content
TypeScript

fix(install): verify archives in homes with backslashes or newlines (#25240)

mariaac49a626c2cf
diff --git a/scripts/install.sh b/scripts/install.sh
index 4ea200589a6..6e7ef3c77ca 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -138,9 +138,9 @@ if [ "${platform}-${arch}" = darwin-x64 ]; then
fi
command -v tar >/dev/null 2>&1 || fail "tar is required"
if command -v sha256sum >/dev/null 2>&1; then
- checksum() { sha256sum "$1" | cut -d' ' -f1; }
+ checksum() { sha256sum < "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
- checksum() { shasum -a 256 "$1" | cut -d' ' -f1; }
+ checksum() { shasum -a 256 < "$1" | cut -d' ' -f1; }
else
fail "sha256sum or shasum is required"
fi
diff --git a/scripts/install.test.ts b/scripts/install.test.ts
index 5be7acc8e00..51d074fffba 100644
--- a/scripts/install.test.ts
+++ b/scripts/install.test.ts
@@ -1,6 +1,7 @@
// @effect-diagnostics nodeBuiltinImport:off - Drives the real shell installer against a local GitHub fixture.
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NodeChildProcess from "node:child_process";
+import * as NodeCrypto from "node:crypto";
import * as NodeFSP from "node:fs/promises";
import * as NodeHttp from "node:http";
import * as NodeOS from "node:os";
@@ -78,3 +79,119 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer chan
}
});
});
+
+describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer checksum", () => {
+ it.each(
+ ["home\\data", "home\ndata"].flatMap((homeName) =>
+ [true, false].map((validChecksum) => ({ homeName, validChecksum })),
+ ),
+ )(
+ "verifies home $homeName with valid checksum $validChecksum",
+ async ({ homeName, validChecksum }) => {
+ const availableTools = ["sha256sum", "shasum"].filter(
+ (tool) =>
+ NodeChildProcess.spawnSync("sh", ["-c", 'command -v "$1"', "sh", tool]).status === 0,
+ );
+ expect(availableTools.length).toBeGreaterThan(0);
+ for (const checksumTool of availableTools) {
+ const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-checksum-"));
+ const home = NodePath.join(root, homeName);
+ const version = "1.2.3";
+ const stem = `t3-${version}-linux-${HostProcess.Architecture.defaultValue()}`;
+ const archiveName = `${stem}.tar.gz`;
+ const server = NodeHttp.createServer();
+ try {
+ const tools = NodePath.join(root, "tools");
+ await NodeFSP.mkdir(tools);
+ for (const command of [
+ "sh",
+ "uname",
+ "tar",
+ "gzip",
+ "cut",
+ "curl",
+ "grep",
+ "mkdir",
+ "mktemp",
+ "rm",
+ "mv",
+ "ln",
+ checksumTool,
+ ]) {
+ const executable = NodeChildProcess.execFileSync(
+ "sh",
+ ["-c", 'command -v "$1"', "sh", command],
+ { encoding: "utf8" },
+ ).trim();
+ await NodeFSP.symlink(executable, NodePath.join(tools, command));
+ }
+ await NodeFSP.mkdir(NodePath.join(root, stem));
+ await NodeFSP.writeFile(
+ NodePath.join(root, stem, "t3"),
+ "#!/bin/sh\necho 't3 v1.2.3'\n",
+ { mode: 0o755 },
+ );
+ const archivePath = NodePath.join(root, archiveName);
+ NodeChildProcess.execFileSync("tar", ["-czf", archivePath, "-C", root, stem]);
+ const archive = await NodeFSP.readFile(archivePath);
+ const digest = NodeCrypto.createHash("sha256").update(archive).digest("hex");
+ server.on("request", (request, response) => {
+ response.end(
+ request.url?.endsWith("/SHA256SUMS")
+ ? `${validChecksum ? digest : "0".repeat(64)} ${archiveName}\n`
+ : archive,
+ );
+ });
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ const address = server.address();
+ if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
+ const { code, output } = await new Promise<{ code: number | null; output: string }>(
+ (resolve, reject) => {
+ const child = NodeChildProcess.spawn(
+ "sh",
+ [NodePath.resolve(import.meta.dirname, "install.sh")],
+ {
+ env: {
+ ...process.env,
+ PATH: tools,
+ T3CODE_VERSION: version,
+ T3CODE_HOME: home,
+ T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),
+ T3CODE_RELEASE_BASE_URL: `http://127.0.0.1:${address.port}`,
+ },
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+ let output = "";
+ child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));
+ child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));
+ child.on("error", reject);
+ child.on("close", (code) => resolve({ code, output }));
+ },
+ );
+ const versions = NodePath.join(home, "runtime/versions");
+ if (validChecksum) {
+ expect(code, `${checksumTool}: ${output}`).toBe(0);
+ expect(
+ await NodeFSP.readFile(NodePath.join(versions, version, ".install-complete"), "utf8"),
+ ).toBe("1.2.3\n");
+ expect(
+ NodeChildProcess.execFileSync(NodePath.join(root, "bin/t3"), ["--version"], {
+ encoding: "utf8",
+ }).trim(),
+ ).toBe("t3 v1.2.3");
+ } else {
+ expect(code).not.toBe(0);
+ expect(output).toContain("checksum mismatch");
+ expect(output).not.toContain("Installed T3 Code");
+ expect(await NodeFSP.readdir(versions)).toEqual([]);
+ }
+ } finally {
+ server.closeAllConnections();
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ await NodeFSP.rm(root, { recursive: true, force: true });
+ }
+ }
+ },
+ );
+});