cloak-api / copilot /adapters /messaging.py
n1th1sh's picture
Upload 35 files
375880c verified
Raw History Blame Contribute Delete
8.39 kB
"""Slack, Microsoft Teams and Google Chat policies (message DLP).
Slack can log or delete (delete needs a connector with an admin token). Teams and Google
Chat have no action at all: matches are recorded, never stopped.
"""
from __future__ import annotations
from ..interpret import Request
from ..model import Draft, Question
from ..platform.context import PlatformContext
from ..rules import RuleDecision
from .base import (domains_of, domains_question, qualifiers_unsupported, Build, answer_for, base_body, channel_group_check, choose_action, exceptions_unsupported,
need_data, pick_connectors, rule_questions, schedule_unsupported, thresholds_unsupported)
LOG_ONLY = {'LOG': 'log', 'ALERT': 'log'}
def _direction(ch: dict, c, only: bool) -> None:
mods = set(c.mods)
if 'internal' in mods:
ch['logInternal'] = True
if 'incoming' in mods:
ch['monitorIncoming'] = True
if 'external' in mods and only and 'internal' not in mods:
ch['logInternal'] = False
if 'incoming' not in mods:
ch['monitorIncoming'] = False
def _only(req: Request) -> bool:
return req.only or 'only' in req.clause.split()
def build_slack(req: Request, rd: RuleDecision, ctx: PlatformContext, index: int, answers: dict) -> Build:
d = Draft('slack', None, rd)
b = Build(d, consumed_exceptions=list(req.consumed))
body = base_body('slack')
s = body['channels']['slack']
c = req.content
s['connectorIds'] = pick_connectors(d, req, ctx, 'slack', index, answers)
scoped = [x for x in ctx.connectors_for('slack') if not s['connectorIds'] or x.id in s['connectorIds']]
can_delete = any(x.hasAdminToken for x in scoped)
actions = [('log', 'Log only'), ('delete', 'Delete the message')]
s['action'] = choose_action(d, req, index, answers, mapping={'LOG': 'log', 'ALERT': 'log', 'DELETE': 'delete',
'REMEDIATE': 'delete', 'REVOKE': 'delete'},
allowed=actions, default='log', type_label='Slack',
impossible_default='delete' if (can_delete or not ctx.connectorsKnown) else 'log')
if req.verb == 'ALERT':
d.issue('CHANNEL_ALERT_IS_LOG', 'info', 'Slack matches are logged and raise incidents; who is notified is '
'set in Policies > Incident rules.')
if s['action'] == 'delete' and ctx.connectorsKnown and not can_delete:
d.issue('SLACK_DELETE_NEEDS_ADMIN_TOKEN', 'blocking',
'Deleting messages needs a Slack connection with an admin token, and none is set up. Without it the '
'policy can only log.')
d.ask(Question('action', 'No Slack connection can delete messages. Log them instead?',
[{'value': 'log', 'label': 'Log only'}], True, 'log'))
s['action'] = 'log'
s['detectionRuleIds'] = list(rd.rule_ids)
mods, only = set(c.mods), _only(req)
if 'public' in mods:
mods.add('public_channels') # "public slack channels"
if only and mods & {'public_channels', 'private_channels', 'dms', 'files'}:
s['scanChannels'] = 'public_channels' in mods
s['scanPrivate'] = 'private_channels' in mods
s['scanGroupDms'] = 'dms' in mods
s['scanFiles'] = 'files' in mods
for k, e in enumerate(req.exceptions):
em = set(e.mods)
if em & {'public_channels', 'private_channels', 'dms', 'files'} and k not in b.consumed_exceptions:
s['scanChannels'] = s['scanChannels'] and 'public_channels' not in em
s['scanPrivate'] = s['scanPrivate'] and 'private_channels' not in em
s['scanGroupDms'] = s['scanGroupDms'] and 'dms' not in em
s['scanFiles'] = s['scanFiles'] and 'files' not in em
b.consumed_exceptions.append(k)
if 'auto_join' in mods:
s['autoJoinPublic'] = True
d.issue('SLACK_AUTO_JOIN_VISIBLE', 'warning', 'The CLOAK app will join every public channel, and people '
'will see it there. Tell them before turning this on.')
_direction(s, c, only)
if s['action'] == 'delete':
d.issue('SLACK_DELETE_DESTROYS', 'warning', 'Deleted messages cannot be recovered.')
_owned(d, s, req, index, answers)
_common(d, req, rd, ctx, index, answers, b)
d.policyBody = body
return b
def build_teams(req: Request, rd: RuleDecision, ctx: PlatformContext, index: int, answers: dict) -> Build:
d = Draft('teams', None, rd)
b = Build(d, consumed_exceptions=list(req.consumed))
body = base_body('teams')
t = body['channels']['teams']
c = req.content
_record_only(d, req, index, answers, 'Microsoft Teams')
t['connectorIds'] = pick_connectors(d, req, ctx, 'teams', index, answers)
t['detectionRuleIds'] = list(rd.rule_ids)
mods, only = set(c.mods), _only(req)
if only and mods & {'channel_messages', 'chats', 'dms', 'public_channels', 'private_channels'}:
t['scanChannels'] = bool(mods & {'channel_messages', 'public_channels', 'private_channels'})
t['scanChats'] = bool(mods & {'chats', 'dms'})
for k, e in enumerate(req.exceptions):
em = set(e.mods)
if em & {'channel_messages', 'chats', 'dms'} and k not in b.consumed_exceptions:
if 'channel_messages' in em:
t['scanChannels'] = False
if em & {'chats', 'dms'}:
t['scanChats'] = False
b.consumed_exceptions.append(k)
_direction(t, c, only)
_owned(d, t, req, index, answers)
_common(d, req, rd, ctx, index, answers, b)
d.policyBody = body
return b
def build_chat(req: Request, rd: RuleDecision, ctx: PlatformContext, index: int, answers: dict) -> Build:
d = Draft('chat', None, rd)
b = Build(d, consumed_exceptions=list(req.consumed))
body = base_body('chat')
ch = body['channels']['chat']
c = req.content
_record_only(d, req, index, answers, 'Google Chat')
ch['detectionRuleIds'] = list(rd.rule_ids)
if 'dms' in c.mods:
ch['scanDms'] = True
_direction(ch, c, _only(req))
_owned(d, ch, req, index, answers)
_common(d, req, rd, ctx, index, answers, b)
d.policyBody = body
return b
def _record_only(d: Draft, req: Request, index: int, answers: dict, label: str) -> None:
"""Teams and Chat policies have no action field: they record matches."""
if req.verb in (None, 'LOG', 'ALERT', 'ENABLE', 'CREATE') or answer_for(answers, 'action', index) == 'log':
return
d.issue('ACTION_NOT_AVAILABLE', 'blocking',
f'{label} policies only record matches; they cannot block, delete or warn.',
requested=(req.verb or '').lower(), type=d.policyType, available=['log'])
d.ask(Question('action', f'{label} can only record matches. Continue with that?',
[{'value': 'log', 'label': 'Record matches'}, {'value': 'cancel', 'label': 'Cancel'}], True, 'log'))
def _owned(d: Draft, cfg: dict, req: Request, index: int, answers: dict) -> None:
"""'Our domains are acme.com and acme.in': what counts as internal."""
doms = domains_of(req.content)
if not doms:
return
role = domains_question(d, req, index, answers, doms, [('owned', 'Our own domain (decides what is external)')])
if role == 'owned':
cfg['ownedDomains'] = list(dict.fromkeys(cfg['ownedDomains'] + doms))
def _common(d: Draft, req: Request, rd: RuleDecision, ctx: PlatformContext, index: int, answers: dict, b: Build) -> None:
if rd.mode == 'none':
need_data(d, rd, ctx, f'{d.policyType.title()} policies flag only what a detection rule describes; with no rule '
'this policy would detect nothing.')
channel_group_check(d, req, ctx, index, answers)
thresholds_unsupported(d, req, index, answers)
schedule_unsupported(d, req, index, answers)
qualifiers_unsupported(d, req, index, answers)
exceptions_unsupported(d, req, b.consumed_exceptions, index, answers, ctx)
rule_questions(d, rd, ctx)
d.spans = [{'start': s, 'end': e, 'label': l} for s, e, l in req.spans]