Skip to content

Commit 6de43e8

Browse files
maria-rcksLucenx9
andcommitted
fix(install): verify archives in homes with backslashes or newlines
Co-authored-by: Simone <[email protected]>
1 parent 3de45c6 commit 6de43e8

2 files changed

Lines changed: 119 additions & 2 deletions

File tree

‎scripts/install.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ case "$(uname -m)" in
135135
esac
136136
command -v tar >/dev/null 2>&1 || fail "tar is required"
137137
if command -v sha256sum >/dev/null 2>&1; then
138-
checksum() { sha256sum "$1" | cut -d' ' -f1; }
138+
checksum() { sha256sum < "$1" | cut -d' ' -f1; }
139139
elif command -v shasum >/dev/null 2>&1; then
140-
checksum() { shasum -a 256 "$1" | cut -d' ' -f1; }
140+
checksum() { shasum -a 256 < "$1" | cut -d' ' -f1; }
141141
else
142142
fail "sha256sum or shasum is required"
143143
fi

‎scripts/install.test.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off - Drives the real shell installer against a local GitHub fixture.
22
import * as HostProcess from "@t3tools/shared/HostProcess";
33
import * as NodeChildProcess from "node:child_process";
4+
import * as NodeCrypto from "node:crypto";
45
import * as NodeFSP from "node:fs/promises";
56
import * as NodeHttp from "node:http";
67
import * as NodeOS from "node:os";
@@ -78,3 +79,119 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer chan
7879
}
7980
});
8081
});
82+
83+
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer checksum", () => {
84+
it.each(
85+
["home\\data", "home\ndata"].flatMap((homeName) =>
86+
[true, false].map((validChecksum) => ({ homeName, validChecksum })),
87+
),
88+
)(
89+
"verifies home $homeName with valid checksum $validChecksum",
90+
async ({ homeName, validChecksum }) => {
91+
const availableTools = ["sha256sum", "shasum"].filter(
92+
(tool) =>
93+
NodeChildProcess.spawnSync("sh", ["-c", 'command -v "$1"', "sh", tool]).status === 0,
94+
);
95+
expect(availableTools.length).toBeGreaterThan(0);
96+
for (const checksumTool of availableTools) {
97+
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-checksum-"));
98+
const home = NodePath.join(root, homeName);
99+
const version = "1.2.3";
100+
const stem = `t3-${version}-linux-${HostProcess.Architecture.defaultValue()}`;
101+
const archiveName = `${stem}.tar.gz`;
102+
const server = NodeHttp.createServer();
103+
try {
104+
const tools = NodePath.join(root, "tools");
105+
await NodeFSP.mkdir(tools);
106+
for (const command of [
107+
"sh",
108+
"uname",
109+
"tar",
110+
"gzip",
111+
"cut",
112+
"curl",
113+
"grep",
114+
"mkdir",
115+
"mktemp",
116+
"rm",
117+
"mv",
118+
"ln",
119+
checksumTool,
120+
]) {
121+
const executable = NodeChildProcess.execFileSync(
122+
"sh",
123+
["-c", 'command -v "$1"', "sh", command],
124+
{ encoding: "utf8" },
125+
).trim();
126+
await NodeFSP.symlink(executable, NodePath.join(tools, command));
127+
}
128+
await NodeFSP.mkdir(NodePath.join(root, stem));
129+
await NodeFSP.writeFile(
130+
NodePath.join(root, stem, "t3"),
131+
"#!/bin/sh\necho 't3 v1.2.3'\n",
132+
{ mode: 0o755 },
133+
);
134+
const archivePath = NodePath.join(root, archiveName);
135+
NodeChildProcess.execFileSync("tar", ["-czf", archivePath, "-C", root, stem]);
136+
const archive = await NodeFSP.readFile(archivePath);
137+
const digest = NodeCrypto.createHash("sha256").update(archive).digest("hex");
138+
server.on("request", (request, response) => {
139+
response.end(
140+
request.url?.endsWith("/SHA256SUMS")
141+
? `${validChecksum ? digest : "0".repeat(64)} ${archiveName}\n`
142+
: archive,
143+
);
144+
});
145+
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
146+
const address = server.address();
147+
if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
148+
const { code, output } = await new Promise<{ code: number | null; output: string }>(
149+
(resolve, reject) => {
150+
const child = NodeChildProcess.spawn(
151+
"sh",
152+
[NodePath.resolve(import.meta.dirname, "install.sh")],
153+
{
154+
env: {
155+
...process.env,
156+
PATH: tools,
157+
T3CODE_VERSION: version,
158+
T3CODE_HOME: home,
159+
T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),
160+
T3CODE_RELEASE_BASE_URL: `http://127.0.0.1:${address.port}`,
161+
},
162+
stdio: ["ignore", "pipe", "pipe"],
163+
},
164+
);
165+
let output = "";
166+
child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));
167+
child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));
168+
child.on("error", reject);
169+
child.on("close", (code) => resolve({ code, output }));
170+
},
171+
);
172+
const versions = NodePath.join(home, "runtime/versions");
173+
if (validChecksum) {
174+
expect(code, `${checksumTool}: ${output}`).toBe(0);
175+
expect(
176+
await NodeFSP.readFile(NodePath.join(versions, version, ".install-complete"), "utf8"),
177+
).toBe("1.2.3\n");
178+
expect(
179+
NodeChildProcess.execFileSync(NodePath.join(root, "bin/t3"), ["--version"], {
180+
encoding: "utf8",
181+
}).trim(),
182+
).toBe("t3 v1.2.3");
183+
} else {
184+
expect(code).not.toBe(0);
185+
expect(output).toContain("checksum mismatch");
186+
expect(output).not.toContain("Installed T3 Code");
187+
expect(await NodeFSP.readdir(versions)).toEqual([]);
188+
}
189+
} finally {
190+
server.closeAllConnections();
191+
await new Promise<void>((resolve) => server.close(() => resolve()));
192+
await NodeFSP.rm(root, { recursive: true, force: true });
193+
}
194+
}
195+
},
196+
);
197+
});

0 commit comments

Comments
 (0)