Skip to content

Commit 6a2a2c6

Browse files
committed
fix(server): flush published secret directory entries
1 parent cb73266 commit 6a2a2c6

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎apps/server/src/auth/ServerSecretStore.publication.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,89 @@ it.layer(NodeServices.layer)("secret publication", (it) => {
204204
if ((yield* HostProcessPlatform) !== "win32") assert.equal(stat.mode & 0o777, 0o600);
205205
}).pipe(Effect.provide(configLayer())),
206206
);
207+
208+
it.effect("flushes the published directory entry before reporting success", () =>
209+
Effect.gen(function* () {
210+
const fs = yield* FileSystem.FileSystem;
211+
const config = yield* ServerConfig.ServerConfig;
212+
const flushing = yield* Deferred.make<void>();
213+
const resume = yield* Deferred.make<void>();
214+
let complete = false;
215+
const store = yield* ServerSecretStore.make.pipe(
216+
Effect.provideService(FileSystem.FileSystem, {
217+
...fs,
218+
open: (path, options) =>
219+
fs.open(path, options).pipe(
220+
Effect.map((file) =>
221+
path === config.secretsDir
222+
? {
223+
...file,
224+
sync: Deferred.succeed(flushing, undefined).pipe(
225+
Effect.andThen(Deferred.await(resume)),
226+
),
227+
}
228+
: file,
229+
),
230+
),
231+
}),
232+
);
233+
const writer = yield* store.create("signing-key", new Uint8Array(32)).pipe(
234+
Effect.tap(() =>
235+
Effect.sync(() => {
236+
complete = true;
237+
}),
238+
),
239+
Effect.forkChild,
240+
);
241+
yield* Deferred.await(flushing);
242+
assert.isFalse(complete);
243+
assert.lengthOf(Option.getOrThrow(yield* store.get("signing-key")), 32);
244+
yield* Deferred.succeed(resume, undefined);
245+
yield* Fiber.join(writer);
246+
assert.isTrue(complete);
247+
}).pipe(Effect.provide(configLayer())),
248+
);
249+
250+
for (const [platform, code, succeeds] of [
251+
["win32", "EPERM", true],
252+
["linux", "EPERM", false],
253+
["win32", "EIO", false],
254+
["linux", "EIO", false],
255+
] as const) {
256+
it.effect(`handles directory flush ${code} on ${platform} without losing published bytes`, () =>
257+
Effect.gen(function* () {
258+
const fs = yield* FileSystem.FileSystem;
259+
const config = yield* ServerConfig.ServerConfig;
260+
const failure = PlatformError.systemError({
261+
_tag: code === "EPERM" ? "PermissionDenied" : "Unknown",
262+
module: "FileSystem",
263+
method: "sync",
264+
pathOrDescriptor: config.secretsDir,
265+
cause: Object.assign(new Error("Injected directory sync failure"), { code }),
266+
});
267+
const store = yield* ServerSecretStore.make.pipe(
268+
Effect.provideService(HostProcessPlatform, platform),
269+
Effect.provideService(FileSystem.FileSystem, {
270+
...fs,
271+
open: (path, options) =>
272+
fs
273+
.open(path, options)
274+
.pipe(
275+
Effect.map((file) =>
276+
path === config.secretsDir ? { ...file, sync: Effect.fail(failure) } : file,
277+
),
278+
),
279+
}),
280+
);
281+
const result = yield* store.create("signing-key", new Uint8Array(32)).pipe(Effect.result);
282+
assert.equal(result._tag, succeeds ? "Success" : "Failure");
283+
if (result._tag === "Failure") {
284+
assert.instanceOf(result.failure, ServerSecretStore.SecretStorePersistError);
285+
assert.equal(result.failure.cause, failure);
286+
}
287+
assert.lengthOf(Option.getOrThrow(yield* store.get("signing-key")), 32);
288+
assert.deepEqual(yield* fs.readDirectory(config.secretsDir), ["signing-key.bin"]);
289+
}).pipe(Effect.provide(configLayer())),
290+
);
291+
}
207292
});

‎apps/server/src/auth/ServerSecretStore.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Path from "effect/Path";
88
import * as Predicate from "effect/Predicate";
99
import * as PlatformError from "effect/PlatformError";
1010
import * as Schema from "effect/Schema";
11+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
1112

1213
import * as ServerConfig from "../config.ts";
1314

@@ -154,6 +155,7 @@ export const make = Effect.gen(function* () {
154155
const fileSystem = yield* FileSystem.FileSystem;
155156
const path = yield* Path.Path;
156157
const serverConfig = yield* ServerConfig.ServerConfig;
158+
const platform = yield* HostProcessPlatform;
157159

158160
yield* fileSystem.makeDirectory(serverConfig.secretsDir, { recursive: true });
159161
yield* fileSystem.chmod(serverConfig.secretsDir, 0o700).pipe(
@@ -243,6 +245,16 @@ export const make = Effect.gen(function* () {
243245
);
244246
// Publish complete bytes without replacing a concurrent creator's secret.
245247
yield* fileSystem.link(temporaryPath, secretPath);
248+
const directory = yield* fileSystem.open(serverConfig.secretsDir, { flag: "r" });
249+
yield* directory.sync.pipe(
250+
Effect.catch((cause) =>
251+
platform === "win32" &&
252+
Predicate.hasProperty(cause.reason.cause, "code") &&
253+
cause.reason.cause.code === "EPERM"
254+
? Effect.void
255+
: Effect.fail(cause),
256+
),
257+
);
246258
}),
247259
).pipe(
248260
Effect.mapError(

0 commit comments

Comments
 (0)