101 lines
3.8 KiB
JavaScript
101 lines
3.8 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Slack incoming-webhook notifications for security and referral fraud events.
|
|
* Each channel uses its own webhook URL; SLACK_WEBHOOK_URL is a legacy fallback.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.resolveSlackWebhookUrl = resolveSlackWebhookUrl;
|
|
exports.getSlackWebhookUrl = getSlackWebhookUrl;
|
|
exports.isSlackChannelConfigured = isSlackChannelConfigured;
|
|
exports.isSlackConfigured = isSlackConfigured;
|
|
exports.isReferralSlackAlertType = isReferralSlackAlertType;
|
|
exports.postSlackMessage = postSlackMessage;
|
|
exports.postSlackAlert = postSlackAlert;
|
|
exports.postSlackSecurityAlert = postSlackSecurityAlert;
|
|
const config_1 = require("../config");
|
|
/** Resolves the webhook URL for a Slack channel (pure, testable). */
|
|
function resolveSlackWebhookUrl(channel, urls) {
|
|
const fallback = urls.fallbackWebhookUrl?.trim() ?? '';
|
|
if (channel === 'security') {
|
|
return urls.securityWebhookUrl?.trim() || fallback;
|
|
}
|
|
return urls.referralWebhookUrl?.trim() || fallback;
|
|
}
|
|
function webhookConfigFromEnv() {
|
|
return {
|
|
securityWebhookUrl: config_1.config.slack.securityWebhookUrl,
|
|
referralWebhookUrl: config_1.config.slack.referralWebhookUrl,
|
|
fallbackWebhookUrl: config_1.config.slack.webhookUrl,
|
|
};
|
|
}
|
|
function getSlackWebhookUrl(channel) {
|
|
return resolveSlackWebhookUrl(channel, webhookConfigFromEnv());
|
|
}
|
|
function isSlackChannelConfigured(channel) {
|
|
return Boolean(getSlackWebhookUrl(channel));
|
|
}
|
|
/** True when at least one Slack channel webhook is configured. */
|
|
function isSlackConfigured() {
|
|
return isSlackChannelConfigured('security') || isSlackChannelConfigured('referral');
|
|
}
|
|
function isReferralSlackAlertType(alertType) {
|
|
return alertType.startsWith('referral_');
|
|
}
|
|
function slackChannelLabel(channel) {
|
|
return channel === 'referral' ? '#referral-fraud' : '#security';
|
|
}
|
|
/** Posts a message to a Slack webhook. Failures are logged, not thrown. */
|
|
async function postSlackMessage(input) {
|
|
const url = input.webhookUrl.trim();
|
|
if (!url)
|
|
return false;
|
|
try {
|
|
const res = await fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
text: input.text,
|
|
blocks: input.blocks,
|
|
}),
|
|
});
|
|
if (!res.ok) {
|
|
const body = await res.text().catch(() => '');
|
|
console.error(`[slack] Webhook failed (${res.status}): ${body.slice(0, 200)}`);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
catch (e) {
|
|
console.error('[slack] Webhook error:', e);
|
|
return false;
|
|
}
|
|
}
|
|
async function postSlackAlert(input) {
|
|
const webhookUrl = getSlackWebhookUrl(input.channel);
|
|
if (!webhookUrl)
|
|
return;
|
|
const emoji = input.severity === 'critical' || input.severity === 'high'
|
|
? ':rotating_light:'
|
|
: input.severity === 'medium'
|
|
? ':warning:'
|
|
: ':information_source:';
|
|
const lines = [
|
|
`*${input.title}*`,
|
|
`Channel: \`${slackChannelLabel(input.channel)}\` · Type: \`${input.alertType}\` · Severity: *${input.severity}*`,
|
|
input.details ? input.details : null,
|
|
input.userId ? `User: \`${input.userId}\`` : null,
|
|
].filter(Boolean);
|
|
if (input.metadata && Object.keys(input.metadata).length > 0) {
|
|
lines.push(`\`\`\`${JSON.stringify(input.metadata, null, 2).slice(0, 800)}\`\`\``);
|
|
}
|
|
await postSlackMessage({
|
|
webhookUrl,
|
|
text: `${emoji} ${input.title}`,
|
|
blocks: [{ type: 'section', text: { type: 'mrkdwn', text: lines.join('\n') } }],
|
|
});
|
|
}
|
|
/** @deprecated Use postSlackAlert({ channel: 'security', ... }) */
|
|
async function postSlackSecurityAlert(input) {
|
|
await postSlackAlert({ channel: 'security', ...input });
|
|
}
|
|
//# sourceMappingURL=slackService.js.map
|