Files
sencho/backend/src/routes/webhooks.ts
T
Anso 865d792874 feat(pricing): collapse to two tiers (#1309)
* feat(pricing): collapse to two tiers (Community + Admiral)

Collapse Sencho's pricing from three tiers (Community / Skipper / Admiral)
to two: a generous free Community tier and a single paid Admiral tier. The
Skipper tier is removed.

Now free in Community: auto-heal, auto-update, scheduled operations,
webhooks, notification routing, Fleet Actions and bulk operations, SSO
preset providers (Google / GitHub / Okta), unlimited users with admin and
viewer roles, and deploy safety (atomic deploys, auto-rollback, and
one-click rollback).

Admiral (paid) is focused on running and governing a fleet: blueprints,
Fleet Secrets, deploy enforcement, vulnerability report export, audit log,
host console, private registries, mesh networking, node cordon, managed
cloud backup, LDAP / Active Directory SSO, and the advanced RBAC roles
(deployer, node-admin, auditor) with per-resource scoped assignments.

Internally the license variant distinction is removed so tier is binary
(community / paid). License validation still verifies the Lemon Squeezy
store and product before granting paid status.

Docs and the contributor guide are updated to the two-tier model.

* docs(pricing): correct licensing page to two-tier pricing and tidy stale tier wording

The licensing docs page kept the old Admiral pricing plus a Founder
Lifetime column and an Enterprise paragraph after the two-tier collapse.
Update it to $12/month or $99/year, drop the lifetime and Enterprise
content, and link to the pricing page for current pricing.

Also fix stale "Skipper" wording in CLA.md, SUPPORT.md, one test title,
and three test comments. Historical CHANGELOG entries and the
retired-Skipper license-guard test are intentionally left as-is.

* docs: align licensing and SSO pages with the two-tier model

Correct the SSO overview so the Google, GitHub, and Okta presets read as
available on every tier, matching the provider table; only LDAP and Active
Directory require Sencho Admiral. Remove the lifetime-plan references from the
licensing, settings, and troubleshooting pages so they reflect subscription-only
Admiral pricing.

* fix(rbac): omit scoped permissions from /me on the Community tier

Scoped role assignments only take effect on the paid tier, but GET /api/permissions/me returned them unconditionally, so a downgraded instance with leftover assignments rendered per-resource affordances the API then rejected with 403. The endpoint now mirrors the permission middleware and includes scoped permissions only on the paid tier. Adds a regression test covering the downgrade case.

* docs: use custom-pricing wording on the contact page

The two-tier model has no Enterprise tier; reword the contact page's enterprise pricing/deals to custom pricing/deals so it does not imply a tier that no longer exists.
2026-06-04 17:45:53 -04:00

213 lines
9.6 KiB
TypeScript

import { Router, type Request, type Response } from 'express';
import { DatabaseService, type WebhookAction } from '../services/DatabaseService';
import { WebhookService } from '../services/WebhookService';
import { authMiddleware } from '../middleware/auth';
import { requireAdmin } from '../middleware/tierGates';
import { webhookTriggerLimiter } from '../middleware/rateLimiters';
const VALID_WEBHOOK_ACTIONS: readonly WebhookAction[] = ['deploy', 'restart', 'stop', 'start', 'pull', 'git-pull'];
const MAX_WEBHOOK_NAME_LENGTH = 100;
function isWebhookAction(value: unknown): value is WebhookAction {
return typeof value === 'string' && (VALID_WEBHOOK_ACTIONS as readonly string[]).includes(value);
}
export const webhooksRouter = Router();
webhooksRouter.get('/', authMiddleware, async (req: Request, res: Response): Promise<void> => {
try {
const webhooks = DatabaseService.getInstance().getWebhooks();
const svc = WebhookService.getInstance();
res.json(webhooks.map(w => ({ ...w, secret: svc.maskSecret(w.secret) })));
} catch (error) {
console.error('[Webhooks] List error:', error);
res.status(500).json({ error: 'Failed to list webhooks' });
}
});
webhooksRouter.post('/', authMiddleware, async (req: Request, res: Response): Promise<void> => {
if (!requireAdmin(req, res)) return;
try {
const { name, stack_name, action, enabled, node_id } = req.body;
if (!name || !stack_name || !action) {
res.status(400).json({ error: 'name, stack_name, and action are required' });
return;
}
if (typeof name !== 'string' || name.length > MAX_WEBHOOK_NAME_LENGTH) {
res.status(400).json({ error: `name must be a string of ${MAX_WEBHOOK_NAME_LENGTH} characters or fewer` });
return;
}
if (!VALID_WEBHOOK_ACTIONS.includes(action)) {
res.status(400).json({ error: `action must be one of: ${VALID_WEBHOOK_ACTIONS.join(', ')}` });
return;
}
if (node_id !== undefined && !Number.isInteger(node_id)) {
res.status(400).json({ error: 'node_id must be an integer' });
return;
}
const targetNodeId = node_id ?? req.nodeId ?? DatabaseService.getInstance().getDefaultNode()?.id;
if (!targetNodeId || !DatabaseService.getInstance().getNode(targetNodeId)) {
res.status(400).json({ error: 'node_id must reference an existing node' });
return;
}
if (action === 'git-pull' && !(await WebhookService.getInstance().gitSourceExists(stack_name, targetNodeId))) {
res.status(400).json({ error: 'Configure a Git source for this stack before creating a git-pull webhook' });
return;
}
const svc = WebhookService.getInstance();
const secret = svc.generateSecret();
const id = DatabaseService.getInstance().addWebhook({
node_id: targetNodeId,
name, stack_name, action, secret, enabled: enabled !== false,
});
// Return the full secret only on creation.
res.status(201).json({ id, secret });
} catch (error) {
console.error('[Webhooks] Create error:', error);
res.status(500).json({ error: 'Failed to create webhook' });
}
});
webhooksRouter.put('/:id', authMiddleware, async (req: Request, res: Response): Promise<void> => {
if (!requireAdmin(req, res)) return;
try {
const id = parseInt(req.params.id as string, 10);
const webhook = DatabaseService.getInstance().getWebhook(id);
if (!webhook) { res.status(404).json({ error: 'Webhook not found' }); return; }
const { name, stack_name, action, enabled, node_id } = req.body;
if (name !== undefined && (typeof name !== 'string' || name.length > MAX_WEBHOOK_NAME_LENGTH)) {
res.status(400).json({ error: `name must be a string of ${MAX_WEBHOOK_NAME_LENGTH} characters or fewer` });
return;
}
if (node_id !== undefined && !Number.isInteger(node_id)) {
res.status(400).json({ error: 'node_id must be an integer' });
return;
}
const targetNodeId = node_id ?? webhook.node_id;
if (node_id !== undefined && !DatabaseService.getInstance().getNode(targetNodeId)) {
res.status(400).json({ error: 'node_id must reference an existing node' });
return;
}
if (action && !VALID_WEBHOOK_ACTIONS.includes(action)) {
res.status(400).json({ error: `action must be one of: ${VALID_WEBHOOK_ACTIONS.join(', ')}` });
return;
}
const effectiveAction = action ?? webhook.action;
const effectiveStackName = stack_name ?? webhook.stack_name;
if (effectiveAction === 'git-pull') {
const targetStack = effectiveStackName;
if (!(await WebhookService.getInstance().gitSourceExists(targetStack, targetNodeId))) {
res.status(400).json({ error: 'Configure a Git source for this stack before enabling a git-pull webhook' });
return;
}
}
DatabaseService.getInstance().updateWebhook(id, { node_id, name, stack_name, action, enabled });
res.json({ success: true });
} catch (error) {
console.error('[Webhooks] Update error:', error);
res.status(500).json({ error: 'Failed to update webhook' });
}
});
webhooksRouter.delete('/:id', authMiddleware, async (req: Request, res: Response): Promise<void> => {
if (!requireAdmin(req, res)) return;
try {
const id = parseInt(req.params.id as string, 10);
DatabaseService.getInstance().deleteWebhook(id);
res.json({ success: true });
} catch (error) {
console.error('[Webhooks] Delete error:', error);
res.status(500).json({ error: 'Failed to delete webhook' });
}
});
webhooksRouter.get('/:id/history', authMiddleware, async (req: Request, res: Response): Promise<void> => {
try {
const id = parseInt(req.params.id as string, 10);
const executions = DatabaseService.getInstance().getWebhookExecutions(id);
res.json(executions);
} catch (error) {
console.error('[Webhooks] History error:', error);
res.status(500).json({ error: 'Failed to fetch webhook history' });
}
});
// Public: authenticated via HMAC signature, not session cookie.
//
// Every unauthenticated rejection returns the same 404 with the same body so
// callers cannot enumerate webhook ids from the response surface. Successful
// authentication still returns 202.
//
// The handler also runs the HMAC computation on every path (using a decoy
// secret and an empty buffer when the real ones are missing) so the wall-
// clock cost of a reject path matches the wall-clock cost of a real-shape
// wrong-secret path. Without this, repeated near-rate-limit probes with a
// large attacker-controlled body could distinguish a valid-and-enabled
// webhook id from the other reject cases via response latency. Timing now
// depends only on the size of the request body, which the attacker already
// controls and which reveals nothing webhook-specific.
webhooksRouter.post('/:id/trigger', webhookTriggerLimiter, async (req: Request, res: Response): Promise<void> => {
const unauthenticated = (): void => {
res.status(404).json({ error: 'Webhook not found or signature invalid' });
};
try {
const id = parseInt(req.params.id as string, 10);
const db = DatabaseService.getInstance();
const webhook = db.getWebhook(id);
const signature = req.headers['x-webhook-signature'] as string | undefined;
// Unconditional HMAC. The decoy secret keeps the work non-skippable when
// the webhook does not exist; an empty buffer keeps it non-skippable
// when express.json()'s verify callback did not capture a body. The
// empty-string signature still flows through validateSignature, which
// is constant-time over every shape and will return false against the
// all-zero sentinel. Reject conditions are checked after the HMAC has
// already run so the timing of every reject path matches the timing of
// a real-shape wrong-secret request.
const svc = WebhookService.getInstance();
const payload = (req.rawBody ?? Buffer.alloc(0)).toString('utf-8');
const secretForHmac = webhook?.secret ?? WebhookService.getDecoySecret();
const sigOk = svc.validateSignature(payload, secretForHmac, signature ?? '');
if (!webhook || !webhook.enabled) return unauthenticated();
if (!signature) return unauthenticated();
if (!req.rawBody) return unauthenticated();
if (!sigOk) return unauthenticated();
// Use action from body if provided, otherwise use webhook default.
// Validate against the action allowlist before queueing execution so an
// attacker-supplied string never reaches recordExecution as a stored
// failure label.
const overrideAction = (req.body as { action?: unknown } | undefined)?.action;
let action: WebhookAction = webhook.action;
if (overrideAction !== undefined) {
if (!isWebhookAction(overrideAction)) {
res.status(400).json({ error: `action must be one of: ${VALID_WEBHOOK_ACTIONS.join(', ')}` });
return;
}
action = overrideAction;
}
const triggerSource = req.headers['user-agent'] || req.ip || null;
// Execute asynchronously; return 202 immediately.
res.status(202).json({ message: 'Webhook accepted', action });
// Pass the already-loaded webhook through so execute() never re-fetches
// by id. If an admin deletes the row between this line and the async
// dispatch the action still completes and recordExecution swallows the
// FK error from the CASCADE. atomic is unconditionally true, so the
// deploy/pull paths always run in atomic mode here.
svc.execute(webhook, action, triggerSource, true).catch(err => {
console.error(`[Webhooks] Execution error for webhook ${id}:`, err);
});
} catch (error) {
console.error('[Webhooks] Trigger error:', error);
res.status(500).json({ error: 'Failed to process webhook' });
}
});