Repository navigation
Persist local actor signing keys #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dahlia
wants to merge
2
commits into
fedify-dev:main
Choose a base branch
from
dahlia:refactor/actor-key-pairs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // DrFed: A web-based platform for developing and debugging ActivityPub apps | ||
| // Copyright (C) 2026 DrFed team | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| import assert from "node:assert/strict"; | ||
| import { spawn } from "node:child_process"; | ||
| import { once } from "node:events"; | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import { type Server, connect, createServer } from "node:net"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import process from "node:process"; | ||
| import { it } from "node:test"; | ||
| import { setTimeout as delay } from "node:timers/promises"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const binary = fileURLToPath( | ||
| new URL("../bin/drfed-server.mjs", import.meta.resolve("@drfed/drfed")), | ||
| ); | ||
| async function reservePort() { | ||
| const server = createServer(); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const address = server.address(); | ||
| assert.ok(address != null && typeof address !== "string"); | ||
| return { server, port: address.port }; | ||
| } | ||
| async function closeServer(server: Server) { | ||
| const closed = once(server, "close"); | ||
| server.close(); | ||
| await closed; | ||
| } | ||
| function startServer(port: number, dataPath: string) { | ||
| const child = spawn( | ||
| process.execPath, | ||
| [ | ||
| binary, | ||
| "--root-origin=http://drfed.test", | ||
| "--login-origin=http://drfed.test", | ||
| `--listen=127.0.0.1:${port}`, | ||
| `--pglite-data-path=${dataPath}`, | ||
| "--log-level=error", | ||
| ], | ||
| { stdio: ["ignore", "pipe", "pipe"] }, | ||
| ); | ||
| let stderr = ""; | ||
| child.stderr.on("data", (chunk) => { | ||
| stderr += chunk.toString(); | ||
| }); | ||
| child.stdout.resume(); | ||
| const exited = once(child, "close"); | ||
| return { child, exited, stderr: () => stderr }; | ||
| } | ||
|
|
||
| async function waitForExit(run: ReturnType<typeof startServer>) { | ||
| const stopTimeout = new AbortController(); | ||
| const timeout = async () => { | ||
| await delay(90_000, undefined, { signal: stopTimeout.signal }); | ||
| assert.fail(`CLI did not exit within 90 seconds: ${run.stderr()}`); | ||
| }; | ||
| try { | ||
| return await Promise.race([run.exited, timeout()]); | ||
| } finally { | ||
| stopTimeout.abort(); | ||
| } | ||
| } | ||
|
|
||
| it("preserves a listen error when server startup fails", async () => { | ||
| const { server, port } = await reservePort(); | ||
| const dataPath = await mkdtemp(join(tmpdir(), "drfed-startup-")); | ||
| const run = startServer(port, dataPath); | ||
| try { | ||
| const [code] = await waitForExit(run); | ||
| assert.equal(code, 1); | ||
| assert.match(run.stderr(), /EADDRINUSE/u); | ||
| } finally { | ||
| run.child.kill("SIGKILL"); | ||
| await closeServer(server); | ||
| await rm(dataPath, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it( | ||
| "force-closes a stalled upload and exits normally on SIGTERM", | ||
| { | ||
| // Windows child.kill("SIGTERM") forcibly terminates without running handlers. | ||
| skip: process.platform === "win32", | ||
| }, | ||
| async () => { | ||
| const { server, port } = await reservePort(); | ||
| await closeServer(server); | ||
| const dataPath = await mkdtemp(join(tmpdir(), "drfed-shutdown-")); | ||
| const run = startServer(port, dataPath); | ||
| try { | ||
| let ready = false; | ||
| const readinessDeadline = performance.now() + 90_000; | ||
| while (performance.now() < readinessDeadline) { | ||
| assert.equal(run.child.exitCode, null, run.stderr()); | ||
| try { | ||
| // oxlint-disable-next-line no-await-in-loop | ||
| const response = await fetch(`http://127.0.0.1:${port}/graphql`, { | ||
| method: "POST", | ||
| signal: AbortSignal.timeout(1000), | ||
| headers: { "content-type": "application/json" }, | ||
| body: '{"query":"{ __typename }"}', | ||
| }); | ||
| if (response.ok) { | ||
| ready = true; | ||
| break; | ||
| } | ||
| } catch { | ||
| // Migrations and listening have not finished yet. | ||
| } | ||
| // oxlint-disable-next-line no-await-in-loop | ||
| await delay(100); | ||
| } | ||
| assert.ok(ready, run.stderr()); | ||
| const stalled = connect({ host: "127.0.0.1", port }); | ||
| // Force-closing a stalled upload may reset its socket. | ||
| stalled.on("error", () => undefined); | ||
| try { | ||
| await once(stalled, "connect"); | ||
| stalled.write( | ||
| "POST /graphql HTTP/1.1\r\nHost: drfed.test\r\nContent-Type: application/json\r\nContent-Length: 100\r\n\r\n{", | ||
| ); | ||
| await delay(100); | ||
| run.child.kill("SIGTERM"); | ||
| const [code, signal] = await waitForExit(run); | ||
| assert.equal(signal, null); | ||
| assert.equal(code, 0, run.stderr()); | ||
| } finally { | ||
| stalled.destroy(); | ||
| } | ||
| } finally { | ||
| run.child.kill("SIGKILL"); | ||
| await rm(dataPath, { recursive: true, force: true }); | ||
| } | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // DrFed: A web-based platform for developing and debugging ActivityPub apps | ||
| // Copyright (C) 2026 DrFed team | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| import assert from "node:assert/strict"; | ||
| import { it } from "node:test"; | ||
|
|
||
| import { privateKeySafeLogger } from "@drfed/drfed/query-logger"; | ||
|
|
||
| it("never forwards key parameters to the ordinary SQL logger", () => { | ||
| const calls: unknown[] = []; | ||
| const logger = privateKeySafeLogger({ | ||
| logQuery(query, params) { | ||
| calls.push([query, params]); | ||
| }, | ||
| }); | ||
| logger.logQuery('insert into "local_actor_keys" values ($1)', [ | ||
| "PRIVATE_SECRET", | ||
| ]); | ||
| logger.logQuery('select * from "local_actor_keys"', []); | ||
| assert.deepEqual(calls, []); | ||
| logger.logQuery("select $1", [42]); | ||
| assert.deepEqual(calls, [["select $1", [42]]]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // DrFed: A web-based platform for developing and debugging ActivityPub apps | ||
| // Copyright (C) 2026 DrFed team | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| import { getLogger as getQueryLogger } from "@logtape/drizzle-orm"; | ||
| import { getLogger } from "@logtape/logtape"; | ||
| import type { Logger } from "drizzle-orm/logger"; | ||
| /** Suppress parameters for SQL that reads or writes private signing material. | ||
| * @returns A logger that never logs private-key parameters. | ||
| */ | ||
| export function privateKeySafeLogger( | ||
| delegate: Logger = getQueryLogger(), | ||
| ): Logger { | ||
| const logger = getLogger(["drfed", "database"]); | ||
| return { | ||
| logQuery(query, params) { | ||
| if (query.includes("local_actor_keys")) { | ||
| logger.debug("Query: {query}", { query }); | ||
| } else delegate.logQuery(query, params); | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 5486
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 7261
🌐 Web query:
GitHub srvx v0.11.16 serve close implementation before listen💡 Result:
🌐 Web query:
github h3js srvx 19efb13 server close source node💡 Result:
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 1924
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 368
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 368
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 35403
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 35403
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 16922
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 6230
🌐 Web query:
official @upyo/smtp closeAllConnections implementation💡 Result:
🌐 Web query:
official PGlite 0.5.3 close Promise postgres.js 3.4.9 end Promise rejection API💡 Result:
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 10353
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 45671
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 45671
🏁 Script executed:
Repository: fedify-dev/drfed
Length of output: 3338
Guard database cleanup before rethrowing the startup error.
The occupied-port path does not need
Promise.allSettled:srvx@0.11.16resolvesserver.close()when the server is not listening, and the worker already catches its own rejection. The awaited database cleanup can still replace the startup error if it rejects. Also awaitcloseAllConnections()so the cleanup runs inside the guard.🛡️ Suggested fix
workerAbort.abort(); await Promise.all([server.close(), worker]); - if (mailer instanceof SmtpTransport) mailer.closeAllConnections(); - await ("driver" in credentials - ? credentials.client.close() - : credentials.client.end()); + try { + if (mailer instanceof SmtpTransport) { + await mailer.closeAllConnections(); + } + await ("driver" in credentials + ? credentials.client.close() + : credentials.client.end()); + } catch { + getLogger(["drfed", "server"]).error("Cleanup after startup failure failed."); + } throw new Error("Could not start the server.", { cause: error });📝 Committable suggestion
🤖 Prompt for AI Agents