mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(admin): guard revisions and tenant targets
Read-time authorization alone cannot protect concurrent edits. Guard revisions, current roles and destination ownership in the write transaction, and use per-group endpoints for isolated changes.
This commit is contained in:
1 parent
4fc784ab9f
commit
2ec541ad7c
10 files changed
+2615
-300
No files matched your search
+80
-48
@@ -1,5 +1,13 @@
|
||||
import type { FilterNode } from "./types";
|
||||
import { log } from "./lib/log";
|
||||
import { canUseD1 } from "./storage/d1";
|
||||
import {
|
||||
ConfigConflictError,
|
||||
groupVersion,
|
||||
groupVersionBump,
|
||||
groupVersionGuard,
|
||||
runGuarded,
|
||||
} from "./storage/config-store";
|
||||
|
||||
export interface NamedFragment {
|
||||
id: string;
|
||||
@@ -15,59 +23,83 @@ interface D1FragmentRow {
|
||||
node: string;
|
||||
}
|
||||
|
||||
export async function loadFragments(db: D1Database): Promise<NamedFragment[]> {
|
||||
try {
|
||||
const stmt = db.prepare("SELECT id, group_id, name, node FROM d1_fragments ORDER BY id");
|
||||
if (typeof stmt.all !== "function") return [];
|
||||
const { results } = await stmt.all<D1FragmentRow>();
|
||||
if (!results || results.length === 0) return [];
|
||||
return results.map((r) => ({
|
||||
id: r.id,
|
||||
groupId: r.group_id || undefined,
|
||||
name: r.name,
|
||||
node: JSON.parse(r.node) as FilterNode,
|
||||
}));
|
||||
} catch (err) {
|
||||
log.warn({ err }, "Failed to load fragments from D1");
|
||||
return [];
|
||||
}
|
||||
/**
|
||||
* Named filter fragments, optionally of one group. Without a usable D1
|
||||
* binding there are none; a D1 error propagates, because callers replace a
|
||||
* group's whole list and must never build that list from a failed read. An
|
||||
* unreadable row is skipped (logged) — or, with `strict`, an error: a write
|
||||
* replacing the group's list must never silently delete a row nobody saw.
|
||||
*/
|
||||
export async function loadFragments(
|
||||
db: D1Database,
|
||||
groupId?: string,
|
||||
strict = false,
|
||||
): Promise<NamedFragment[]> {
|
||||
if (!canUseD1(db)) return [];
|
||||
const stmt =
|
||||
groupId === undefined
|
||||
? db.prepare("SELECT id, group_id, name, node FROM d1_fragments ORDER BY id")
|
||||
: db
|
||||
.prepare(
|
||||
"SELECT id, group_id, name, node FROM d1_fragments WHERE group_id = ? ORDER BY id",
|
||||
)
|
||||
.bind(groupId);
|
||||
const { results } = await stmt.all<D1FragmentRow>();
|
||||
return (results ?? []).flatMap((r) => {
|
||||
try {
|
||||
return [
|
||||
{
|
||||
id: r.id,
|
||||
groupId: r.group_id || undefined,
|
||||
name: r.name,
|
||||
node: JSON.parse(r.node) as FilterNode,
|
||||
},
|
||||
];
|
||||
} catch (err) {
|
||||
if (strict) {
|
||||
throw new Error(`filter fragment "${r.id}" is unreadable; repair the row before saving`);
|
||||
}
|
||||
log.warn({ err, id: r.id }, "Ignoring malformed filter fragment");
|
||||
return [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function saveFragments(db: D1Database, fragments: NamedFragment[]): Promise<void> {
|
||||
/**
|
||||
* Replace one group's fragments in a single transaction; other groups are
|
||||
* untouched. `expect` sees the stored fragments first and may throw; the write
|
||||
* then commits only if nothing changed the group in between (version guard).
|
||||
*/
|
||||
export async function saveGroupFragments(
|
||||
db: D1Database,
|
||||
groupId: string,
|
||||
fragments: NamedFragment[],
|
||||
opts?: {
|
||||
expect?: (current: NamedFragment[]) => void | Promise<void>;
|
||||
/** Guard statements committed first in the write (see `GuardedWrite.guards`). */
|
||||
guards?: (db: D1Database) => D1PreparedStatement[];
|
||||
},
|
||||
): Promise<NamedFragment[]> {
|
||||
if (!canUseD1(db)) throw new Error("Filter fragments require the D1 binding");
|
||||
const version = await groupVersion(db, groupId);
|
||||
if (version == null) throw new ConfigConflictError(`group "${groupId}" does not exist`);
|
||||
// Loaded strictly even without `expect`: an unreadable row aborts here.
|
||||
const stored = await loadFragments(db, groupId, true);
|
||||
await opts?.expect?.(stored);
|
||||
const now = Date.now();
|
||||
|
||||
// Load existing fragments to determine what to delete
|
||||
const existing = await loadFragments(db);
|
||||
const existingKeys = new Set(existing.map((f) => `${f.id}:${f.groupId ?? ""}`));
|
||||
const newKeys = new Set(fragments.map((f) => `${f.id}:${f.groupId ?? ""}`));
|
||||
|
||||
const statements: D1PreparedStatement[] = [];
|
||||
|
||||
// Delete fragments that are no longer present
|
||||
for (const key of existingKeys) {
|
||||
if (!newKeys.has(key)) {
|
||||
const [id, groupId] = key.split(":");
|
||||
statements.push(
|
||||
db.prepare("DELETE FROM d1_fragments WHERE id = ? AND group_id = ?").bind(id, groupId),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Upsert all fragments
|
||||
for (const f of fragments) {
|
||||
statements.push(
|
||||
await runGuarded(db, [
|
||||
groupVersionGuard(db, groupId, version),
|
||||
...(opts?.guards?.(db) ?? []),
|
||||
groupVersionBump(db, groupId),
|
||||
db.prepare("DELETE FROM d1_fragments WHERE group_id = ?").bind(groupId),
|
||||
...fragments.map((f) =>
|
||||
db
|
||||
.prepare(
|
||||
`INSERT INTO d1_fragments (id, group_id, name, node, version, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, 1, ?, ?)
|
||||
ON CONFLICT(id, group_id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
node = excluded.node,
|
||||
updated_at = excluded.updated_at`,
|
||||
VALUES (?, ?, ?, ?, 1, ?, ?)`,
|
||||
)
|
||||
.bind(f.id, f.groupId ?? "", f.name, JSON.stringify(f.node), now, now),
|
||||
);
|
||||
}
|
||||
|
||||
await db.batch(statements);
|
||||
.bind(f.id, groupId, f.name, JSON.stringify(f.node), now, now),
|
||||
),
|
||||
]);
|
||||
return loadFragments(db, groupId);
|
||||
}
|
||||
+1240
-217
File diff suppressed because it is too large.
Load diff
@@ -1,7 +1,7 @@
|
||||
import type { H3Event } from "h3";
|
||||
import { createError, getHeader } from "h3";
|
||||
import type { Group, GroupRole } from "../types";
|
||||
import { getAdminSession, isDebugMode, type AdminSession } from "./session";
|
||||
import { authLogin, getAdminSession, isDebugMode, type AdminSession } from "./session";
|
||||
import {
|
||||
loadGroups,
|
||||
resolveScope,
|
||||
@@ -28,14 +28,15 @@ const AUTH_KEY = "auth";
|
||||
export async function loadAuth(event: H3Event): Promise<AuthContext | null> {
|
||||
const env = cfEnv(event);
|
||||
initConfigStore(env);
|
||||
const groups = await loadGroups(env.KV);
|
||||
if (isDebugMode(env)) {
|
||||
const session: AdminSession = { userId: "debug", login: "debug" };
|
||||
return { session, scope: resolveScope(env, groups, session.userId, session.login), groups };
|
||||
}
|
||||
const session = await getAdminSession(env.KV, getHeader(event, "cookie"));
|
||||
// The session first: a request without one reads no configuration.
|
||||
const session: AdminSession | null = isDebugMode(env)
|
||||
? { userId: "debug", login: "debug" }
|
||||
: await getAdminSession(env.KV, getHeader(event, "cookie"), env.DB);
|
||||
if (!session) return null;
|
||||
const scope = resolveScope(env, groups, session.userId, session.login);
|
||||
// Access checks read the authoritative store: a membership revoked in
|
||||
// another isolate must not linger in this one's cache.
|
||||
const groups = await loadGroups(env.KV, { fresh: true });
|
||||
const scope = resolveScope(env, groups, session.userId, authLogin(session));
|
||||
return { session, scope, groups };
|
||||
}
|
||||
|
||||
|
||||
+86
-13
@@ -1,12 +1,28 @@
|
||||
import { log } from "../lib/log";
|
||||
import type { Group } from "../types";
|
||||
import { updateGroup } from "./groups";
|
||||
|
||||
/**
|
||||
* Per-group webhook tenant secrets. A group can opt into its own webhook
|
||||
* ingress (`POST /webhook/{groupId}`) with an independent secret, so SaaS
|
||||
* users can configure their own GitHub/Gitea/custom webhooks without sharing
|
||||
* (or even knowing) the operator's global secrets.
|
||||
*
|
||||
* Secrets are keyed by the group's immutable `instance`: a rename keeps the
|
||||
* secret without moving anything, and a group re-created under a former id
|
||||
* never inherits the old group's secret. A secret stored before instances
|
||||
* existed (`tenant:{groupId}`) is still read — only by the group that had that
|
||||
* id back then (`legacyId`) — until the secret is regenerated or removed. It is
|
||||
* never copied over: a lazy copy could race a rotation and resurrect the old
|
||||
* secret.
|
||||
*/
|
||||
const TENANT_KEY = (groupId: string): string => `tenant:${groupId}`;
|
||||
type TenantGroup = Pick<Group, "id" | "instance" | "legacyId" | "webhookSecret">;
|
||||
|
||||
const LEGACY_KEY = (groupId: string): string => `tenant:${groupId}`;
|
||||
const INSTANCE_KEY = (instance: string): string => `tenant:i:${instance}`;
|
||||
|
||||
function keyOf(group: TenantGroup): string {
|
||||
return group.instance ? INSTANCE_KEY(group.instance) : LEGACY_KEY(group.id);
|
||||
}
|
||||
|
||||
/** 32 random bytes → 64 hex chars. */
|
||||
export function generateTenantSecret(): string {
|
||||
@@ -17,22 +33,79 @@ export function generateTenantSecret(): string {
|
||||
.join("");
|
||||
}
|
||||
|
||||
export async function getTenantSecret(kv: KVNamespace, groupId: string): Promise<string | null> {
|
||||
try {
|
||||
return await kv.get(TENANT_KEY(groupId), "text");
|
||||
} catch (err) {
|
||||
log.warn({ err, groupId }, "Failed to read tenant webhook secret");
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* The group's webhook secret, or null when it has none. Read errors propagate:
|
||||
* a failed read must never pass for "no secret".
|
||||
*/
|
||||
export async function getTenantSecret(kv: KVNamespace, group: TenantGroup): Promise<string | null> {
|
||||
// Stored with the group (how secrets are written now); KV keys are read for
|
||||
// secrets set before that.
|
||||
// "" is an explicit "none" (removed): no older KV copy counts then.
|
||||
if (group.webhookSecret != null) return group.webhookSecret || null;
|
||||
const secret = await kv.get(keyOf(group), "text");
|
||||
// Only the group that had the id before instances may use a pre-instance
|
||||
// secret — not a group created since, nor one renamed to that id.
|
||||
if (secret || !group.instance || !group.legacyId) return secret;
|
||||
return kv.get(LEGACY_KEY(group.legacyId), "text");
|
||||
}
|
||||
|
||||
/**
|
||||
* The pre-instance key the group's secret may still be found under. Keys
|
||||
* under its current id are left alone: they may belong to the group that had
|
||||
* that id before instances (now under another id).
|
||||
*/
|
||||
function legacyKeys(group: TenantGroup): string[] {
|
||||
return group.legacyId ? [LEGACY_KEY(group.legacyId)] : [];
|
||||
}
|
||||
|
||||
/** Generate (or regenerate) the group's webhook secret. */
|
||||
export async function setTenantSecret(kv: KVNamespace, groupId: string): Promise<string> {
|
||||
export async function setTenantSecret(kv: KVNamespace, group: TenantGroup): Promise<string> {
|
||||
const secret = generateTenantSecret();
|
||||
await kv.put(TENANT_KEY(groupId), secret);
|
||||
await kv.put(keyOf(group), secret);
|
||||
// The new secret replaces the group's pre-instance one.
|
||||
if (group.instance) for (const key of legacyKeys(group)) await kv.delete(key);
|
||||
return secret;
|
||||
}
|
||||
|
||||
export async function deleteTenantSecret(kv: KVNamespace, groupId: string): Promise<void> {
|
||||
await kv.delete(TENANT_KEY(groupId));
|
||||
/**
|
||||
* Generate (or regenerate) the group's webhook secret, stored with the group
|
||||
* in a compare-and-swap: `check` sees the group as it is at that moment and
|
||||
* may throw (e.g. the writer is no longer an owner), so a request authorized
|
||||
* earlier can never overwrite a later rotation. Older KV copies are dropped.
|
||||
* Null when the group does not exist.
|
||||
*/
|
||||
export async function rotateTenantSecret(
|
||||
kv: KVNamespace,
|
||||
groupId: string,
|
||||
check: (current: Group) => void,
|
||||
): Promise<string | null> {
|
||||
const secret = generateTenantSecret();
|
||||
const saved = await updateGroup(kv, groupId, (g) => {
|
||||
check(g);
|
||||
return { ...g, webhookSecret: secret };
|
||||
});
|
||||
if (!saved) return null;
|
||||
await deleteTenantSecret(kv, saved).catch(() => undefined);
|
||||
return secret;
|
||||
}
|
||||
|
||||
/** Remove the group's webhook secret (the same compare-and-swap as rotation). */
|
||||
export async function removeTenantSecret(
|
||||
kv: KVNamespace,
|
||||
groupId: string,
|
||||
check: (current: Group) => void,
|
||||
): Promise<boolean> {
|
||||
const saved = await updateGroup(kv, groupId, (g) => {
|
||||
check(g);
|
||||
return { ...g, webhookSecret: null };
|
||||
});
|
||||
if (!saved) return false;
|
||||
await deleteTenantSecret(kv, saved);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Drop the KV copies of a group's secret (from before secrets lived with the group). */
|
||||
export async function deleteTenantSecret(kv: KVNamespace, group: TenantGroup): Promise<void> {
|
||||
await kv.delete(keyOf(group));
|
||||
for (const key of legacyKeys(group)) await kv.delete(key);
|
||||
}
|
||||
@@ -43,7 +43,10 @@ export async function processWebhook(
|
||||
if (!groups.some((g) => g.id === tenantId)) {
|
||||
return { status: 404, body: { error: "Group not found" } };
|
||||
}
|
||||
const secret = await getTenantSecret(env.KV, tenantId);
|
||||
const secret = await getTenantSecret(
|
||||
env.KV,
|
||||
groups.find((g) => g.id === tenantId)!,
|
||||
);
|
||||
if (!secret) {
|
||||
return { status: 404, body: { error: "Webhook disabled for this group" } };
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import {
|
||||
adminApiMe,
|
||||
adminApiGroupsGet,
|
||||
adminApiGroupsPut,
|
||||
adminGroupCreate,
|
||||
adminGroupUpdate,
|
||||
adminGroupDelete,
|
||||
adminApiRoutesGet,
|
||||
adminApiRoutesPut,
|
||||
adminApiLogs,
|
||||
@@ -40,6 +43,11 @@ export default defineEventHandler((event) => {
|
||||
if (seg[0] === "groups" && seg.length === 1) {
|
||||
if (method === "GET") return adminApiGroupsGet(event);
|
||||
if (method === "PUT") return adminApiGroupsPut(event);
|
||||
if (method === "POST") return adminGroupCreate(event);
|
||||
}
|
||||
if (seg[0] === "groups" && seg.length === 2) {
|
||||
if (method === "PUT") return adminGroupUpdate(event, seg[1]!);
|
||||
if (method === "DELETE") return adminGroupDelete(event, seg[1]!);
|
||||
}
|
||||
if (seg[0] === "routes" && seg.length === 1) {
|
||||
if (method === "GET") return adminApiRoutesGet(event);
|
||||
|
||||
@@ -9,7 +9,7 @@ import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import { loadGroups, invalidateGroupsCache } from "../server/lib/web/groups";
|
||||
import { loadRoutes } from "../server/lib/config";
|
||||
import { createInvite, listInvites } from "../server/lib/web/invites";
|
||||
import { getTenantSecret, setTenantSecret } from "../server/lib/web/tenants";
|
||||
import { getTenantSecret } from "../server/lib/web/tenants";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
import type { Env, Route } from "../server/lib/types";
|
||||
|
||||
@@ -85,8 +85,9 @@ describe("admin handlers (h3)", () => {
|
||||
},
|
||||
] as Route[]),
|
||||
);
|
||||
await setTenantSecret(kv, "old-team");
|
||||
const originalSecret = (await getTenantSecret(kv, "old-team"))!;
|
||||
// A secret stored before group instances existed (keyed by group id).
|
||||
const originalSecret = "legacy-secret";
|
||||
await kv.put("tenant:old-team", originalSecret);
|
||||
await createInvite(kv, {
|
||||
groupId: "old-team",
|
||||
role: "viewer",
|
||||
@@ -114,8 +115,8 @@ describe("admin handlers (h3)", () => {
|
||||
const routes = await loadRoutes(kv);
|
||||
expect(routes[0]!.groupId).toBe("new-team");
|
||||
|
||||
expect(await getTenantSecret(kv, "new-team")).toBe(originalSecret);
|
||||
expect(await getTenantSecret(kv, "old-team")).toBeNull();
|
||||
// The secret follows the group (it is keyed by the group's instance).
|
||||
expect(await getTenantSecret(kv, groups[0]!)).toBe(originalSecret);
|
||||
|
||||
const invites = await listInvites(kv, "new-team");
|
||||
expect(invites).toHaveLength(1);
|
||||
|
||||
+22
-8
@@ -296,6 +296,20 @@ describe("ensureInstallationGroup", () => {
|
||||
});
|
||||
|
||||
describe("tenant webhook secrets", () => {
|
||||
const G1 = { id: "g1", instance: "inst-g1" };
|
||||
|
||||
it("never lets a group created later adopt a secret left under its id", async () => {
|
||||
const kv = createMockKV();
|
||||
await kv.put("tenant:g1", "old-secret");
|
||||
expect(await getTenantSecret(kv, G1)).toBeNull();
|
||||
// The group that had the id before instances still uses its old secret,
|
||||
// under whatever id it has now — and without copying it anywhere.
|
||||
expect(await getTenantSecret(kv, { ...G1, id: "moved", legacyId: "g1" })).toBe("old-secret");
|
||||
// A pre-instance group renamed to the id later never adopts it.
|
||||
expect(await getTenantSecret(kv, { ...G1, legacyId: "other" })).toBeNull();
|
||||
expect(await kv.get("tenant:i:" + G1.instance)).toBeNull();
|
||||
});
|
||||
|
||||
it("generates 64-char hex secrets", () => {
|
||||
const s = generateTenantSecret();
|
||||
expect(s).toMatch(/^[0-9a-f]{64}$/);
|
||||
@@ -303,19 +317,19 @@ describe("tenant webhook secrets", () => {
|
||||
|
||||
it("creates, reads, and deletes a tenant secret", async () => {
|
||||
const kv = createMockKV();
|
||||
expect(await getTenantSecret(kv, "g1")).toBeNull();
|
||||
const secret = await setTenantSecret(kv, "g1");
|
||||
expect(await getTenantSecret(kv, "g1")).toBe(secret);
|
||||
await deleteTenantSecret(kv, "g1");
|
||||
expect(await getTenantSecret(kv, "g1")).toBeNull();
|
||||
expect(await getTenantSecret(kv, G1)).toBeNull();
|
||||
const secret = await setTenantSecret(kv, G1);
|
||||
expect(await getTenantSecret(kv, G1)).toBe(secret);
|
||||
await deleteTenantSecret(kv, G1);
|
||||
expect(await getTenantSecret(kv, G1)).toBeNull();
|
||||
});
|
||||
|
||||
it("regenerating replaces the previous secret", async () => {
|
||||
const kv = createMockKV();
|
||||
const a = await setTenantSecret(kv, "g1");
|
||||
const b = await setTenantSecret(kv, "g1");
|
||||
const a = await setTenantSecret(kv, G1);
|
||||
const b = await setTenantSecret(kv, G1);
|
||||
expect(a).not.toBe(b);
|
||||
expect(await getTenantSecret(kv, "g1")).toBe(b);
|
||||
expect(await getTenantSecret(kv, G1)).toBe(b);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,931 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import type { H3Event } from "h3";
|
||||
import {
|
||||
adminApiGroupsGet,
|
||||
adminApiGroupsPut,
|
||||
adminGroupCreate,
|
||||
adminGroupDelete,
|
||||
adminGroupFragmentsGet,
|
||||
adminGroupFragmentsPut,
|
||||
adminGroupInvitesPost,
|
||||
adminInviteDelete,
|
||||
adminGroupRename,
|
||||
adminGroupRoutesGet,
|
||||
adminGroupRoutesPut,
|
||||
adminGroupUpdate,
|
||||
adminGroupWebhookRegenerate,
|
||||
channelFreeGuard,
|
||||
roleGuard,
|
||||
} from "../server/lib/web/admin";
|
||||
import { ConfigConflictError } from "../server/lib/storage/config-store";
|
||||
import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import {
|
||||
createGroup,
|
||||
ensureInstallationGroup,
|
||||
loadGroups,
|
||||
updateGroup,
|
||||
} from "../server/lib/web/groups";
|
||||
import {
|
||||
initConfigStore,
|
||||
loadRoutes,
|
||||
resetConfigStore,
|
||||
saveGroupRoutes,
|
||||
} from "../server/lib/config";
|
||||
import { saveToken, findUserIdByToken } from "../server/lib/github/store";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
import { resolveGroupAlias } from "../server/lib/web/group-lifecycle";
|
||||
import { acceptInvite, getInvite } from "../server/lib/web/invites";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { Env, Group, Route } from "../server/lib/types";
|
||||
import type { Database } from "bun:sqlite";
|
||||
|
||||
let env: Env;
|
||||
let sqlite: Database;
|
||||
|
||||
function group(id: string, owner: string, extra: Partial<Group> = {}): Group {
|
||||
return { id, name: id, adminIds: [owner], members: [{ login: owner, role: "owner" }], ...extra };
|
||||
}
|
||||
|
||||
function route(id: string, extra: Partial<Route> = {}): Route {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ platform: "discord", channelId: "1" }],
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
async function cookieFor(userId: string, login: string): Promise<string> {
|
||||
return adminCookie(await createAdminSession(env.KV, userId, login));
|
||||
}
|
||||
|
||||
/** Delete a group the way the console does: confirmed on its shown revisions. */
|
||||
async function deleteAs(cookie: string, id: string): Promise<void> {
|
||||
const routes = (await adminGroupRoutesGet(
|
||||
call(`/admin/api/groups/${id}/routes`, cookie),
|
||||
id,
|
||||
)) as {
|
||||
revision: string;
|
||||
version: number;
|
||||
};
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = listed.groups.find((g) => g.id === id)!.revision;
|
||||
const del = call(
|
||||
`/admin/api/groups/${id}?revision=${rev}&routesRevision=${routes.revision}&version=${routes.version}`,
|
||||
cookie,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, id);
|
||||
expect(responseStatus(del)).toBe(200);
|
||||
}
|
||||
|
||||
function call(path: string, cookie: string, method = "GET", body?: unknown): H3Event {
|
||||
return makeEvent(path, {
|
||||
method,
|
||||
headers: { cookie, "content-type": "application/json" },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
env,
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
const d1 = createSqliteD1();
|
||||
sqlite = d1.sqlite;
|
||||
const { kv } = createMemoryKV();
|
||||
env = { GITHUB_WEBHOOK_SECRET: "s", KV: kv, DB: d1.db, ADMIN_USER_IDS: "1" };
|
||||
resetConfigStore(kv);
|
||||
initConfigStore(env);
|
||||
await createGroup(kv, group("alpha", "alice"));
|
||||
await createGroup(
|
||||
kv,
|
||||
group("beta", "bob", {
|
||||
members: [
|
||||
{ login: "bob", role: "owner" },
|
||||
{ login: "alice", role: "viewer" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("delivery targets", () => {
|
||||
it("never lets a tenant aim its routes at a channel another group delivers to", async () => {
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "discord", channelId: "beta-ch" }] }),
|
||||
]);
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const hijack = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "beta-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(hijack, "alpha");
|
||||
expect(responseStatus(hijack)).toBe(403);
|
||||
// Nor through another spelling of the same channel (a URL path alias).
|
||||
const alias = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "x/../beta-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(alias, "alpha");
|
||||
expect(responseStatus(alias)).toBe(400);
|
||||
for (const target of [
|
||||
{ platform: "telegram", chatId: "-0100" },
|
||||
// Wraps around to an existing chat in Telegram's 64-bit parsing.
|
||||
{ platform: "telegram", chatId: "-18446744073709652393" },
|
||||
{ platform: "discord", channelId: "0123" },
|
||||
]) {
|
||||
const padded = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [target as never] })],
|
||||
});
|
||||
await adminGroupRoutesPut(padded, "alpha");
|
||||
expect(responseStatus(padded)).toBe(400);
|
||||
}
|
||||
const username = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "telegram", chatId: "@beta_channel" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(username, "alpha");
|
||||
expect(responseStatus(username)).toBe(400);
|
||||
// A channel nobody else uses is fine; so is one the writer also manages.
|
||||
const own = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "alpha-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(own, "alpha");
|
||||
expect(responseStatus(own)).toBe(200);
|
||||
const root = await cookieFor("1", "root");
|
||||
const shared = call("/admin/api/groups/alpha/routes", root, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "beta-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(shared, "alpha");
|
||||
expect(responseStatus(shared)).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shared and aliased targets", () => {
|
||||
it("compares a stored Telegram @username by the chat id it names", async () => {
|
||||
env.TELEGRAM_TOKEN = "bot";
|
||||
// Stored before only numeric ids were accepted.
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "telegram", chatId: "@beta_chat" }] }),
|
||||
]);
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async (input: unknown) => {
|
||||
expect(String(input)).toContain("getChat?chat_id=%40beta_chat");
|
||||
return new Response(JSON.stringify({ ok: true, result: { id: -100777 } }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const same = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "telegram", chatId: "-100777" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(same, "alpha");
|
||||
expect(responseStatus(same)).toBe(403);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
});
|
||||
|
||||
it("looks up no Telegram usernames for a save without Telegram targets", async () => {
|
||||
env.TELEGRAM_TOKEN = "bot";
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "telegram", chatId: "@beta_chat" }] }),
|
||||
]);
|
||||
let lookups = 0;
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
lookups++;
|
||||
return new Response(JSON.stringify({ ok: true, result: { id: -100777 } }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const discord = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "alpha-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(discord, "alpha");
|
||||
expect(responseStatus(discord)).toBe(200);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
expect(lookups).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps a channel shared from older data only where it already was", async () => {
|
||||
const shared = [{ platform: "discord" as const, channelId: "shared-ch" }];
|
||||
await saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha", targets: shared })]);
|
||||
await saveGroupRoutes(env.KV, "beta", [route("b1", { groupId: "beta", targets: shared })]);
|
||||
const alice = await cookieFor("2", "alice");
|
||||
// Unchanged: fine.
|
||||
const same = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: shared })],
|
||||
});
|
||||
await adminGroupRoutesPut(same, "alpha");
|
||||
expect(responseStatus(same)).toBe(200);
|
||||
// Another topic/thread of the shared channel on the same route: refused.
|
||||
const thread = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [
|
||||
route("a1", {
|
||||
targets: [...shared, { platform: "discord", channelId: "shared-ch", threadId: "t2" }],
|
||||
}),
|
||||
],
|
||||
});
|
||||
await adminGroupRoutesPut(thread, "alpha");
|
||||
expect(responseStatus(thread)).toBe(403);
|
||||
// A new route sending more into the shared channel: refused.
|
||||
const more = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: shared }), route("a2", { targets: shared })],
|
||||
});
|
||||
await adminGroupRoutesPut(more, "alpha");
|
||||
expect(responseStatus(more)).toBe(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe("webhook secrets", () => {
|
||||
it("keeps the secret with the group, re-checked at commit and never sent to clients", async () => {
|
||||
const { rotateTenantSecret, getTenantSecret } = await import("../server/lib/web/tenants");
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const rotate = call("/admin/api/groups/alpha/webhook/regenerate", alice, "POST");
|
||||
const { secret } = (await adminGroupWebhookRegenerate(rotate, "alpha")) as { secret: string };
|
||||
expect(secret).toMatch(/^[0-9a-f]{64}$/);
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(await getTenantSecret(env.KV, stored)).toBe(secret);
|
||||
// Never in what clients read.
|
||||
const listed = await adminApiGroupsGet(call("/admin/api/groups", alice));
|
||||
expect(JSON.stringify(listed)).not.toContain(secret);
|
||||
// A rotation whose writer lost the right meanwhile changes nothing.
|
||||
await expect(
|
||||
rotateTenantSecret(env.KV, "alpha", () => {
|
||||
throw new Error("no longer an owner");
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
const after = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(after.webhookSecret).toBe(secret);
|
||||
// A plain group edit keeps it.
|
||||
await updateGroup(env.KV, "alpha", (g) => ({
|
||||
...g,
|
||||
name: "Renamed",
|
||||
webhookSecret: undefined,
|
||||
}));
|
||||
const edited = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(edited.webhookSecret).toBe(secret);
|
||||
});
|
||||
|
||||
it("creates groups with their members bound to their accounts", async () => {
|
||||
await saveToken(env.KV, "1", "root-token", 3600);
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () =>
|
||||
new Response(JSON.stringify({ id: 77, login: "bob" }), {
|
||||
status: 200,
|
||||
})) as unknown as typeof fetch;
|
||||
try {
|
||||
const root = await cookieFor("1", "root");
|
||||
const create = call("/admin/api/groups", root, "POST", { group: group("delta", "bob") });
|
||||
await adminGroupCreate(create);
|
||||
expect(responseStatus(create)).toBe(200);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
const delta = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "delta")!;
|
||||
expect(delta.members).toEqual([{ login: "bob", role: "owner", userId: "77" }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sources and links", () => {
|
||||
it("accepts and keeps the custom source restriction", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", alice))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const save = call("/admin/api/groups/alpha", alice, "PUT", {
|
||||
group: { ...alpha, providers: ["github", "custom"] },
|
||||
});
|
||||
await adminGroupUpdate(save, "alpha");
|
||||
expect(responseStatus(save)).toBe(200);
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(stored.providers).toEqual(["github", "custom"]);
|
||||
});
|
||||
|
||||
it("uses an account-link confirmation once", async () => {
|
||||
const { handleLinkConfirm } = await import("../server/lib/web/oauth");
|
||||
const nonce = "0123456789abcdef0123456789abcdef";
|
||||
const link = JSON.stringify({
|
||||
platform: "discord",
|
||||
userId: "d1",
|
||||
githubUserId: "42",
|
||||
login: "alice",
|
||||
});
|
||||
await env.KV.put(`link:${nonce}`, link);
|
||||
const confirm = (): H3Event =>
|
||||
makeEvent("/auth/github/link", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nonce }),
|
||||
env,
|
||||
});
|
||||
await handleLinkConfirm(confirm());
|
||||
// Replayed later (a stale KV copy elsewhere still has the confirmation).
|
||||
await env.KV.put(`link:${nonce}`, link);
|
||||
const again = confirm();
|
||||
await handleLinkConfirm(again);
|
||||
expect(responseStatus(again)).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("repairing unreadable routes", () => {
|
||||
it("lets an admin explicitly replace a group whose stored route cannot be read", async () => {
|
||||
await saveGroupRoutes(env.KV, "alpha", [route("broken", { groupId: "alpha" })]);
|
||||
sqlite.exec("UPDATE d1_routes SET targets = '{oops' WHERE id = 'broken'");
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const shown = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", alice),
|
||||
"alpha",
|
||||
)) as { unreadable: boolean };
|
||||
expect(shown.unreadable).toBe(true);
|
||||
// A plain save is refused (the unseen row would be lost)...
|
||||
const plain = call("/admin/api/groups/alpha/routes", alice, "PUT", { routes: [route("a1")] });
|
||||
await adminGroupRoutesPut(plain, "alpha");
|
||||
expect(responseStatus(plain)).not.toBe(200);
|
||||
// ...an explicit repair goes through.
|
||||
const repair = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1")],
|
||||
discardUnreadable: true,
|
||||
});
|
||||
await adminGroupRoutesPut(repair, "alpha");
|
||||
expect(responseStatus(repair)).toBe(200);
|
||||
expect(sqlite.query("SELECT id FROM d1_routes WHERE group_id = 'alpha'").all()).toEqual([
|
||||
{ id: "a1" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("authorization at commit time", () => {
|
||||
it("refuses a route write by someone removed from the group meanwhile", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const write = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "fresh" }] })],
|
||||
});
|
||||
// Authorized now (the request's access check runs and is kept)...
|
||||
const { requireAnyAccess } = await import("../server/lib/web/auth");
|
||||
await requireAnyAccess(write);
|
||||
// ...but an owner removes alice before the write lands.
|
||||
await updateGroup(env.KV, "alpha", (g) => ({
|
||||
...g,
|
||||
members: [{ login: "carol", role: "owner" }],
|
||||
adminIds: ["carol"],
|
||||
}));
|
||||
// The write itself re-checks the role at commit.
|
||||
await expect(
|
||||
saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha" })], {
|
||||
guards: roleGuard(write, "alpha", "admin"),
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await loadRoutes(env.KV, { fresh: true })).some((r) => r.id === "a1")).toBe(false);
|
||||
});
|
||||
|
||||
it("voids an invite whose issuing owner was removed before it was used", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
await updateGroup(env.KV, "alpha", (g) => ({
|
||||
...g,
|
||||
members: [{ login: "carol", role: "owner" }],
|
||||
adminIds: ["carol"],
|
||||
}));
|
||||
expect(await acceptInvite(env, created.token, "9", "mallory")).toEqual({
|
||||
ok: false,
|
||||
reason: "invalid",
|
||||
});
|
||||
});
|
||||
|
||||
it("refuses a filter node that mixes condition kinds", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const mixed = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { ast: { all: 0, any: [{ type: "event", match: "push" }] } as never })],
|
||||
});
|
||||
await adminGroupRoutesPut(mixed, "alpha");
|
||||
expect(responseStatus(mixed)).toBe(400);
|
||||
});
|
||||
|
||||
it("refuses a glob longer than the matcher can run", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const long = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [
|
||||
route("a1", { filters: [{ type: "branch", match: `${"a".repeat(201)}*`, exclude: true }] }),
|
||||
],
|
||||
});
|
||||
await adminGroupRoutesPut(long, "alpha");
|
||||
expect(responseStatus(long)).toBe(400);
|
||||
});
|
||||
|
||||
it("never accepts an invite revoked after it was read", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
const { revokeInvite } = await import("../server/lib/web/invites");
|
||||
// Revoked with its D1 record; the KV copy another isolate may still read
|
||||
// is put back to stand for that stale read.
|
||||
const raw = await env.KV.get(`invite:${created.token}`);
|
||||
await revokeInvite(env.KV, created.token, env.DB);
|
||||
await env.KV.put(`invite:${created.token}`, raw!);
|
||||
expect(await acceptInvite(env, created.token, "9", "mallory")).toEqual({
|
||||
ok: false,
|
||||
reason: "invalid",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("delivery target claims at commit time", () => {
|
||||
it("aborts a write whose new channel another group took meanwhile", async () => {
|
||||
// The pre-check passed; then beta committed a route and a log target.
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "telegram", chatId: "-100" }] }),
|
||||
]);
|
||||
await updateGroup(env.KV, "beta", (g) => ({
|
||||
...g,
|
||||
logTarget: { platform: "discord", channelId: "log-ch" },
|
||||
}));
|
||||
for (const channel of ["telegram:-100", "discord:log-ch"]) {
|
||||
await expect(
|
||||
saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha" })], {
|
||||
guards: (db) => [channelFreeGuard(db, channel, ["alpha"])],
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
}
|
||||
// Same platform id on another platform, or a group the writer may edit: free.
|
||||
await saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha" })], {
|
||||
guards: (db) => [
|
||||
channelFreeGuard(db, "discord:-100", ["alpha"]),
|
||||
channelFreeGuard(db, "telegram:-100", ["alpha", "beta"]),
|
||||
],
|
||||
});
|
||||
expect((await loadRoutes(env.KV, { fresh: true })).some((r) => r.id === "a1")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("member binding", () => {
|
||||
it("binds a member added by login to the account holding it now", async () => {
|
||||
await saveToken(env.KV, "2", "alice-token", 3600);
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async (input: unknown) => {
|
||||
expect(String(input)).toBe("https://api.github.com/users/bob");
|
||||
return new Response(JSON.stringify({ id: 99, login: "bob" }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const event = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, members: [...alpha.members!, { login: "bob", role: "viewer" }] },
|
||||
});
|
||||
await adminGroupUpdate(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(stored.members).toContainEqual({ login: "bob", role: "viewer", userId: "99" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("group writes", () => {
|
||||
it("an owner of one group can update it while viewing another", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const event = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, name: "Alpha!" },
|
||||
});
|
||||
await adminGroupUpdate(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!.name).toBe(
|
||||
"Alpha!",
|
||||
);
|
||||
|
||||
// The legacy bulk API accepts the visible-but-not-owned group unchanged.
|
||||
const again = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const bulk = call("/admin/api/groups", cookie, "PUT", { groups: again.groups });
|
||||
await adminApiGroupsPut(bulk);
|
||||
expect(responseStatus(bulk)).toBe(200);
|
||||
});
|
||||
|
||||
it("refuses an update built on a stale copy", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const first = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, name: "first" },
|
||||
});
|
||||
await adminGroupUpdate(first, "alpha");
|
||||
const second = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, lang: "zh" },
|
||||
});
|
||||
await adminGroupUpdate(second, "alpha");
|
||||
expect(responseStatus(second)).toBe(409);
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(stored.name).toBe("first");
|
||||
expect(stored.lang).toBeUndefined();
|
||||
});
|
||||
|
||||
it("owners cannot change the source-isolation fields", async () => {
|
||||
await createGroup(env.KV, group("u-2", "alice", { isolated: true, installationId: 5 }));
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const event = call("/admin/api/groups/u-2", cookie, "PUT", {
|
||||
group: { ...group("u-2", "alice"), owners: [], isolated: false, installationId: 99 },
|
||||
});
|
||||
await adminGroupUpdate(event, "u-2");
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "u-2")!;
|
||||
expect(stored.isolated).toBe(true);
|
||||
expect(stored.installationId).toBe(5);
|
||||
});
|
||||
|
||||
it("a stale bulk save no longer reverts or drops another group", async () => {
|
||||
const admin = await cookieFor("1", "root");
|
||||
const snapshot = (await adminApiGroupsGet(call("/admin/api/groups", admin))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
// Someone else edits beta and creates gamma afterwards.
|
||||
const bob = await cookieFor("3", "bob");
|
||||
const beta = snapshot.groups.find((g) => g.id === "beta")!;
|
||||
await adminGroupUpdate(
|
||||
call("/admin/api/groups/beta", bob, "PUT", { group: { ...beta, name: "Beta v2" } }),
|
||||
"beta",
|
||||
);
|
||||
// The super admin saves an alpha change from the old snapshot (old beta).
|
||||
const edited = snapshot.groups.map((g) => (g.id === "alpha" ? { ...g, lang: "zh" } : g));
|
||||
const bulk = call("/admin/api/groups", admin, "PUT", { groups: edited });
|
||||
await adminApiGroupsPut(bulk);
|
||||
expect(responseStatus(bulk)).toBe(200);
|
||||
const stored = await loadGroups(env.KV, { fresh: true });
|
||||
expect(stored.find((g) => g.id === "alpha")!.lang).toBe("zh");
|
||||
// beta came back unedited from the old snapshot: bob's change survives.
|
||||
expect(stored.find((g) => g.id === "beta")!.name).toBe("Beta v2");
|
||||
});
|
||||
|
||||
it("a bulk save based on an outdated list is refused", async () => {
|
||||
const admin = await cookieFor("1", "root");
|
||||
const snapshot = (await adminApiGroupsGet(call("/admin/api/groups", admin))) as {
|
||||
groups: Group[];
|
||||
revision: string;
|
||||
};
|
||||
await adminGroupCreate(
|
||||
call("/admin/api/groups", admin, "POST", { group: group("gamma", "carol") }),
|
||||
);
|
||||
const bulk = call("/admin/api/groups", admin, "PUT", {
|
||||
groups: snapshot.groups,
|
||||
revision: snapshot.revision,
|
||||
});
|
||||
await adminApiGroupsPut(bulk);
|
||||
expect(responseStatus(bulk)).toBe(409);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).map((g) => g.id)).toContain("gamma");
|
||||
});
|
||||
|
||||
it("never deletes what was added to a group after its deletion was confirmed", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const shown = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string; version: number };
|
||||
const groups = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = groups.groups.find((g) => g.id === "alpha")!.revision;
|
||||
// A filter fragment saved meanwhile (the routes are unchanged).
|
||||
const frag = call("/admin/api/groups/alpha/fragments", cookie, "PUT", {
|
||||
fragments: [{ id: "f1", name: "F", node: { type: "event", match: "push" } }],
|
||||
});
|
||||
await adminGroupFragmentsPut(frag, "alpha");
|
||||
expect(responseStatus(frag)).toBe(200);
|
||||
const del = call(
|
||||
`/admin/api/groups/alpha?revision=${rev}&routesRevision=${shown.revision}&version=${shown.version}`,
|
||||
cookie,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, "alpha");
|
||||
expect(responseStatus(del)).toBe(409);
|
||||
});
|
||||
|
||||
it("deleting a group detaches its history and retires its id", async () => {
|
||||
sqlite.exec(
|
||||
"INSERT INTO send_logs (ts, route_id, group_id, event, target, ok) VALUES (1, 'r', 'alpha', 'push', 't', 1)",
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
// Confirmed on the group and its routes as shown.
|
||||
const shown = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string; version: number };
|
||||
const groups = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = groups.groups.find((g) => g.id === "alpha")!.revision;
|
||||
const del = call(
|
||||
`/admin/api/groups/alpha?revision=${rev}&routesRevision=${shown.revision}&version=${shown.version}`,
|
||||
cookie,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, "alpha");
|
||||
expect(responseStatus(del)).toBe(200);
|
||||
expect(sqlite.query("SELECT group_id FROM send_logs").get()).toEqual({ group_id: null });
|
||||
|
||||
const admin = await cookieFor("1", "root");
|
||||
const recreate = call("/admin/api/groups", admin, "POST", { group: group("alpha", "eve") });
|
||||
await adminGroupCreate(recreate);
|
||||
expect(responseStatus(recreate)).toBe(409);
|
||||
});
|
||||
|
||||
it("renaming moves routes and history in one go", async () => {
|
||||
sqlite.exec(
|
||||
"INSERT INTO send_logs (ts, route_id, group_id, event, target, ok) VALUES (1, 'r', 'alpha', 'push', 't', 1)",
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
await adminGroupRoutesPut(
|
||||
call("/admin/api/groups/alpha/routes", cookie, "PUT", { routes: [route("r1")] }),
|
||||
"alpha",
|
||||
);
|
||||
const event = call("/admin/api/groups/alpha/rename", cookie, "PUT", { newId: "omega" });
|
||||
await adminGroupRename(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
expect((await loadRoutes(env.KV, { fresh: true })).map((r) => r.groupId)).toEqual(["omega"]);
|
||||
expect(sqlite.query("SELECT group_id FROM send_logs").get()).toEqual({ group_id: "omega" });
|
||||
expect(await resolveGroupAlias(env, "alpha")).toBe("omega");
|
||||
});
|
||||
});
|
||||
|
||||
describe("route writes", () => {
|
||||
it("refuses a save built on routes changed meanwhile", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const loaded = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string };
|
||||
const first = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("a")],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
const saved = (await adminGroupRoutesPut(first, "alpha")) as { revision: string };
|
||||
expect(responseStatus(first)).toBe(200);
|
||||
const stale = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("b")],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
await adminGroupRoutesPut(stale, "alpha");
|
||||
expect(responseStatus(stale)).toBe(409);
|
||||
const fresh = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("a"), route("b")],
|
||||
revision: saved.revision,
|
||||
});
|
||||
await adminGroupRoutesPut(fresh, "alpha");
|
||||
expect(responseStatus(fresh)).toBe(200);
|
||||
});
|
||||
|
||||
it("rejects regexes RE2 cannot run and non-snowflake role ids", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const backref = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("x", { filters: [{ type: "keyword", match: "/(a)\\1/" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(backref, "alpha");
|
||||
expect(responseStatus(backref)).toBe(400);
|
||||
const mention = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("x", { discordRoleIds: ["1> @everyone <@&2"] })],
|
||||
});
|
||||
await adminGroupRoutesPut(mention, "alpha");
|
||||
expect(responseStatus(mention)).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("fragment writes", () => {
|
||||
it("replaces only the group's own fragments and detects stale saves", async () => {
|
||||
sqlite.exec(
|
||||
"INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('keep', 'beta', 'K', '{\"type\":\"event\",\"match\":\"push\"}', 1, 1)",
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const loaded = (await adminGroupFragmentsGet(
|
||||
call("/admin/api/groups/alpha/fragments", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string };
|
||||
const node = { type: "event", match: "push" };
|
||||
const save = call("/admin/api/groups/alpha/fragments", cookie, "PUT", {
|
||||
fragments: [{ id: "f1", name: "F1", node }],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
await adminGroupFragmentsPut(save, "alpha");
|
||||
expect(responseStatus(save)).toBe(200);
|
||||
const stale = call("/admin/api/groups/alpha/fragments", cookie, "PUT", {
|
||||
fragments: [],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
await adminGroupFragmentsPut(stale, "alpha");
|
||||
expect(responseStatus(stale)).toBe(409);
|
||||
expect(sqlite.query("SELECT group_id, id FROM d1_fragments ORDER BY group_id").all()).toEqual([
|
||||
{ group_id: "alpha", id: "f1" },
|
||||
{ group_id: "beta", id: "keep" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("token rotation", () => {
|
||||
it("stops an old token from authenticating after re-authorization", async () => {
|
||||
await saveToken(env.KV, "42", "old-token", 3600);
|
||||
expect(await findUserIdByToken(env.KV, "old-token")).toBe("42");
|
||||
await saveToken(env.KV, "42", "new-token", 3600);
|
||||
expect(await findUserIdByToken(env.KV, "old-token")).toBeNull();
|
||||
expect(await findUserIdByToken(env.KV, "new-token")).toBe("42");
|
||||
});
|
||||
|
||||
it("rejects a token whose reverse key outlived its replacement", async () => {
|
||||
await saveToken(env.KV, "42", "old-token", 3600);
|
||||
const reverse = [...(await env.KV.list({ prefix: "token-reverse:" })).keys][0]!.name;
|
||||
const value = await env.KV.get(reverse);
|
||||
await saveToken(env.KV, "42", "new-token", 3600);
|
||||
await env.KV.put(reverse, value!);
|
||||
expect(await findUserIdByToken(env.KV, "old-token")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("reserved and squatted installation ids", () => {
|
||||
it("does not let owners rename into the system id space", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const event = call("/admin/api/groups/alpha/rename", cookie, "PUT", { newId: "inst-555" });
|
||||
await adminGroupRename(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(400);
|
||||
});
|
||||
|
||||
it("never binds an installation to an unbound group that merely has its name", async () => {
|
||||
await createGroup(env.KV, group("inst-777", "mallory"));
|
||||
const bound = await ensureInstallationGroup(env.KV, 777, "victim-org");
|
||||
expect(bound).toBeNull();
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "inst-777")!;
|
||||
expect(stored.installationId).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("round-2 regressions", () => {
|
||||
it("keeps a legacy personal group isolated when it is renamed", async () => {
|
||||
sqlite.exec(
|
||||
`INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES ('u-42', 'me', '{"id":"u-42","name":"me","adminIds":["alice"],"members":[{"login":"alice","role":"owner"}]}', 1, 0, 0)`,
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const event = call("/admin/api/groups/u-42/rename", cookie, "PUT", { newId: "team" });
|
||||
await adminGroupRename(event, "u-42");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
const team = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "team")!;
|
||||
expect(team.isolated).toBe(true);
|
||||
});
|
||||
|
||||
it("never lets an old invite join a group re-created under the same id", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
await deleteAs(owner, "alpha");
|
||||
// The invite's KV record survived (as if its cleanup had failed).
|
||||
await env.KV.put(
|
||||
`invite:${created.token}`,
|
||||
JSON.stringify({
|
||||
groupId: "alpha",
|
||||
role: "admin",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "alice",
|
||||
groupInstance: "gone",
|
||||
}),
|
||||
);
|
||||
sqlite.exec("DELETE FROM dedup_keys");
|
||||
await createGroup(env.KV, group("alpha", "eve"));
|
||||
const result = await acceptInvite(env, created.token, "9", "mallory");
|
||||
expect(result).toEqual({ ok: false, reason: "invalid" });
|
||||
});
|
||||
|
||||
it("never lets a second person in on an invite whose first use committed unseen", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
// The first acceptance committed, but its answer was lost: its claim was
|
||||
// given back while the "used" record stayed.
|
||||
sqlite.exec(
|
||||
`INSERT INTO dedup_keys (key, claimed_at, expires_at) VALUES ('accepted:invite-used:${created.token}', 0, ${Date.now() + 86400_000})`,
|
||||
);
|
||||
expect(await acceptInvite(env, created.token, "9", "mallory")).toEqual({
|
||||
ok: false,
|
||||
reason: "invalid",
|
||||
});
|
||||
const alpha = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(alpha.members!.some((m) => m.login === "mallory")).toBe(false);
|
||||
});
|
||||
|
||||
it("never records an invite as used by a member write that changed nothing", async () => {
|
||||
// What the "used" record is conditioned on: the write right before it.
|
||||
await env.DB.batch([
|
||||
env.DB.prepare(
|
||||
"UPDATE d1_groups SET version = version + 1 WHERE id = ? AND version = ?",
|
||||
).bind("alpha", -1),
|
||||
env.DB.prepare(
|
||||
"INSERT INTO dedup_keys (key, claimed_at, expires_at) SELECT 'probe', 0, 1 WHERE changes() > 0",
|
||||
),
|
||||
]);
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM dedup_keys WHERE key = 'probe'").get()).toEqual({
|
||||
n: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("confirms the invite as used in the transaction that adds the member", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "viewer" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
const result = await acceptInvite(env, created.token, "9", "mallory");
|
||||
expect(result).toMatchObject({ ok: true, groupId: "alpha" });
|
||||
expect(
|
||||
sqlite
|
||||
.query("SELECT COUNT(*) AS n FROM dedup_keys WHERE key = ?")
|
||||
.get(`accepted:invite-used:${created.token}`),
|
||||
).toEqual({ n: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("deletion confirmed on a former group", () => {
|
||||
it("never deletes a later group that reused the id, however alike", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const routes = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", owner),
|
||||
"alpha",
|
||||
)) as { revision: string; version: number };
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", owner))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = listed.groups.find((g) => g.id === "alpha")!.revision;
|
||||
await deleteAs(owner, "alpha");
|
||||
// Past the retirement: the id is taken again, by a group alike in every
|
||||
// field the console shows (and at the same version).
|
||||
sqlite.exec("DELETE FROM dedup_keys");
|
||||
await createGroup(env.KV, group("alpha", "alice"));
|
||||
const del = call(
|
||||
`/admin/api/groups/alpha?revision=${rev}&routesRevision=${routes.revision}&version=${routes.version}`,
|
||||
owner,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, "alpha");
|
||||
expect(responseStatus(del)).toBe(409);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).some((g) => g.id === "alpha")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("bulk deletion and unreadable fragments", () => {
|
||||
it("never deletes a group whose fragments cannot be read", async () => {
|
||||
sqlite.exec(
|
||||
`INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('f', 'alpha', 'f', '{', 0, 0)`,
|
||||
);
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", owner))) as {
|
||||
groups: Array<Group & { revision: string }>;
|
||||
revision: string;
|
||||
};
|
||||
const event = call("/admin/api/groups", owner, "PUT", {
|
||||
groups: listed.groups.filter((g) => g.id !== "alpha"),
|
||||
revision: listed.revision,
|
||||
});
|
||||
await adminApiGroupsPut(event);
|
||||
expect(responseStatus(event)).toBe(409);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).some((g) => g.id === "alpha")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("invite revocation at commit time", () => {
|
||||
it("refuses a revocation whose owner was removed before it committed", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
// Alice is removed while her request is under way (after its checks).
|
||||
const kvGet = env.KV.get.bind(env.KV);
|
||||
let reads = 0;
|
||||
env.KV.get = (async (key: string, ...rest: unknown[]) => {
|
||||
if (key === `invite:${created.token}` && ++reads === 2) {
|
||||
sqlite.exec(
|
||||
`UPDATE d1_groups SET data = json_set(data, '$.members', json('[{"login":"bob","role":"owner"}]'), '$.adminIds', json('["bob"]')) WHERE id = 'alpha'`,
|
||||
);
|
||||
}
|
||||
return (kvGet as (...a: unknown[]) => Promise<unknown>)(key, ...rest);
|
||||
}) as KVNamespace["get"];
|
||||
const del = call(`/admin/api/invites/${created.token}`, owner, "DELETE");
|
||||
await adminInviteDelete(del, created.token);
|
||||
env.KV.get = kvGet;
|
||||
expect(responseStatus(del)).toBe(403);
|
||||
expect(await getInvite(env.KV, created.token)).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,229 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { isAdminUser } from "../server/lib/web/session";
|
||||
import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import {
|
||||
bindMemberIds,
|
||||
carryMemberIds,
|
||||
loadGroups,
|
||||
memberRole,
|
||||
saveGroups,
|
||||
} from "../server/lib/web/groups";
|
||||
import { adminApiGroupsGet, adminGroupDelete, adminGroupRoutesGet } from "../server/lib/web/admin";
|
||||
import { saveGroupRoutes, resetConfigStore } from "../server/lib/config";
|
||||
import { createInvite, listInvites } from "../server/lib/web/invites";
|
||||
import {
|
||||
claimOnce,
|
||||
confirmClaim,
|
||||
deliveryKey,
|
||||
idempotencyStore,
|
||||
} from "../server/lib/lib/idempotency";
|
||||
import { handleTelegramUpdate } from "../server/lib/drivers/telegram/commands";
|
||||
import { kvFallbackActive, noteKvFallback } from "../server/lib/storage/d1";
|
||||
import { d1ConfigStore } from "../server/lib/storage/config-store";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { Env, Group, Route } from "../server/lib/types";
|
||||
|
||||
const route = (id: string, groupId: string): Route => ({
|
||||
id,
|
||||
name: id,
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ platform: "discord", channelId: "c1" }],
|
||||
groupId,
|
||||
});
|
||||
|
||||
describe("member identity", () => {
|
||||
it("never matches one configured string both as a user id and as a login", () => {
|
||||
const env = { ADMIN_USER_IDS: "123, alice" } as Env;
|
||||
// User id 123 is the admin; a different account whose login is "123" is not.
|
||||
expect(isAdminUser(env, "123", "someone")).toBe(true);
|
||||
expect(isAdminUser(env, "999", "123")).toBe(false);
|
||||
expect(isAdminUser(env, "5", "ALICE")).toBe(true);
|
||||
});
|
||||
|
||||
it("matches a bound member by user id only, so a reclaimed login inherits nothing", () => {
|
||||
const group: Group = {
|
||||
id: "g",
|
||||
name: "G",
|
||||
adminIds: [],
|
||||
members: [{ login: "alice", role: "owner", userId: "42" }],
|
||||
};
|
||||
expect(memberRole(group, "42", "alice-renamed")).toBe("owner");
|
||||
// Someone who took over the old login after a rename.
|
||||
expect(memberRole(group, "77", "alice")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("binds a member's user id on sign-in and keeps it across member edits", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
resetConfigStore(kv);
|
||||
await saveGroups(kv, [
|
||||
{
|
||||
id: "g",
|
||||
name: "G",
|
||||
adminIds: [],
|
||||
members: [
|
||||
{ login: "alice", role: "owner" },
|
||||
{ login: "bob", role: "viewer" },
|
||||
],
|
||||
},
|
||||
]);
|
||||
await bindMemberIds(kv, await loadGroups(kv, { fresh: true }), "42", "Alice");
|
||||
const stored = (await loadGroups(kv, { fresh: true }))[0]!;
|
||||
expect(stored.members).toEqual([
|
||||
{ login: "alice", role: "owner", userId: "42" },
|
||||
{ login: "bob", role: "viewer" },
|
||||
]);
|
||||
// A client edit (which never carries ids) keeps the binding.
|
||||
const edited = carryMemberIds(
|
||||
{
|
||||
...stored,
|
||||
members: [
|
||||
{ login: "alice", role: "owner" },
|
||||
{ login: "bob", role: "admin" },
|
||||
],
|
||||
},
|
||||
stored,
|
||||
);
|
||||
expect(edited.members![0]).toEqual({ login: "alice", role: "owner", userId: "42" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("group deletion", () => {
|
||||
it("refuses to delete a group whose routes changed after the deletion was confirmed", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
resetConfigStore(kv);
|
||||
await saveGroups(kv, [
|
||||
{ id: "g", name: "G", adminIds: [], members: [{ login: "alice", role: "owner" }] },
|
||||
]);
|
||||
const env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: {} as D1Database } as Env;
|
||||
const sessionId = await createAdminSession(kv, "42", "alice");
|
||||
const headers = { cookie: adminCookie(sessionId) };
|
||||
const view = makeEvent("/admin/api/groups/g/routes", { headers, env });
|
||||
const { revision } = (await adminGroupRoutesGet(view, "g")) as { revision: string };
|
||||
const listed = (await adminApiGroupsGet(makeEvent("/admin/api/groups", { headers, env }))) as {
|
||||
groups: Array<{ revision: string }>;
|
||||
};
|
||||
const groupRev = listed.groups[0]!.revision;
|
||||
// Someone adds a route after the owner saw an empty group.
|
||||
await saveGroupRoutes(kv, "g", [route("r1", "g")]);
|
||||
const del = makeEvent(`/admin/api/groups/g?revision=${groupRev}&routesRevision=${revision}`, {
|
||||
method: "DELETE",
|
||||
headers,
|
||||
env,
|
||||
});
|
||||
await adminGroupDelete(del, "g");
|
||||
expect(responseStatus(del)).toBe(409);
|
||||
expect((await loadGroups(kv, { fresh: true })).map((g) => g.id)).toEqual(["g"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("one-time claims", () => {
|
||||
it("tells a confirmed run from one left unconfirmed", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const store = idempotencyStore({} as D1Database, kv);
|
||||
expect(await claimOnce(store, "k", 60)).toBe("claimed");
|
||||
expect(await claimOnce(store, "k", 60)).toBe("pending");
|
||||
await confirmClaim(store, "k", 600);
|
||||
expect(await claimOnce(store, "k", 60)).toBe("done");
|
||||
// A delivery id crafted to look like a confirmation never passes for one.
|
||||
await confirmClaim(store, deliveryKey("custom", "g", "x#accepted"), 600);
|
||||
await confirmClaim(store, deliveryKey("custom", "g", "accepted:x"), 600);
|
||||
expect(await claimOnce(store, deliveryKey("custom", "g", "x"), 60)).toBe("claimed");
|
||||
});
|
||||
|
||||
it("makes Telegram redeliver an update whose earlier run is unconfirmed", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: {} as D1Database } as Env;
|
||||
// An earlier run took the update and could neither finish nor give it back.
|
||||
await idempotencyStore(env.DB, kv).claim("bot:telegram::7", 300);
|
||||
const update = { update_id: 7, message: { text: "/gh", chat: { id: 1 } } };
|
||||
expect(handleTelegramUpdate(env, update)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("invite listing", () => {
|
||||
it("lists both of two invites whose index writes overwrote each other", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const invite = {
|
||||
groupId: "team",
|
||||
role: "viewer" as const,
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
};
|
||||
const a = await createInvite(kv, invite);
|
||||
const b = await createInvite(kv, invite);
|
||||
// The second creation read the index before the first wrote it.
|
||||
await kv.put("invite:group:team", JSON.stringify([b]));
|
||||
expect((await listInvites(kv, "team")).map((i) => i.token).sort()).toEqual([a, b].sort());
|
||||
});
|
||||
});
|
||||
|
||||
describe("storage fallbacks", () => {
|
||||
it("retries the KV fallback marker until it is actually stored", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
const put = kv.put.bind(kv);
|
||||
let failing = true;
|
||||
kv.put = async (key: string, value: string, opts?: KVNamespacePutOptions): Promise<void> => {
|
||||
if (failing) throw new Error("KV down");
|
||||
return put(key, value, opts);
|
||||
};
|
||||
await expect(noteKvFallback(kv)).rejects.toThrow();
|
||||
failing = false;
|
||||
await noteKvFallback(kv);
|
||||
expect(Number(store.get("storage:kv-fallback"))).toBeGreaterThan(Date.now());
|
||||
expect(await kvFallbackActive(kv)).toBe(true);
|
||||
});
|
||||
|
||||
it("remembers KV-only state, so a D1 binding added later still honors it", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
const kvOnly = idempotencyStore({} as D1Database, kv);
|
||||
expect(await kvOnly.claim("delivery:github:@global:d9", 600)).toBe(true);
|
||||
expect(store.has("storage:kv-fallback")).toBe(true);
|
||||
// The deployment gets D1 (tables migrated) in a fresh isolate.
|
||||
const { db } = createSqliteD1();
|
||||
const { kv: sameKv } = { kv: { ...kv } as KVNamespace };
|
||||
expect(await idempotencyStore(db, sameKv).claim("delivery:github:@global:d9", 600)).toBe(false);
|
||||
});
|
||||
|
||||
it("never re-seeds a configuration another isolate seeded and an admin emptied", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([{ id: "legacy", name: "L", adminIds: [] }]));
|
||||
store.set("config:routes", JSON.stringify([route("r1", "legacy")]));
|
||||
const first = d1ConfigStore(db, kv);
|
||||
expect((await first.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["legacy"]);
|
||||
await first.deleteGroup("legacy");
|
||||
// A second isolate that had already passed the KV marker check.
|
||||
store.delete("config:d1-seeded");
|
||||
const late = d1ConfigStore(db, kv);
|
||||
expect(await late.loadGroups({ fresh: true })).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps listing invites when the index cannot be read, instead of dropping them", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
const get = kv.get.bind(kv);
|
||||
kv.get = (async (key: string, type?: never) => {
|
||||
if (key === "invite:group:team") throw new Error("KV down");
|
||||
return get(key, type);
|
||||
}) as typeof kv.get;
|
||||
await expect(
|
||||
createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
kv.get = get;
|
||||
// Neither invite is lost from the list: the half-created one is listed
|
||||
// through its own marker, so it can still be found and revoked.
|
||||
expect(await listInvites(kv, "team")).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user