fix(security): harden webhook delivery pipeline and group storage (#11)

* fix(log): preserve structured error details

Error objects lose their useful details when serialized as plain JSON.
Keep the message, stack and cause in structured log output.

* fix(runtime): bound shared in-flight waits

A canceled request can leave its shared promise pending forever.
Bound joins and coordinate a single replacement so later requests
make progress without multiplying upstream work.

* fix(bot): reject oversized callback bodies

Reject declared oversized bot callbacks before reading their bodies.
This keeps unauthenticated requests from wasting worker resources.

* test(d1): exercise real sqlite transactions

Exercise real foreign keys, transactions and conditional statements
instead of mocks that cannot reproduce lost updates or cascades.

* fix(storage): preserve claims across kv fallback

Only missing tables justify KV fallback; other failures must surface.
Remember fallback writes so migration to D1 cannot forget claims
and replay completed work. Share marker checks without extra writes.

* fix(filter): use isolated linear-time patterns

Backtracking patterns can exhaust worker CPU on untrusted payloads.
Use linear-time matching and separate bounded cache namespaces so
glob and regex entries cannot corrupt each other.

* fix(filter): read each event branch correctly

Deployment and check branches live in event-specific nested fields.
Read those fields and remove only the actual leading ref prefix.

* fix(filter): read only own payload properties

Inherited object properties are not webhook payload data.
Read own properties only so JSONPath cannot traverse prototypes.

* fix(filter): validate executable condition trees

Malformed trees and unsupported patterns must not silently turn an
exclude rule into a pass. Validate the shape and only the patterns
the evaluator actually executes.

* fix(i18n): bound and cache translation loads

Repeated misses and simultaneous override loads spend KV reads for
the same language. Cache confirmed misses and share bounded loads
without caching read failures as missing translations.

* i18n(bot): explain command and account failures

Explain account linking, command failures and skipped delivery
outcomes consistently in both supported languages.

* fix(github): bound and reuse app token fetches

Repeated token exchanges waste requests and JWT work for every event.
Reuse valid installation tokens, share refreshes and back off briefly
after failures while keeping every shared wait bounded.

* fix(github): restrict enrichment request urls

Webhook-provided URLs must never receive an installation token on
another host. Restrict enrichment to GitHub repository API paths,
reject redirects and bound optional requests.

* fix(formatter): preserve invalid code points

Out-of-range numeric entities can throw while formatting a delivery.
Keep invalid code points verbatim instead of aborting the message.

* fix(formatter): preserve bounded markdown text

Code spans, fences, indentation and autolinks are Markdown content,
not HTML tags to strip. Preserve them while bounding cleanup input
and reusing results across routes for the same event.

* fix(formatter): strip only leading ref prefixes

A short branch or tag may itself contain refs/heads or refs/tags.
Strip one leading full-ref prefix at the caller and keep short names
unchanged when generating labels and links.

* fix(gitea): link push authors to their forge

A Gitea author is not the same-named GitHub account.
Build author profile links from the originating forge.

* fix(formatter): render deployment state values

The deployment status object stringifies as [object Object].
Render its actual state in the status field.

* fix(formatter): read security severity levels

The scanner severity is not the security severity of an alert.
Prefer the security level so critical and high alerts get the right
label and color.

* fix(formatter): hide actions on other forges

Action buttons call the GitHub API as the linked user.
Hide them for other forges to avoid operating on a same-named
GitHub repository.

* fix(check): isolate updates by check suite

Same-named checks from different suites must not edit one message.
Include the suite or app identity and retain the former key for
controlled adoption of existing tracked messages.

* fix(logs): scope queries before limiting results

Filter authorized groups in SQL before LIMIT and aggregate only
visible rows. Preserve current group attribution for late writes,
surface read failures and combine related totals in one scan.

* perf(d1): index pruning and failed send queries

Pruning and failure views should not scan all historical rows.
Index the high-value access paths without adding a write-heavy
index for infrequent delivery-detail lookups.

* fix(discord): bound individual api requests

An unbounded API request can outlive an update-chain lock.
Bound each request so another holder cannot race a late edit.

* fix(discord): defer long rate-limit waits

Long rate-limit sleeps block delivery progress inside a worker.
Return a retryable 429 for the queue when the wait exceeds the bound.

* fix(discord): encode channel path segments

Stored channel and message ids are path segments, not URL paths.
Encode them so aliases cannot address another Discord resource.

* fix(discord): reuse nonces after lost responses

A successful create may lose its response. Reuse a stable nonce
with enforce_nonce so retries recover that message instead of
posting duplicates or accepting an unreadable success.

* fix(discord): constrain embeds and role mentions

Respect combined embed and component limits as well as field caps.
Restrict allowed mentions to validated route roles so payload text
cannot notify everyone.

* fix(discord): resume bounded command syncs

Application changes, command rollbacks and large guild lists can
leave commands stale indefinitely. Track the current hash per app
and resume bounded paginated work without needless KV writes.

* fix(github): verify installation management

An installation id is user-controlled, and repository access is not
installation management. Verify the owning account or an active
organization admin using the user token and read:org scope.

* fix(github): resolve immutable member ids

A login can be renamed and reclaimed before its first sign-in.
Resolve the current immutable user id when membership is granted
so a future holder of the login cannot inherit access.

* fix(auth): revoke stale sessions and tokens

Revoked or rotated credentials must stop working despite stale KV
replicas. Track the live token and revocations in D1 and preserve
immutable identity bindings rather than trusting a recycled login.

* fix(auth): bind sessions and tokens to identity

Automatic retries can repeat non-idempotent user actions.
Disable those retries and expose a write hook so bot handlers can
record completion immediately before the GitHub write.

* fix(discord): defer and deduplicate interactions

Acknowledge slow actions before Discord times out and deduplicate
interaction writes. Preserve comment text and reject edits whose
source changed after the modal opened.

* fix(telegram): always verify webhook secrets

Webhook verification must not silently disable itself when the
optional secret is absent. Derive a stable secret from the bot
token and reject callbacks when verification is unavailable.

* fix(telegram): bound individual api requests

Bound individual platform calls so they cannot outlive the lock
protecting a message update or stall a whole delivery invocation.

* fix(telegram): honor nested rate-limit delays

Telegram places retry_after inside parameters, not at the top level.
Read that value and defer long waits to the delivery queue.

* fix(telegram): report unreadable send results

An unreadable successful response may already have sent the message.
Report an explicit uncertain result instead of retrying a create
without an idempotency key or pretending it succeeded.

* fix(telegram): keep editable messages as text

Guessing photo versus text from message content makes later edits
use the wrong API and a smaller caption budget. Always send text
and use the avatar as a link preview.

* fix(telegram): render hard breaks as newlines

Telegram HTML does not support br tags. Render Markdown hard breaks
as plain newlines so otherwise valid messages are accepted.

* fix(telegram): clamp visible html characters

Telegram counts visible characters, not HTML source bytes.
Truncate without splitting tags, entities or surrogate pairs.

* fix(telegram): honor the author emoji toggle

The author marker must obey the group emoji preference just like
formatter-added decoration.

* fix(telegram): keep completed commands claimed

Redelivery after a completed write must never post another comment.
Keep completion claims despite reply failures, scope updates per bot
and only issue account-login links in private chats.

* fix(feishu): bound individual api requests

Bound individual platform calls so they cannot outlive the lock
protecting a message update or stall a whole delivery invocation.

* fix(feishu): classify retryable api failures

Business error codes can be transient even in HTTP 200 responses.
Classify token and rate-limit errors for retry and defer long waits.

* fix(feishu): retry sends with stable uuids

An unreadable response does not prove a send failed.
Reuse the same uuid for retries so recovering the result cannot
create a second Feishu message.

* fix(feishu): refresh and evict tokens safely

Separate app token caches, stop reusing refused tokens and share
refreshes. Cache failures briefly and keep valid tokens in memory
when a best-effort KV cache write fails.

* fix(feishu): use the card update endpoint

Delayed card updates use the card update endpoint; the config
endpoint cannot update an interaction response.

* fix(feishu): neutralize untrusted mentions

Forge text is untrusted and can contain at-all tags.
Neutralize mentions in card bodies and footers before rendering.

* fix(feishu): authenticate and dedupe callbacks

Authenticate signed or token-verified callbacks before dispatching
commands. Decode encrypted envelopes and handle card actions using
the correct fields without claiming irrelevant chat events.

* fix(target): normalize actual destinations

Different target spellings can name the same actual destination.
Normalize channel, thread and topic identities for ownership checks
and per-route duplicate suppression.

* 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.

* fix(group): preserve identity through lifecycle

Group names are mutable and reusable, so they cannot own durable
state. Preserve immutable identities across lifecycle operations and
commit invite use with membership under transaction-time guards.

* fix(oauth): confirm account links and installs

A login link or installation id is not proof of account ownership.
Verify installation management, bind browser state and require an
explicit account-link confirmation before granting chat access.

* fix(queue): fence leases and record target outcomes

Retries must remember completed targets and distinguish active
lease holders. Fence state updates, preserve permanent outcomes and
reject missing parked payloads instead of dispatching empty data.

* fix(dispatch): isolate and resume tenant sends

Check tenant sources before filters and never dispatch orphan routes.
Record each target outcome, isolate update chains by forge and group
identity, and keep stale events from overwriting newer messages.

* fix(queue): bound batches and resume deliveries

A queue invocation has shared query and subrequest limits.
Use a small batch and shared send budget, renew fenced leases and
resume only unfinished work without exhausting unstarted messages.

* 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.

* fix(webhook): confirm only accepted deliveries

Acknowledging before queue acceptance permanently loses deliveries.
Confirm only accepted work, release failed claims, scope replay
identities and strip tenant-supplied installation authority.

* fix(storage): prune abandoned parked payloads

Oversized payloads can remain after interrupted enqueue attempts.
Prune objects beyond their retry lifetime without deleting payloads
that a current delivery may still need.

* fix(console): expose api conflict statuses

Callers need the HTTP status to distinguish stale edits from other
failures and reload current data after a conflict.

* refactor(console): model group revisions

Carry server revisions explicitly so console writes can reject
changes based on an outdated group snapshot.

* fix(console): send loaded route revisions

Send the revision actually loaded with the route list and adopt the
server response after a save. Reload on conflict instead of silently
overwriting another editor.

* fix(console): guard fragment saves by revision

Fragments share a group version with other configuration writes.
Submit the loaded revision and reload on conflict to preserve
concurrent edits.

* fix(console): save groups with guarded writes

Whole-list saves can overwrite unrelated groups. Use per-group
revision-aware writes, retain rename results and confirm deletion
on the exact group, routes and version shown to the user.

* fix(console): reject edits of stale routes

A conflict reload can leave an older route editor open.
Compare its original value before saving so it cannot overwrite
the newly loaded route.

* fix(console): show overview loading failures

Failed overview requests must not look like an empty dashboard or
a successful refresh. Show the reason while retaining prior data.

* fix(console): explain rejected install bindings

Explain why an installation binding was refused instead of silently
returning to the console with no visible outcome.

* fix(console): allow selecting a group named all

The all sentinel collides with a valid group id.
Use a value that cannot be a group id so that group stays selectable.

* fix(console): discard stale log detail responses

An older detail request can finish after a newer selection or close.
Discard it so the dialog never shows the previously clicked record.

* fix(console): retain legacy group owners

Groups predating member roles store owners in adminIds.
Load those as owners so adding a member never drops all old owners.

* fix(console): lock member edits during saves

Edits made during a save are overwritten by its result.
Disable member controls and guard handlers until the write settles.

* fix(console): show invite loading failures

Failed invite loads must not look like no pending invites.
Show the error and discard responses from a previously shown group.

* fix(console): retain legacy discord log targets

A stored log target without platform means Discord.
Keep that meaning in the editor instead of clearing the target.

* fix(console): retain undisplayed forge sources

An editor that only displays some providers must preserve the others.
Keep stored custom sources when saving unrelated group settings.

* fix(console): lock the group editor while saving

Prevent edits while a group save is in flight and make super-only
installation settings readonly instead of silently ignoring edits.

* fix(console): lock route edits and show failures

Lock the route editor while saving and show fragment load failures
with a retry action rather than presenting a false empty list.

* fix(config): expose the legal contact override

Declare the public runtime key so the documented environment
override reaches legal pages in deployed Nuxt builds.

* build(deploy): migrate d1 before deployment

Apply pending D1 migrations before deploying code that requires
them. Stop deployment when migration fails instead of running
against an incomplete schema.

* docs(worker): align guides with runtime behavior

Synchronize both language guides, API references and examples with
the implemented authorization, storage and delivery behavior.
Document migration requirements and accepted operational limits.

* fix(storage): repair orphaned KV routes

Repair orphaned KV routes before serving KV-only configuration.

* fix(security): fully escape forge HTML

---------

Co-authored-by: RhenCloud <i@rhen.cloud>
This commit is contained in:
翅膀喵andRhenCloud authored and GitHub committed 2026-10-24 16:19:06 +08:00
1 parent dbf162bf74
commit 978c53eeb3
163 files changed
+18459 -3607

No files matched your search

+1 -1
View File
@@ -33,7 +33,7 @@ async function userOctokit(
event: H3Event,
userId: string,
): Promise<Awaited<ReturnType<typeof getUserOctokit>>> {
return getUserOctokit(userId, cfEnv(event).KV);
return getUserOctokit(userId, cfEnv(event));
}
function fail(event: H3Event, status: number, error: string): Record<string, unknown> {
+1272 -249
View File
File diff suppressed because it is too large. Load diff
+11 -9
View File
@@ -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 };
}
@@ -99,7 +100,8 @@ export async function bearerUserId(event: H3Event): Promise<string> {
const auth = getHeader(event, "authorization");
if (!auth?.startsWith("Bearer "))
throw createError({ statusCode: 401, statusMessage: "Missing authorization" });
const userId = await findUserIdByToken(env.KV, auth.slice(7));
// Resolves only the user's *current* token: a rotated or revoked one fails.
const userId = await findUserIdByToken(env.KV, auth.slice(7), env.DB);
if (!userId) throw createError({ statusCode: 401, statusMessage: "Invalid or expired token" });
return userId;
}
+148
View File
@@ -0,0 +1,148 @@
import type { Env } from "../types";
import { retiredGroupKey, type LifecycleStatements } from "../storage/config-store";
import { idempotencyStore, instanceScope } from "../lib/idempotency";
import { canUseD1 } from "../storage/d1";
import { log } from "../lib/log";
/**
* A deleted or renamed group id stays retired this long: queued deliveries
* still addressed to it (queue retries span minutes, the DLQ up to days) can
* then never reach an unrelated group that took over the id.
*/
const RETIRED_TTL_SECONDS = 4 * 86400;
const ALIAS_TTL_SECONDS = 4 * 86400;
const retiredKey = retiredGroupKey;
function aliasKey(id: string): string {
return `group:renamed:${id}`;
}
/** D1 alias row: `group-alias:{from} {to}` (ids never contain spaces). */
function aliasRowPrefix(from: string): string {
return `group-alias:${from} `;
}
/**
* The first key past every key starting with `prefix` (its last character
* incremented): `key >= prefix AND key < end` is a primary-key range, where
* `substr(key, …) = prefix` scans the whole table.
*/
function prefixEnd(prefix: string): string {
return prefix.slice(0, -1) + String.fromCharCode(prefix.charCodeAt(prefix.length - 1) + 1);
}
function retireStatement(db: D1Database, id: string): D1PreparedStatement {
const now = Date.now();
return db
.prepare(
`INSERT INTO dedup_keys (key, claimed_at, expires_at) VALUES (?, ?, ?)
ON CONFLICT(key) DO UPDATE SET claimed_at = excluded.claimed_at, expires_at = excluded.expires_at`,
)
.bind(retiredKey(id), now, now + RETIRED_TTL_SECONDS * 1000);
}
/**
* Statements committed with a group's deletion: its send logs and audit
* entries stay for super admins but no longer belong to any group (a later
* group with the same id must not inherit them), its message tracking is
* dropped, and the id is retired.
*/
export function deleteLifecycle(
id: string,
instance?: string,
legacyId?: string,
): LifecycleStatements {
// The group's own chains only: under its instance, and — from before
// instances — under the id it had back then. Rows under its current id may
// belong to the group that had that id before instances.
const prefixes = [
...(instance ? [`${instanceScope(instance)}:`] : []),
...(legacyId ? [`${legacyId}:`] : []),
];
return (db) => [
db.prepare("UPDATE send_logs SET group_id = NULL WHERE group_id = ?").bind(id),
db.prepare("UPDATE audit_logs SET group_id = NULL WHERE group_id = ?").bind(id),
...prefixes.map((prefix) =>
db
.prepare("DELETE FROM message_tracking WHERE event_id >= ? AND event_id < ?")
.bind(prefix, prefixEnd(prefix)),
),
retireStatement(db, id),
];
}
/**
* Statements committed with a group's rename: its history moves to the new id
* (visible to the group, to nobody else), and the old id is retired. Message
* tracking stays put: chains are keyed by the group's instance, and those from
* before instances by its `legacyId` — neither changes with a rename.
*/
export function renameLifecycle(from: string, to: string): LifecycleStatements {
return (db) => [
db.prepare("UPDATE send_logs SET group_id = ? WHERE group_id = ?").bind(to, from),
db.prepare("UPDATE audit_logs SET group_id = ? WHERE group_id = ?").bind(to, from),
retireStatement(db, from),
// Committed with the rename itself: queued deliveries for the old id can
// always find the new one.
db
.prepare(
`INSERT INTO dedup_keys (key, claimed_at, expires_at) VALUES (?, ?, ?)
ON CONFLICT(key) DO UPDATE SET claimed_at = excluded.claimed_at, expires_at = excluded.expires_at`,
)
.bind(`${aliasRowPrefix(from)}${to}`, Date.now(), Date.now() + ALIAS_TTL_SECONDS * 1000),
];
}
/** Mark a group id as recently used (KV-only deployments; D1 does it in the transaction). */
export async function retireGroupId(env: Env, id: string): Promise<void> {
if (canUseD1(env.DB)) return;
try {
await idempotencyStore(env.DB, env.KV).claim(retiredKey(id), RETIRED_TTL_SECONDS);
} catch (err) {
log.warn({ err, id }, "Failed to retire group id");
}
}
export async function isGroupIdRetired(env: Env, id: string): Promise<boolean> {
return idempotencyStore(env.DB, env.KV).has(retiredKey(id));
}
/** Remember where a renamed group went (KV-only deployments; D1 records it in the rename). */
export async function recordGroupRename(env: Env, from: string, to: string): Promise<void> {
if (canUseD1(env.DB)) return;
await env.KV.put(aliasKey(from), to, { expirationTtl: ALIAS_TTL_SECONDS });
}
async function nextAlias(env: Env, id: string): Promise<string | null> {
if (canUseD1(env.DB)) {
const prefix = aliasRowPrefix(id);
const row = await env.DB.prepare(
`SELECT key FROM dedup_keys WHERE key >= ? AND key < ? AND expires_at > ?
ORDER BY claimed_at DESC LIMIT 1`,
)
.bind(prefix, prefixEnd(prefix), Date.now())
.first<{ key: string }>();
return row ? row.key.slice(prefix.length) : null;
}
return env.KV.get(aliasKey(id));
}
/**
* The current id of a group that may have been renamed (follows chains).
* Read errors propagate: an unknown alias must not pass for "no alias".
*/
export async function resolveGroupAlias(env: Env, id: string): Promise<string> {
return (await groupAliasChain(env, id)).at(-1)!;
}
/** Every id a group had from `id` on, oldest first (`id` itself included). */
export async function groupAliasChain(env: Env, id: string): Promise<string[]> {
const chain = [id];
for (let i = 0; i < 5; i++) {
const next = await nextAlias(env, chain.at(-1)!);
if (!next || chain.includes(next)) break;
chain.push(next);
}
return chain;
}
+261 -89
View File
@@ -1,12 +1,15 @@
import type { Env, Group, GroupMember, GroupRole } from "../types";
import { isAdminUser } from "./session";
import { log } from "../lib/log";
import { migrateGroups, validateGroups } from "../config/schema";
import { identityEntryMatches, isAdminUser } from "./session";
import { getConfigStore } from "../config";
const GROUPS_KEY = "config:groups";
const GROUPS_CACHE_TTL = 300_000;
let groupsCache: { groups: Group[]; expiresAt: number } | null = null;
import { idempotencyStore, unrecordedGuard } from "../lib/idempotency";
import { ConfigConflictError } from "../storage/config-store";
import { canUseD1 } from "../storage/d1";
import type {
GroupMutation,
GuardedWrite,
LifecycleStatements,
LoadOptions,
} from "../storage/config-store";
/**
* Normalizes a group's member list. Groups stored with the legacy `adminIds`
@@ -24,7 +27,8 @@ export function normalizeGroupMembers(group: Group): GroupMember[] {
if (!login || seen.has(login.toLowerCase())) continue;
const role: GroupRole = m?.role === "admin" || m?.role === "viewer" ? m.role : "owner";
seen.add(login.toLowerCase());
out.push({ login, role });
const userId = typeof m?.userId === "string" && /^\d+$/.test(m.userId) ? m.userId : "";
out.push(userId ? { login, role, userId } : { login, role });
}
return out;
}
@@ -40,44 +44,155 @@ export function normalizeGroupMembers(group: Group): GroupMember[] {
return out;
}
export async function loadGroups(kv: KVNamespace): Promise<Group[]> {
const store = getConfigStore(kv);
if (store) return store.loadGroups();
if (groupsCache && Date.now() < groupsCache.expiresAt) {
return groupsCache.groups;
}
try {
const stored = await kv.get<Group[]>(GROUPS_KEY, "json");
const groups = Array.isArray(stored) ? validateGroups(migrateGroups(stored)) : [];
groupsCache = { groups, expiresAt: Date.now() + GROUPS_CACHE_TTL };
return groups;
} catch (err) {
log.warn({ err }, "Failed to load groups from KV");
}
return [];
export async function loadGroups(kv: KVNamespace, opts?: LoadOptions): Promise<Group[]> {
return getConfigStore(kv).loadGroups(opts);
}
/**
* Replace the full group set (super-admin bulk write): groups left out are
* deleted with their routes and fragments, only changed groups are rewritten.
*/
export async function saveGroups(kv: KVNamespace, groups: Group[]): Promise<void> {
const store = getConfigStore(kv);
if (store) {
await store.saveGroups(groups);
groupsCache = null;
return;
}
await kv.put(GROUPS_KEY, JSON.stringify(groups));
groupsCache = null;
await getConfigStore(kv).saveGroups(groups);
}
export function invalidateGroupsCache(): void {
groupsCache = null;
/** Insert a group unless its id is taken; true when it was created. */
export async function createGroup(kv: KVNamespace, group: Group): Promise<boolean> {
return getConfigStore(kv).createGroup(group);
}
/** Case-insensitive match of a GitHub userId or login against a list of ids/logins. */
/**
* Atomically read-modify-write one group. `mutate` gets the current stored
* group and returns the replacement, or null to keep it unchanged.
*/
export async function updateGroup(
kv: KVNamespace,
id: string,
mutate: GroupMutation,
opts?: Pick<GuardedWrite<Group>, "guards" | "after">,
): Promise<Group | null> {
return getConfigStore(kv).updateGroup(id, mutate, opts);
}
/** Delete one group with its routes and filter fragments. */
export async function deleteGroup(
kv: KVNamespace,
id: string,
opts?: GuardedWrite<Group> & { extra?: LifecycleStatements; version?: number },
): Promise<void> {
await getConfigStore(kv).deleteGroup(id, opts);
}
/** Move a group (with its routes and fragments) to a new id atomically. */
export async function renameGroup(
kv: KVNamespace,
from: string,
to: string,
opts?: GuardedWrite<Group> & { extra?: LifecycleStatements; transform?: (g: Group) => Group },
): Promise<Group> {
return getConfigStore(kv).renameGroup(from, to, opts);
}
/** Kept for callers that reset state between tests; the store owns its caches. */
export function invalidateGroupsCache(): void {}
/** Whether any entry names the user: all-digit entries are user ids, others logins. */
export function identityMatches(ids: string[], userId: string, login: string): boolean {
const wanted = ids.map((s) => s.trim()).filter(Boolean);
if (wanted.length === 0) return false;
return wanted.some((id) => id === userId || id.toLowerCase() === login.toLowerCase());
return ids.some((id) => identityEntryMatches(id, userId, login));
}
/**
* Whether a member entry is this user. A member bound to a GitHub user id is
* that account only — a login can be renamed and then claimed by someone
* else, an id never changes. Unbound entries match like `identityMatches`.
*/
export function memberMatches(member: GroupMember, userId: string, login: string): boolean {
if (member.userId) return member.userId === userId;
return identityEntryMatches(member.login, userId, login);
}
/**
* A guard statement that aborts its transaction unless the user holds one of
* `roles` in group `gid` as stored at that moment. Member entries match the
* way `memberMatches` does: a bound entry by user id, an all-digit one by
* user id, any other by `login` (pass "" to match by id only); groups still
* on legacy `adminIds` count those as owners.
*/
export function memberRoleStatement(
db: D1Database,
gid: string,
userId: string,
login: string,
roles: GroupRole[],
): D1PreparedStatement {
return db
.prepare(
`SELECT CASE WHEN EXISTS (
SELECT 1 FROM (SELECT id, CASE WHEN json_valid(data) THEN data ELSE '{}' END AS data FROM d1_groups) g, json_each(g.data, '$.members') m
WHERE g.id = ? AND json_extract(m.value, '$.role') IN (${roles.map(() => "?").join(", ")})
AND (json_extract(m.value, '$.userId') = ?
OR (json_extract(m.value, '$.userId') IS NULL AND (
(json_extract(m.value, '$.login') NOT GLOB '*[^0-9]*'
AND json_extract(m.value, '$.login') = ?)
OR (json_extract(m.value, '$.login') GLOB '*[^0-9]*'
AND lower(json_extract(m.value, '$.login')) = lower(?)))))
) OR (? IN (${roles.map(() => "?").join(", ")}) AND EXISTS (
SELECT 1 FROM (SELECT id, CASE WHEN json_valid(data) THEN data ELSE '{}' END AS data FROM d1_groups) g, json_each(g.data, '$.adminIds') a
WHERE g.id = ? AND coalesce(json_array_length(g.data, '$.members'), 0) = 0
AND ((a.value NOT GLOB '*[^0-9]*' AND a.value = ?)
OR (a.value GLOB '*[^0-9]*' AND lower(a.value) = lower(?)))
)) THEN 1 ELSE json('role revoked') END AS ok`,
)
.bind(gid, ...roles, userId, userId, login, "owner", ...roles, gid, userId, login);
}
/**
* Carry the user ids already bound to members over to a submitted member
* list (matched by login): clients edit logins and roles, never the binding.
*/
export function carryMemberIds(next: Group, prev: Group | undefined): Group {
if (!prev || !Array.isArray(next.members)) return next;
const bound = new Map(
normalizeGroupMembers(prev)
.filter((m) => m.userId)
.map((m) => [m.login.toLowerCase(), m.userId!]),
);
if (bound.size === 0) return next;
return {
...next,
members: next.members.map((m) => {
const userId = bound.get(m.login.trim().toLowerCase());
return userId ? { ...m, userId } : m;
}),
};
}
/**
* Bind the signed-in user's id to the member entries that name them only by
* login, so a later owner of that login (after a rename) never inherits the
* membership. Best-effort; each group is a compare-and-swap.
*/
export async function bindMemberIds(
kv: KVNamespace,
groups: Group[],
userId: string,
login: string,
): Promise<void> {
const unbound = (g: Group): boolean =>
normalizeGroupMembers(g).some(
(m) => !m.userId && !/^\d+$/.test(m.login) && memberMatches(m, userId, login),
);
for (const group of groups.filter(unbound)) {
await updateGroup(kv, group.id, (g) => {
if (!unbound(g)) return null;
const members = normalizeGroupMembers(g).map((m) =>
!m.userId && !/^\d+$/.test(m.login) && memberMatches(m, userId, login)
? { ...m, userId }
: m,
);
return { ...g, members };
});
}
}
export function isGroupAdmin(group: Group, userId: string, login: string): boolean {
@@ -89,12 +204,8 @@ export function isGroupAdmin(group: Group, userId: string, login: string): boole
* `adminIds` groups resolve to "owner" for every listed admin.
*/
export function memberRole(group: Group, userId: string, login: string): GroupRole | undefined {
const members = normalizeGroupMembers(group);
const wanted = members.filter((m) => {
const candidate = m.login.trim().toLowerCase();
return candidate === login.toLowerCase() || candidate === userId;
});
return wanted.length > 0 ? wanted[0]!.role : undefined;
const member = normalizeGroupMembers(group).find((m) => memberMatches(m, userId, login));
return member?.role;
}
/**
@@ -130,6 +241,27 @@ export function groupAcceptsInstallation(group: Group, installationId?: number):
return installationId != null && group.installationId === installationId;
}
/**
* Whether the group may receive an event that arrived on the legacy global
* endpoint (`POST /webhook`). Isolated groups (self-service personal groups)
* only take events from their own `/webhook/{groupId}` ingress, unless a super
* admin bound them to a source: a GitHub App installation or an `owners` list.
*/
/**
* Whether a group is isolated. Personal groups (`u-{userId}`) created before
* the flag existed count as isolated too, unless a super admin explicitly set
* `isolated: false`.
*/
export function isIsolated(group: Group): boolean {
return group.isolated ?? /^u-\d+$/.test(group.id);
}
export function groupAcceptsGlobalIngress(group: Group): boolean {
const isolated = isIsolated(group);
if (!isolated) return true;
return group.installationId != null || (group.owners ?? []).some((o) => o.trim().length > 0);
}
/**
* Auto-provision a GitHub App installation on `installation.created`:
* 1. No-op when a group is already bound to this installation id.
@@ -137,63 +269,103 @@ export function groupAcceptsInstallation(group: Group, installationId?: number):
* account login (so existing org groups light up automatically).
* 3. Otherwise create a dedicated `inst-{installationId}` group bound to the
* installation. Returns the group that now owns the installation.
*
* Every write is an atomic create-if-absent or compare-and-swap update, so
* concurrent deliveries converge on the same group without a lock.
*/
/** An automatic installation binding is remembered as long as delivery state (7 days). */
const AUTO_BOUND_TTL_SECONDS = 7 * 86400;
export async function ensureInstallationGroup(
kv: KVNamespace,
installationId: number,
accountLogin: string,
db?: D1Database,
): Promise<Group | null> {
// A per-installation lock serializes concurrent `installation.created`
// webhooks: without it two requests both pass the "no group bound" check,
// both create `inst-{id}`, and the second save overwrites the first.
const lockKey = `inst:lock:${installationId}`;
for (let attempt = 0; attempt < 5; attempt++) {
const holder = await kv.get(lockKey);
if (!holder) break;
const groups = await loadGroups(kv);
const existing = groups.find((g) => g.installationId === installationId);
if (existing) return existing;
await new Promise((r) => setTimeout(r, 40 * (attempt + 1)));
}
// Each automatic binding is recorded (per installation and group) in the
// transaction that makes it: a retry binds the groups an earlier attempt
// did not reach, never again one it bound — an admin may have undone that.
const claims = db ? idempotencyStore(db, kv) : null;
const boundKey = (g: Group): string =>
`installation-bound:${installationId}:${g.instance ?? g.id}`;
const groups = await loadGroups(kv, { fresh: true });
// Already bound somewhere (an earlier attempt): the matching groups still
// unbound are bound all the same — that attempt may have failed midway.
const existing = groups.find((g) => g.installationId === installationId);
let locked = false;
try {
await kv.put(lockKey, "1", { expirationTtl: 60 });
locked = true;
const groups = await loadGroups(kv);
const existing = groups.find((g) => g.installationId === installationId);
if (existing) return existing;
const login = accountLogin.trim().toLowerCase();
const candidates = groups.filter(
const login = accountLogin.trim().toLowerCase();
const candidates = groups.filter(
(g) =>
g.installationId == null &&
login.length > 0 &&
(g.owners ?? []).some((o) => o.trim().toLowerCase() === login),
);
let bound: Group | null = null;
for (const candidate of candidates) {
if (claims && (await claims.has(boundKey(candidate)))) continue;
const key = boundKey(candidate);
const now = Date.now();
// Re-checked on the group as it is at the write: the same group (not one
// that took over the id), still unbound, still owned by this account —
// an `owners` list cleared meanwhile must not be reopened by a binding.
const updated = await updateGroup(
kv,
candidate.id,
(g) =>
g.instance === candidate.instance &&
g.installationId == null &&
login.length > 0 &&
(g.owners ?? []).some((o) => o.trim().toLowerCase() === login),
);
if (candidates.length > 0) {
const next = groups.map((g) => (candidates.includes(g) ? { ...g, installationId } : g));
await saveGroups(kv, next);
return next.find((g) => g.id === candidates[0]!.id) ?? null;
(g.owners ?? []).some((o) => o.trim().toLowerCase() === login)
? { ...g, installationId }
: null,
db && canUseD1(db)
? {
// Checked again at the write: an attempt that read before another
// one bound (and an admin unbound) the group never binds it again.
guards: (d): D1PreparedStatement[] => [unrecordedGuard(d, key)],
// Recorded only when the binding was written (the statement before).
after: (d): D1PreparedStatement[] => [
d
.prepare(
`INSERT INTO dedup_keys (key, claimed_at, expires_at) SELECT ?, ?, ? WHERE changes() > 0
ON CONFLICT(key) DO UPDATE SET
claimed_at = excluded.claimed_at, expires_at = excluded.expires_at`,
)
.bind(key, now, now + AUTO_BOUND_TTL_SECONDS * 1000),
],
}
: undefined,
).catch((err: unknown) => {
// Refused at the write (bound before by another attempt, or still
// contended): this group is left as it is.
if (err instanceof ConfigConflictError) return null;
throw err;
});
if (updated?.installationId === installationId) {
bound ??= updated;
// Without D1 transactions, recorded right after (best effort).
if (claims && !(db && canUseD1(db))) await claims.claim(key, AUTO_BOUND_TTL_SECONDS);
}
const gid = `inst-${installationId}`;
const dedicated = groups.find((g) => g.id === gid);
const group: Group = {
id: gid,
name: accountLogin.trim() || `Installation ${installationId}`,
adminIds: [],
installationId,
};
await saveGroups(
kv,
dedicated ? groups.map((g) => (g.id === gid ? { ...g, ...group } : g)) : [...groups, group],
);
return group;
} finally {
if (locked) await kv.delete(lockKey);
}
if (existing) return existing;
if (bound) return bound;
const gid = `inst-${installationId}`;
const group: Group = {
id: gid,
name: accountLogin.trim() || `Installation ${installationId}`,
adminIds: [],
installationId,
};
if (await createGroup(kv, group)) {
// As stored (with the instance the server gave it), for its audit.
return (await loadGroups(kv, { fresh: true })).find((g) => g.id === gid) ?? group;
}
// The id exists already: only a group already bound to this installation
// (a concurrent delivery) is used. An unbound group that merely carries the
// name is never bound — its members did not prove they own the installation.
const current = (await loadGroups(kv, { fresh: true })).find((g) => g.id === gid);
return current?.installationId === installationId ? current : null;
}
export interface AccessScope {
+407 -79
View File
@@ -1,4 +1,21 @@
import { loadGroups, saveGroups, identityMatches, normalizeGroupMembers } from "./groups";
import type { Env, Group } from "../types";
import {
loadGroups,
updateGroup,
memberMatches,
memberRole,
memberRoleStatement,
normalizeGroupMembers,
} from "./groups";
import {
acceptedKey,
claimOnce,
confirmClaim,
idempotencyStore,
recordStatement,
} from "../lib/idempotency";
import { canUseD1 } from "../storage/d1";
import { ConfigConflictError } from "../storage/config-store";
import { log } from "../lib/log";
export interface Invite {
@@ -8,9 +25,46 @@ export interface Invite {
expiresAt: number;
createdBy: string;
note?: string;
/**
* The invited group's immutable `instance` id. A group later re-created
* under the same id has another instance, so the invite never admits anyone
* to it. Absent on invites created before instances existed.
*/
groupInstance?: string;
/**
* Who issued it: a super admin, or a member who must still be an owner of
* the group when the invite is used — an owner removed meanwhile cannot
* have it let anyone in. Absent on older invites.
*/
issuer?: { super: true } | { userId: string; login: string };
}
const INVITE_TTL = 24 * 3600;
/** The invite belongs to an earlier group that had the same id. */
class StaleInviteError extends Error {}
/**
* The login is still on a member entry bound to another account (a member
* who renamed since). Adding a second entry with that login would be dropped
* as a duplicate — a "successful" join without access — so the owner must
* clear the stale entry first.
*/
class MemberConflictError extends Error {}
function revokedKey(token: string): string {
return `invite-revoked:${token}`;
}
/** How long an invite stays claimed while one acceptance is being processed. */
const INVITE_CLAIM_SECONDS = 120;
/** Invites are valid for seven days; their KV keys live exactly that long. */
export const INVITE_LIFETIME_MS = 7 * 86400_000;
const INDEX_TTL = INVITE_LIFETIME_MS / 1000;
/** KV TTL (seconds) for an invite expiring at `expiresAt` (KV minimum: 60). */
function ttlFor(expiresAt: number): number {
return Math.max(60, Math.ceil((expiresAt - Date.now()) / 1000));
}
function generateInviteToken(): string {
const bytes = new Uint8Array(32);
@@ -33,91 +87,208 @@ function indexKey(groupId: string): string {
return `invite:group:${groupId}`;
}
/**
* Tokens of a group's index. A failed read propagates: taken for an empty
* index, it would be written back without the existing tokens, leaving those
* invites valid but unlisted (and so impossible to revoke from the console).
*/
async function readIndex(kv: KVNamespace, groupId: string): Promise<string[]> {
try {
const raw = await kv.get<string[]>(indexKey(groupId), "json");
return Array.isArray(raw) ? raw : [];
} catch (err) {
log.warn({ err, groupId }, "Failed to read invite index");
return [];
}
const raw = await kv.get<string[]>(indexKey(groupId), "json");
return Array.isArray(raw) ? raw : [];
}
async function writeIndex(kv: KVNamespace, groupId: string, tokens: string[]): Promise<void> {
await kv.put(indexKey(groupId), JSON.stringify(tokens), { expirationTtl: INVITE_TTL });
await kv.put(indexKey(groupId), JSON.stringify(tokens), { expirationTtl: INDEX_TTL });
}
async function removeFromIndex(kv: KVNamespace, groupId: string, token: string): Promise<void> {
const tokens = (await readIndex(kv, groupId)).filter((t) => t !== token);
await writeIndex(kv, groupId, tokens);
/**
* Per-invite membership key, written on its own (no read-modify-write): two
* invites created at once can both lose their index entry to the other's
* index write, but never their marker. Listing reads both — the index for
* fresh writes, the markers (an eventually consistent `list`) for durability.
*/
function markerPrefix(groupId: string): string {
return `invite:by-group:${groupId}:`;
}
function listMarkers(kv: KVNamespace, groupId: string): Promise<string[]> {
return listPrefix(kv, markerPrefix(groupId));
}
/**
* Per-invite key under the issuing group's instance: it never moves, so the
* group finds its invites even when moving them to a new id after a rename
* failed halfway.
*/
function instancePrefix(instance: string): string {
return `invite:by-instance:${instance}:`;
}
async function listPrefix(kv: KVNamespace, prefix: string): Promise<string[]> {
const tokens: string[] = [];
let cursor: string | undefined;
do {
const page = await kv.list({ prefix, cursor });
for (const k of page.keys) {
if (k.name.startsWith(prefix)) tokens.push(k.name.slice(prefix.length));
}
cursor = page.list_complete ? undefined : (page as { cursor?: string }).cursor;
} while (cursor);
return tokens;
}
/** Every token listed for a group: by its index, its id markers or its instance markers. */
async function groupTokens(kv: KVNamespace, groupId: string, instance?: string): Promise<string[]> {
const [indexed, marked, owned] = await Promise.all([
readIndex(kv, groupId),
listMarkers(kv, groupId),
instance ? listPrefix(kv, instancePrefix(instance)) : Promise.resolve([]),
]);
return [...new Set([...indexed, ...marked, ...owned])];
}
/**
* Write the index without the given tokens — only when it lists any of them:
* KV writes are the scarcest quota (Workers Free), and an unchanged index
* needs no rewrite.
*/
async function dropFromIndex(kv: KVNamespace, groupId: string, gone: string[]): Promise<void> {
const current = await readIndex(kv, groupId);
const kept = current.filter((t) => !gone.includes(t));
if (kept.length !== current.length) await writeIndex(kv, groupId, kept);
}
/** Remove every listing link of an invite that is gone (index, id and instance markers). */
async function removeFromIndex(kv: KVNamespace, invite: Invite, token: string): Promise<void> {
await kv.delete(`${markerPrefix(invite.groupId)}${token}`);
if (invite.groupInstance) await kv.delete(`${instancePrefix(invite.groupInstance)}${token}`);
await dropFromIndex(kv, invite.groupId, [token]);
}
export async function createInvite(kv: KVNamespace, invite: Invite): Promise<string> {
const token = generateInviteToken();
await kv.put(inviteKey(token), JSON.stringify(invite), { expirationTtl: INVITE_TTL });
const ttl = { expirationTtl: ttlFor(invite.expiresAt) };
await kv.put(inviteKey(token), JSON.stringify(invite), ttl);
await kv.put(`${markerPrefix(invite.groupId)}${token}`, "1", ttl);
if (invite.groupInstance) {
await kv.put(`${instancePrefix(invite.groupInstance)}${token}`, "1", ttl);
}
const tokens = await readIndex(kv, invite.groupId);
tokens.push(token);
await writeIndex(kv, invite.groupId, tokens);
return token;
}
/** The invite, or null when it does not exist (any more). Read errors propagate. */
export async function getInvite(kv: KVNamespace, token: string): Promise<Invite | null> {
try {
const raw = await kv.get<Invite>(inviteKey(token), "json");
if (!raw) return null;
if (Date.now() > raw.expiresAt) {
await kv.delete(inviteKey(token));
await removeFromIndex(kv, raw.groupId, token);
return null;
}
return raw;
} catch (err) {
log.warn({ err }, "Failed to load invite");
const raw = await kv.get<Invite>(inviteKey(token), "json");
if (!raw) return null;
if (Date.now() > raw.expiresAt) {
await kv.delete(inviteKey(token));
await removeFromIndex(kv, raw, token);
return null;
}
return raw;
}
export async function consumeInvite(kv: KVNamespace, token: string): Promise<void> {
const raw = await kv.get<Invite>(inviteKey(token), "json");
await kv.delete(inviteKey(token));
if (raw) await removeFromIndex(kv, raw.groupId, token);
if (raw) await removeFromIndex(kv, raw, token);
}
/** All pending (unexpired) invites of a group, via the per-group index. */
/**
* Whether an invite was issued by this very group, not by an earlier group
* that had the same id (its index may outlive a failed cleanup). An invite from
* before instances belongs to the group that had its id back then (`legacyId`).
*/
function issuedBy(
invite: Invite,
group: Pick<Group, "instance" | "legacyId"> | undefined,
): boolean {
if (!group?.instance) return true;
if (invite.groupInstance) return invite.groupInstance === group.instance;
return group.legacyId === invite.groupId;
}
/**
* All pending (unexpired) invites of a group, via the per-group index. With
* the group given, invites of an earlier group under the same id are left
* out (and removed): their links must not reach the new group's owners.
*/
export async function listInvites(
kv: KVNamespace,
groupId: string,
group?: Pick<Group, "instance" | "legacyId">,
): Promise<Array<Invite & { token: string }>> {
try {
const tokens = await readIndex(kv, groupId);
const out: Array<Invite & { token: string }> = [];
const stale: string[] = [];
for (const token of tokens) {
const invite = await getInvite(kv, token);
if (invite && invite.groupId === groupId) {
out.push({ ...invite, token });
} else {
stale.push(token);
// Read errors propagate (the console shows a failure, not "no invites"),
// and only tokens whose invite is known to be gone are pruned.
// A pre-instance group may still have invites filed under the id it had
// back then (a rename whose invite move failed).
const formerId = group?.legacyId && group.legacyId !== groupId ? group.legacyId : undefined;
const tokens = [
...new Set([
...(await groupTokens(kv, groupId, group?.instance)),
...(formerId ? await groupTokens(kv, formerId) : []),
]),
];
const out: Array<Invite & { token: string }> = [];
const stale: string[] = [];
for (const token of tokens) {
const invite = await getInvite(kv, token);
// Also the group's own invites still filed under a former id (a rename
// whose invite move failed): they are usable, so they must be listed.
const ours =
invite != null &&
issuedBy(invite, group) &&
(invite.groupId === groupId ||
(group?.instance != null && invite.groupInstance === group.instance) ||
(formerId != null && !invite.groupInstance && invite.groupId === formerId));
if (invite && ours) {
out.push({ ...invite, token });
} else {
// Not this group's (gone, or issued by a former holder of the id — which
// may live on under another id): only the listing link to this id goes;
// the invite itself is not ours to revoke and expires on its own.
await kv.delete(`${markerPrefix(groupId)}${token}`);
// A gone invite's marker under this group's instance goes too, or every
// later listing would find (and clean up after) it again until it expires.
if (!invite && group?.instance) {
await kv.delete(`${instancePrefix(group.instance)}${token}`);
}
stale.push(token);
}
if (stale.length > 0) {
await writeIndex(
kv,
groupId,
tokens.filter((t) => !stale.includes(t)),
);
}
return out;
} catch (err) {
log.warn({ err }, "Failed to list invites");
return [];
}
// Re-read: an invite created meanwhile must stay listed.
if (stale.length > 0) await dropFromIndex(kv, groupId, stale);
return out;
}
export async function revokeInvite(kv: KVNamespace, token: string): Promise<void> {
/**
* Revoke an invite. With `db`, the revocation is first recorded in the
* idempotency store (for the invite's lifetime): an acceptance already past
* its KV read checks it inside its member write, so it cannot still join.
*/
export async function revokeInvite(
kv: KVNamespace,
token: string,
db?: D1Database,
guards?: (db: D1Database) => D1PreparedStatement[],
): Promise<void> {
const raw = await kv.get<Invite>(inviteKey(token), "json");
if (db) {
const ttl = ttlFor(raw?.expiresAt ?? Date.now() + INVITE_LIFETIME_MS);
if (guards && canUseD1(db)) {
// The revocation record commits only while the guards hold (e.g. the
// revoking owner still is one): a request authorized before a role
// change can no longer act after it.
await db.batch([...guards(db), recordStatement(db, revokedKey(token), ttl)]);
} else {
await idempotencyStore(db, kv).claim(revokedKey(token), ttl);
}
}
await kv.delete(inviteKey(token));
if (raw) await removeFromIndex(kv, raw.groupId, token);
if (raw) await removeFromIndex(kv, raw, token);
}
/**
@@ -125,9 +296,14 @@ export async function revokeInvite(kv: KVNamespace, token: string): Promise<void
* Best-effort: a failure leaves the old invites in place (they will be
* rejected as group-missing after the rename).
*/
export async function migrateInvites(kv: KVNamespace, from: string, to: string): Promise<void> {
export async function migrateInvites(
kv: KVNamespace,
from: string,
to: string,
group?: Pick<Group, "instance" | "legacyId">,
): Promise<void> {
try {
const tokens = await readIndex(kv, from);
const tokens = await groupTokens(kv, from);
if (tokens.length === 0) {
await kv.delete(indexKey(from));
return;
@@ -135,59 +311,211 @@ export async function migrateInvites(kv: KVNamespace, from: string, to: string):
const moved: string[] = [];
for (const token of tokens) {
const invite = await kv.get<Invite>(inviteKey(token), "json");
if (invite && invite.groupId === from) {
await kv.put(inviteKey(token), JSON.stringify({ ...invite, groupId: to }), {
expirationTtl: INVITE_TTL,
});
// Only the renamed group's own invites move (a former group's leftovers
// stay behind); they get its instance, so they keep working under the
// new id even when they date from before instances.
if (invite && invite.groupId === from && issuedBy(invite, group)) {
const groupInstance = invite.groupInstance ?? group?.instance;
const ttl = { expirationTtl: ttlFor(invite.expiresAt) };
await kv.put(
inviteKey(token),
JSON.stringify({ ...invite, groupId: to, groupInstance }),
ttl,
);
await kv.put(`${markerPrefix(to)}${token}`, "1", ttl);
if (groupInstance) await kv.put(`${instancePrefix(groupInstance)}${token}`, "1", ttl);
await kv.delete(`${markerPrefix(from)}${token}`);
moved.push(token);
}
}
await kv.put(indexKey(to), JSON.stringify(moved), { expirationTtl: INVITE_TTL });
const kept = (await readIndex(kv, to)).filter((t) => !moved.includes(t));
await kv.put(indexKey(to), JSON.stringify([...kept, ...moved]), {
expirationTtl: INDEX_TTL,
});
await kv.delete(indexKey(from));
} catch (err) {
log.warn({ err, from, to }, "Failed to migrate invites on group rename");
}
}
/** Revoke every pending invite of a deleted group. */
export async function deleteGroupInvites(
kv: KVNamespace,
groupId: string,
group?: Pick<Group, "instance" | "legacyId">,
): Promise<void> {
const instance = group?.instance;
try {
for (const token of await groupTokens(kv, groupId, instance)) {
// Only the deleted group's own invites are revoked: one listed under
// its id may have been issued by a group that lives on under another.
const invite = await kv.get<Invite>(inviteKey(token), "json");
if (
invite &&
issuedBy(invite, group) &&
(invite.groupId === groupId || (instance != null && invite.groupInstance === instance))
) {
await kv.delete(inviteKey(token));
}
await kv.delete(`${markerPrefix(groupId)}${token}`);
if (instance) await kv.delete(`${instancePrefix(instance)}${token}`);
}
await kv.delete(indexKey(groupId));
} catch (err) {
log.warn({ err, groupId }, "Failed to delete invites of a removed group");
}
}
/**
* Adds the accepting user to the invited group (or upgrades their role when
* they are already a viewer) and consumes the invite. The invited role is
* never an owner — ownership stays with the inviter's discretion.
*
* The invite is claimed atomically before the membership changes, so two
* people accepting the same link at once cannot both join; the member write is
* a compare-and-swap on the group, so it never reverts a concurrent edit.
*/
export async function acceptInvite(
kv: KVNamespace,
env: Pick<Env, "KV" | "DB">,
token: string,
userId: string,
login: string,
/** The login identity checks may use ("" when it is not trusted as this user's). */
matchLogin = login,
): Promise<
| { ok: true; groupId: string; role: "admin" | "viewer" }
| { ok: false; reason: "invalid" | "group-missing" }
| { ok: true; groupId: string; groupInstance?: string; role: "admin" | "viewer" }
| { ok: false; reason: "invalid" | "group-missing" | "conflict" }
> {
const kv = env.KV;
const invite = await getInvite(kv, token);
if (!invite) return { ok: false, reason: "invalid" };
const groups = await loadGroups(kv);
const group = groups.find((g) => g.id === invite.groupId);
if (!group) return { ok: false, reason: "group-missing" };
const members = normalizeGroupMembers(group);
const idx = members.findIndex((m) => identityMatches([m.login], userId, login));
if (idx >= 0) {
if (invite.role === "admin" && members[idx]!.role === "viewer") {
members[idx]!.role = "admin";
}
} else {
members.push({ login, role: invite.role });
const claims = idempotencyStore(env.DB, kv);
const claimKey = `invite-used:${token}`;
// A short claim while joining, confirmed for the invite's lifetime once
// used: an attempt that fails and cannot give the claim back blocks the
// invite for a couple of minutes, not until it expires.
if ((await claimOnce(claims, claimKey, INVITE_CLAIM_SECONDS)) !== "claimed") {
return { ok: false, reason: "invalid" };
}
const next = groups.map((g) =>
g.id === group.id
? {
...g,
if (await claims.has(revokedKey(token))) {
await claims.release(claimKey).catch(() => undefined);
return { ok: false, reason: "invalid" };
}
let joined = false;
let joinedId: string | undefined;
let joinedInstance: string | undefined;
/** The member who issued the invite is no longer an owner (it is void then). */
const issuerGone = async (): Promise<boolean> => {
const by = invite.issuer && "userId" in invite.issuer ? invite.issuer : null;
if (!by) return false;
const group = (await loadGroups(kv, { fresh: true })).find((g) =>
invite.groupInstance ? g.instance === invite.groupInstance : g.legacyId === invite.groupId,
);
return !group || memberRole(group, by.userId, by.login) !== "owner";
};
try {
// The invited group is the instance the invite was issued for, under
// whatever id it has now. An invite from before instances only applies to
// a group that existed back then, never to one created since.
const groups = await loadGroups(kv, { fresh: true });
const target = invite.groupInstance
? groups.find((g) => g.instance === invite.groupInstance)
: groups.find((g) => g.legacyId === invite.groupId);
// Its group instance is gone for good: the invite can never be used.
if (!target && invite.groupInstance) throw new StaleInviteError();
if (!target) return { ok: false, reason: "group-missing" };
// The invite is confirmed as used in the same transaction that adds the
// member — only when that write went through (`changes()`: the member
// update right before; a lost compare-and-swap changes nothing) and the
// member is there after it: no crash between the two can leave a member
// added and the invite still open, nor a retried write consume it unused.
const confirmWithJoin = (db: D1Database): D1PreparedStatement[] => [
db
.prepare(
`INSERT INTO dedup_keys (key, claimed_at, expires_at)
SELECT ?, ?, ? WHERE changes() > 0 AND EXISTS (
SELECT 1 FROM (SELECT id, CASE WHEN json_valid(data) THEN data ELSE '{}' END AS data FROM d1_groups) g, json_each(g.data, '$.members') m
WHERE g.id = ? AND json_extract(m.value, '$.userId') = ?)
ON CONFLICT(key) DO UPDATE SET expires_at = excluded.expires_at`,
)
.bind(acceptedKey(claimKey), Date.now(), invite.expiresAt, target.id, userId),
];
// And it only commits while the invite is unused and not revoked: an
// earlier acceptance whose answer was lost (its claim given back, its use
// committed), or a revocation that landed after the KV read, aborts it.
const issuer = invite.issuer && "userId" in invite.issuer ? invite.issuer : null;
const unusedGuard = (db: D1Database): D1PreparedStatement[] => [
db
.prepare(
"SELECT CASE WHEN EXISTS (SELECT 1 FROM dedup_keys WHERE key IN (?, ?) AND expires_at > ?) THEN json('invite used') ELSE 1 END AS ok",
)
.bind(acceptedKey(claimKey), revokedKey(token), Date.now()),
...(issuer
? [memberRoleStatement(db, target.id, issuer.userId, issuer.login, ["owner"])]
: []),
];
const updated = await updateGroup(
kv,
target.id,
(group) => {
const sameInstance = invite.groupInstance
? group.instance === invite.groupInstance
: group.legacyId === invite.groupId;
if (!sameInstance) throw new StaleInviteError();
const members = normalizeGroupMembers(group);
const idx = members.findIndex((m) => memberMatches(m, userId, matchLogin));
if (idx >= 0) {
if (invite.role === "admin" && members[idx]!.role === "viewer") {
members[idx]!.role = "admin";
}
members[idx]!.userId ??= userId;
} else {
if (members.some((m) => m.login.toLowerCase() === login.toLowerCase())) {
throw new MemberConflictError();
}
// Bound to the account that accepted, not to its (renamable) login.
members.push({ login, role: invite.role, userId });
}
return {
...group,
members,
adminIds: members.filter((m) => m.role === "owner").map((m) => m.login),
}
: g,
};
},
{ guards: unusedGuard, after: confirmWithJoin },
);
joined = updated != null;
joinedId = updated?.id;
joinedInstance = updated?.instance;
} catch (err) {
// The invite stays usable once the owner resolved the conflict.
if (err instanceof MemberConflictError) return { ok: false, reason: "conflict" };
// Used meanwhile (by an acceptance whose answer was lost).
if (
err instanceof ConfigConflictError &&
((await claims.has(acceptedKey(claimKey))) ||
(await claims.has(revokedKey(token))) ||
(await issuerGone()))
) {
return { ok: false, reason: "invalid" };
}
if (!(err instanceof StaleInviteError)) throw err;
await consumeInvite(kv, token);
return { ok: false, reason: "invalid" };
} finally {
if (!joined) await claims.release(claimKey).catch(() => undefined);
}
if (!joined) return { ok: false, reason: "group-missing" };
await confirmClaim(claims, claimKey, ttlFor(invite.expiresAt)).catch((err: unknown) =>
log.warn({ err }, "Failed to confirm an invite claim"),
);
await saveGroups(kv, next);
await consumeInvite(kv, token);
return { ok: true, groupId: group.id, role: invite.role };
return {
ok: true,
groupId: joinedId ?? invite.groupId,
groupInstance: joinedInstance,
role: invite.role,
};
}
+316 -111
View File
@@ -11,19 +11,32 @@ import {
import {
getOAuthURL,
handleOAuthCallback as handleGithubOAuthCallback,
getInstallationAccount,
getInstallationDetails,
userManagesInstallation,
} from "../github/oauth";
import { removeToken, saveDiscordLink, saveTelegramLink, saveFeishuLink } from "../github/store";
import { createAdminSession, adminCookie, getAdminSession } from "./session";
import { getToken, linkStatement, removeToken } from "../github/store";
import {
createAdminSession,
adminCookie,
getAdminSession,
isAdminUser,
authLogin,
claimLogin,
} from "./session";
import {
loadGroups,
saveGroups,
createGroup,
updateGroup,
resolveScope,
hasAnyAccess,
ensureInstallationGroup,
normalizeGroupMembers,
roleAt,
bindMemberIds,
memberRole,
} from "./groups";
import { log } from "../lib/log";
import { idempotencyStore, recordStatement, unrecordedGuard } from "../lib/idempotency";
import { clientIp } from "./auth";
import { recordAudit } from "../lib/audit";
import { sendMessage } from "../drivers/telegram/rest";
@@ -32,9 +45,22 @@ import { cfEnv } from "../cf";
import { initConfigStore } from "../config";
import type { Env, Group } from "../types";
/** Which chat account a bot `/gh login` link binds, with a name to show. */
interface PendingLink {
platform: "discord" | "telegram" | "feishu";
userId: string;
/** Display name of the chat account, shown on the confirmation page. */
name?: string;
/** Chat to notify once the link is confirmed (Telegram / Feishu). */
chatId?: string;
}
interface PendingState {
redirectTo: string;
expiresAt: number;
/** Set when the flow was started by a bot command instead of a browser login. */
link?: PendingLink;
// Legacy shapes of `link`, still accepted for states created before it.
discordUserId?: string;
telegramUserId?: string;
telegramChatId?: string;
@@ -42,8 +68,75 @@ interface PendingState {
feishuChatId?: string;
}
function linkedPage(login: string): string {
return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>绑定成功</title><style>body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;font-family:system-ui,-apple-system,'Segoe UI',sans-serif;background:#f6f7f9;color:#1f2328}.card{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:32px 40px;text-align:center;box-shadow:0 1px 3px rgba(0,0,0,.06)}.ok{color:#16a34a;font-size:40px}h1{font-size:18px;margin:12px 0 4px}p{color:#57606a;font-size:14px;margin:0}</style></head><body><div class="card"><div class="ok">✓</div><h1>GitHub 账号已绑定</h1><p>已连接为 <b>@${login}</b>,现在可以回到 Discord 用 GitHub 评论了。</p></div></body></html>`;
/** A confirmed OAuth identity waiting for the user to approve a bot link. */
interface LinkConfirmation extends PendingLink {
githubUserId: string;
login: string;
}
const STATE_COOKIE = "wh_oauth_state";
const STATE_TTL = 600;
const LINK_TTL = 600;
function stateCookie(state: string): string {
return `${STATE_COOKIE}=${state}; Path=/auth/github; HttpOnly; Secure; SameSite=Lax; Max-Age=${STATE_TTL}`;
}
function clearStateCookie(): string {
return `${STATE_COOKIE}=; Path=/auth/github; HttpOnly; Secure; SameSite=Lax; Max-Age=0`;
}
function cookieValue(header: string | undefined, name: string): string | undefined {
for (const part of (header ?? "").split(";")) {
const idx = part.indexOf("=");
if (idx !== -1 && part.slice(0, idx).trim() === name) return part.slice(idx + 1).trim();
}
return undefined;
}
function pendingLink(pending: PendingState): PendingLink | null {
if (pending.link) return pending.link;
if (pending.discordUserId) return { platform: "discord", userId: pending.discordUserId };
if (pending.telegramUserId) {
return {
platform: "telegram",
userId: pending.telegramUserId,
chatId: pending.telegramChatId,
};
}
if (pending.feishuUserId) {
return { platform: "feishu", userId: pending.feishuUserId, chatId: pending.feishuChatId };
}
return null;
}
const PAGE_STYLE =
"body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;font-family:system-ui,-apple-system,'Segoe UI',sans-serif;background:#f6f7f9;color:#1f2328}.card{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:28px 32px;width:min(480px,92vw);box-sizing:border-box;text-align:center;box-shadow:0 1px 3px rgba(0,0,0,.06)}.ok{color:#16a34a;font-size:40px}h1{font-size:18px;margin:12px 0 6px}p{color:#57606a;font-size:14px;margin:6px 0}b{color:#1f2328}.btn{display:inline-block;margin-top:16px;background:#1f2328;color:#fff;border:0;border-radius:8px;padding:10px 18px;font-size:14px;cursor:pointer}.btn:hover{background:#32383f}.warn{color:#b45309;font-size:13px}";
const PLATFORM_NAMES: Record<PendingLink["platform"], string> = {
discord: "Discord",
telegram: "Telegram",
feishu: "飞书",
};
function linkedPage(login: string, platform: PendingLink["platform"]): string {
return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>绑定成功</title><style>${PAGE_STYLE}</style></head><body><div class="card"><div class="ok">✓</div><h1>GitHub 账号已绑定</h1><p>已连接为 <b>@${escapeHtml(login)}</b>,现在可以回到 ${PLATFORM_NAMES[platform]} 用 GitHub 评论了。</p></div></body></html>`;
}
/**
* Asks the GitHub user to approve the binding before it happens: a `/gh
* login` link may have been forwarded, so the page names both accounts and
* nothing is linked until this user confirms.
*/
function confirmLinkPage(nonce: string, link: LinkConfirmation): string {
const chatAccount = link.name
? `${escapeHtml(link.name)}(ID ${escapeHtml(link.userId)})`
: `ID ${escapeHtml(link.userId)}`;
return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>确认绑定</title><style>${PAGE_STYLE}</style></head><body><div class="card"><h1>确认绑定 GitHub 账号</h1><p>将 GitHub 账号 <b>@${escapeHtml(link.login)}</b></p><p>绑定到 ${PLATFORM_NAMES[link.platform]} 账号 <b>${chatAccount}</b>?</p><p class="warn">绑定后,该 ${PLATFORM_NAMES[link.platform]} 账号可以用你的 GitHub 身份评论、合并或关闭 PR。若这个链接不是你本人发起的,请直接关闭本页。</p><form method="post" action="/auth/github/link"><input type="hidden" name="nonce" value="${escapeHtml(nonce)}"><button class="btn" type="submit">确认绑定</button></form></div></body></html>`;
}
function messagePage(title: string, text: string): string {
return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escapeHtml(title)}</title><style>${PAGE_STYLE}</style></head><body><div class="card"><h1>${escapeHtml(title)}</h1><p>${escapeHtml(text)}</p></div></body></html>`;
}
function generateRandomHex(length: number): string {
@@ -54,12 +147,26 @@ function generateRandomHex(length: number): string {
.join("");
}
function safeRedirectPath(value: string | undefined): string {
export function safeRedirectPath(value: string | undefined): string {
if (!value) return "/";
if (!value.startsWith("/")) return "/";
if (value.startsWith("//")) return "/";
if (/^\/\\/.test(value)) return "/";
return value;
// Browsers drop tabs and line breaks from URLs (`/\t/evil.example` becomes
// `//evil.example`): refused, and the path is checked as a browser reads
// it — it must stay on this origin, and only that normalized path is used.
if (/[\u0000-\u001f\u007f]/.test(value)) return "/";
const base = "https://webhooker.invalid";
let url: URL;
try {
url = new URL(value, base);
} catch {
return "/";
}
// Dot segments can normalize into `//host` (`/a/..//evil.example`): read
// again, that would leave the site.
if (url.origin !== base || url.pathname.startsWith("//")) return "/";
return `${url.pathname}${url.search}${url.hash}`;
}
function escapeHtml(s: string): string {
@@ -79,19 +186,22 @@ function selfSignupEnabled(env: Env): boolean {
* Opt-in self service: users without any group access get a personal group
* they own, so they can configure their own routing without a super admin.
* The group id is deterministic (`u-{userId}`), so it is created at most once.
* Personal groups are isolated: only their own `/webhook/{groupId}` ingress
* dispatches to them, never the operator's global endpoint.
*/
async function ensurePersonalGroup(env: Env, userId: string, login: string): Promise<boolean> {
if (!selfSignupEnabled(env)) return false;
const groups = await loadGroups(env.KV);
const gid = `u-${userId}`;
if (groups.some((g) => g.id === gid)) return true;
const personal: Group = {
id: gid,
name: `@${login}`,
members: [{ login, role: "owner" }],
members: [{ login, role: "owner", userId }],
adminIds: [login],
isolated: true,
};
await saveGroups(env.KV, [...groups, personal]);
if (!(await createGroup(env.KV, personal))) return true;
// Filed under the instance the server gave the group (it follows renames).
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === gid);
await recordAudit(env.DB, {
ts: Date.now(),
actorId: userId,
@@ -100,6 +210,7 @@ async function ensurePersonalGroup(env: Env, userId: string, login: string): Pro
targetType: "group",
targetId: gid,
groupId: gid,
groupInstance: stored?.instance,
detail: { auto: true },
});
return true;
@@ -131,6 +242,29 @@ function installPage(opts: {
return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>安装 GitHub App</title><style>body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;font-family:system-ui,-apple-system,'Segoe UI',sans-serif;background:#f6f7f9;color:#1f2328}.card{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:28px 32px;width:min(480px,92vw);box-shadow:0 1px 3px rgba(0,0,0,.06)}h1{font-size:17px;margin:0 0 4px}p{color:#57606a;font-size:13.5px;margin:6px 0}code{background:#f0f1f3;border-radius:4px;padding:1px 5px;font-size:12.5px}.opt{display:flex;align-items:flex-start;gap:8px;padding:9px 10px;border:1px solid #e5e7eb;border-radius:8px;margin-top:8px;cursor:pointer}.opt:hover{background:#fafbfc}.hint{font-size:12.5px;color:#8b949e;margin-top:10px}.btn{display:inline-block;margin-top:14px;background:#1f2328;color:#fff;border:0;border-radius:8px;padding:10px 18px;font-size:14px;cursor:pointer}.btn:hover{background:#32383f}.skip{margin-left:12px;color:#8b949e;font-size:13px;text-decoration:none}</style></head><body><div class="card"><h1>GitHub App 安装成功</h1>${accountLine}<p>将安装绑定到哪个分组?建议直接创建新分组,之后可以在控制台添加路由与成员。</p><form method="post" action="/auth/github/install/bind"><input type="hidden" name="installation_id" value="${installationId}"><label class="opt"><input type="radio" name="group" value="" checked><span><b>创建新分组</b> <code>inst-${installationId}</code></span></label>${ownedNote}${ownedOptions}<button class="btn" type="submit">确定</button></form></div></body></html>`;
}
/**
* Whether the signed-in user may bind this installation. Super admins (the
* operator) may; everyone else must prove access with their own GitHub token,
* because the Setup URL's `installation_id` can be set to anything.
*/
async function canManageInstallation(
env: Env,
session: { userId: string; login: string },
installationId: number,
): Promise<{ ok: boolean; accountLogin: string }> {
const account = await getInstallationDetails(
env.GITHUB_APP_ID ?? "",
env.GITHUB_PRIVATE_KEY ?? "",
installationId,
);
const accountLogin = account?.login ?? "";
if (isAdminUser(env, session.userId, authLogin(session))) return { ok: true, accountLogin };
const token = await getToken(env, session.userId);
if (!token) return { ok: false, accountLogin };
const ok = await userManagesInstallation(token, session.userId, installationId, account);
return { ok, accountLogin };
}
/** GET /auth/github — start the OAuth flow. */
export async function handleOAuthStart(event: H3Event): Promise<void> {
const env = cfEnv(event);
@@ -140,9 +274,12 @@ export async function handleOAuthStart(event: H3Event): Promise<void> {
const state = generateRandomHex(16);
const pending: PendingState = {
redirectTo,
expiresAt: Date.now() + 10 * 60 * 1000,
expiresAt: Date.now() + STATE_TTL * 1000,
};
await env.KV.put(`state:${state}`, JSON.stringify(pending), { expirationTtl: 600 });
await env.KV.put(`state:${state}`, JSON.stringify(pending), { expirationTtl: STATE_TTL });
// Binds the flow to this browser: the callback only signs in the browser
// that started it (no login CSRF with a callback URL from another session).
appendResponseHeader(event, "Set-Cookie", stateCookie(state));
await sendRedirect(event, getOAuthURL(env.GITHUB_CLIENT_ID ?? "", state));
}
@@ -156,29 +293,28 @@ export async function handleInstallPage(event: H3Event): Promise<string | void>
if (!rawId || !Number.isInteger(installationId) || installationId <= 0) {
throw createError({ statusCode: 400, statusMessage: "Missing installation_id" });
}
const session = await getAdminSession(env.KV, getHeader(event, "cookie"));
const session = await getAdminSession(env.KV, getHeader(event, "cookie"), env.DB);
if (!session) {
const target = `/auth/github/install?installation_id=${installationId}`;
await sendRedirect(event, `/auth/github?redirect=${encodeURIComponent(target)}`);
return;
}
const accountLogin =
(await getInstallationAccount(
env.GITHUB_APP_ID ?? "",
env.GITHUB_PRIVATE_KEY ?? "",
installationId,
)) ?? "";
const groups = await loadGroups(env.KV);
const scope = resolveScope(env, groups, session.userId, session.login);
const access = await canManageInstallation(env, session, installationId);
if (!access.ok) {
await sendRedirect(event, "/admin?error=install-forbidden");
return;
}
const groups = await loadGroups(env.KV, { fresh: true });
const scope = resolveScope(env, groups, session.userId, authLogin(session));
const owned = groups.filter((g) => roleAt(scope, g.id) === "owner");
return installPage({ installationId, accountLogin, owned });
return installPage({ installationId, accountLogin: access.accountLogin, owned });
}
/** POST /auth/github/install/bind — provision the chosen binding. */
export async function handleInstallBind(event: H3Event): Promise<void> {
const env = cfEnv(event);
initConfigStore(env);
const session = await getAdminSession(env.KV, getHeader(event, "cookie"));
const session = await getAdminSession(env.KV, getHeader(event, "cookie"), env.DB);
if (!session) {
await sendRedirect(event, "/admin?error=forbidden");
return;
@@ -189,49 +325,60 @@ export async function handleInstallBind(event: H3Event): Promise<void> {
if (!Number.isInteger(installationId) || installationId <= 0) {
throw createError({ statusCode: 400, statusMessage: "Missing installation_id" });
}
const access = await canManageInstallation(env, session, installationId);
if (!access.ok) {
await sendRedirect(event, "/admin?error=install-forbidden");
return;
}
const accountLogin = access.accountLogin;
const chosenGroupId = String(body["group"] ?? "").trim();
const groups = await loadGroups(env.KV);
const scope = resolveScope(env, groups, session.userId, session.login);
const groups = await loadGroups(env.KV, { fresh: true });
const scope = resolveScope(env, groups, session.userId, authLogin(session));
const bind = async (groupId: string, group: Group | null): Promise<void> => {
if (!group) {
if (chosenGroupId) {
// Binding to an existing group requires owner permission on it.
if (!groups.some((g) => g.id === chosenGroupId) || roleAt(scope, chosenGroupId) !== "owner") {
await sendRedirect(event, "/admin?error=forbidden");
return;
}
// Still an owner of the group as it is now (the write is a CAS on it).
let revoked = false;
const bound = await updateGroup(env.KV, chosenGroupId, (g) => {
const owner =
isAdminUser(env, session.userId, authLogin(session)) ||
memberRole(g, session.userId, authLogin(session)) === "owner";
if (!owner) {
revoked = true;
return null;
}
return { ...g, installationId };
});
if (revoked) {
await sendRedirect(event, "/admin?error=forbidden");
return;
}
if (!bound) {
await sendRedirect(event, "/admin?error=install");
return;
}
const next = groups.map((g) => (g.id === group.id ? { ...g, installationId } : g));
await saveGroups(env.KV, next);
await recordAudit(env.DB, {
ts: Date.now(),
actorId: session.userId,
actorLogin: session.login,
action: "installation.bind",
targetType: "group",
targetId: groupId,
groupId,
targetId: chosenGroupId,
groupId: chosenGroupId,
groupInstance: bound.instance,
detail: { installationId },
ip: clientIp(event),
});
await sendRedirect(event, "/admin?install=ok");
};
if (chosenGroupId) {
// Binding to an existing group requires owner permission on it.
const group = groups.find((g) => g.id === chosenGroupId);
if (!group || roleAt(scope, chosenGroupId) !== "owner") {
await sendRedirect(event, "/admin?error=forbidden");
return;
}
return bind(chosenGroupId, group);
return;
}
// Default: auto-create a dedicated inst-{id} group.
const accountLogin =
(await getInstallationAccount(
env.GITHUB_APP_ID ?? "",
env.GITHUB_PRIVATE_KEY ?? "",
installationId,
)) ?? "";
const group = await ensureInstallationGroup(env.KV, installationId, accountLogin);
const group = await ensureInstallationGroup(env.KV, installationId, accountLogin, env.DB);
if (!group) {
await sendRedirect(event, "/admin?error=install");
return;
@@ -244,27 +391,29 @@ export async function handleInstallBind(event: H3Event): Promise<void> {
targetType: "group",
targetId: group.id,
groupId: group.id,
groupInstance: group.instance,
detail: { source: "setup_url", account: accountLogin || undefined },
ip: clientIp(event),
});
// Self-service SaaS: the installer manages their own auto-created group.
if (selfSignupEnabled(env)) {
const members = normalizeGroupMembers(group);
const alreadyMember = members.some(
(m) => m.login.toLowerCase() === session.login.toLowerCase() || m.login === session.userId,
);
if (!alreadyMember) {
const updated: Group = {
...group,
members: [...members, { login: session.login, role: "owner" }],
adminIds: [...new Set([...(group.adminIds ?? []), session.login])],
// Self-service SaaS: the verified installer manages the group created for
// their installation — only the dedicated `inst-{id}` group, only while it is
// still bound to this installation, and only while nobody manages it yet.
// Any other group (an org group bound via `owners`, an operator group, one
// claimed earlier) is never handed to a new owner this way.
if (selfSignupEnabled(env) && group.id === `inst-${installationId}`) {
let added = false;
await updateGroup(env.KV, group.id, (g) => {
const members = normalizeGroupMembers(g);
if (g.installationId !== installationId || members.length > 0) return null;
added = true;
return {
...g,
members: [{ login: session.login, role: "owner", userId: session.userId }],
adminIds: [session.login],
};
const all = await loadGroups(env.KV);
await saveGroups(
env.KV,
all.map((g) => (g.id === group.id ? updated : g)),
);
});
if (added) {
await recordAudit(env.DB, {
ts: Date.now(),
actorId: session.userId,
@@ -273,6 +422,7 @@ export async function handleInstallBind(event: H3Event): Promise<void> {
targetType: "group",
targetId: group.id,
groupId: group.id,
groupInstance: group.instance,
detail: { login: session.login, role: "owner", auto: true },
ip: clientIp(event),
});
@@ -300,12 +450,19 @@ export async function handleOAuthCallback(event: H3Event): Promise<unknown> {
return { error: "Invalid or expired state" };
}
const pending = raw as PendingState;
await env.KV.delete(`state:${state}`);
if (Date.now() > pending.expiresAt) {
await env.KV.delete(`state:${state}`);
setResponseStatus(event, 400);
return { error: "Invalid or expired state" };
}
await env.KV.delete(`state:${state}`);
const link = pendingLink(pending);
// A browser sign-in must come back to the browser that started it. Bot
// links are opened elsewhere by design; they are confirmed explicitly below.
if (!link && cookieValue(getHeader(event, "cookie"), STATE_COOKIE) !== state) {
setResponseStatus(event, 400);
return { error: "Invalid or expired state" };
}
if (!link) appendResponseHeader(event, "Set-Cookie", clearStateCookie());
const result = await handleGithubOAuthCallback(
env.GITHUB_CLIENT_ID ?? "",
@@ -313,47 +470,24 @@ export async function handleOAuthCallback(event: H3Event): Promise<unknown> {
code,
state,
env.KV,
env.DB,
);
if (!result) {
setResponseStatus(event, 400);
return { error: "OAuth failed" };
}
// Discord account-linking flow: bind the Discord user to this GitHub account.
if (pending.discordUserId) {
await saveDiscordLink(env.DB, pending.discordUserId, result.userId);
const isBrowserLink = (getHeader(event, "accept") ?? "").includes("text/html");
if (isBrowserLink) return linkedPage(result.login);
return { ok: true, discordUserId: pending.discordUserId, login: result.login };
}
// Telegram account-linking flow: bind the Telegram user to this GitHub account.
if (pending.telegramUserId) {
await saveTelegramLink(env.DB, pending.telegramUserId, result.userId);
if (pending.telegramChatId && env.TELEGRAM_TOKEN) {
await sendMessage(
env.TELEGRAM_TOKEN,
pending.telegramChatId,
`✅ GitHub 账号已绑定:**@${result.login}**。现在可以用 /gh comment 评论了。`,
).catch(() => undefined);
}
return { ok: true, telegramUserId: pending.telegramUserId, login: result.login };
}
// Feishu account-linking flow: bind the Feishu user to this GitHub account.
if (pending.feishuUserId) {
await saveFeishuLink(env.DB, pending.feishuUserId, result.userId);
if (pending.feishuChatId) {
const tokenRes = await getTenantAccessToken(env);
if (tokenRes.ok && tokenRes.token) {
await sendFeishuText(
tokenRes.token,
pending.feishuChatId,
`✅ GitHub 账号已绑定:**@${result.login}**。现在可以用 /gh comment 评论了。`,
).catch(() => undefined);
}
}
return { ok: true, feishuUserId: pending.feishuUserId, login: result.login };
// Bot account linking: never bind on the callback itself. The person who
// authorized GitHub confirms which chat account gets their identity.
if (link) {
const nonce = generateRandomHex(16);
const confirmation: LinkConfirmation = {
...link,
githubUserId: result.userId,
login: result.login,
};
await env.KV.put(`link:${nonce}`, JSON.stringify(confirmation), { expirationTtl: LINK_TTL });
return confirmLinkPage(nonce, confirmation);
}
const isBrowser = (getHeader(event, "accept") ?? "").includes("text/html");
@@ -365,14 +499,17 @@ export async function handleOAuthCallback(event: H3Event): Promise<unknown> {
pending.redirectTo.startsWith("/admin/invite") ||
pending.redirectTo.startsWith("/admin/invite?");
let groups = await loadGroups(env.KV);
let scope = resolveScope(env, groups, result.userId, result.login);
let groups = await loadGroups(env.KV, { fresh: true });
// A login first seen with another account grants nothing by name.
const trusted = await claimLogin(env.KV, result.userId, result.login);
const name = trusted ? result.login : "";
let scope = resolveScope(env, groups, result.userId, name);
if (!hasAnyAccess(scope) && !isInviteFlow) {
const created = await ensurePersonalGroup(env, result.userId, result.login);
if (created) {
groups = await loadGroups(env.KV);
scope = resolveScope(env, groups, result.userId, result.login);
groups = await loadGroups(env.KV, { fresh: true });
scope = resolveScope(env, groups, result.userId, name);
}
}
if (!hasAnyAccess(scope) && !isInviteFlow) {
@@ -380,7 +517,13 @@ export async function handleOAuthCallback(event: H3Event): Promise<unknown> {
return;
}
const sessionId = await createAdminSession(env.KV, result.userId, result.login);
// Tie the member entries naming this login to the account itself.
if (trusted) {
await bindMemberIds(env.KV, groups, result.userId, result.login).catch((err: unknown) =>
log.warn({ err, userId: result.userId }, "Failed to bind member ids"),
);
}
const sessionId = await createAdminSession(env.KV, result.userId, result.login, trusted);
appendResponseHeader(event, "Set-Cookie", adminCookie(sessionId));
await recordAudit(env.DB, {
ts: Date.now(),
@@ -400,15 +543,77 @@ export async function handleOAuthCallback(event: H3Event): Promise<unknown> {
};
}
/** DELETE /auth/token/:userId — revoke a stored user token. */
/** POST /auth/github/link — the GitHub user approved a bot account link. */
export async function handleLinkConfirm(event: H3Event): Promise<string> {
const env = cfEnv(event);
const body = (await readBody(event).catch(() => ({}))) as Record<string, string | undefined>;
const nonce = String(body["nonce"] ?? "");
const raw = /^[0-9a-f]{32}$/.test(nonce) ? await env.KV.get(`link:${nonce}`, "json") : null;
// One use, recorded in D1: a replayed confirmation (or a stale KV copy
// elsewhere) can never restore a link that was undone or replaced since.
const claims = idempotencyStore(env.DB, env.KV);
const usedKey = `link-used:${nonce}`;
const expired = (): string => {
setResponseStatus(event, 400);
return messagePage("链接已失效", "请回到聊天里重新发送 /gh login。");
};
if (!raw || (await claims.has(usedKey))) return expired();
const link = raw as LinkConfirmation;
const notice = `✅ GitHub 账号已绑定:@${link.login}。现在可以用 /gh comment 评论了。`;
// The link and its one-use record commit in one transaction, refused once
// the record exists: a replay can never restore a link undone or replaced
// since — not even after a write whose answer was lost (the record then
// tells it went through). A failure before the commit leaves the pending
// link, so submitting it again works.
try {
await env.DB.batch([
unrecordedGuard(env.DB, usedKey),
recordStatement(env.DB, usedKey, LINK_TTL),
linkStatement(env.DB, link.platform, link.userId, link.githubUserId),
]);
} catch (err) {
if (!(await claims.has(usedKey).catch(() => false))) throw err;
// Recorded: committed here (its answer lost), or by a concurrent submit.
if (/already recorded|malformed JSON/i.test(String(err))) return expired();
}
await env.KV.delete(`link:${nonce}`).catch(() => undefined);
if (link.platform === "telegram") {
if (link.chatId && env.TELEGRAM_TOKEN) {
await sendMessage(env.TELEGRAM_TOKEN, link.chatId, escapeHtml(notice)).catch(() => undefined);
}
} else if (link.platform === "feishu") {
if (link.chatId) {
const tokenRes = await getTenantAccessToken(env);
if (tokenRes.ok && tokenRes.token) {
await sendFeishuText(tokenRes.token, link.chatId, notice).catch(() => undefined);
}
}
}
await recordAudit(env.DB, {
ts: Date.now(),
actorId: link.githubUserId,
actorLogin: link.login,
action: "account.link",
targetType: link.platform,
targetId: link.userId,
ip: clientIp(event),
});
return linkedPage(link.login, link.platform);
}
/** DELETE /auth/token/:userId — revoke a stored user token (own token, or any for super admins). */
export async function handleTokenDelete(event: H3Event, userId: string): Promise<unknown> {
const env = cfEnv(event);
const session = await getAdminSession(env.KV, getHeader(event, "cookie"));
const session = await getAdminSession(env.KV, getHeader(event, "cookie"), env.DB);
if (!session) {
setResponseStatus(event, 401);
return { error: "Unauthorized" };
}
await removeToken(env.KV, userId);
if (session.userId !== userId && !isAdminUser(env, session.userId, authLogin(session))) {
setResponseStatus(event, 403);
return { error: "Forbidden" };
}
await removeToken(env.KV, userId, env.DB);
await recordAudit(env.DB, {
ts: Date.now(),
actorId: session.userId,
+60 -7
View File
@@ -1,4 +1,5 @@
import type { Env } from "../types";
import { idempotencyStore } from "../lib/idempotency";
const SESSION_COOKIE = "wh_admin_session";
const SESSION_TTL = 24 * 3600;
@@ -6,6 +7,33 @@ const SESSION_TTL = 24 * 3600;
export interface AdminSession {
userId: string;
login: string;
/**
* False when the login was first seen with another GitHub account (a name
* released by a rename and taken over since): identity checks then match
* this session by its user id only, never by the login.
*/
loginTrusted?: boolean;
}
/** The login identity checks may use for this session ("" — by user id only). */
export function authLogin(session: AdminSession): string {
return session.loginTrusted === false ? "" : session.login;
}
/**
* Whether `login` belongs to `userId` as far as this deployment knows: the
* first account seen with a login owns it here (`login-owner:{login}`). A
* GitHub login can be renamed away and registered by someone else; that
* newcomer then never inherits what was granted to the login (an admin
* entry, a member entry not yet bound to an id).
*/
export async function claimLogin(kv: KVNamespace, userId: string, login: string): Promise<boolean> {
const key = `login-owner:${login.toLowerCase()}`;
const owner = await kv.get(key);
if (owner) return owner === userId;
await kv.put(key, userId);
// Read back: of two first sign-ins racing, only the one stored wins.
return (await kv.get(key)) === userId;
}
export function isDebugMode(env: Env): boolean {
@@ -20,14 +48,24 @@ export function isDebugMode(env: Env): boolean {
return false;
}
/**
* Whether one configured identity names this user. An all-digit entry is a
* GitHub user id, anything else a login (case-insensitive) — one string is
* never matched both ways, so a user whose login is digits can never pass for
* the user with that id.
*/
export function identityEntryMatches(entry: string, userId: string, login: string): boolean {
const e = entry.trim();
if (!e) return false;
if (/^\d+$/.test(e)) return e === userId;
return e.toLowerCase() === login.toLowerCase();
}
export function isAdminUser(env: Env, userId: string, login: string): boolean {
if (isDebugMode(env)) return true;
const ids = (env.ADMIN_USER_IDS ?? "")
return (env.ADMIN_USER_IDS ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (ids.length === 0) return false;
return ids.includes(userId) || ids.some((id) => id.toLowerCase() === login.toLowerCase());
.some((entry) => identityEntryMatches(entry, userId, login));
}
function generateSessionId(): string {
@@ -42,9 +80,10 @@ export async function createAdminSession(
kv: KVNamespace,
userId: string,
login: string,
loginTrusted = true,
): Promise<string> {
const sessionId = generateSessionId();
const session: AdminSession = { userId, login };
const session: AdminSession = loginTrusted ? { userId, login } : { userId, login, loginTrusted };
await kv.put(`session:${sessionId}`, JSON.stringify(session), {
expirationTtl: SESSION_TTL,
});
@@ -70,21 +109,35 @@ function parseCookies(header: string | undefined): Record<string, string> {
return out;
}
/**
* Sessions are revoked in the idempotency store (D1) as well as deleted from
* KV: a KV delete takes a while to reach every location, and a stolen cookie
* must stop working at once.
*/
function revokedSessionKey(sessionId: string): string {
return `session-revoked:${sessionId}`;
}
export async function getAdminSession(
kv: KVNamespace,
cookieHeader: string | undefined,
db?: D1Database,
): Promise<AdminSession | null> {
const sessionId = parseCookies(cookieHeader)[SESSION_COOKIE];
if (!sessionId) return null;
const raw = await kv.get<AdminSession>(`session:${sessionId}`, "json");
if (!raw) return null;
if (db && (await idempotencyStore(db, kv).has(revokedSessionKey(sessionId)))) return null;
return raw;
}
export async function destroyAdminSession(
kv: KVNamespace,
cookieHeader: string | undefined,
db?: D1Database,
): Promise<void> {
const sessionId = parseCookies(cookieHeader)[SESSION_COOKIE];
if (sessionId) await kv.delete(`session:${sessionId}`);
if (!sessionId) return;
if (db) await idempotencyStore(db, kv).claim(revokedSessionKey(sessionId), SESSION_TTL);
await kv.delete(`session:${sessionId}`);
}
+86 -13
View File
@@ -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);
}