Skip to content

Commit 643f981

Browse files
committed
fix(server): harden HOME hydration
1 parent 99f6455 commit 643f981

2 files changed

Lines changed: 39 additions & 16 deletions

File tree

‎apps/server/src/os-jank.test.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1-
import { assert, it } from "@effect/vitest";
1+
import * as NodeOS from "node:os";
2+
import { assert, it } from "vite-plus/test";
23

34
import { hydratePosixHome } from "./os-jank.ts";
45

5-
it("hydrates HOME for minimal service environments", () => {
6+
it("hydrates HOME for minimal service environments from the user account", () => {
67
const env: NodeJS.ProcessEnv = {};
78

8-
hydratePosixHome(env, "/home/service-user");
9+
hydratePosixHome(env);
910

10-
assert.equal(env.HOME, "/home/service-user");
11+
assert.equal(env.HOME, NodeOS.userInfo().homedir);
1112
});
1213

13-
it("hydrates a blank HOME value", () => {
14+
it("hydrates HOME independently of a blank process HOME", () => {
15+
const originalHome = process.env.HOME;
1416
const env: NodeJS.ProcessEnv = { HOME: " " };
1517

16-
hydratePosixHome(env, "/home/service-user");
17-
18-
assert.equal(env.HOME, "/home/service-user");
18+
try {
19+
process.env.HOME = " ";
20+
hydratePosixHome(env);
21+
} finally {
22+
if (originalHome === undefined) {
23+
delete process.env.HOME;
24+
} else {
25+
process.env.HOME = originalHome;
26+
}
27+
}
28+
29+
assert.equal(env.HOME, NodeOS.userInfo().homedir);
1930
});
2031

2132
it("preserves an explicitly configured HOME", () => {
2233
const env: NodeJS.ProcessEnv = { HOME: "/custom/home" };
2334

24-
hydratePosixHome(env, "/home/service-user");
35+
hydratePosixHome(env, () => {
36+
throw new Error("HOME lookup should not run");
37+
});
2538

2639
assert.equal(env.HOME, "/custom/home");
2740
});

‎apps/server/src/os-jank.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): vo
3636
}
3737
}
3838

39-
export function hydratePosixHome(env: NodeJS.ProcessEnv, homeDir = NodeOS.homedir()): void {
40-
if ((env.HOME?.trim() ?? "").length === 0 && homeDir.length > 0) {
39+
export function hydratePosixHome(
40+
env: NodeJS.ProcessEnv,
41+
resolveHomeDir = () => NodeOS.userInfo().homedir,
42+
): void {
43+
if ((env.HOME?.trim() ?? "").length > 0) return;
44+
45+
const homeDir = resolveHomeDir();
46+
if (homeDir.length > 0) {
4147
env.HOME = homeDir;
4248
}
4349
}
@@ -69,13 +75,17 @@ export const fixPath = Effect.fn("fixPath")(function* (): Effect.fn.Return<
6975

7076
if (platform !== "darwin" && platform !== "linux") return;
7177

72-
yield* Effect.sync(() => {
73-
hydratePosixHome(env);
74-
hydratePosixPath(env, platform);
75-
}).pipe(
78+
yield* Effect.sync(() => hydratePosixHome(env)).pipe(
79+
Effect.catchDefect((defect) =>
80+
Effect.sync(() => {
81+
logPathHydrationWarning("Failed to hydrate HOME from the user account.", defect);
82+
}),
83+
),
84+
);
85+
yield* Effect.sync(() => hydratePosixPath(env, platform)).pipe(
7686
Effect.catchDefect((defect) =>
7787
Effect.sync(() => {
78-
logPathHydrationWarning("Failed to hydrate the user environment.", defect);
88+
logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
7989
}),
8090
),
8191
);

0 commit comments

Comments
 (0)