fix(config): transact per-group configuration

Whole-table writes and cached snapshots can overwrite other groups
or resurrect removed configuration. Use per-group guarded writes,
one-time identity migration and explicit unreadable-row failures.
This commit is contained in:
翅膀 committed 2026-10-23 23:23:25 +08:00
1 parent ceb906d479
commit c853f2910f
7 files changed
+1764 -587

No files matched your search

+72 -51
View File
@@ -1,19 +1,31 @@
import type { Env, Config, Route } from "./types";
import { log } from "./lib/log";
import { migrateRoutes, validateRoutes } from "./config/schema";
import { d1ConfigStore, type ConfigStore } from "./storage/config-store";
import type { Env, Config, Group, Route } from "./types";
import {
createConfigStore,
kvConfigStore,
type ConfigStore,
type GuardedWrite,
type LoadOptions,
} from "./storage/config-store";
import { canUseD1 } from "./storage/d1";
const CONFIG_CACHE_TTL = 300_000;
const ROUTES_KEY = "config:routes";
let configCache: { config: Config; expiresAt: number } | null = null;
const configStores = new WeakMap<KVNamespace, ConfigStore>();
export function getConfigStore(kv: KVNamespace): ConfigStore | null {
return configStores.get(kv) ?? null;
/**
* The configuration store bound to this KV namespace. Every request, queue
* batch and scheduled task registers the D1-backed store first (see
* `initConfigStore`); a namespace that was never registered — a test, or a
* deployment without D1 — reads and writes the KV-only store.
*/
export function getConfigStore(kv: KVNamespace): ConfigStore {
return configStores.get(kv) ?? kvConfigStore(kv);
}
export function initConfigStore(env: Env): ConfigStore {
return ensureStore(env);
const existing = configStores.get(env.KV);
if (existing && (existing.kind === "d1" || !canUseD1(env.DB))) return existing;
const store = createConfigStore(env.DB, env.KV);
configStores.set(env.KV, store);
return store;
}
export function resetConfigStore(kv?: KVNamespace): void {
@@ -23,52 +35,64 @@ export function resetConfigStore(kv?: KVNamespace): void {
}
}
function ensureStore(env: Env): ConfigStore {
let store = configStores.get(env.KV);
if (!store) {
store = d1ConfigStore(env.DB, env.KV);
configStores.set(env.KV, store);
}
return store;
}
export async function loadRoutes(kv: KVNamespace): Promise<Route[]> {
const store = configStores.get(kv);
if (store) return store.loadRoutes();
try {
const stored = await kv.get<Route[]>(ROUTES_KEY, "json");
if (stored) return validateRoutes(migrateRoutes(stored));
} catch (err) {
log.warn({ err }, "Failed to load routes from KV");
}
return [];
export async function loadRoutes(kv: KVNamespace, opts?: LoadOptions): Promise<Route[]> {
return getConfigStore(kv).loadRoutes(opts);
}
export async function saveRoutes(kv: KVNamespace, routes: Route[]): Promise<void> {
const store = configStores.get(kv);
if (store) {
await store.saveRoutes(routes);
configCache = null;
return;
}
await kv.put(ROUTES_KEY, JSON.stringify(routes));
configCache = null;
await getConfigStore(kv).saveRoutes(routes);
}
export function invalidateConfigCache(): void {
configCache = null;
export async function saveGroupRoutes(
kv: KVNamespace,
groupId: string,
routes: Route[],
opts?: GuardedWrite<Route[]>,
): Promise<Route[]> {
return getConfigStore(kv).saveGroupRoutes(groupId, routes, opts);
}
/** Drop every cached configuration snapshot held by this isolate. */
export function invalidateConfigCache(kv?: KVNamespace): void {
if (kv) configStores.get(kv)?.invalidateCache();
}
/** One group, read fresh (its row alone): see `ConfigStore.loadGroup`. */
export function loadGroupFresh(
env: Env,
id: string,
): Promise<{ group: Group | null; unreadable: boolean }> {
return initConfigStore(env).loadGroup(id);
}
/**
* Configuration and groups of one consistent moment, for a dispatch that
* needs both (routes of a renamed group must never meet its old id, nor new
* targets meet stale source restrictions).
*/
export async function loadConfigSnapshot(
env: Env,
opts?: LoadOptions,
): Promise<{ config: Config; groups: Group[] }> {
const store = initConfigStore(env);
const { routes, groups, unreadable } = await store.loadSnapshot(opts);
const config = buildConfig(env, routes);
if (unreadable?.length) config.unreadableGroups = unreadable;
return { config, groups };
}
/**
* The configuration for one dispatch. Each call returns a fresh object with
* its own `routes` array, so a caller narrowing the routes (tenant scoping)
* can never leak that change into another request served by this isolate.
*/
export async function loadConfig(env: Env): Promise<Config> {
if (configCache && Date.now() < configCache.expiresAt) {
return configCache.config;
}
initConfigStore(env);
return buildConfig(env, await loadRoutes(env.KV));
}
ensureStore(env);
const routes = await loadRoutes(env.KV);
const config: Config = {
function buildConfig(env: Env, routes: Route[]): Config {
return {
baseUrl: env.BASE_URL ?? "https://webhooker.example.workers.dev",
github: {
webhookSecret: env.GITHUB_WEBHOOK_SECRET,
@@ -80,9 +104,6 @@ export async function loadConfig(env: Env): Promise<Config> {
discord: {
token: env.DISCORD_TOKEN ?? "",
},
routes,
routes: [...routes],
};
configCache = { config, expiresAt: Date.now() + CONFIG_CACHE_TTL };
return config;
}
+6
View File
@@ -61,6 +61,7 @@ export const routeSchema = v.object({
export const groupMemberSchema = v.object({
login: v.string(),
role: v.picklist(["owner", "admin", "viewer"]),
userId: v.optional(v.string()),
});
export const forgeSourceSchema = v.object({
@@ -77,6 +78,11 @@ export const groupSchema = v.object({
owners: v.optional(v.array(v.string())),
providers: v.optional(v.array(v.picklist(["github", "gitea", "gitlab", "custom"]))),
installationId: v.optional(v.number()),
isolated: v.optional(v.boolean()),
instance: v.optional(v.string()),
instanceBackfilled: v.optional(v.boolean()),
legacyId: v.optional(v.string()),
webhookSecret: v.optional(v.nullable(v.string())),
emoji: v.optional(v.boolean()),
forgeSources: v.optional(v.array(forgeSourceSchema)),
lang: v.optional(v.string()),
File diff suppressed because it is too large. Load diff
+46
View File
@@ -51,6 +51,12 @@ export interface Config {
token: string;
};
routes: Route[];
/**
* Groups with a stored route that could not be read. Their events are
* reported as transient failures instead of passing for delivered while a
* route that might have matched was silently left out.
*/
unreadableGroups?: string[];
}
export interface RouteTarget {
@@ -138,6 +144,46 @@ export interface Group {
* `owners` list. Empty/omitted = no installation restriction.
*/
installationId?: number;
/**
* Isolated groups (self-service personal groups) only receive events from
* their own `/webhook/{groupId}` ingress; the global endpoint dispatches to
* them only once a super admin binds a source (`installationId` or
* `owners`). Only super admins may edit this field.
*/
isolated?: boolean;
/**
* Immutable identity of this group, assigned by the server when the group is
* created and kept across renames. A group re-created under a former id gets
* a new one, so invites and delivery ledgers of the old group never apply to
* it. Clients cannot set it.
*/
instance?: string;
/**
* Set when the group got its `instance` from the one-time backfill, i.e. it
* existed before instances: only such a group may adopt state stored under
* its id (a pre-instance webhook secret or invite). Server-owned.
*/
instanceBackfilled?: boolean;
/**
* For a backfilled group: the id it had when it got its instance. State
* stored under a group id before instances (a webhook secret, an invite, a
* queued delivery) belongs to the group with that `legacyId` only — not to
* whichever group holds the id now (another one may have been renamed to
* it). Server-owned, kept across renames.
*/
legacyId?: string;
/**
* The group's webhook secret (per-group ingress). Server-owned, stored with
* the group so it changes only through a compare-and-swap that re-checks the
* writer's role; never sent to clients. `null` in a mutation removes it.
*/
webhookSecret?: string | null;
/**
* Opaque revision of the stored group, set on read by the admin API and
* echoed back on update so a stale edit is rejected instead of overwriting a
* newer one. Never persisted.
*/
revision?: string;
/**
* Whether to include emoji in messages sent through this group's routes.
* Defaults to true when omitted.