|
| 1 | +import { |
| 2 | + filterSlackFindings, |
| 3 | + formatSlackMessage, |
| 4 | + getAllowedSlackActions, |
| 5 | + getSlackNotificationConfig, |
| 6 | + safeSlackFilter, |
| 7 | +} from './slack-webhook.mts' |
| 8 | +import { apiFetch } from '../../../utils/api.mts' |
| 9 | +import { logger } from '../../../utils/logger.mts' |
| 10 | + |
| 11 | +import type { NotificationReport } from './report.mts' |
| 12 | + |
| 13 | +const SLACK_POST_MESSAGE_URL = 'https://slack.com/api/chat.postMessage' |
| 14 | +const REQUEST_TIMEOUT_MS = 10_000 |
| 15 | + |
| 16 | +type BotDestination = { |
| 17 | + name: string |
| 18 | + channels: string[] |
| 19 | + filter: ReturnType<typeof safeSlackFilter> |
| 20 | +} |
| 21 | + |
| 22 | +function botDestinations(config: Record<string, unknown>): BotDestination[] { |
| 23 | + const input = config['bot_configs'] |
| 24 | + if (!Array.isArray(input)) { |
| 25 | + if (input !== undefined) { |
| 26 | + logger.warn('Slack bot_configs must be an array; skipping bot delivery.') |
| 27 | + } |
| 28 | + return [] |
| 29 | + } |
| 30 | + return input.flatMap((item, index) => { |
| 31 | + if (!item || typeof item !== 'object' || Array.isArray(item)) { |
| 32 | + logger.warn( |
| 33 | + `Slack bot configuration ${index + 1} is invalid; skipping it.`, |
| 34 | + ) |
| 35 | + return [] |
| 36 | + } |
| 37 | + const source = item as Record<string, unknown> |
| 38 | + const name = |
| 39 | + typeof source['name'] === 'string' ? source['name'] : `bot_${index}` |
| 40 | + const channels = source['channels'] |
| 41 | + if ( |
| 42 | + !Array.isArray(channels) || |
| 43 | + channels.some(channel => typeof channel !== 'string') |
| 44 | + ) { |
| 45 | + logger.warn( |
| 46 | + `Slack bot configuration "${name}" has invalid channels; skipping it.`, |
| 47 | + ) |
| 48 | + return [] |
| 49 | + } |
| 50 | + if (channels.length === 0) { |
| 51 | + logger.warn( |
| 52 | + `Slack bot configuration "${name}" has no channels; skipping it.`, |
| 53 | + ) |
| 54 | + return [] |
| 55 | + } |
| 56 | + const filter = safeSlackFilter(source) |
| 57 | + if (!filter) { |
| 58 | + logger.warn( |
| 59 | + `Slack bot configuration "${name}" has an invalid filter; skipping it.`, |
| 60 | + ) |
| 61 | + return [] |
| 62 | + } |
| 63 | + return [{ name, channels, filter }] |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +/** Post selected scan findings to channels configured for a Slack bot. */ |
| 68 | +export async function sendSlackBotNotifications( |
| 69 | + report: NotificationReport, |
| 70 | + config: |
| 71 | + | Record<string, unknown> |
| 72 | + | null |
| 73 | + | undefined = getSlackNotificationConfig(), |
| 74 | +): Promise<void> { |
| 75 | + const token = process.env['SOCKET_SLACK_BOT_TOKEN'] |
| 76 | + if (!token) { |
| 77 | + return |
| 78 | + } |
| 79 | + if (!token.startsWith('xoxb-')) { |
| 80 | + logger.warn( |
| 81 | + 'SOCKET_SLACK_BOT_TOKEN must be a bot OAuth token beginning with xoxb-.', |
| 82 | + ) |
| 83 | + return |
| 84 | + } |
| 85 | + if (!config) { |
| 86 | + if (!process.env['SOCKET_SLACK_CONFIG_JSON']) { |
| 87 | + logger.warn('Slack bot delivery requires SOCKET_SLACK_CONFIG_JSON.') |
| 88 | + } |
| 89 | + return |
| 90 | + } |
| 91 | + const destinations = botDestinations(config) |
| 92 | + if (!destinations.length) { |
| 93 | + logger.warn( |
| 94 | + 'No valid Slack bot destinations are configured; skipping delivery.', |
| 95 | + ) |
| 96 | + return |
| 97 | + } |
| 98 | + const allowedActions = getAllowedSlackActions() |
| 99 | + for (const destination of destinations) { |
| 100 | + const findings = filterSlackFindings( |
| 101 | + report, |
| 102 | + destination.filter ?? {}, |
| 103 | + allowedActions, |
| 104 | + ) |
| 105 | + if (!findings.length) { |
| 106 | + continue |
| 107 | + } |
| 108 | + const message = formatSlackMessage(report, findings) |
| 109 | + for (const channel of destination.channels) { |
| 110 | + if (!channel.trim() || channel.length > 80 || /[\r\n]/u.test(channel)) { |
| 111 | + logger.warn( |
| 112 | + `Slack bot destination "${destination.name}" contains an invalid channel; skipping it.`, |
| 113 | + ) |
| 114 | + continue |
| 115 | + } |
| 116 | + try { |
| 117 | + // Slack's fixed endpoint and manual redirect policy prevent sending the bot token elsewhere. |
| 118 | + // eslint-disable-next-line no-await-in-loop |
| 119 | + const response = await apiFetch(SLACK_POST_MESSAGE_URL, { |
| 120 | + method: 'POST', |
| 121 | + headers: { |
| 122 | + authorization: `Bearer ${token}`, |
| 123 | + 'content-type': 'application/json; charset=utf-8', |
| 124 | + }, |
| 125 | + body: JSON.stringify({ channel, ...message }), |
| 126 | + rejectRedirects: true, |
| 127 | + timeout: REQUEST_TIMEOUT_MS, |
| 128 | + }) |
| 129 | + if (!response.ok) { |
| 130 | + logger.warn( |
| 131 | + `Slack bot delivery to "${destination.name}" failed with HTTP ${response.status}; scan policy is unchanged.`, |
| 132 | + ) |
| 133 | + continue |
| 134 | + } |
| 135 | + // The body is one small Slack API acknowledgement, needed to check its `ok` field. |
| 136 | + // eslint-disable-next-line no-await-in-loop |
| 137 | + const responseData: unknown = await response.json() |
| 138 | + if ( |
| 139 | + !responseData || |
| 140 | + typeof responseData !== 'object' || |
| 141 | + (responseData as { ok?: unknown }).ok !== true |
| 142 | + ) { |
| 143 | + logger.warn( |
| 144 | + `Slack bot delivery to "${destination.name}" was rejected by Slack; scan policy is unchanged.`, |
| 145 | + ) |
| 146 | + } |
| 147 | + } catch { |
| 148 | + logger.warn( |
| 149 | + `Slack bot delivery to "${destination.name}" failed; scan policy is unchanged.`, |
| 150 | + ) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments