Let bots manage their hooks, with the user confirming what widens them

Bots get list_hooks, pause_hook and delete_hook, which act directly since
they only take away. A new hook, or a new name, brief or filter, or
switching a paused one back on, goes through propose_hook: it stores a
hook_proposals row and puts a [hook-proposal:ID] card in the reply, which
the user it was proposed to confirms or rejects. The card is read from
what Confirm saves, with the bot's reason labelled as its own; confirming
checks everything again and saves through the Hooks modal's helpers. A
new hook's URL is shown on the card once, to the user only, never in the
outcome the bot is told. What calls a hook, its computers and its signing
are never proposed. Only chat replies propose, and a hook call's own run
gets no hook tools, so a caller's payload can't reach them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-25 15:18:54 +01:00
co-authored by Claude Opus 5.5
parent 045167715d
commit 380d3ebb2f
54 changed files with 1983 additions and 55 deletions
@@ -0,0 +1,52 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import confirmHookProposal from "@/src/functions/backend/db/hook-proposals/confirm-hook-proposal";
import grabHookProposalView from "@/src/functions/backend/db/hook-proposals/grab-hook-proposal-view";
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
import { HookProposalIdRequestSchema } from "@/src/schemas/bot-hook-schema";
import type { HookProposalDecision } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Confirms a hook a bot proposed to the session user, which makes or
* changes it as theirs. Returns the proposal with its outcome (which the
* card then tells the bot in chat) and a new hook's token, shown this
* once on the card and never to the bot.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<HookProposalDecision>> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user, user_types } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
if (isReadOnlyUser({ user_types: user_types || [] })) {
throw new AppError(`This user can only read records.`);
}
const { proposal_id } = parseWithSchema({ schema: HookProposalIdRequestSchema, data: body });
const { proposal, token } = await confirmHookProposal({ user_id: user.id, proposal_id });
const view = await grabHookProposalView({ proposal_id, user_id: user.id });
await logEvent({
title: view.status == "failed" ? "Hook proposal failed" : "Hook proposal confirmed",
category: "hook",
user_id: user.id,
plain_text: `User #${user.id} confirmed the hook proposal #${proposal.id} (${proposal.action} "${proposal.title}") from bot #${proposal.bot_id}: ${view.outcome}`,
metadata: { proposal_id: proposal.id, action: proposal.action, bot_id: proposal.bot_id, hook_id: view.result_id },
ip_address: grabRequestIP(req),
});
return { success: true, msg: view.outcome, singleRes: { view, token } };
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
@@ -0,0 +1,32 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabHookProposalView from "@/src/functions/backend/db/hook-proposals/grab-hook-proposal-view";
import { HookProposalIdRequestSchema } from "@/src/schemas/bot-hook-schema";
import type { HookProposalView } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* `GET ?proposal_id=`: a hook a bot proposed to the session user, as its
* chip and confirm card show it. Nobody else reads it.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<HookProposalView>> = async ({ req, query }) => {
try {
if (req.method != "GET") {
throw new AppError(`Method not allowed`);
}
const { user } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
const { proposal_id } = parseWithSchema({ schema: HookProposalIdRequestSchema, data: query });
return { success: true, singleRes: await grabHookProposalView({ proposal_id, user_id: user.id }) };
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
@@ -0,0 +1,48 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabHookProposalView from "@/src/functions/backend/db/hook-proposals/grab-hook-proposal-view";
import rejectHookProposal from "@/src/functions/backend/db/hook-proposals/reject-hook-proposal";
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
import { HookProposalIdRequestSchema } from "@/src/schemas/bot-hook-schema";
import type { HookProposalDecision } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Rejects a hook a bot proposed to the session user. Nothing is saved.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<HookProposalDecision>> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user, user_types } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
if (isReadOnlyUser({ user_types: user_types || [] })) {
throw new AppError(`This user can only read records.`);
}
const { proposal_id } = parseWithSchema({ schema: HookProposalIdRequestSchema, data: body });
const proposal = await rejectHookProposal({ user_id: user.id, proposal_id });
await logEvent({
title: "Hook proposal rejected",
category: "hook",
user_id: user.id,
plain_text: `User #${user.id} rejected the hook proposal #${proposal.id} (${proposal.action} "${proposal.title}") from bot #${proposal.bot_id}.`,
metadata: { proposal_id: proposal.id, action: proposal.action, bot_id: proposal.bot_id },
ip_address: grabRequestIP(req),
});
return { success: true, singleRes: { view: await grabHookProposalView({ proposal_id, user_id: user.id }) } };
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};