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

+138 -35
View File
@@ -75,16 +75,44 @@ export const APP_COMMANDS = [
{ name: MSG_CMD_DEL, type: COMMAND_TYPE.MESSAGE },
];
async function shortHash(text: string): Promise<string> {
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
return [...new Uint8Array(digest).slice(0, 8)]
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
/**
* Registration markers are kept per application (and guild) and hold the hash
* of the command definitions last registered there: another application, or
* definitions that differ from the registered ones — newer or rolled back —
* register again instead of being skipped.
*/
function commandsHash(): Promise<string> {
return shortHash(JSON.stringify(APP_COMMANDS));
}
const applicationIds = new Map<string, string>();
export async function getApplicationId(env: Env): Promise<string | null> {
if (env.DISCORD_APPLICATION_ID) return env.DISCORD_APPLICATION_ID;
const token = env.DISCORD_TOKEN ?? "";
if (!token) return null;
// Cached per bot token: a token of another application never gets the
// id resolved for the previous one. Held in memory too (one KV read per
// isolate, not one per sync run).
const cacheKey = `config:discord-app-id:${await shortHash(token)}`;
const known = applicationIds.get(cacheKey);
if (known) return known;
try {
const cached = await env.KV.get("config:discord-app-id");
if (cached) return cached;
const cached = await env.KV.get(cacheKey);
if (cached) {
applicationIds.set(cacheKey, cached);
return cached;
}
} catch {
// fall through to the API
}
const token = env.DISCORD_TOKEN ?? "";
if (!token) return null;
const res = await fetch(`${DISCORD_API}/oauth2/applications/@me`, {
headers: { Authorization: `Bot ${token}` },
});
@@ -95,7 +123,8 @@ export async function getApplicationId(env: Env): Promise<string | null> {
const app = (await res.json()) as { id?: string };
if (app.id) {
try {
await env.KV.put("config:discord-app-id", app.id);
applicationIds.set(cacheKey, app.id);
await env.KV.put(cacheKey, app.id);
} catch {
// cache is best-effort
}
@@ -107,13 +136,15 @@ export async function getApplicationId(env: Env): Promise<string | null> {
export async function registerGlobalCommands(env: Env): Promise<void> {
const token = env.DISCORD_TOKEN ?? "";
if (!token) return;
const appId = await getApplicationId(env);
if (!appId) return;
const marker = `cmd:registered:global:${appId}`;
const hash = await commandsHash();
try {
if (await env.KV.get("cmd:registered:global")) return;
if ((await env.KV.get(marker)) === hash) return;
} catch {
// fall through and register
}
const appId = await getApplicationId(env);
if (!appId) return;
const res = await fetch(`${DISCORD_API}/applications/${appId}/commands`, {
method: "PUT",
headers: { Authorization: `Bot ${token}`, "Content-Type": "application/json" },
@@ -121,7 +152,7 @@ export async function registerGlobalCommands(env: Env): Promise<void> {
});
if (res.ok) {
try {
await env.KV.put("cmd:registered:global", "1", { expirationTtl: 86400 });
await env.KV.put(marker, hash, { expirationTtl: 86400 });
} catch {
// best-effort
}
@@ -132,38 +163,110 @@ export async function registerGlobalCommands(env: Env): Promise<void> {
}
}
const GUILD_PAGE_SIZE = 200;
/**
* Discord calls one run makes for guild commands — pages listed and
* registrations together — well within a Worker invocation's subrequest
* limit (50 on the Free plan, shared with the run's other work). A run picks
* up where the previous one stopped, so a bot in any number of guilds keeps
* making progress.
*/
const GUILD_CALLS_PER_RUN = 30;
/** Per application: which guild holds which definitions, and where the walk is. */
interface GuildRecord {
hashes: Record<string, string>;
/** The guild after which the next run continues ("" from the start). */
after: string;
}
export async function syncGuildCommands(env: Env): Promise<void> {
const token = env.DISCORD_TOKEN ?? "";
if (!token) return;
const appId = await getApplicationId(env);
if (!appId) return;
const res = await fetch(`${DISCORD_API}/users/@me/guilds`, {
headers: { Authorization: `Bot ${token}` },
});
if (!res.ok) {
const err = await res.text();
log.warn({ status: res.status, err }, "Failed to list guilds");
return;
}
const guilds = (await res.json()) as Array<{ id: string }>;
for (const guild of guilds) {
try {
if (await env.KV.get(`cmd:guild:${guild.id}`)) continue;
const r = await fetch(`${DISCORD_API}/applications/${appId}/guilds/${guild.id}/commands`, {
method: "PUT",
headers: { Authorization: `Bot ${token}`, "Content-Type": "application/json" },
body: JSON.stringify(APP_COMMANDS),
});
if (r.ok) {
await env.KV.put(`cmd:guild:${guild.id}`, "1");
log.info({ guildId: guild.id }, "Registered guild application commands");
} else {
const err = await r.text();
log.warn({ guildId: guild.id, status: r.status, err }, "Command registration failed");
}
} catch (err) {
log.warn({ guildId: guild.id, err: String(err) }, "Command registration failed");
const hash = await commandsHash();
// One KV record per application, read and written once per run (not one
// read per guild).
const recordKey = `cmd:guilds:${appId}`;
const stored = await env.KV.get<GuildRecord | Record<string, string>>(recordKey, "json").catch(
() => null,
);
const record: GuildRecord =
stored && typeof stored === "object" && "hashes" in stored
? {
hashes: { ...(stored as GuildRecord).hashes },
after: (stored as GuildRecord).after ?? "",
}
: { hashes: { ...((stored as Record<string, string> | null) ?? {}) }, after: "" };
const startedAfter = record.after;
let calls = GUILD_CALLS_PER_RUN;
let changed = false;
/** Stopped at a guild still to register: where to resume must be kept. */
let pending = false;
/** A guild needed registering this run (successful or not). */
let attempted = false;
walk: while (calls > 0) {
calls--;
const res = await fetch(
`${DISCORD_API}/users/@me/guilds?limit=${GUILD_PAGE_SIZE}${record.after ? `&after=${record.after}` : ""}`,
{ headers: { Authorization: `Bot ${token}` } },
);
if (!res.ok) {
const err = await res.text();
log.warn({ status: res.status, err }, "Failed to list guilds");
break;
}
const page = (await res.json()) as Array<{ id: string }>;
for (const guild of page) {
if (record.hashes[guild.id] !== hash) {
attempted = true;
// Out of calls: the next run resumes at this guild.
if (calls <= 0) {
pending = true;
break walk;
}
calls--;
try {
const r = await fetch(
`${DISCORD_API}/applications/${appId}/guilds/${guild.id}/commands`,
{
method: "PUT",
headers: { Authorization: `Bot ${token}`, "Content-Type": "application/json" },
body: JSON.stringify(APP_COMMANDS),
},
);
if (r.ok) {
record.hashes[guild.id] = hash;
changed = true;
log.info({ guildId: guild.id }, "Registered guild application commands");
} else {
const err = await r.text();
log.warn({ guildId: guild.id, status: r.status, err }, "Command registration failed");
}
} catch (err) {
log.warn({ guildId: guild.id, err: String(err) }, "Command registration failed");
}
}
record.after = guild.id;
}
if (page.length < GUILD_PAGE_SIZE) {
// The end of the list: the next run starts over (picking up guilds
// joined meanwhile, and redoing ones whose registration failed).
record.after = "";
break;
}
}
// Out of calls right after a full page, with work found on the way: the
// walk must resume past it (or the guilds after it would never be reached).
if (calls <= 0 && record.after !== "" && attempted) pending = true;
// Written only when something changed — a registration, a walk stopped
// with work left, or the cursor reset after one: a run that only checked
// writes nothing (KV writes are scarce; this runs every few minutes).
if (changed || pending || (record.after === "" && startedAfter !== "")) {
await env.KV.put(recordKey, JSON.stringify(record)).catch((err: unknown) =>
log.warn({ err: String(err) }, "Failed to record guild command registrations"),
);
}
}
+7 -1
View File
@@ -12,7 +12,13 @@ export class DiscordDriver implements PlatformDriver {
return { ok: false, error: "target.channelId is required", errorCode: "NO_TARGET" };
}
const token = env.DISCORD_TOKEN ?? "";
return sendMessage(token, channelId, renderNeutralMessage(message), target.threadId);
return sendMessage(
token,
channelId,
renderNeutralMessage(message),
target.threadId,
message.sendKey,
);
}
async edit(
+226 -67
View File
@@ -9,6 +9,7 @@ import {
closePullRequestAsUser,
} from "../../github/oauth";
import { getDiscordLink, removeDiscordLink } from "../../github/store";
import { idempotencyStore } from "../../lib/idempotency";
import type { Env } from "../../types";
import { MSG_CMD_ADD, MSG_CMD_EDIT, MSG_CMD_DEL } from "./commands";
@@ -22,16 +23,84 @@ const EPHEMERAL = 64;
// Modal custom_id encodings (delimiter '|' never appears in owner/repo).
const MODAL_ADD = "ghc|add|"; // ghc|add|owner|repo|issueNumber
const MODAL_EDIT = "ghc|edit|"; // ghc|edit|owner|repo|commentId
const MODAL_EDIT = "ghc|edit|"; // ghc|edit|owner|repo|commentId (read for older modals)
/** ghc|ed|owner|repo|commentId|base: an edit that remembers the text it was opened on. */
const MODAL_EDIT_BASE = "ghc|ed|";
/**
* ghc|m|{context id}: the context kept in KV instead — only when the custom
* id would pass Discord's 100 characters (a long owner/repo). KV writes are
* scarce (Workers Free), so the context travels in the custom id whenever
* it fits.
*/
const MODAL_CTX = "ghc|m|";
const MAX_CUSTOM_ID = 100;
/** Hex characters of the comment hash an edit modal remembers. */
const BASE_HASH_LENGTH = 16;
const MODAL_CTX_TTL_SECONDS = 86400;
interface ModalContext {
op: "add" | "edit";
owner: string;
repo: string;
/** Issue/PR number (add) or comment id (edit). */
ref: string;
/** SHA-256 (hex, possibly shortened) of the comment as the edit modal showed it. */
base?: string;
}
async function sha256Hex(text: string): Promise<string> {
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
}
async function modalCustomId(env: Env, context: ModalContext): Promise<string> {
const inline =
context.op === "add"
? `${MODAL_ADD}${context.owner}|${context.repo}|${context.ref}`
: `${MODAL_EDIT_BASE}${context.owner}|${context.repo}|${context.ref}|${context.base ?? ""}`;
if (inline.length <= MAX_CUSTOM_ID) return inline;
const contextId = crypto.randomUUID().replace(/-/g, "");
await env.KV.put(`discord-modal:${contextId}`, JSON.stringify(context), {
expirationTtl: MODAL_CTX_TTL_SECONDS,
});
return `${MODAL_CTX}${contextId}`;
}
async function modalContext(env: Env, customId: string): Promise<ModalContext | null> {
if (customId.startsWith(MODAL_CTX)) {
return env.KV.get<ModalContext>(`discord-modal:${customId.slice(MODAL_CTX.length)}`, "json");
}
if (customId.startsWith(MODAL_EDIT_BASE)) {
const rest = customId.slice(MODAL_EDIT_BASE.length);
const cut = rest.lastIndexOf("|");
const [owner, repo, ref] = splitThree(rest.slice(0, cut));
const base = rest.slice(cut + 1);
return cut > 0 && owner && repo && ref ? { op: "edit", owner, repo, ref, base } : null;
}
for (const [prefix, op] of [
[MODAL_ADD, "add"],
[MODAL_EDIT, "edit"],
] as const) {
if (!customId.startsWith(prefix)) continue;
const [owner, repo, ref] = splitThree(customId.slice(prefix.length));
return owner && repo && ref ? { op, owner, repo, ref } : null;
}
return null;
}
// PR notification button custom_id encodings.
const BTN_MERGE = "ghpr|merge|"; // ghpr|merge|owner|repo|pullNumber
const BTN_CLOSE = "ghpr|close|"; // ghpr|close|owner|repo|pullNumber
// Comment link (has the comment id); check this BEFORE the plain issue regex.
// Both only accept github.com itself (not e.g. `evilgithub.com`): the actions
// run against the GitHub API.
const GITHUB_COMMENT_RE =
/github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/\d+#issuecomment-(\d+)/;
const GITHUB_ISSUE_RE = /github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/(\d+)/;
/https?:\/\/(?:www\.)?github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/\d+#issuecomment-(\d+)/;
const GITHUB_ISSUE_RE =
/https?:\/\/(?:www\.)?github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/(\d+)/;
/** Discord caps a text input's value (and max_length) at 4000 characters. */
const MAX_MODAL_TEXT = 4000;
/**
* Split a pipe-delimited custom-id suffix into [owner, repo, number]. The
@@ -46,13 +115,23 @@ function splitThree(rest: string): [string, string, string] {
return [owner ?? "", repo ?? "", number ?? ""];
}
interface DiscordUser {
id?: string;
username?: string;
global_name?: string | null;
}
interface Interaction {
id: string;
/** The application the interaction belongs to (follow-up webhooks use it). */
application_id?: string;
token: string;
type: number;
channel_id?: string;
member?: { user?: { id?: string } };
user?: { id?: string };
member?: { user?: DiscordUser };
user?: DiscordUser;
/** The message a component (button) is attached to. */
message?: { id?: string };
data?: Record<string, unknown>;
}
@@ -146,6 +225,15 @@ export async function handleInteractionRequest(request: Request, env: Env): Prom
});
}
// Discord does not redeliver interactions, but a replayed request (same
// signed body within the timestamp window) must not run its action twice.
if (
interaction.id &&
!(await idempotencyStore(env.DB, env.KV).claim(`bot:discord:${interaction.id}`, 3600))
) {
return new Response(null, { status: 202 });
}
// Handle the interaction via the callback webhook; respond 202 with no body
// as required for interactions received over the HTTP endpoint.
await handleInteraction(env, interaction).catch((err) =>
@@ -155,19 +243,22 @@ export async function handleInteractionRequest(request: Request, env: Env): Prom
}
async function handleInteraction(env: Env, interaction: Interaction): Promise<void> {
const userId = interaction.member?.user?.id ?? interaction.user?.id ?? null;
const user = interaction.member?.user ?? interaction.user;
const userId = user?.id ?? null;
const id = interaction.id;
const token = interaction.token;
const applicationId = interaction.application_id ?? env.DISCORD_APPLICATION_ID ?? "";
if (interaction.type === INTERACTION_TYPE.BUTTON) {
const data = interaction.data as { custom_id?: string; message?: { id?: string } };
const data = interaction.data as { custom_id?: string };
return handleButton(
env,
id,
token,
applicationId,
userId,
interaction.channel_id,
data.message?.id,
interaction.message?.id,
data.custom_id,
);
}
@@ -203,13 +294,15 @@ async function handleInteraction(env: Env, interaction: Interaction): Promise<vo
if (!op) return;
const target = data.target_id ? data.resolved?.messages?.[data.target_id] : undefined;
const source = target?.embeds?.[0]?.url ?? target?.content ?? "";
return commentOp(env, id, token, userId, op, source);
return commentOp(env, id, token, applicationId, userId, op, source);
}
// Slash command /gh ...
if (data.name === "gh" && data.type === COMMAND_TYPE.CHAT_INPUT) {
const top = data.options?.[0];
if (top?.name === "login") return cmdLogin(env, id, token, userId);
if (top?.name === "login") {
return cmdLogin(env, id, token, userId, user?.global_name || user?.username);
}
if (top?.name === "logout") return cmdLogout(env, id, token, userId);
if (top?.name === "comment") {
const sub = top.options?.[0];
@@ -223,7 +316,7 @@ async function handleInteraction(env: Env, interaction: Interaction): Promise<vo
: null;
if (!op) return;
const link = sub?.options?.find((o) => o.name === "link")?.value ?? "";
return commentOp(env, id, token, userId, op, link);
return commentOp(env, id, token, applicationId, userId, op, link);
}
return;
}
@@ -231,7 +324,7 @@ async function handleInteraction(env: Env, interaction: Interaction): Promise<vo
}
if (interaction.type === INTERACTION_TYPE.MODAL_SUBMIT) {
return modalSubmit(env, id, token, userId, interaction.data);
return modalSubmit(env, id, token, applicationId, userId, interaction.data);
}
}
@@ -243,12 +336,13 @@ async function respond(env: Env, id: string, token: string, content: string): Pr
});
}
/** Answer an interaction; whether Discord accepted the answer. */
async function interactionCallback(
env: Env,
id: string,
token: string,
body: unknown,
): Promise<void> {
): Promise<boolean> {
const res = await fetch(`${DISCORD_API}/interactions/${id}/${token}/callback`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -258,11 +352,23 @@ async function interactionCallback(
const err = await res.text();
log.warn({ status: res.status, err }, "Interaction callback failed");
}
return res.ok;
}
/** Replace the deferred (ephemeral) response body with the final result. */
async function updateOriginal(env: Env, id: string, token: string, content: string): Promise<void> {
const res = await fetch(`${DISCORD_API}/interactions/${id}/${token}/messages/@original`, {
/**
* Replace the deferred (ephemeral) response body with the final result. The
* original response is edited through the application's follow-up webhook.
*/
async function updateOriginal(
applicationId: string,
token: string,
content: string,
): Promise<void> {
if (!applicationId) {
log.warn("Cannot update interaction response: application id unknown");
return;
}
const res = await fetch(`${DISCORD_API}/webhooks/${applicationId}/${token}/messages/@original`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content }),
@@ -273,6 +379,35 @@ async function updateOriginal(env: Env, id: string, token: string, content: stri
}
}
/**
* Acknowledge first (deferred, ephemeral), then run the GitHub action and
* show its outcome: Discord fails an interaction not answered within 3
* seconds, and a slow GitHub call must not leave a done action looking failed
* (the user would submit it again).
*/
async function deferThen(
env: Env,
id: string,
token: string,
applicationId: string,
work: () => Promise<string>,
): Promise<void> {
// Not acknowledged: Discord shows the interaction as failed, so acting on
// GitHub now would leave a done action looking failed (and resubmitted).
const acked = await interactionCallback(env, id, token, {
type: CALLBACK_TYPE.DEFERRED_MESSAGE,
data: { flags: EPHEMERAL },
});
if (!acked) return;
let content: string;
try {
content = await work();
} catch (err) {
content = errText(err);
}
await updateOriginal(applicationId, token, content);
}
/**
* PR notification buttons: merge or close the PR as the clicker's linked
* GitHub account. The clicker must have run `/gh login` first.
@@ -281,6 +416,7 @@ async function handleButton(
env: Env,
id: string,
token: string,
applicationId: string,
userId: string | null,
channelId: string | undefined,
messageId: string | undefined,
@@ -308,17 +444,18 @@ async function handleButton(
if (!owner || !repo || !number) return;
// Acknowledge first (deferred, ephemeral) so the clicker sees a spinner
// while the GitHub API call runs.
await interactionCallback(env, id, token, {
// while the GitHub API call runs; unacknowledged, nothing is done.
const acked = await interactionCallback(env, id, token, {
type: CALLBACK_TYPE.DEFERRED_MESSAGE,
data: { flags: EPHEMERAL },
});
if (!acked) return;
try {
if (op === "merge") {
await mergePullRequestAsUser(env.KV, githubUserId, owner, repo, Number(number));
await mergePullRequestAsUser(env, githubUserId, owner, repo, Number(number));
} else {
await closePullRequestAsUser(env.KV, githubUserId, owner, repo, Number(number));
await closePullRequestAsUser(env, githubUserId, owner, repo, Number(number));
}
// Remove the buttons from the notification so nobody double-clicks.
if (channelId && messageId) {
@@ -332,13 +469,19 @@ async function handleButton(
}).catch((err) => log.warn({ err: String(err) }, "Failed to strip PR buttons"));
}
const label = op === "merge" ? "合并" : "关闭";
await updateOriginal(env, id, token, `✅ 已${label} PR ${owner}/${repo}#${number}`);
await updateOriginal(applicationId, token, `✅ 已${label} PR ${owner}/${repo}#${number}`);
} catch (err) {
await updateOriginal(env, id, token, errText(err));
await updateOriginal(applicationId, token, errText(err));
}
}
async function cmdLogin(env: Env, id: string, token: string, userId: string | null): Promise<void> {
async function cmdLogin(
env: Env,
id: string,
token: string,
userId: string | null,
name: string | undefined,
): Promise<void> {
if (!userId) return respond(env, id, token, "无法识别你的 Discord 账号。");
const clientId = env.GITHUB_CLIENT_ID;
if (!clientId) return respond(env, id, token, "服务器未配置 GitHub OAuth(GITHUB_CLIENT_ID)。");
@@ -346,7 +489,11 @@ async function cmdLogin(env: Env, id: string, token: string, userId: string | nu
const state = crypto.randomUUID().replace(/-/g, "");
await env.KV.put(
`state:${state}`,
JSON.stringify({ redirectTo: "/", discordUserId: userId, expiresAt: Date.now() + 600_000 }),
JSON.stringify({
redirectTo: "/",
link: { platform: "discord", userId, name },
expiresAt: Date.now() + 600_000,
}),
{ expirationTtl: 600 },
);
const url = getOAuthURL(clientId, state);
@@ -386,6 +533,7 @@ async function commentOp(
env: Env,
id: string,
token: string,
applicationId: string,
userId: string | null,
op: "add" | "edit" | "del",
source: string,
@@ -409,7 +557,7 @@ async function commentOp(
env,
id,
token,
`${MODAL_ADD}${m[1]}|${m[2]}|${m[3]}`,
await modalCustomId(env, { op: "add", owner: m[1]!, repo: m[2]!, ref: m[3]! }),
`评论 ${m[1]}/${m[2]}#${m[3]}`,
);
}
@@ -427,27 +575,41 @@ async function commentOp(
const [, owner, repo, commentId] = m;
if (op === "del") {
try {
await deleteCommentAsUser(env.KV, githubUserId, owner!, repo!, Number(commentId));
return respond(env, id, token, `已删除评论 ${owner}/${repo}#issuecomment-${commentId}。`);
} catch (err) {
return respond(env, id, token, errText(err));
}
return deferThen(env, id, token, applicationId, async () => {
await deleteCommentAsUser(env, githubUserId, owner!, repo!, Number(commentId));
return `已删除评论 ${owner}/${repo}#issuecomment-${commentId}。`;
});
}
// edit: fetch current body to prefill the modal.
let prefill = "";
try {
const { body } = await getCommentAsUser(env.KV, githubUserId, owner!, repo!, Number(commentId));
const { body } = await getCommentAsUser(env, githubUserId, owner!, repo!, Number(commentId));
prefill = body;
} catch (err) {
return respond(env, id, token, errText(err));
}
// The modal can only hold part of a longer comment, and saving it would
// replace the whole comment with that part: such comments are edited on GitHub.
if (prefill.length > MAX_MODAL_TEXT) {
return respond(
env,
id,
token,
`这条评论超过 ${MAX_MODAL_TEXT} 字,无法在 Discord 中完整编辑,请到 GitHub 上编辑:${m[0]}`,
);
}
return openCommentModal(
env,
id,
token,
`${MODAL_EDIT}${owner}|${repo}|${commentId}`,
await modalCustomId(env, {
op: "edit",
owner: owner!,
repo: repo!,
ref: commentId!,
base: (await sha256Hex(prefill)).slice(0, BASE_HASH_LENGTH),
}),
`编辑评论 #${commentId}`,
prefill,
);
@@ -477,8 +639,8 @@ async function openCommentModal(
label: "评论内容",
style: 2,
required: true,
max_length: 2000,
value: prefill.slice(0, 2000) || undefined,
max_length: MAX_MODAL_TEXT,
value: prefill || undefined,
},
],
},
@@ -491,6 +653,7 @@ async function modalSubmit(
env: Env,
id: string,
token: string,
applicationId: string,
userId: string | null,
data: unknown,
): Promise<void> {
@@ -501,51 +664,47 @@ async function modalSubmit(
const customId = d.custom_id;
if (!userId || !customId) return;
const body = d.components?.[0]?.components?.find((c) => c.custom_id === "body")?.value?.trim();
if (!body) return respond(env, id, token, "评论内容不能为空。");
// Submitted as written (an indented first line is a Markdown code block):
// blank only decides emptiness.
const body = d.components?.[0]?.components?.find((c) => c.custom_id === "body")?.value ?? "";
if (!body.trim()) return respond(env, id, token, "评论内容不能为空。");
const githubUserId = await getDiscordLink(env.DB, userId);
if (!githubUserId) {
return respond(env, id, token, "你还没有绑定 GitHub 账号,请先使用 `/gh login`。");
}
// ghc|add|owner|repo|issueNumber
if (customId.startsWith(MODAL_ADD)) {
const [owner, repo, number] = splitThree(customId.slice(MODAL_ADD.length));
if (!owner || !repo || !number)
return respond(env, id, token, "内部错误:无法解析目标 issue。");
try {
const context = await modalContext(env, customId);
if (!context) {
return respond(env, id, token, "这个评论框已失效,请重新打开。");
}
const { owner, repo, ref } = context;
if (context.op === "add") {
return deferThen(env, id, token, applicationId, async () => {
const { htmlUrl, login } = await commentAsUser(
env.KV,
env,
githubUserId,
owner,
repo,
Number(number),
Number(ref),
body,
);
return respond(env, id, token, `已以 **@${login}** 身份评论:${htmlUrl}`);
} catch (err) {
return respond(env, id, token, errText(err));
}
return `已以 **@${login}** 身份评论:${htmlUrl}`;
});
}
// ghc|edit|owner|repo|commentId
if (customId.startsWith(MODAL_EDIT)) {
const [owner, repo, commentId] = splitThree(customId.slice(MODAL_EDIT.length));
if (!owner || !repo || !commentId)
return respond(env, id, token, "内部错误:无法解析目标评论。");
try {
const { htmlUrl } = await editCommentAsUser(
env.KV,
githubUserId,
owner,
repo,
Number(commentId),
body,
);
return respond(env, id, token, `已更新评论:${htmlUrl}`);
} catch (err) {
return respond(env, id, token, errText(err));
return deferThen(env, id, token, applicationId, async () => {
// Changed on GitHub since the modal showed it: saving would silently
// replace those changes (GitHub has no conditional update — this check
// narrows the window to the moment between it and the save).
if (context.base) {
const { body: current } = await getCommentAsUser(env, githubUserId, owner, repo, Number(ref));
if ((await sha256Hex(current)).slice(0, context.base.length) !== context.base) {
return "这条评论在你打开编辑框后已被修改。为避免覆盖这些修改,请重新打开编辑框。";
}
}
}
const { htmlUrl } = await editCommentAsUser(env, githubUserId, owner, repo, Number(ref), body);
return `已更新评论:${htmlUrl}`;
});
}
+68 -8
View File
@@ -9,6 +9,54 @@ import {
splitMessageTitle,
} from "../../formatters/helpers";
/** Discord's cap on the combined characters of all embeds in a message. */
const MAX_EMBED_TOTAL = 6000;
/** Discord's caps on a button's custom_id and label. */
const MAX_CUSTOM_ID = 100;
const MAX_BUTTON_LABEL = 80;
type Embed = NonNullable<FormattedMessage["embeds"]>[number];
function embedLength(embed: Embed): number {
return (
(embed.title?.length ?? 0) +
(embed.description?.length ?? 0) +
(embed.author?.name.length ?? 0) +
(embed.footer?.text.length ?? 0) +
(embed.fields ?? []).reduce((n, f) => n + f.name.length + f.value.length, 0)
);
}
/** Description length kept before any field is given up. */
const MIN_DESCRIPTION = 1024;
/** Shorten the description by `over` characters (never below `floor`); returns what is left over. */
function shrinkDescription(embed: Embed, over: number, floor: number): number {
const text = embed.description;
if (!text || over <= 0 || text.length <= floor) return over;
const keep = Math.max(floor, text.length - over - 1);
embed.description = keep > 0 ? `${text.slice(0, keep)}…` : undefined;
return over - (text.length - (embed.description?.length ?? 0));
}
/**
* Fit an embed into the 6000-character total so the request is never
* rejected whole: a long description shrinks first (down to 1024 characters),
* then trailing fields are dropped, then the description shrinks further.
*/
function fitEmbed(embed: Embed): Embed {
let over = embedLength(embed) - MAX_EMBED_TOTAL;
if (over <= 0) return embed;
const out: Embed = { ...embed, fields: embed.fields ? [...embed.fields] : undefined };
over = shrinkDescription(out, over, MIN_DESCRIPTION);
while (over > 0 && out.fields && out.fields.length > 0) {
const f = out.fields.pop()!;
over -= f.name.length + f.value.length;
}
shrinkDescription(out, over, 0);
return out;
}
function toStyle(style: NeutralActionStyle): number {
switch (style) {
case "primary":
@@ -20,10 +68,16 @@ function toStyle(style: NeutralActionStyle): number {
}
}
/**
* Most roles one message mentions: well within Discord's 100 allowed-mention
* roles and 2000 content characters (a mention takes at most 24 with its
* space). Routes are validated against it; this is the safety net.
*/
export const MAX_ROLE_MENTIONS = 50;
export function renderNeutralMessage(message: NeutralMessage): FormattedMessage {
const content = message.mentionRoleIds?.length
? message.mentionRoleIds.map((id) => `<@&${id}>`).join(" ")
: undefined;
const roleIds = [...new Set(message.mentionRoleIds ?? [])].slice(0, MAX_ROLE_MENTIONS);
const content = roleIds.length ? roleIds.map((id) => `<@&${id}>`).join(" ") : undefined;
// Discord embed titles can only be linked as a whole, so only the repo head
// goes into the title (linked to the event's object URL); the `: subject`
@@ -41,10 +95,16 @@ export function renderNeutralMessage(message: NeutralMessage): FormattedMessage
[message.forge?.name, message.footer].filter(Boolean).join(" · "),
MAX_FOOTER,
);
// Buttons whose id would exceed Discord's limit (very long repo names) are
// left out rather than failing the whole message.
const actions = (message.actions ?? []).filter((a) => a.id.length <= MAX_CUSTOM_ID);
return {
content,
// Only the route's own roles may ping — never @everyone, @here or users,
// whatever text ends up in the message.
allowed_mentions: { parse: [], roles: roleIds },
embeds: [
{
fitEmbed({
title: cap(head, MAX_TITLE),
url: message.url,
color: message.color,
@@ -63,16 +123,16 @@ export function renderNeutralMessage(message: NeutralMessage): FormattedMessage
})),
footer: footerText ? { text: footerText, icon_url: message.forge?.iconUrl } : undefined,
timestamp: message.timestamp,
},
}),
],
components: message.actions?.length
components: actions.length
? [
{
type: 1,
components: message.actions.map((action) => ({
components: actions.map((action) => ({
type: 2,
style: toStyle(action.style),
label: action.label,
label: cap(action.label, MAX_BUTTON_LABEL),
custom_id: action.id,
})),
},
+45 -18
View File
@@ -7,6 +7,18 @@ interface DiscordMessage {
id?: string;
}
/**
* Longest rate-limit wait (seconds) slept inside one request; a longer one is
* returned as a 429 failure so the delivery queue retries it later instead.
*/
const MAX_INLINE_RETRY_AFTER = 10;
/**
* Longest a single API call may take. Bounded, so a message edit always ends
* well within the update-chain lock it runs under (see `deliverUpdate`).
*/
export const REQUEST_TIMEOUT_MS = 15_000;
async function request(
url: string,
method: string,
@@ -23,13 +35,24 @@ async function request(
"Content-Type": "application/json",
},
body: JSON.stringify(message),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
if (res.status === 429) {
const rateLimit = (await res.json()) as { retry_after?: number };
const retryAfter = (rateLimit.retry_after ?? 1) * 1000;
const rateLimit = (await res.json().catch(() => ({}))) as { retry_after?: number };
const seconds = Number(rateLimit.retry_after);
const retryAfter = Number.isFinite(seconds) && seconds > 0 ? seconds : 1;
log.warn({ retryAfter, attempt }, "Rate limited");
await new Promise((r) => setTimeout(r, retryAfter));
if (retryAfter > MAX_INLINE_RETRY_AFTER || attempt === 2) {
return {
ok: false,
error: `Rate limited (retry_after=${retryAfter})`,
errorCode: "RATE_LIMITED",
status: 429,
attempts: attempt + 1,
};
}
await new Promise((r) => setTimeout(r, retryAfter * 1000));
continue;
}
@@ -58,15 +81,11 @@ async function request(
};
}
let messageId: string | undefined;
try {
const data = (await res.json()) as DiscordMessage;
messageId = data.id;
} catch {
// ignore malformed success body
}
return { ok: true, status: res.status, messageId, attempts: attempt + 1 };
// A success whose body cannot be read (cut off, timed out) is not known
// to have happened: tried again — a create carries its nonce, so it is
// never doubled.
const data = (await res.json()) as DiscordMessage | null;
return { ok: true, status: res.status, messageId: data?.id, attempts: attempt + 1 };
} catch (err) {
log.error({ err, attempt, channelId }, "Failed to send message");
if (attempt === 2)
@@ -83,11 +102,19 @@ export async function sendMessage(
channelId: string,
message: unknown,
threadId?: string,
sendKey?: string,
): Promise<SendResult> {
const url = threadId
? `${DISCORD_API}/channels/${threadId}/messages`
: `${DISCORD_API}/channels/${channelId}/messages`;
return request(url, "POST", token, message, channelId);
// Ids are path segments: encoded, so no stored value can address another
// resource (`x/../123` is never the channel `123`).
const url = `${DISCORD_API}/channels/${encodeURIComponent(threadId || channelId)}/messages`;
// One nonce for every attempt of this send — and, given a `sendKey`, for
// every attempt of the delivery: when a create went through but its
// response was lost, Discord answers the retry with that same message
// instead of posting a second one (`enforce_nonce`, a few minutes' window).
const nonce = (sendKey ?? crypto.randomUUID().replace(/-/g, "")).slice(0, 25);
const body =
message && typeof message === "object" ? { ...message, nonce, enforce_nonce: true } : message;
return request(url, "POST", token, body, channelId);
}
export async function editMessage(
@@ -97,7 +124,7 @@ export async function editMessage(
message: unknown,
threadId?: string,
): Promise<SendResult> {
const base = threadId ?? channelId;
const url = `${DISCORD_API}/channels/${base}/messages/${messageId}`;
const base = encodeURIComponent(threadId || channelId);
const url = `${DISCORD_API}/channels/${base}/messages/${encodeURIComponent(messageId)}`;
return request(url, "PATCH", token, message, channelId);
}
+13 -3
View File
@@ -1,6 +1,6 @@
import type { RouteTarget, Env, NeutralMessage } from "../../types";
import type { PlatformDriver, SendResult } from "../types";
import { getTenantAccessToken, sendMessage, updateMessage } from "./rest";
import { forgetRejectedToken, getTenantAccessToken, sendMessage, updateMessage } from "./rest";
import { renderNeutralMessage } from "./render";
export class FeishuDriver implements PlatformDriver {
@@ -17,9 +17,14 @@ export class FeishuDriver implements PlatformDriver {
ok: false,
error: tokenRes.error ?? "No token",
errorCode: tokenRes.errorCode ?? "NO_TOKEN",
status: tokenRes.status,
};
}
return sendMessage(tokenRes.token, chatId, renderNeutralMessage(message));
return forgetRejectedToken(
await sendMessage(tokenRes.token, chatId, renderNeutralMessage(message), message.sendKey),
env,
tokenRes.token,
);
}
async edit(
@@ -38,8 +43,13 @@ export class FeishuDriver implements PlatformDriver {
ok: false,
error: tokenRes.error ?? "No token",
errorCode: tokenRes.errorCode ?? "NO_TOKEN",
status: tokenRes.status,
};
}
return updateMessage(tokenRes.token, messageId, renderNeutralMessage(message));
return forgetRejectedToken(
await updateMessage(tokenRes.token, messageId, renderNeutralMessage(message)),
env,
tokenRes.token,
);
}
}
+15 -2
View File
@@ -26,8 +26,21 @@ function colorToTemplate(color?: number): FeishuTemplate {
return "blue";
}
/**
* Text from the forge (titles, bodies, names) is anyone's to write: an
* `<at id=all></at>` in an issue title would make the bot notify the whole
* chat. The card never mentions anyone itself, so every `<at` tag is broken
* up (a zero-width space after `<`) and shows as text.
*/
function noMentions(content: string): string {
// Escape every angle bracket, not only `<at`. Replacing just the mention
// prefix still leaves values such as `<script` in Feishu markdown, where an
// incomplete tag can be interpreted as HTML by the client.
return content.replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function mdText(content: string): { tag: "lark_md"; content: string } {
return { tag: "lark_md", content: cap(content, MAX_MARKDOWN) };
return { tag: "lark_md", content: cap(noMentions(content), MAX_MARKDOWN) };
}
function divMarkdown(content: string): { tag: "div"; text: { tag: "lark_md"; content: string } } {
@@ -92,7 +105,7 @@ export function renderNeutralMessage(message: NeutralMessage): Record<string, un
if (elements.length) elements.push({ tag: "hr" });
elements.push({
tag: "note",
elements: [{ tag: "lark_md", content: cap(meta.join(" · "), MAX_NOTE) }],
elements: [{ tag: "lark_md", content: cap(noMentions(meta.join(" · ")), MAX_NOTE) }],
});
}
+166 -9
View File
@@ -1,9 +1,16 @@
import { log } from "../../lib/log";
import { sharedFlight, type Flight } from "../../lib/shared-flight";
import type { Env } from "../../types";
import type { SendResult } from "../types";
const FEISHU_API = "https://open.feishu.cn";
const TOKEN_KEY = "feishu:token";
/**
* The cached tenant token of the configured app: switching apps (another
* FEISHU_APP_ID) never sends with the previous app's token.
*/
export function tokenKey(env: Env): string {
return `feishu:token:${env.FEISHU_APP_ID?.trim() ?? ""}`;
}
interface FeishuResponse {
code?: number;
@@ -12,6 +19,29 @@ interface FeishuResponse {
error?: { message?: string };
}
/** Feishu business codes for "slow down" (group message rate limit, API frequency limit). */
const RATE_LIMIT_CODES = new Set([230020, 99991400]);
/** Feishu business codes for an invalid or expired tenant access token. */
const TOKEN_CODES = new Set([99991661, 99991663, 99991664, 99991668]);
/**
* The error code of a failed call: transient business codes map to the codes
* the delivery queue retries (`RATE_LIMITED`, `FEISHU_TOKEN`), whatever HTTP
* status Feishu answered with.
*/
function errorCodeOf(status: number, code: number | undefined): string {
if (code != null && RATE_LIMIT_CODES.has(code)) return "RATE_LIMITED";
if (code != null && TOKEN_CODES.has(code)) return "FEISHU_TOKEN";
if (status >= 500) return "FEISHU_5XX";
return code != null && code !== 0 ? `FEISHU_${code}` : "FEISHU_ERROR";
}
/**
* Longest a single API call may take. Bounded, so a message edit always ends
* well within the update-chain lock it runs under (see `deliverUpdate`).
*/
export const REQUEST_TIMEOUT_MS = 15_000;
async function feishuRequest(
url: string,
method: string,
@@ -33,18 +63,35 @@ async function feishuRequest(
method,
headers,
body: JSON.stringify(body),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
lastStatus = res.status;
if (res.status === 429) {
const retryAfter = Number(res.headers.get("retry-after") || "1");
const header = Number(res.headers.get("retry-after") || "1");
const retryAfter = Number.isFinite(header) && header > 0 ? header : 1;
lastError = `Rate limited (retry_after=${retryAfter})`;
log.warn({ retryAfter, attempt, label }, "Feishu rate limited");
if (retryAfter > 10 || attempt === 2) {
return {
ok: false,
error: lastError,
errorCode: "RATE_LIMITED",
status: 429,
attempts: attempt + 1,
};
}
await new Promise((r) => setTimeout(r, retryAfter * 1000));
continue;
}
const data = (await res.json().catch(() => null)) as FeishuResponse | null;
// A 200 whose body cannot be read (or carries no result code) is not
// known to have succeeded: tried again (the request's uuid keeps a
// send from doubling).
if (res.ok && (!data || typeof data.code !== "number")) {
throw new Error("Feishu answered without a result code");
}
if (!res.ok) {
lastError = data?.msg ?? data?.error?.message ?? `HTTP ${res.status}`;
@@ -55,7 +102,7 @@ async function feishuRequest(
return {
ok: false,
error: lastError,
errorCode: res.status >= 500 ? "FEISHU_5XX" : "FEISHU_ERROR",
errorCode: errorCodeOf(res.status, data?.code),
status: res.status,
attempts: attempt + 1,
};
@@ -70,7 +117,7 @@ async function feishuRequest(
return {
ok: false,
error: lastError,
errorCode: `FEISHU_${data.code}`,
errorCode: errorCodeOf(res.status, data.code),
status: res.status,
attempts: attempt + 1,
};
@@ -107,6 +154,15 @@ async function feishuRequest(
};
}
/** How long this isolate uses a token before reading KV again. */
const HELD_TOKEN_MS = 5 * 60_000;
/**
* A failed token request is answered from memory this long: targets sent one
* after another in the same delivery then fail at once instead of each
* reading KV and asking Feishu again (the delivery is retried later anyway).
*/
const FAILED_TOKEN_MS = 5_000;
interface TokenResult {
ok: boolean;
token?: string;
@@ -115,6 +171,52 @@ interface TokenResult {
status?: number;
}
/**
* Tokens this isolate holds (per cache key), and refreshes in flight: routes
* sending in parallel share one token request and one KV write instead of
* each fetching and storing its own.
*/
const heldTokens = new Map<string, { token: string; until: number }>();
const refreshing = new Map<string, Flight<TokenResult>>();
const failedTokens = new Map<string, { result: TokenResult; until: number }>();
/** Tokens Feishu refused (bounded): never taken from KV again. */
const rejected = new Set<string>();
/** Refused tokens whose KV copy this isolate removed (or is removing). */
const cleared = new Set<string>();
/**
* A rejected token is dropped from the cache, so the retry fetches a new one.
* From memory every time; from KV once per isolate (every target refused
* with it would otherwise delete the same key again) — tried again by the
* next refusal when that delete fails — and never a newer token another
* request stored meanwhile.
*/
export async function forgetRejectedToken(
result: SendResult,
env: Env,
token: string,
): Promise<SendResult> {
if (result.errorCode !== "FEISHU_TOKEN") return result;
const key = tokenKey(env);
if (heldTokens.get(key)?.token === token) heldTokens.delete(key);
if (!rejected.has(token)) {
if (rejected.size >= 100) {
rejected.clear();
cleared.clear();
}
rejected.add(token);
}
if (!cleared.has(token)) {
cleared.add(token);
try {
if ((await env.KV.get(key)) === token) await env.KV.delete(key);
} catch {
cleared.delete(token);
}
}
return result;
}
export async function getTenantAccessToken(env: Env): Promise<TokenResult> {
const appId = env.FEISHU_APP_ID?.trim();
const appSecret = env.FEISHU_APP_SECRET?.trim();
@@ -126,8 +228,39 @@ export async function getTenantAccessToken(env: Env): Promise<TokenResult> {
};
}
const cached = await env.KV.get(TOKEN_KEY);
if (cached) return { ok: true, token: cached };
const key = tokenKey(env);
const held = heldTokens.get(key);
if (held && Date.now() < held.until) return { ok: true, token: held.token };
const failed = failedTokens.get(key);
if (failed && Date.now() < failed.until) return failed.result;
return sharedFlight(
refreshing,
key,
async () => {
const result = await fetchTenantAccessToken(env, appId, appSecret);
if (result.ok) failedTokens.delete(key);
else failedTokens.set(key, { result, until: Date.now() + FAILED_TOKEN_MS });
return result;
},
REQUEST_TIMEOUT_MS + 5_000,
);
}
async function fetchTenantAccessToken(
env: Env,
appId: string,
appSecret: string,
): Promise<TokenResult> {
const key = tokenKey(env);
const cached = await env.KV.get(key);
// A copy Feishu already refused (its KV delete failed or has not spread
// yet) is not used again: a new token is requested instead.
if (cached && !rejected.has(cached)) {
// Held a few minutes here: KV is read again now and then, so a token
// another isolate replaced is picked up.
heldTokens.set(key, { token: cached, until: Date.now() + HELD_TOKEN_MS });
return { ok: true, token: cached };
}
const url = `${FEISHU_API}/open-apis/auth/v3/tenant_access_token/internal`;
try {
@@ -135,6 +268,7 @@ export async function getTenantAccessToken(env: Env): Promise<TokenResult> {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ app_id: appId, app_secret: appSecret }),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
const data = (await res.json().catch(() => null)) as {
code?: number;
@@ -146,8 +280,24 @@ export async function getTenantAccessToken(env: Env): Promise<TokenResult> {
const err = data?.msg ?? `HTTP ${res.status}`;
return { ok: false, error: err, errorCode: "FEISHU_TOKEN", status: res.status };
}
const ttl = Math.max(60, (data.expire ?? 7200) - 60);
await env.KV.put(TOKEN_KEY, data.tenant_access_token, { expirationTtl: ttl });
// Kept in KV until a held copy (read from KV just before it expires
// there, then held HELD_TOKEN_MS) still ends ahead of the token itself.
const ttl = Math.max(60, (data.expire ?? 7200) - 60 - HELD_TOKEN_MS / 1000);
heldTokens.set(key, {
token: data.tenant_access_token,
until: Date.now() + Math.min(HELD_TOKEN_MS, ttl * 1000),
});
// Shared with other isolates when it can be: a failed KV write (say, the
// daily write quota spent) never throws away a valid token — it is then
// held here until it nears expiry, instead of being fetched (and its
// write retried) again every few minutes.
const token = data.tenant_access_token;
// Feishu handing out a token refused before means it is valid after all.
rejected.delete(token);
cleared.delete(token);
await env.KV.put(key, token, { expirationTtl: ttl }).catch(() => {
heldTokens.set(key, { token, until: Date.now() + ttl * 1000 });
});
return { ok: true, token: data.tenant_access_token };
} catch (err) {
return {
@@ -162,12 +312,17 @@ export async function sendMessage(
token: string,
chatId: string,
content: Record<string, unknown>,
sendKey?: string,
): Promise<SendResult> {
const url = `${FEISHU_API}/open-apis/im/v1/messages?receive_id_type=chat_id`;
// One `uuid` for every attempt of this send (of the delivery, given a
// `sendKey`): Feishu delivers at most one message per uuid within an hour,
// so a retry after a lost response never posts a duplicate.
const body: Record<string, unknown> = {
receive_id: chatId,
msg_type: "interactive",
content: JSON.stringify(content),
uuid: (sendKey ?? crypto.randomUUID()).slice(0, 50),
};
return feishuRequest(url, "POST", token, body, chatId);
}
@@ -188,6 +343,7 @@ export async function sendText(token: string, chatId: string, text: string): Pro
receive_id: chatId,
msg_type: "text",
content: JSON.stringify({ text }),
uuid: crypto.randomUUID(),
};
return feishuRequest(url, "POST", token, body, chatId);
}
@@ -197,7 +353,8 @@ export async function updateCard(
cardToken: string,
card: Record<string, unknown>,
): Promise<SendResult> {
const url = `${FEISHU_API}/open-apis/interactive/v1/config/update`;
// Delayed update of an interactive card after a callback (`token` from it).
const url = `${FEISHU_API}/open-apis/interactive/v1/card/update`;
const body: Record<string, unknown> = { token: cardToken, card };
return feishuRequest(url, "POST", token, body, "card");
}
+348 -72
View File
@@ -5,13 +5,31 @@ import {
commentAsUser,
mergePullRequestAsUser,
closePullRequestAsUser,
BeforeGitHubError,
CommandTakenError,
githubRefused,
} from "../../github/oauth";
import { getFeishuLink, removeFeishuLink } from "../../github/store";
import { getTenantAccessToken, sendText, updateCard } from "./rest";
import { getFeishuLink, unlinkAsCommand } from "../../github/store";
import { acceptedKey, claimOnce, confirmClaim, idempotencyStore } from "../../lib/idempotency";
import { forgetRejectedToken, getTenantAccessToken, sendText, updateCard } from "./rest";
const FEISHU_API = "https://open.feishu.cn";
const MAX_BODY_SIZE = 1024 * 1024;
/** Handled event ids are remembered this long; replays of older requests are refused. */
const EVENT_CLAIM_SECONDS = 86400;
/** An event is held this long while it runs (then a retry may take it). */
const EVENT_PROCESSING_SECONDS = 300;
/**
* Oldest signed request accepted. It stays well inside the event claim, so a
* request old enough for its claim to have expired can never run again,
* while Feishu's own retries (spread over hours) still pass.
*/
const MAX_REQUEST_AGE_SECONDS = 12 * 3600;
const MAX_CLOCK_SKEW_SECONDS = 300;
const GITHUB_TARGET_RE = /github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/(\d+)/;
// github.com itself only (never e.g. `evilgithub.com`): actions hit the GitHub API.
const GITHUB_TARGET_RE =
/https?:\/\/(?:www\.)?github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/(\d+)/;
const BTN_PREFIX = "ghpr|";
interface FeishuSender {
@@ -19,6 +37,8 @@ interface FeishuSender {
}
interface FeishuMessage {
chat_id?: string;
/** "p2p" for the private chat with the bot, "group" otherwise. */
chat_type?: string;
content?: string;
}
interface FeishuAction {
@@ -29,9 +49,22 @@ interface FeishuEvent {
sender?: FeishuSender;
message?: FeishuMessage;
action?: FeishuAction;
operator?: { operator_id?: { open_id?: string } };
/** `card.action.trigger` (schema 2.0): the clicking user is `operator.open_id`. */
operator?: { open_id?: string; operator_id?: { open_id?: string } };
token?: string;
open_message_id?: string;
context?: { open_message_id?: string };
}
/** Tracks whether a command reached GitHub, so a failed run knows if it may be retried. */
interface CommandRun {
acted: boolean;
/** Records the command as done right before its GitHub write. */
beforeWrite?: () => Promise<void>;
/** This run wrote the "done" (only then may it give it back). */
markedDone?: boolean;
/** The command's "done" record, for writes committed together with it. */
done?: { key: string; ttl: number };
}
function splitThree(rest: string): [string, string, string] {
@@ -51,31 +84,53 @@ function timingSafeEqual(a: string, b: string): boolean {
return diff === 0;
}
async function hmacSha256Base64(secret: string, payload: string): Promise<string> {
const key = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(secret),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign"],
);
const sig = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(payload));
let binary = "";
const bytes = new Uint8Array(sig);
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i] ?? 0);
return btoa(binary);
async function sha256Hex(data: string): Promise<string> {
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(data));
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
/**
* Feishu event signature (when an Encrypt Key is configured): `X-Lark-Signature`
* is the hex SHA-256 of `timestamp + nonce + encryptKey + rawBody`, with the
* timestamp and nonce from `X-Lark-Request-Timestamp` / `X-Lark-Request-Nonce`.
*/
export async function verifyFeishuSignature(
appSecret: string,
encryptKey: string,
timestamp: string,
nonce: string,
body: string,
signature: string,
): Promise<boolean> {
const raw = `${timestamp}\n${nonce}\n${body}`;
const computed = await hmacSha256Base64(appSecret, raw);
return timingSafeEqual(computed, signature);
if (!signature) return false;
const computed = await sha256Hex(`${timestamp}${nonce}${encryptKey}${body}`);
return timingSafeEqual(computed, signature.toLowerCase());
}
function base64ToBytes(value: string): Uint8Array {
const bin = atob(value);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
return bytes;
}
/**
* Decrypt an `{"encrypt": "..."}` body: AES-256-CBC with key SHA-256(encryptKey)
* and the IV in the first 16 bytes of the base64-decoded ciphertext.
*/
export async function decryptFeishuPayload(encryptKey: string, encrypted: string): Promise<string> {
const keyBytes = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(encryptKey));
const data = base64ToBytes(encrypted);
const key = await crypto.subtle.importKey("raw", keyBytes, { name: "AES-CBC" }, false, [
"decrypt",
]);
const plain = await crypto.subtle.decrypt(
{ name: "AES-CBC", iv: data.slice(0, 16) },
key,
data.slice(16),
);
return new TextDecoder().decode(plain);
}
function describeError(err: unknown): string {
@@ -98,22 +153,50 @@ function extractTarget(text: string): { owner: string; repo: string; number: num
return { owner: m[1] ?? "", repo: m[2] ?? "", number };
}
/** Whether a failed reply may succeed later (then the event is given back). */
function replyRetryable(result: { status?: number; errorCode?: string }): boolean {
if (!result.status || result.status >= 500 || result.status === 429) return true;
return result.errorCode === "FEISHU_TOKEN" || result.errorCode === "RATE_LIMITED";
}
/**
* Send a reply. One that could not be sent for now throws, so the event is
* retried rather than confirmed unanswered (a login link never sent); a
* refusal that a retry cannot change is logged.
*/
async function replyToken(env: Env, chatId: string, text: string): Promise<void> {
const tokenRes = await getTenantAccessToken(env);
if (!tokenRes.ok || !tokenRes.token) {
log.warn({ err: tokenRes.error }, "Feishu token unavailable for reply");
return;
throw new Error("Feishu token unavailable for reply");
}
await sendText(tokenRes.token, chatId, text);
const result = await forgetRejectedToken(
await sendText(tokenRes.token, chatId, text),
env,
tokenRes.token,
);
if (result.ok) return;
log.warn({ chatId, err: result.error }, "Feishu reply failed");
if (replyRetryable(result)) throw new Error(`Feishu reply failed: ${result.error ?? "unknown"}`);
}
async function handleLogin(env: Env, openId: string, chatId: string): Promise<void> {
async function handleLogin(
env: Env,
openId: string,
chatId: string,
chatType: string | undefined,
): Promise<void> {
// An authorization link posted in a group can be opened by anyone there;
// binding happens in the private chat with the bot only.
if (chatType !== "p2p") {
await replyToken(env, chatId, "请私聊机器人发送 /gh login 绑定 GitHub 账号。");
return;
}
const state = crypto.randomUUID();
const pending = {
redirectTo: "/admin",
expiresAt: Date.now() + 10 * 60 * 1000,
feishuUserId: openId,
feishuChatId: chatId,
link: { platform: "feishu", userId: openId, chatId },
};
await env.KV.put(`state:${state}`, JSON.stringify(pending), { expirationTtl: 600 });
const url = getOAuthURL(env.GITHUB_CLIENT_ID ?? "", state);
@@ -128,13 +211,21 @@ async function handleLogin(env: Env, openId: string, chatId: string): Promise<vo
);
}
async function handleLogout(env: Env, openId: string, chatId: string): Promise<void> {
async function handleLogout(
env: Env,
openId: string,
chatId: string,
run: CommandRun,
): Promise<void> {
const linked = await getFeishuLink(env.DB, openId);
if (!linked) {
await replyToken(env, chatId, "你还没有绑定 GitHub 账号。");
return;
}
await removeFeishuLink(env.DB, openId);
await unlinkAsCommand(env, "feishu", openId, run.done);
// Done from here: should the reply fail, a retry must not unlink again
// (by then it could remove a link made since).
run.acted = true;
await replyToken(env, chatId, "已解绑 GitHub 账号。");
}
@@ -143,6 +234,7 @@ async function handleComment(
openId: string,
chatId: string,
text: string,
run: CommandRun,
): Promise<void> {
const githubUserId = await getFeishuLink(env.DB, openId);
if (!githubUserId) {
@@ -159,25 +251,37 @@ async function handleComment(
return;
}
const ghIdx = text.indexOf("/gh");
const rest = text.slice(ghIdx + 3).trim();
const parts = rest.split(/\s+/);
const linkIdx = parts.findIndex((p) => p.includes("github.com"));
const body =
parts
.slice(linkIdx + 1)
.join(" ")
.trim() || "(来自飞书)";
// Only leading space goes: a comment's trailing lines are its own.
const rest = text.slice(ghIdx + 3).replace(/^\s+/, "");
// The comment is what follows the link, as written (line breaks and
// indentation kept).
const link = /\S*github\.com\S*/.exec(rest);
const written = (link ? rest.slice(link.index + link[0].length) : rest).replace(
/^[ \t]*\r?\n?/,
"",
);
const body = written.trim() ? written : "(来自飞书)";
try {
const res = await commentAsUser(
env.KV,
env,
githubUserId,
target.owner,
target.repo,
target.number,
body,
{ beforeWrite: run.beforeWrite },
);
run.acted = true;
await replyToken(env, chatId, `✅ 已评论:[查看](${res.htmlUrl})(@${res.login})`);
} catch (err) {
// Nothing reached GitHub yet: give the event back for a retry.
if (err instanceof BeforeGitHubError) throw err;
// Done, only its reply failed: the event must not run again.
if (run.acted) throw err;
// GitHub was called and may have acted (its answer lost): never run
// again. A refusal did nothing: should its reply fail, the command is
// given back and runs again.
if (!githubRefused(err)) run.acted = true;
await replyToken(env, chatId, `❌ ${describeError(err)}`);
}
}
@@ -188,6 +292,7 @@ async function handleMergeOrClose(
chatId: string,
action: "merge" | "close",
body: string,
run: CommandRun,
): Promise<void> {
const githubUserId = await getFeishuLink(env.DB, openId);
if (!githubUserId) {
@@ -207,25 +312,48 @@ async function handleMergeOrClose(
}
try {
if (action === "merge") {
await mergePullRequestAsUser(env.KV, githubUserId, target.owner, target.repo, target.number);
await mergePullRequestAsUser(
env,
githubUserId,
target.owner,
target.repo,
target.number,
undefined,
{
beforeWrite: run.beforeWrite,
},
);
} else {
await closePullRequestAsUser(env.KV, githubUserId, target.owner, target.repo, target.number);
await closePullRequestAsUser(env, githubUserId, target.owner, target.repo, target.number, {
beforeWrite: run.beforeWrite,
});
}
run.acted = true;
await replyToken(
env,
chatId,
`✅ 已${action === "merge" ? "合并" : "关闭"} ${target.owner}/${target.repo}#${target.number}`,
);
} catch (err) {
// Nothing reached GitHub yet: give the event back for a retry.
if (err instanceof BeforeGitHubError) throw err;
// Done, only its reply failed: the event must not run again.
if (run.acted) throw err;
// GitHub was called and may have acted (its answer lost): never run
// again. A refusal did nothing: should its reply fail, the command is
// given back and runs again.
if (!githubRefused(err)) run.acted = true;
await replyToken(env, chatId, `❌ ${describeError(err)}`);
}
}
async function handleMessage(env: Env, event: FeishuEvent): Promise<void> {
const sender = event.sender ?? {};
const openId = sender.sender_id?.open_id ?? sender.sender_id?.union_id ?? "";
/**
* The `/gh` command a message carries, or null: only a message that is a
* command — `/gh` first, after any @mentions of the bot — runs one; quoting
* or discussing a command never does.
*/
function commandOf(event: FeishuEvent): string | null {
const message = event.message ?? {};
const chatId = message.chat_id ?? "";
let text = "";
if (message.content) {
try {
@@ -235,25 +363,34 @@ async function handleMessage(env: Env, event: FeishuEvent): Promise<void> {
text = "";
}
}
const idx = text.indexOf("/gh");
if (idx < 0) return;
const rest = text.slice(idx + 3).trim();
const command = text.replace(/^\s*(?:@\S+\s+)*/, "");
return /^\/gh(?:\s|$)/.test(command) ? command : null;
}
async function handleMessage(env: Env, event: FeishuEvent, run: CommandRun): Promise<void> {
const sender = event.sender ?? {};
const openId = sender.sender_id?.open_id ?? sender.sender_id?.union_id ?? "";
const message = event.message ?? {};
const chatId = message.chat_id ?? "";
const command = commandOf(event);
if (!command) return;
const rest = command.slice(3).trim();
const sub = rest.split(/\s+/)[0] ?? "";
switch (sub) {
case "login":
await handleLogin(env, openId, chatId);
await handleLogin(env, openId, chatId, message.chat_type);
break;
case "logout":
await handleLogout(env, openId, chatId);
await handleLogout(env, openId, chatId, run);
break;
case "comment":
await handleComment(env, openId, chatId, text);
await handleComment(env, openId, chatId, command, run);
break;
case "merge":
await handleMergeOrClose(env, openId, chatId, "merge", text);
await handleMergeOrClose(env, openId, chatId, "merge", command, run);
break;
case "close":
await handleMergeOrClose(env, openId, chatId, "close", text);
await handleMergeOrClose(env, openId, chatId, "close", command, run);
break;
default:
await replyToken(
@@ -276,13 +413,21 @@ function buildResultCard(ok: boolean, message: string): Record<string, unknown>
};
}
async function handleCardAction(env: Env, payload: Record<string, unknown>): Promise<void> {
async function handleCardAction(
env: Env,
payload: Record<string, unknown>,
run: CommandRun,
): Promise<void> {
const event = (payload.event ?? {}) as FeishuEvent;
const action = event.action ?? {};
const actionId = action.action_id ?? action.value?.v ?? "";
const openId = event.operator?.operator_id?.open_id ?? event.sender?.sender_id?.open_id ?? "";
const openId =
event.operator?.open_id ??
event.operator?.operator_id?.open_id ??
event.sender?.sender_id?.open_id ??
"";
const cardToken = event.token ?? "";
const openMessageId = event.open_message_id ?? "";
const openMessageId = event.context?.open_message_id ?? event.open_message_id ?? "";
if (!actionId.startsWith(BTN_PREFIX)) return;
@@ -303,16 +448,28 @@ async function handleCardAction(env: Env, payload: Record<string, unknown>): Pro
}
try {
if (isMerge) {
await mergePullRequestAsUser(env.KV, githubUserId, owner, repo, number);
await mergePullRequestAsUser(env, githubUserId, owner, repo, number, undefined, {
beforeWrite: run.beforeWrite,
});
} else {
await closePullRequestAsUser(env.KV, githubUserId, owner, repo, number);
await closePullRequestAsUser(env, githubUserId, owner, repo, number, {
beforeWrite: run.beforeWrite,
});
}
run.acted = true;
await replyCard(
env,
cardToken,
buildResultCard(true, `${isMerge ? "已合并" : "已关闭"} ${owner}/${repo}#${number}`),
);
} catch (err) {
if (err instanceof BeforeGitHubError) throw err;
// Done, only its card update failed: the event must not run again.
if (run.acted) throw err;
// GitHub was called and may have acted (its answer lost): never run
// again. A refusal did nothing: should its reply fail, the command is
// given back and runs again.
if (!githubRefused(err)) run.acted = true;
await replyCard(env, cardToken, buildResultCard(false, describeError(err)));
}
return;
@@ -329,12 +486,40 @@ async function replyCard(
card: Record<string, unknown>,
): Promise<void> {
const tokenRes = await getTenantAccessToken(env);
if (!tokenRes.ok || !tokenRes.token) return;
await updateCard(tokenRes.token, cardToken, card);
if (!tokenRes.ok || !tokenRes.token) {
log.warn({ err: tokenRes.error }, "Feishu token unavailable for card update");
throw new Error("Feishu token unavailable for card update");
}
const result = await forgetRejectedToken(
await updateCard(tokenRes.token, cardToken, card),
env,
tokenRes.token,
);
if (result.ok) return;
log.warn({ err: result.error }, "Failed to update the Feishu card");
if (replyRetryable(result))
throw new Error(`Feishu card update failed: ${result.error ?? "unknown"}`);
}
/**
* Inbound Feishu callbacks (events and card actions). Every request must be
* authenticated by the app's own credentials before anything runs:
* - with `FEISHU_ENCRYPT_KEY`, the `X-Lark-Signature` must verify and an
* encrypted body is decrypted with that key;
* - with `FEISHU_VERIFICATION_TOKEN`, the token inside the payload must match.
* At least one of them is required; without either the endpoint refuses all
* requests, since an unauthenticated one could act as any linked user.
*/
export async function handleFeishuWebhookRequest(request: Request, env: Env): Promise<Response> {
// Unauthenticated until verified below: bound what is read and parsed.
const contentLength = Number(request.headers.get("content-length") ?? 0);
if (contentLength > MAX_BODY_SIZE) {
return new Response("Request too large", { status: 413 });
}
const rawBody = await request.text();
if (rawBody.length > MAX_BODY_SIZE) {
return new Response("Request too large", { status: 413 });
}
let payload: Record<string, unknown>;
try {
payload = JSON.parse(rawBody);
@@ -342,20 +527,54 @@ export async function handleFeishuWebhookRequest(request: Request, env: Env): Pr
return new Response("invalid json", { status: 400 });
}
const encryptKey = env.FEISHU_ENCRYPT_KEY?.trim() ?? "";
const verificationToken = env.FEISHU_VERIFICATION_TOKEN?.trim() ?? "";
if (!encryptKey && !verificationToken) {
log.warn("Feishu callback refused: FEISHU_ENCRYPT_KEY / FEISHU_VERIFICATION_TOKEN not set");
return new Response("Feishu callback verification is not configured", { status: 403 });
}
if (encryptKey && typeof payload.encrypt === "string") {
try {
payload = JSON.parse(await decryptFeishuPayload(encryptKey, payload.encrypt));
} catch (err) {
log.warn({ err: String(err) }, "Feishu payload decryption failed");
return new Response("invalid payload", { status: 400 });
}
}
const header = payload.header as Record<string, unknown> | undefined;
const eventPart = payload.event as Record<string, unknown> | undefined;
const type = (payload.type as string) ?? header?.event_type ?? eventPart?.type ?? "";
// The verification token travels inside the (decrypted) payload: in
// `header.token` for schema 2.0 events, at the top level otherwise.
const token = String(header?.token ?? payload.token ?? "");
if (verificationToken && !timingSafeEqual(token, verificationToken)) {
log.warn("Feishu verification token mismatch");
return new Response("invalid token", { status: 401 });
}
if (type === "url_verification") {
return Response.json({ challenge: (payload.challenge as string) ?? "" });
}
const appSecret = env.FEISHU_APP_SECRET?.trim() ?? "";
const signature = request.headers.get("x-lark-signature") ?? "";
const timestamp = request.headers.get("x-lark-timestamp") ?? "";
const nonce = request.headers.get("x-lark-nonce") ?? "";
if (appSecret && signature) {
const ok = await verifyFeishuSignature(appSecret, timestamp, nonce, rawBody, signature);
if (encryptKey) {
const timestamp = request.headers.get("x-lark-request-timestamp") ?? "";
// The signature covers the timestamp: a stale one marks a replay that
// may outlive the event claim below.
const age = Math.floor(Date.now() / 1000) - Number(timestamp);
if (!Number.isFinite(age) || age > MAX_REQUEST_AGE_SECONDS || age < -MAX_CLOCK_SKEW_SECONDS) {
log.warn({ timestamp }, "Feishu callback refused: request timestamp out of range");
return new Response("stale request", { status: 401 });
}
const ok = await verifyFeishuSignature(
encryptKey,
timestamp,
request.headers.get("x-lark-request-nonce") ?? "",
rawBody,
request.headers.get("x-lark-signature") ?? "",
);
if (!ok) {
log.warn("Feishu signature verification failed");
return new Response("invalid signature", { status: 401 });
@@ -363,16 +582,73 @@ export async function handleFeishuWebhookRequest(request: Request, env: Env): Pr
}
const eventType = (header?.event_type as string) ?? eventPart?.type ?? type;
try {
if (eventType === "im.message.receive_v1" || eventType === "message") {
await handleMessage(env, (payload.event ?? payload) as FeishuEvent);
} else if (eventType === "card.action.trigger") {
await handleCardAction(env, payload);
}
} catch (err) {
log.error({ err }, "Feishu webhook handling failed");
const isMessage = eventType === "im.message.receive_v1" || eventType === "message";
// Nothing to do for an ordinary chat message (no `/gh` command) or an
// event type not handled: answered at once, with no dedup records written
// (each would be D1 writes for an event that has no side effect).
if (
(isMessage && !commandOf((payload.event ?? payload) as FeishuEvent)) ||
(!isMessage && eventType !== "card.action.trigger")
) {
return new Response("ok", { status: 200 });
}
// Feishu retries a callback it considers failed; run each event once. An
// event whose earlier run is unconfirmed is retried later, never dropped.
const eventId = String(header?.event_id ?? payload.uuid ?? "");
const claims = idempotencyStore(env.DB, env.KV);
const claimKey = `bot:feishu:${eventId}`;
if (eventId) {
const state = await claimOnce(claims, claimKey, EVENT_PROCESSING_SECONDS);
if (state === "done") return new Response("ok", { status: 200 });
if (state === "pending") return new Response("temporarily unavailable", { status: 503 });
}
const run: CommandRun = { acted: false };
// Done on record before GitHub is written to: should the confirmation
// after it fail, a retry still never repeats the write.
if (eventId) {
run.done = { key: acceptedKey(claimKey), ttl: EVENT_CLAIM_SECONDS };
run.beforeWrite = async (): Promise<void> => {
// Taken: a run that outlived its claim finds another run's "done" here
// and writes nothing.
if (!(await claims.claim(acceptedKey(claimKey), EVENT_CLAIM_SECONDS)))
throw new CommandTakenError();
run.markedDone = true;
};
}
try {
if (isMessage) {
await handleMessage(env, (payload.event ?? payload) as FeishuEvent, run);
} else if (eventType === "card.action.trigger") {
await handleCardAction(env, payload, run);
}
} catch (err) {
// Finished by another run meanwhile: nothing to give back or repeat.
if (err instanceof CommandTakenError) return new Response("ok", { status: 200 });
log.error({ err }, "Feishu webhook handling failed");
// Failed before reaching GitHub: give the event back and answer with an
// error, so Feishu's retry runs it. After GitHub acted it stays claimed.
if (!run.acted) {
if (eventId) {
// An early "done" this run wrote (right before a write GitHub
// refused) too — never one another run wrote meanwhile.
if (run.markedDone) {
await claims.release(acceptedKey(claimKey)).catch(() => undefined);
}
await claims.release(claimKey).catch(() => undefined);
}
return new Response("temporarily unavailable", { status: 503 });
}
}
// A "done" already written before the GitHub write needs no second write.
if (eventId && !run.markedDone) {
// Best-effort: unconfirmed, the claim still holds off retries until it expires.
await confirmClaim(claims, claimKey, EVENT_CLAIM_SECONDS).catch((err: unknown) =>
log.warn({ err, eventId }, "Failed to confirm a Feishu event"),
);
}
return new Response("ok", { status: 200 });
}
+17
View File
@@ -16,3 +16,20 @@ export function getDriver(target: RouteTarget): PlatformDriver {
if (!driver) throw new Error(`No driver for platform "${platform}"`);
return driver;
}
/**
* Where a target's messages actually go: two targets naming the same place
* (a Discord thread given alone, or with its parent channel) are one
* destination, and get one copy of each message.
*/
export function destinationOf(target: RouteTarget): string {
const platform = target.platform ?? "discord";
if (platform === "telegram") {
// Read as the driver sends it (a number): `01` and `1` are one topic.
const topic = target.topicId ?? "";
const id = topic && Number.isFinite(Number(topic)) ? String(Number(topic)) : topic;
return `telegram:${target.chatId ?? ""}/${id}`;
}
if (platform === "feishu") return `feishu:${target.chatId ?? ""}`;
return `discord:${target.threadId || target.channelId || ""}`;
}
+238 -30
View File
@@ -4,13 +4,25 @@ import {
commentAsUser,
mergePullRequestAsUser,
closePullRequestAsUser,
BeforeGitHubError,
CommandTakenError,
githubRefused,
} from "../../github/oauth";
import { getTelegramLink, removeTelegramLink } from "../../github/store";
import { getTelegramLink, unlinkAsCommand } from "../../github/store";
import { acceptedKey, claimOnce, confirmClaim, idempotencyStore } from "../../lib/idempotency";
import type { Env } from "../../types";
import { sendMessage } from "./rest";
import { telegramWebhookSecret } from "./secret";
const GITHUB_ISSUE_RE = /github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/(\d+)/;
const GITHUB_PR_RE = /github\.com\/([^/\s]+)\/([^/\s]+)\/pull\/(\d+)/;
// github.com itself only (never e.g. `evilgithub.com`): actions hit the GitHub API.
/** An update is held this long while its command runs (then a redelivery may take it). */
const COMMAND_CLAIM_SECONDS = 300;
/** Handled updates are remembered this long. */
const COMMAND_DONE_SECONDS = 86400;
const GITHUB_ISSUE_RE =
/^https?:\/\/(?:www\.)?github\.com\/([^/\s]+)\/([^/\s]+)\/(?:issues|pull)\/(\d+)/;
const GITHUB_PR_RE = /^https?:\/\/(?:www\.)?github\.com\/([^/\s]+)\/([^/\s]+)\/pull\/(\d+)/;
interface TelegramMessage {
message_id?: number;
@@ -23,6 +35,26 @@ interface TelegramMessage {
reply_to_message?: TelegramMessage;
}
let knownBotUsername: { token: string; name: string } | null = null;
/** This bot's username (lower case), from `getMe`; remembered per isolate. */
async function botUsername(env: Env): Promise<string> {
const token = env.TELEGRAM_TOKEN ?? "";
if (knownBotUsername?.token === token) return knownBotUsername.name;
const res = await fetch(`https://api.telegram.org/bot${token}/getMe`, {
signal: AbortSignal.timeout(15_000),
});
const data = (await res.json().catch(() => null)) as {
result?: { username?: string };
} | null;
const name = data?.result?.username;
// Unknown: thrown, so Telegram redelivers the update rather than the
// command being dropped.
if (!res.ok || !name) throw new Error(`Telegram getMe failed (HTTP ${res.status})`);
knownBotUsername = { token, name: name.toLowerCase() };
return knownBotUsername.name;
}
interface Target {
owner: string;
repo: string;
@@ -54,13 +86,23 @@ function extractTarget(msg: TelegramMessage, prOnly = false): Target | null {
return null;
}
/**
* Send a reply. One that could not be sent for now throws, so the update is
* redelivered rather than confirmed unanswered (a login link never sent);
* a refusal that a retry cannot change (e.g. the bot was blocked) is logged.
*/
async function reply(
env: Env,
chatId: string,
topicId: string | undefined,
text: string,
): Promise<void> {
await sendMessage(env.TELEGRAM_TOKEN ?? "", chatId, text, topicId);
const result = await sendMessage(env.TELEGRAM_TOKEN ?? "", chatId, text, topicId);
if (result.ok) return;
log.warn({ chatId, err: result.error }, "Telegram reply failed");
if (!result.status || result.status >= 500 || result.status === 429) {
throw new Error(`Telegram reply failed: ${result.error ?? "unknown"}`);
}
}
function errText(err: unknown): string {
@@ -77,17 +119,23 @@ async function cmdLogin(env: Env, msg: TelegramMessage): Promise<void> {
if (!chatId || !telegramUserId) return;
const topicId = msg.message_thread_id != null ? String(msg.message_thread_id) : undefined;
// An authorization link posted in a group can be opened by anyone there;
// binding happens in the private chat with the bot only.
if (msg.chat?.type !== "private") {
return reply(env, chatId, topicId, "请私聊机器人发送 /gh login 绑定 GitHub 账号。");
}
const clientId = env.GITHUB_CLIENT_ID;
if (!clientId)
return reply(env, chatId, topicId, "服务器未配置 GitHub OAuth(GITHUB_CLIENT_ID)。");
const name = msg.from?.username ? `@${msg.from.username}` : msg.from?.first_name;
const state = crypto.randomUUID().replace(/-/g, "");
await env.KV.put(
`state:${state}`,
JSON.stringify({
redirectTo: "/",
telegramUserId,
telegramChatId: chatId,
link: { platform: "telegram", userId: telegramUserId, name, chatId },
expiresAt: Date.now() + 600_000,
}),
{ expirationTtl: 600 },
@@ -97,20 +145,39 @@ async function cmdLogin(env: Env, msg: TelegramMessage): Promise<void> {
env,
chatId,
topicId,
`点击链接授权 GitHub,即可用**本人身份**评论(10 分钟内有效):\n${url}`,
`点击链接授权 GitHub,即可用<b>本人身份</b>评论(10 分钟内有效):\n${url}`,
);
}
async function cmdLogout(env: Env, msg: TelegramMessage): Promise<void> {
async function cmdLogout(env: Env, msg: TelegramMessage, run: CommandRun): Promise<void> {
const chatId = chatIdOf(msg);
const telegramUserId = userIdOf(msg);
if (!chatId || !telegramUserId) return;
const topicId = msg.message_thread_id != null ? String(msg.message_thread_id) : undefined;
await removeTelegramLink(env.DB, telegramUserId);
await unlinkAsCommand(env, "telegram", telegramUserId, run.done);
// Done from here: should the reply fail, a redelivery must not unlink again
// (by then it could remove a link made since).
run.acted = true;
await reply(env, chatId, topicId, "已解绑你的 GitHub 账号。");
}
async function cmdComment(env: Env, msg: TelegramMessage, body: string): Promise<void> {
/** Tracks whether a command reached GitHub, so a failed run knows if it may be retried. */
interface CommandRun {
acted: boolean;
/** Records the command as done right before its GitHub write. */
beforeWrite?: () => Promise<void>;
/** This run wrote the "done" (only then may it give it back). */
markedDone?: boolean;
/** The command's "done" record, for writes committed together with it. */
done?: { key: string; ttl: number };
}
async function cmdComment(
env: Env,
msg: TelegramMessage,
body: string,
run: CommandRun,
): Promise<void> {
const chatId = chatIdOf(msg);
const telegramUserId = userIdOf(msg);
if (!chatId || !telegramUserId) return;
@@ -134,20 +201,35 @@ async function cmdComment(env: Env, msg: TelegramMessage, body: string): Promise
try {
const { htmlUrl, login } = await commentAsUser(
env.KV,
env,
githubUserId,
target.owner,
target.repo,
target.number,
body,
{ beforeWrite: run.beforeWrite },
);
run.acted = true;
await reply(env, chatId, topicId, `已以 **@${login}** 身份评论:${htmlUrl}`);
} catch (err) {
// Nothing reached GitHub yet: give the update back for a retry.
if (err instanceof BeforeGitHubError) throw err;
// Done, only its reply failed: the update must not run again.
if (run.acted) throw err;
// GitHub was called and may have acted (its answer lost): never run
// again. A refusal did nothing: should its reply fail, the command is
// given back and runs again.
if (!githubRefused(err)) run.acted = true;
await reply(env, chatId, topicId, errText(err));
}
}
async function cmdMergeClose(env: Env, msg: TelegramMessage, op: "merge" | "close"): Promise<void> {
async function cmdMergeClose(
env: Env,
msg: TelegramMessage,
op: "merge" | "close",
run: CommandRun,
): Promise<void> {
const chatId = chatIdOf(msg);
const telegramUserId = userIdOf(msg);
if (!chatId || !telegramUserId) return;
@@ -171,10 +253,23 @@ async function cmdMergeClose(env: Env, msg: TelegramMessage, op: "merge" | "clos
try {
if (op === "merge") {
await mergePullRequestAsUser(env.KV, githubUserId, target.owner, target.repo, target.number);
await mergePullRequestAsUser(
env,
githubUserId,
target.owner,
target.repo,
target.number,
undefined,
{
beforeWrite: run.beforeWrite,
},
);
} else {
await closePullRequestAsUser(env.KV, githubUserId, target.owner, target.repo, target.number);
await closePullRequestAsUser(env, githubUserId, target.owner, target.repo, target.number, {
beforeWrite: run.beforeWrite,
});
}
run.acted = true;
const label = op === "merge" ? "合并" : "关闭";
await reply(
env,
@@ -183,6 +278,14 @@ async function cmdMergeClose(env: Env, msg: TelegramMessage, op: "merge" | "clos
`✅ 已${label} PR ${target.owner}/${target.repo}#${target.number}`,
);
} catch (err) {
// Nothing reached GitHub yet: give the update back for a retry.
if (err instanceof BeforeGitHubError) throw err;
// Done, only its reply failed: the update must not run again.
if (run.acted) throw err;
// GitHub was called and may have acted (its answer lost): never run
// again. A refusal did nothing: should its reply fail, the command is
// given back and runs again.
if (!githubRefused(err)) run.acted = true;
await reply(env, chatId, topicId, errText(err));
}
}
@@ -193,21 +296,45 @@ export async function syncTelegramWebhook(env: Env): Promise<void> {
if (!token) return;
const baseUrl = env.BASE_URL;
if (!baseUrl) return;
const secret = env.TELEGRAM_WEBHOOK_SECRET;
// Always registered with a secret (explicit or derived from the bot token),
// so the webhook endpoint can authenticate every call.
const secret = await telegramWebhookSecret(env);
const registration = {
url: `${baseUrl.replace(/\/$/, "")}/telegram/webhook`,
secret_token: secret,
allowed_updates: ["message"],
};
// Registered again only when the registration changed (or once a day, to
// repair a webhook changed elsewhere): this runs every few minutes, and
// each run would otherwise be a subrequest. One KV read per run; a write
// only when it registers.
const digest = [
...new Uint8Array(
await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(`${token}|${JSON.stringify(registration)}`),
),
),
]
.slice(0, 12)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
// One record per bot holding what it is registered with now: switching
// A -> B -> A registers A again (a record per digest would still hold A's).
const marker = `telegram:webhook:${token.split(":")[0]}`;
if ((await env.KV.get(marker).catch(() => null)) === digest) return;
const res = await fetch(`https://api.telegram.org/bot${token}/setWebhook`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
url: `${baseUrl.replace(/\/$/, "")}/telegram/webhook`,
secret_token: secret || undefined,
allowed_updates: ["message"],
}),
body: JSON.stringify(registration),
});
if (!res.ok) {
const err = await res.text();
log.error({ status: res.status, err }, "Failed to set Telegram webhook");
return;
}
await env.KV.put(marker, digest, { expirationTtl: 86400 }).catch(() => undefined);
}
/** Handle a single Telegram update (message). Called from the webhook route. */
@@ -215,32 +342,113 @@ export async function handleTelegramUpdate(env: Env, update: unknown): Promise<v
const message = (update as { message?: TelegramMessage })?.message;
if (!message?.text) return;
const text = message.text.trim();
const m = text.match(/^\/gh(?:\s+|$)(.*)$/s);
// Only leading space goes: a comment's trailing lines are its own.
const text = message.text.replace(/^\s+/, "");
// `/gh@BotName ...` is how group chats address one bot among several: run
// it only when the name is this bot's.
const m = text.match(/^\/gh(?:@(\w+))?(?:\s+|$)(.*)$/s);
if (!m) return;
if (m[1] && m[1].toLowerCase() !== (await botUsername(env))) return;
const rest = (m[1] ?? "").trim();
const [sub, ...args] = rest.split(/\s+/);
const body = args.join(" ").trim();
// Telegram redelivers an update until the webhook answers 200; a command
// that already ran (e.g. its reply failed afterwards) must not run again,
// and one whose run is unconfirmed is retried later, never dropped.
const updateId = (update as { update_id?: number }).update_id;
const claims = idempotencyStore(env.DB, env.KV);
// Update ids count per bot: the key names the bot (the id before the
// token's ":", stable across token changes), so another bot's update with
// the same number is never taken for this one.
const botId = (env.TELEGRAM_TOKEN ?? "").split(":")[0] ?? "";
const claimKey = `bot:telegram:${botId}:${updateId}`;
if (updateId != null) {
// Handled under the key used before bots were told apart (kept one day).
if (await claims.has(acceptedKey(`bot:telegram:${updateId}`))) return;
const state = await claimOnce(claims, claimKey, COMMAND_CLAIM_SECONDS);
if (state === "done") return;
if (state === "pending") throw new Error("Telegram update is already being processed");
}
const rest = (m[2] ?? "").replace(/^\s+/, "");
const sub = rest.split(/\s/, 1)[0] ?? "";
// The comment as written: line breaks and indentation kept (only the
// separator after the subcommand goes — up to its first line break).
const body = rest.slice(sub.length).replace(/^[ \t]*\r?\n?/, "");
const run: CommandRun = { acted: false };
// Done on record before GitHub is written to: should the confirmation
// after it fail, a redelivery still never repeats the write.
if (updateId != null) {
run.done = { key: acceptedKey(claimKey), ttl: COMMAND_DONE_SECONDS };
run.beforeWrite = async (): Promise<void> => {
// Taken: a run that outlived its claim finds another run's "done" here
// and writes nothing.
if (!(await claims.claim(acceptedKey(claimKey), COMMAND_DONE_SECONDS)))
throw new CommandTakenError();
run.markedDone = true;
};
}
try {
await runCommand(env, message, sub, body, text, run);
} catch (err) {
// Finished by another run meanwhile: nothing to give back or repeat.
if (err instanceof CommandTakenError) return;
// Failed before reaching GitHub (e.g. a storage error) or refused by it:
// give the update back (an early "done" too), so Telegram's redelivery
// runs it. After GitHub may have acted, it is done for good — running
// again would repeat the comment / merge.
if (updateId != null) {
if (run.acted) {
// Done for good; a "done" written before the GitHub write needs no
// second write.
if (!run.markedDone) await confirmDone();
} else {
// Only a "done" this run wrote: one another run wrote while this one
// outlived its claim stays.
if (run.markedDone) {
await claims.release(acceptedKey(claimKey)).catch(() => undefined);
}
await claims.release(claimKey).catch(() => undefined);
}
}
throw err;
}
// A "done" already written before the GitHub write needs no second write.
if (updateId != null && !run.markedDone) await confirmDone();
/** Best-effort: unconfirmed, the claim still holds off redeliveries until it expires. */
async function confirmDone(): Promise<void> {
await confirmClaim(claims, claimKey, COMMAND_DONE_SECONDS).catch((err: unknown) =>
log.warn({ err, updateId }, "Failed to confirm a Telegram update"),
);
}
}
async function runCommand(
env: Env,
message: TelegramMessage,
sub: string | undefined,
body: string,
text: string,
run: CommandRun,
): Promise<void> {
switch (sub) {
case "login":
return cmdLogin(env, message);
case "logout":
return cmdLogout(env, message);
return cmdLogout(env, message, run);
case "comment":
if (!body) {
if (!body.trim()) {
const chatId = chatIdOf(message);
const topicId =
message.message_thread_id != null ? String(message.message_thread_id) : undefined;
if (chatId) await reply(env, chatId, topicId, "请附上评论内容:/gh comment 你的评论");
return;
}
return cmdComment(env, message, body);
return cmdComment(env, message, body, run);
case "merge":
return cmdMergeClose(env, message, "merge");
return cmdMergeClose(env, message, "merge", run);
case "close":
return cmdMergeClose(env, message, "close");
return cmdMergeClose(env, message, "close", run);
default:
log.info({ text }, "Unhandled /gh command from Telegram");
}
+25 -21
View File
@@ -1,7 +1,7 @@
import MarkdownIt from "markdown-it";
import type { RouteTarget, Env, NeutralMessage } from "../../types";
import type { PlatformDriver, SendResult } from "../types";
import { sendMessage, sendPhoto, editMessageText, editMessageCaption } from "./rest";
import { sendMessage, editMessageText } from "./rest";
import { renderNeutralMessage } from "./render";
const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false });
@@ -23,6 +23,19 @@ function richHeaderUrl(message: NeutralMessage, avatar: string, host: string): s
return `${host.replace(/\/+$/, "")}/api/richheader?${params.toString()}`;
}
/**
* The avatar shown above the text as a link preview: the rich-header card
* when a host serves it, the plain (small) avatar image otherwise. Messages
* are always text messages (never photos with captions), so every edit is an
* `editMessageText` with the full 4096-character budget.
*/
function previewUrl(message: NeutralMessage, env: Env): string | undefined {
const avatar = message.author?.iconUrl;
if (!avatar) return undefined;
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
return richHeaderHost ? richHeaderUrl(message, avatar, richHeaderHost) : smallAvatar(avatar);
}
export class TelegramDriver implements PlatformDriver {
readonly id = "telegram";
@@ -33,16 +46,7 @@ export class TelegramDriver implements PlatformDriver {
}
const token = env.TELEGRAM_TOKEN ?? "";
const text = renderNeutralMessage(message);
const avatar = message.author?.iconUrl;
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
if (avatar && richHeaderHost) {
const rhUrl = richHeaderUrl(message, avatar, richHeaderHost);
return sendMessage(token, chatId, text, target.topicId, rhUrl);
}
if (avatar) {
return sendPhoto(token, chatId, smallAvatar(avatar), text, target.topicId);
}
return sendMessage(token, chatId, text, target.topicId);
return sendMessage(token, chatId, text, target.topicId, previewUrl(message, env));
}
async edit(
@@ -57,15 +61,15 @@ export class TelegramDriver implements PlatformDriver {
}
const token = env.TELEGRAM_TOKEN ?? "";
const text = renderNeutralMessage(message);
const avatar = message.author?.iconUrl;
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
if (avatar && richHeaderHost) {
const rhUrl = richHeaderUrl(message, avatar, richHeaderHost);
return editMessageText(token, chatId, messageId, text, target.topicId, rhUrl);
}
if (avatar) {
return editMessageCaption(token, chatId, messageId, text, target.topicId);
}
return editMessageText(token, chatId, messageId, text, target.topicId);
// A photo sent by an older version has no text to edit; Telegram's error
// then makes dispatch send a fresh message instead.
return editMessageText(
token,
chatId,
messageId,
text,
target.topicId,
previewUrl(message, env),
);
}
}
+47 -17
View File
@@ -19,6 +19,9 @@ md.renderer.rules.strong_open = (): string => "<b>";
md.renderer.rules.strong_close = (): string => "</b>";
md.renderer.rules.em_open = (): string => "<i>";
md.renderer.rules.em_close = (): string => "</i>";
// Telegram's HTML has no <br>: a Markdown hard break ("two spaces, newline")
// would get the whole message rejected. A plain newline breaks the line there.
md.renderer.rules.hardbreak = (): string => "\n";
function mdToHtml(s: string): string {
return md.renderInline(s);
@@ -37,26 +40,53 @@ function formatTimestamp(ts?: string): string {
* an incomplete trailing tag and closes every tag still open at the cut
* point, so parse_mode HTML never rejects the message.
*/
/**
* Clamp HTML to `max` characters as Telegram counts them — the text shown,
* tags left out and an entity (`&amp;`) as one character — keeping tags and
* entities whole and closing the tags still open (an ellipsis marks the cut).
*/
function capHtml(text: string, max: number): string {
if (text.length <= max) return text;
let cut = text.slice(0, max).replace(/<[^>]*$/u, "");
const stack: string[] = [];
const re = /<(\/?)([a-z]+)(?:\s[^>]*)?>/g;
let m: RegExpExecArray | null;
while ((m = re.exec(cut)) !== null) {
const tag = m[2]!;
if (m[1] === "/") {
const open = stack.lastIndexOf(tag);
if (open >= 0) stack.splice(open);
} else {
stack.push(tag);
let shown = 0;
let i = 0;
while (i < text.length) {
if (text[i] === "<") {
const end = text.indexOf(">", i);
if (end < 0) break;
const m = /^<(\/?)([a-z]+)/.exec(text.slice(i, end + 1));
if (m) {
const tag = m[2]!;
if (m[1] === "/") {
const open = stack.lastIndexOf(tag);
if (open >= 0) stack.splice(open);
} else {
stack.push(tag);
}
}
i = end + 1;
continue;
}
const entity =
text[i] === "&" ? /^&(?:#\d+|#x[0-9a-f]+|[a-z]+);/i.exec(text.slice(i, i + 12)) : null;
const code = text.charCodeAt(i);
// A surrogate pair counts (and is cut) as the two code units it is.
const step = entity ? entity[0].length : code >= 0xd800 && code <= 0xdbff ? 2 : 1;
const weight = entity ? 1 : step;
// One character is kept for the ellipsis.
if (shown + weight > max - 1) {
const rest = text.slice(i).replace(/<[^>]*>/g, "");
// What is left fits after all (no ellipsis needed).
if (shown + rest.replace(/&(?:#\d+|#x[0-9a-f]+|[a-z]+);/gi, "x").length <= max) return text;
const closers = [...stack]
.reverse()
.map((tag) => `</${tag}>`)
.join("");
return `${text.slice(0, i)}…${closers}`;
}
shown += weight;
i += step;
}
const closers = stack
.reverse()
.map((tag) => `</${tag}>`)
.join("");
return `${cut}…${closers}`;
return text;
}
export function renderNeutralMessage(message: NeutralMessage): string {
@@ -76,7 +106,7 @@ export function renderNeutralMessage(message: NeutralMessage): string {
if (message.author) {
const name = mdToHtml(message.author.name);
const author = message.author.url ? `<a href="${esc(message.author.url)}">${name}</a>` : name;
parts.push(`👤 ${author}`);
parts.push(message.emoji === false ? author : `👤 ${author}`);
}
if (message.description) {
+42 -2
View File
@@ -7,8 +7,21 @@ interface TelegramResponse {
ok?: boolean;
description?: string;
result?: { message_id?: number };
parameters?: { retry_after?: number };
}
/**
* Longest rate-limit wait (seconds) slept inside one request; a longer one is
* returned as a 429 failure so the delivery queue retries it later instead.
*/
const MAX_INLINE_RETRY_AFTER = 10;
/**
* Longest a single API call may take. Bounded, so a message edit always ends
* well within the update-chain lock it runs under (see `deliverUpdate`).
*/
export const REQUEST_TIMEOUT_MS = 15_000;
async function post(
token: string,
method: string,
@@ -25,13 +38,25 @@ async function post(
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
lastStatus = res.status;
const data = (await res.json().catch(() => null)) as TelegramResponse | null;
if (res.status === 429) {
const retryAfter = (data as { retry_after?: number })?.retry_after ?? 1;
// Telegram reports the wait in `parameters.retry_after` (seconds).
const raw = Number(data?.parameters?.retry_after);
const retryAfter = Number.isFinite(raw) && raw > 0 ? raw : 1;
lastError = data?.description ?? `Rate limited (retry_after=${retryAfter})`;
if (retryAfter > MAX_INLINE_RETRY_AFTER || attempt === 2) {
return {
ok: false,
error: lastError,
errorCode: "RATE_LIMITED",
status: 429,
attempts: attempt + 1,
};
}
await new Promise((r) => setTimeout(r, retryAfter * 1000));
continue;
}
@@ -51,10 +76,25 @@ async function post(
};
}
// Accepted, but the answer is unreadable (cut off, timed out): the
// message may well exist, and Telegram has no idempotency key, so a
// retry could post it twice — a visible failure, not retried (and not
// a success without a message id, which an update chain would lose).
if (data?.ok !== true) {
log.error({ status: res.status, chatId }, "Unreadable Telegram response");
return {
ok: false,
error: "Telegram accepted the request, but its answer could not be read",
errorCode: "TELEGRAM_UNCERTAIN",
status: res.status,
attempts: attempt + 1,
};
}
return {
ok: true,
status: res.status,
messageId: data?.result?.message_id != null ? String(data.result.message_id) : undefined,
messageId: data.result?.message_id != null ? String(data.result.message_id) : undefined,
attempts: attempt + 1,
};
} catch (err) {
+22
View File
@@ -0,0 +1,22 @@
import type { Env } from "../../types";
/**
* The `secret_token` Telegram must send with every webhook call. It is
* `TELEGRAM_WEBHOOK_SECRET` when set, otherwise derived from the bot token
* (which is itself secret), so the webhook endpoint is never left
* unauthenticated. Empty when no bot token is configured.
*/
export async function telegramWebhookSecret(env: Env): Promise<string> {
const explicit = env.TELEGRAM_WEBHOOK_SECRET?.trim();
if (explicit) return explicit;
const token = env.TELEGRAM_TOKEN?.trim();
if (!token) return "";
const digest = await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(`webhooker:telegram-webhook:${token}`),
);
// Telegram allows 1-256 characters of A-Z, a-z, 0-9, `_` and `-`.
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
+12 -6
View File
@@ -1,5 +1,7 @@
import type { Env } from "../../types";
import { log } from "../../lib/log";
import { handleTelegramUpdate } from "./commands";
import { telegramWebhookSecret } from "./secret";
const MAX_BODY_SIZE = 1024 * 1024;
@@ -19,12 +21,16 @@ export async function handleTelegramWebhookRequest(request: Request, env: Env):
return new Response("Request too large", { status: 413 });
}
const secret = env.TELEGRAM_WEBHOOK_SECRET;
if (secret) {
const token = request.headers.get("X-Telegram-Bot-Api-Secret-Token");
if (!token || !timingSafeEqual(token, secret)) {
return new Response("Invalid secret", { status: 401 });
}
// Every call must carry the webhook secret. Without a bot token there is
// no secret to check and nothing to act with, so the endpoint stays closed.
const secret = await telegramWebhookSecret(env);
if (!secret) {
log.warn("Telegram webhook called but TELEGRAM_TOKEN is not configured");
return new Response("Telegram is not configured", { status: 403 });
}
const token = request.headers.get("X-Telegram-Bot-Api-Secret-Token");
if (!token || !timingSafeEqual(token, secret)) {
return new Response("Invalid secret", { status: 401 });
}
const rawBody = await request.text();