cloak-api / copilot /adapters /endpoint.py
n1th1sh's picture
Upload 35 files
375880c verified
Raw History Blame Contribute Delete
42.9 kB
"""Endpoint policies: the agent + browser extension form (dashboard/features/agent/PolicyForm.tsx).
One request can touch several sections of the same form: AI prompt protection, chat
permissions, device controls, clipboard, block lists, browser hardening, sign-in restriction
and IDE / AI coding. Each section below is applied only when the request asks for it.
"""
from __future__ import annotations
import copy
import re
from ..interpret import Request
from ..lexicon import load
from ..model import Draft, Question, set_path
from ..platform import catalog
from ..platform.context import PlatformContext
from ..rules import RuleDecision
from ..text import is_domain
from .base import (Build, answer_for, base_body, choose_action, exceptions_unsupported, need_data,
qualifiers_unsupported, resolve_group, rule_questions, schedule_unsupported,
thresholds_unsupported)
RESTRICT_VERBS = {'BLOCK', 'DISABLE', 'DELETE', 'REVOKE'}
PROMPT_ACTIONS = [('block', 'Block'), ('warn', 'Warn the user'), ('redact', 'Redact the data'), ('log', 'Log only')]
PROMPT_MAP = {'BLOCK': 'block', 'DISABLE': 'block', 'WARN': 'warn', 'REDACT': 'redact', 'DELETE': 'redact',
'LOG': 'log', 'ALERT': 'log'}
IDE_PROMPT_MAP = {'BLOCK': 'block', 'DISABLE': 'block', 'WARN': 'warn', 'REDACT': 'redact', 'DELETE': 'redact',
'LOG': 'log', 'ALERT': 'log', 'ALLOW': 'allow'}
ACCESS_MAP = {'BLOCK': 'block', 'DISABLE': 'block', 'WARN': 'warn', 'LOG': 'log', 'ALERT': 'log'}
# Controls the agent's pre-transfer scan covers ("Scans file for PII before USB write or cloud upload").
DATA_AWARE = {'usb': 'USB storage', 'cloud_upload': 'cloud storage uploads', 'dropbox': 'Dropbox', 'box': 'Box',
'icloud': 'iCloud', 'google_drive': 'Google Drive', 'onedrive': 'OneDrive', 'sharepoint': 'SharePoint'}
SECRETS = set(next(r for r in catalog.builtin_rules() if r['id'] == 'builtin_rule_credentials')['entities'])
ID_FIN = {'AADHAAR', 'PAN', 'VOTER_ID', 'GSTIN', 'PASSPORT_NUMBER', 'DRIVER_LICENSE', 'SSN', 'ITIN', 'EIN',
'CREDIT_CARD', 'CREDIT_CARD_EXPIRATION', 'CVV', 'BANK_ACCOUNT', 'UPI_ID', 'IBAN', 'IFSC', 'SWIFT_CODE',
'ROUTING_NUMBER'}
# Hosts that are the same service under another name, for paste and block lists.
EXT_ID = re.compile(r'^[a-p]{32}$')
HOST_ALIASES = {'chatgpt.com': ['chatgpt.com', 'chat.openai.com']}
# AI websites and the coding-tool provider behind each (server/src/modules/ide/catalog.ts ids).
SITE_PROVIDER = {'chatgpt.com': 'openai', 'claude.ai': 'anthropic', 'gemini.google.com': 'gemini',
'copilot.microsoft.com': 'copilot', 'github.com': 'copilot', 'chat.deepseek.com': 'deepseek',
'chat.mistral.ai': 'mistral', 'www.perplexity.ai': 'perplexity', 'grok.com': 'xai'}
DUAL_PATHS = {
'google_drive': {'agentControl.blockCloudUpload': True, 'agentControl.blockGoogleDrive': True},
'onedrive': {'agentControl.blockCloudUpload': True, 'agentControl.blockOneDrive': True},
'sharepoint': {'agentControl.blockCloudUpload': True, 'agentControl.blockSharePoint': True},
'gmail': {'agentControl.blockPersonalEmail': True, 'agentControl.blockGmail': True},
'outlook': {'agentControl.blockPersonalEmail': True, 'agentControl.blockOutlookPersonal': True},
}
# Switches the form saves but the Windows agent (agent-cs) does not act on yet: the tray lists them as
# active and UsbMonitor reads read-only only to word its log line. Remove an entry once the agent enforces it.
# (dlpScanBeforeTransfer left this list with agent 2.1.6: TransferScanner enforces it.)
NOT_ENFORCED = {
'removableStorageReadOnly': ('USB read-only', 'USB drives stay writable; plugging one in is only logged'),
'blockWebUpload': ('web and FTP upload blocking', 'uploads to websites and FTP go through'),
}
# Controls the form shows as "Coming soon": it cannot switch them on, so neither may the copilot.
NOT_AVAILABLE = {
'watermark': 'Print watermarks are not available yet (the policy form shows them as "Coming soon")',
}
def build(req: Request, rd: RuleDecision, ctx: PlatformContext, index: int, answers: dict) -> Build:
lx = load()
d = Draft('endpoint', None, rd)
b = Build(d, consumed_exceptions=list(req.consumed))
body = base_body('endpoint')
c = req.content
verb = req.verb
applied: list[str] = []
data_on = rd.mode != 'none'
ide_context = bool(c.ide_tools or c.ide_providers or c.ide_any or c.ide_features)
ai_context = bool(c.ai_sites or c.ai_any or (c.prompt and not ide_context))
if ide_context and c.ai_sites and (req.only or verb == 'ALLOW' or not data_on) and not c.ai_any:
# "only allow Claude for AI coding": the site names mean the coding tools' providers here
c.ide_providers = list(dict.fromkeys(c.ide_providers + [SITE_PROVIDER[h] for h in c.ai_sites
if h in SITE_PROVIDER]))
c.ai_sites = []
ai_context = bool(c.ai_any or (c.prompt and not ide_context))
restrictive = verb in RESTRICT_VERBS
controls = [x for x in c.controls]
perms = [x for x in controls if x in ('file_uploads_ai', 'code_paste')] if ai_context else []
controls = [x for x in controls if x not in ('file_uploads_ai', 'code_paste')]
clipboard = 'clipboard' in controls
controls = [x for x in controls if x != 'clipboard']
sign_in = [x for x in controls if x in ('sign_in', 'social_login', 'strict_login')]
controls = [x for x in controls if x not in sign_in]
if verb == 'ALLOW' and not req.only:
# nothing to build, so who it is for does not matter ("let developers use Cursor")
d.issue('ALLOW_IS_DEFAULT', 'info',
'Nothing is blocked unless a policy blocks it, so allowing needs no policy. A group with its own '
'policy is exempt from org-wide AI prompt rules, but not from org-wide device and browser '
'controls (USB, printing, block lists): those apply to every group.')
return b
body['groupId'] = resolve_group(d, req, ctx, index, answers)
if verb == 'ALLOW' and req.only and c.ai_sites and not data_on and not ide_context:
req.exceptions.append(type(c)(ai_sites=list(c.ai_sites)))
c.ai_sites, c.ai_any = [], True
req.verb = verb = 'BLOCK'
# ---- exceptions that this form can express
use_prompt = data_on and not clipboard and (ai_context or not (controls or req.dual_endpoint or ide_context))
site_minus: list[str] = []
if use_prompt or perms:
# "in all AI tools except Claude": watch every AI site but Claude. (Without a prompt part,
# the same exception is applied to a block list below instead.)
for k, e in enumerate(req.exceptions):
if k in b.consumed_exceptions:
continue
if e.ai_sites and not (e.groups or e.unknown_groups or e.domains or e.emails or e.serials):
site_minus += e.ai_sites
b.consumed_exceptions.append(k)
not_inspected = data_on and not ai_context and not ide_context and not clipboard and (c.apps or c.sites)
if not_inspected and answer_for(answers, 'control', index) != 'block_all':
names = ', '.join(c.apps + c.sites)
d.issue('SURFACE_NOT_INSPECTED', 'blocking',
f'CLOAK cannot look inside {names}, so it cannot stop only certain data there. It can block '
f'{names} completely.', surface=names)
d.ask(Question('control', f'Block {names} completely?',
[{'value': 'block_all', 'label': 'Yes, block it completely'},
{'value': 'cancel', 'label': 'Cancel'}], True, 'block_all'))
if not_inspected:
use_prompt = False
# ---- AI prompt protection
sites = _sites(c, site_minus)
ans_sites = answer_for(answers, 'sites', index)
if isinstance(ans_sites, list) and any(h in catalog.ai_hosts() for h in ans_sites if isinstance(h, str)):
sites = [h for h in catalog.ai_hosts() if h in ans_sites]
if use_prompt or perms:
if not sites:
d.issue('NO_SITES_LEFT', 'blocking', 'Every AI site was excluded, so the policy would watch nothing.')
d.ask(Question('sites', 'Which AI sites should this policy watch?',
[{'value': h, 'label': s['name']} for s, h in zip(catalog.ai_sites(), catalog.ai_hosts())],
True, []))
body['enabledSites'] = sites or catalog.ai_hosts()
custom = [x for x in c.domains if x not in catalog.ai_hosts()] if (use_prompt and not c.sites) else []
if custom:
body['customSites'] = custom
d.issue('CUSTOM_SITE_ADDED', 'info',
f'{", ".join(custom)} will be watched as an AI site, in addition to the built-in AI sites '
f'that are switched on.', sites=custom)
if use_prompt:
mapping = dict(PROMPT_MAP)
if 'monitor_only' in c.mods:
mapping = {k: 'log' for k in mapping}
body['action'] = choose_action(d, req, index, answers, mapping=mapping, allowed=PROMPT_ACTIONS,
default='warn', type_label='AI prompt')
if verb == 'ALERT':
d.issue('ENDPOINT_ALERT_IS_LOG', 'info',
'Endpoint policies record the event (action "Log"); who gets notified is set in '
'Policies > Incident rules.')
body['extensionControl']['detectionRuleIds'] = list(rd.rule_ids)
applied.append('prompt')
if 'file_uploads_ai' in perms:
perms.remove('file_uploads_ai') # "uploading files with Aadhaar": scan uploads, don't ban them
elif (ai_context and not perms and not clipboard and not controls and not ide_context
and verb in ('LOG', 'ALERT', 'WARN', 'REDACT')):
# "log every prompt sent to AI": prompts are checked against detection rules, so one is needed
body['action'] = PROMPT_MAP[verb]
body['enabledSites'] = sites or catalog.ai_hosts()
need_data(d, rd, ctx, 'AI prompts are checked against detection rules; tell me what data to look for.')
applied.append('prompt_pending')
else:
body['action'] = 'log' # no prompt rules: 'log' leaves the strictest-action merge untouched
for p in perms:
if restrictive or verb is None:
if p == 'file_uploads_ai':
body['allowFileUpload'] = False
else:
body['allowCodePaste'] = False
applied.append(p)
# ---- device, network and browser switches
scanned: list[str] = [] # controls left open behind the pre-transfer scan ("aadhaar to usb")
for name in controls + [f'dual:{x}' for x in req.dual_endpoint]:
_control(d, body, req, name, data_on, rd, ctx, index, answers, applied, lx, scanned)
if 'extension_allowlist' in controls:
# "all extensions except grammarly": the names went into the extension-id question
for k, e in enumerate(req.exceptions):
if (e.words or e.ext_ids or e.unknown_groups) and e.data.empty() and not e.groups:
b.consumed_exceptions.append(k)
# ---- clipboard
if clipboard:
_clipboard(d, body, req, data_on, applied)
# ---- sign-in restriction
if sign_in and (verb != 'ALLOW' or req.only or req.control_verb):
_sign_in(d, body, req, sign_in, index, answers, applied)
# ---- block lists, named sites and apps
cverb = req.control_verb or verb
if (not data_on or req.control_verb or not_inspected) and not clipboard and (cverb in RESTRICT_VERBS or verb is None):
_block_lists(d, body, req, ctx, b, index, answers, applied, ai_category=not perms and not ide_context)
# ---- IDE / AI coding
if (ide_context or (c.paths and not ai_context)) and not clipboard:
# with clipboard protection, coding tools named are paste targets, not tools to block
_ide(d, body, req, rd, data_on, index, answers, applied)
# ---- USB allowlist
if 'usb' in controls:
serials = list(c.serials)
for k, e in enumerate(req.exceptions):
if e.serials and k not in b.consumed_exceptions:
serials += e.serials
b.consumed_exceptions.append(k)
if serials:
body['agentControl']['allowedUsbDeviceIds'] = list(dict.fromkeys(serials))
if c.emails and not sign_in and answer_for(answers, 'scope', index) != 'org':
d.issue('USER_SCOPE_NOT_SUPPORTED', 'blocking',
f'Endpoint policies apply to a group, not to one person ({", ".join(c.emails)}). Put the person in a '
f'group and apply the policy to that group.', users=c.emails)
d.ask(Question('scope', f'The policy cannot target {", ".join(c.emails)} alone. Apply it to the whole '
f'group chosen instead?',
[{'value': 'org', 'label': 'Yes, apply it to the group (or everyone)'},
{'value': 'cancel', 'label': 'Cancel'}], True, 'org'))
if 'test_mode' in c.mods:
body['testMode'] = True
if 'activate_now' in c.mods:
d.issue('ACTIVATION_IS_MANUAL', 'info', 'Copilot policies are saved switched off. Turn it on from the '
'confirm screen once you have checked it.')
if not applied:
if any(i.code == 'WEB_CATEGORY_MISSING' for i in d.issues):
# only a category that does not exist: say what to create, there is nothing to continue with
d.questions = [q for q in d.questions if q.slot != 'webcategory']
return b
d.issue('NOTHING_TO_DO', 'info', 'I could not find anything in this request that an endpoint policy '
'can enforce.')
return b
_not_enforced(d, body, scanned, index, answers, lx)
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)
if 'prompt' in applied or 'ide_prompt' in applied:
rule_questions(d, rd, ctx)
d.policyBody = body
d.spans = [{'start': s, 'end': e, 'label': l} for s, e, l in req.spans]
return b
def _sites(c, minus: list[str]) -> list[str]:
hosts = catalog.ai_hosts()
picked = list(c.ai_sites) if c.ai_sites and not c.ai_any else list(hosts)
return [h for h in hosts if h in picked and h not in minus]
def _control(d: Draft, body: dict, req: Request, name: str, data_on: bool, rd: RuleDecision,
ctx: PlatformContext, index: int, answers: dict, applied: list[str], lx,
scanned: list[str]) -> None:
verb = req.control_verb or req.verb
if req.control_verb:
data_on = False # the data in this request belongs to its prompt part, not to these switches
if name.startswith('dual:'):
key = name[5:]
spec = {'polarity': 'restrict', 'set': DUAL_PATHS[key]}
name = key
if key in ('gmail', 'outlook'):
d.issue('AMBIGUOUS_TERM', 'warning',
f'"{key}" is blocked as personal webmail in the browser. If your company mail also runs on '
f'{"Gmail" if key == "gmail" else "Outlook on the web"}, this blocks it too.', term=key)
else:
if name in NOT_AVAILABLE:
d.issue('FEATURE_NOT_AVAILABLE', 'info', f'{NOT_AVAILABLE[name]}, so this policy leaves them out.',
control=name)
return
spec = lx.controls[name]
if spec['polarity'] == 'feature':
if verb == 'DISABLE' or (verb == 'BLOCK' and spec.get('unset')):
for path, v in spec.get('unset', {}).items():
set_path(body, path, v)
applied.append(name)
return
if verb in (None, 'ENABLE', 'LOG', 'ALERT', 'BLOCK', 'CREATE', 'WARN'):
for path, v in spec['set'].items():
set_path(body, path, v)
if name == 'dlp_scan':
body['agentControl']['sensitivityThreshold'] = _threshold(rd, req.content.mods)
applied.append(name)
return
# restrict polarity
# "only USB serial X", "allow only approved extensions": an allow-list is turned on, not off
allowlist = req.only and ((name == 'usb' and req.content.serials) or name == 'extension_allowlist')
if verb in ('ALLOW', 'ENABLE') and not allowlist:
if verb == 'ALLOW':
d.issue('ALLOW_IS_DEFAULT', 'info', f'{name.replace("_", " ").title()} is allowed unless a policy '
f'blocks it, so there is nothing to switch on.')
return
if verb == 'READONLY':
if name == 'usb':
set_path(body, 'agentControl.removableStorageReadOnly', True)
set_path(body, 'agentControl.blockRemovableStorage', False)
applied.append(name)
return
if not req.negated:
return
verb = 'BLOCK' # "read-only USB and not print": the other switches were negated
if data_on and name in DATA_AWARE:
level = _threshold(rd, req.content.mods)
body['agentControl']['dlpScanBeforeTransfer'] = True
body['agentControl']['sensitivityThreshold'] = level
d.issue('DEVICE_SCAN_USES_THRESHOLD', 'warning',
'Files going to USB or cloud storage are checked by the agent\'s pre-transfer scan, which uses a '
f'sensitivity level ("{level}"), not your detection rules. It blocks files at that level '
'or above.', threshold=level)
applied.append(name)
scanned.append(name)
return
if data_on and name not in DATA_AWARE and answer_for(answers, 'control', index) != 'block_all':
d.issue('CONTROL_NOT_DATA_AWARE', 'blocking',
f'{name.replace("_", " ").title()} cannot be limited to certain data: it is either blocked '
f'completely or not at all.', control=name)
d.ask(Question('control', f'Block {name.replace("_", " ")} completely?',
[{'value': 'block_all', 'label': 'Yes, block it completely'},
{'value': 'cancel', 'label': 'Cancel'}], True, 'block_all'))
elif verb in ('LOG', 'ALERT', 'WARN', 'REDACT') and answer_for(answers, 'control', index) != 'block_all':
d.issue('CONTROL_HAS_NO_SOFT_MODE', 'blocking',
f'{name.replace("_", " ").title()} can only be blocked; the agent has no warn or monitor-only mode '
f'for it.', control=name)
d.ask(Question('control', f'Block {name.replace("_", " ")} instead?',
[{'value': 'block_all', 'label': 'Yes, block it'}, {'value': 'cancel', 'label': 'Cancel'}],
True, 'block_all'))
elif verb is None and req.verb_conf >= 0.5 and answer_for(answers, 'control', index) != 'block_all':
d.ask(Question('control', f'Block {name.replace("_", " ")}?',
[{'value': 'block_all', 'label': 'Yes, block it'}, {'value': 'cancel', 'label': 'Cancel'}],
True, 'block_all'))
for path, v in spec['set'].items():
set_path(body, path, v)
if name == 'extension_allowlist':
_extension_ids(d, body, req, index, answers)
applied.append(name)
def _not_enforced(d: Draft, body: dict, scanned: list[str], index: int, answers: dict, lx) -> None:
"""A switch the agent ignores would look protective and do nothing, so the admin decides what to save."""
ac = body['agentControl']
keys = [k for k in NOT_ENFORCED if ac.get(k) is True]
if not keys:
return
# what "block it completely" means: read-only USB -> block USB (web upload has no stricter form)
fallback: dict[str, object] = {}
if 'removableStorageReadOnly' in keys:
fallback |= {'agentControl.blockRemovableStorage': True, 'agentControl.removableStorageReadOnly': False}
what = ', '.join(NOT_ENFORCED[k][0] for k in keys)
title = what[0].upper() + what[1:]
effects = '; '.join(NOT_ENFORCED[k][1] for k in keys)
target = 'USB storage' if 'removableStorageReadOnly' in keys else ''
ans = answer_for(answers, 'unenforced', index)
if ans == 'block_instead' and fallback:
for path, v in fallback.items():
set_path(body, path, v)
d.issue('NOT_ENFORCED_REPLACED', 'info',
f'{title}: not enforced by the agent yet, so this policy blocks {target} completely instead.',
settings=keys)
return
if ans == 'keep':
d.issue('NOT_ENFORCED_BY_AGENT', 'warning',
f'Saved as asked, but the Windows agent does not enforce {what} yet: {effects}.', settings=keys)
return
d.issue('NOT_ENFORCED_BY_AGENT', 'blocking',
f'The Windows agent does not enforce {what} yet: {effects}. The setting would be saved and shown as '
f'active on the laptop, but nothing would be stopped.', settings=keys)
options = [{'value': 'keep', 'label': 'Save it anyway (no effect until the agent supports it)'},
{'value': 'cancel', 'label': 'Cancel'}]
if fallback:
options.insert(0, {'value': 'block_instead', 'label': f'Block {target} completely instead'})
d.ask(Question('unenforced', f'{title}: not enforced by the agent yet. What should the policy do?',
options, True, None))
def _extension_ids(d: Draft, body: dict, req: Request, index: int, answers: dict) -> None:
"""The allowlist blocks every extension not listed, so the ids that stay allowed must be given."""
ids = list(req.content.ext_ids)
names: list[str] = []
for e in req.exceptions:
ids += e.ext_ids
names += e.words
ans = answer_for(answers, 'extensions', index)
if isinstance(ans, list):
ids += [x for x in ans if isinstance(x, str) and EXT_ID.match(x)]
body['extensionControl']['allowedExtensions'] = list(dict.fromkeys(ids))
if ids or ans == 'none':
return
named = f' (you named {", ".join(names)}: their Chrome Web Store ids are needed, not their names)' if names else ''
d.issue('EXTENSION_IDS_NEEDED', 'blocking',
'Every browser extension except CLOAK and the ones listed will be blocked' + named + '.')
d.ask(Question('extensions', 'Which extensions should stay allowed? Give their 32-letter Chrome Web Store ids.',
[{'value': 'none', 'label': 'None: allow only CLOAK'}], True, None))
def _threshold(rd: RuleDecision, mods: list[str] = ()) -> str:
"""The agent scan's level: Low = any PII, Medium = financial & ID data, High = critical secrets only."""
for mod, level in (('severity_high', 'high'), ('severity_medium', 'medium'), ('severity_low', 'low')):
if mod in mods:
return level
ents = set((rd.new_rule or {}).get('entities', []))
for rid in rd.rule_ids:
r = next((x for x in catalog.builtin_rules() if x['id'] == rid), None)
if r:
ents |= set(r['entities'])
if ents and ents <= SECRETS:
return 'high'
if ents and ents & ID_FIN:
return 'medium'
return 'low' if ents else 'medium'
def _clipboard(d: Draft, body: dict, req: Request, data_on: bool, applied: list[str]) -> None:
c, verb = req.content, req.control_verb or req.verb
ac = body['agentControl']
if verb == 'ALLOW':
return
if verb in ('LOG', 'ALERT') or 'monitor_only' in c.mods:
ac['clipboardMode'] = 'audit'
applied.append('clipboard')
return
snap = catalog.snapshot()
sites: list[str] = []
apps: list[str] = []
for h in c.ai_sites:
sites += HOST_ALIASES.get(h, [h])
if c.ai_any:
sites += snap['genaiPasteSites']
apps += snap['genaiPasteApps']
sites += [x for x in c.domains]
tools = {t['key']: t['name'] for t in catalog.ide()['tools']}
apps += [tools[t].replace(' desktop app', '').replace(' app', '') for t in c.ide_tools if t in tools]
apps += [a[:-4].title() for a in c.apps]
if (sites or apps) and 'everywhere' not in c.mods:
ac['clipboardMode'] = 'targets'
ac['pasteBlockedSites'] = list(dict.fromkeys(sites))
ac['pasteBlockedApps'] = list(dict.fromkeys(apps))
else:
ac['clipboardMode'] = 'everywhere'
if data_on:
d.issue('CLIPBOARD_IS_PII_WIDE', 'info',
'Clipboard protection reacts to any personal data the agent finds on the clipboard, not to '
'specific detection rules.')
applied.append('clipboard')
def _sign_in(d: Draft, body: dict, req: Request, names: list[str], index: int, answers: dict,
applied: list[str]) -> None:
c = req.content
sr = body['agentControl']['signInRestriction']
domains = [x for x in c.domains if x not in catalog.ai_hosts()]
domains += [e.split('@')[1] for e in c.emails if '@' in e]
ans = answer_for(answers, 'domain', index)
if isinstance(ans, str) and ans.strip():
domains = [x.strip().lower() for x in ans.split(',') if x.strip()]
elif isinstance(ans, list):
domains = [str(x).strip().lower() for x in ans if str(x).strip()]
bad = [x for x in domains if not is_domain(x)]
if bad:
d.issue('NOT_A_DOMAIN', 'warning', f'{", ".join(bad)} is not an email domain (for example acme.com).')
domains = list(dict.fromkeys(x for x in domains if is_domain(x)))
if not domains:
d.issue('SIGN_IN_NEEDS_DOMAIN', 'blocking',
'To restrict sign-in, CLOAK needs your company\'s email domain(s). Accounts on other domains '
'are refused.')
d.ask(Question('domain', 'Which email domains are your company accounts on? (for example acme.com)',
[], True, None))
applied.append('sign_in_pending')
return
sr['enabled'] = True
sr['allowedDomains'] = domains
if 'social_login' in names:
sr['blockSocialLogin'] = True
if 'strict_login' in names:
sr['strictLogin'] = True
d.issue('STRICT_LOGIN', 'warning', 'Sign-in will be refused on every website that is not on the approved '
'list. Add approved sites in the policy form before turning it on.')
applied.append('sign_in')
def _block_lists(d: Draft, body: dict, req: Request, ctx: PlatformContext, b: Build, index: int,
answers: dict, applied: list[str], ai_category: bool = True) -> None:
c = req.content
ac = body['agentControl']
_web_categories(d, body, req, ctx, index, answers, applied)
blists = list(c.blists)
if c.ai_any and not c.ai_sites and ai_category and not req.control_verb and 'builtin_web_ai' not in blists:
# "block AI for interns": the built-in list of public AI chatbots
blists.append('builtin_web_ai')
lists = [ctx.block_list(x) for x in blists if ctx.block_list(x)]
for bl in lists:
minus: list[str] = []
for k, e in enumerate(req.exceptions):
if k in b.consumed_exceptions:
continue
ex = set(e.sites) | set(e.domains) | set(e.apps)
ex |= {h for s in e.ai_sites for h in HOST_ALIASES.get(s, [s])}
hit = [x for x in bl.entries if x in ex]
if hit:
minus += hit
b.consumed_exceptions.append(k)
if minus:
entries = [x for x in bl.entries if x not in minus]
ph = f'$NEW_LIST_{len(d.newBlockLists) + 1}'
d.newBlockLists.append({'placeholder': ph, 'body': {
'name': f'{bl.name} (without {", ".join(minus)})'[:100], 'kind': bl.kind,
'description': f'Copy of "{bl.name}" made by Policy Copilot.', 'entries': entries}})
_add_list(ac, bl.kind, ph)
d.issue('NEW_BLOCK_LIST', 'info', f'A copy of "{bl.name}" without {", ".join(minus)} will be created, '
'because built-in lists cannot be edited.')
else:
_add_list(ac, bl.kind, bl.id)
applied.append('blocklist')
# Named things to block outright ("block chatgpt", "block facebook", "block anydesk").
web: list[str] = []
for h in c.ai_sites:
web += HOST_ALIASES.get(h, [h])
web += list(c.sites)
web += [x for x in c.domains if x not in web]
app = list(c.apps)
if 'similar' in c.mods:
# "pastebin and similar sites": the built-in list that holds the named site
for name in web + app:
home = next((bl for bl in ctx.blockLists if bl.builtin and name in bl.entries), None)
if home:
_add_list(ac, home.kind, home.id)
applied.append('blocklist')
if (c.sites or c.domains) and c.words and not c.apps:
guessed = [f'{w}.com' for w in c.words if w.isalpha() and len(w) >= 3]
if guessed:
web += [g for g in guessed if g not in web]
d.issue('SITE_GUESSED', 'warning', f'I guessed the address of {", ".join(guessed)}. Check them.',
sites=guessed)
chosen = {e for lid in ac['webBlockListIds'] + ac['appBlockListIds'] if ctx.block_list(lid)
for e in ctx.block_list(lid).entries}
web = [x for x in web if x not in chosen] # already blocked by a list we picked
app = [x for x in app if x not in chosen]
if c.ai_sites and (c.controls or c.prompt):
web = [x for x in web if x not in {h for s in c.ai_sites for h in HOST_ALIASES.get(s, [s])}]
ans = answer_for(answers, 'blocklist', index)
if c.ai_sites and ans == 'builtin_web_ai':
_add_list(ac, 'web', 'builtin_web_ai')
web = [x for x in web if x not in {h for s in c.ai_sites for h in HOST_ALIASES.get(s, [s])}]
applied.append('blocklist')
for kind, entries in (('web', web), ('app', app)):
if not entries:
continue
entries = list(dict.fromkeys(entries))
existing = next((bl for bl in ctx.blockLists if bl.kind == kind and set(bl.entries) == set(entries)), None)
if existing:
_add_list(ac, kind, existing.id)
else:
ph = f'$NEW_LIST_{len(d.newBlockLists) + 1}'
label = 'sites' if kind == 'web' else 'apps'
shown = ', '.join(entries[:3]) + (f' +{len(entries) - 3}' if len(entries) > 3 else '')
names = {bl.name.lower() for bl in ctx.blockLists}
name = f'Copilot - Blocked {label}: {shown}'[:100]
n = 2
while name.lower() in names:
name = f'Copilot - Blocked {label}: {shown}'[:94] + f' ({n})'
n += 1
d.newBlockLists.append({'placeholder': ph, 'body': {
'name': name, 'kind': kind, 'description': 'Created by Policy Copilot.', 'entries': entries}})
_add_list(ac, kind, ph)
d.issue('NEW_BLOCK_LIST', 'info', f'A new block list with {", ".join(entries)} will be created.')
applied.append('blocklist')
# "Block ChatGPT but allow Claude": Claude was never on the list, so the exception already holds.
blocked = set(web) | set(app)
for lid in ac['webBlockListIds'] + ac['appBlockListIds']:
bl = ctx.block_list(lid)
nl = next((x for x in d.newBlockLists if x['placeholder'] == lid), None)
blocked |= set(bl.entries if bl else (nl['body']['entries'] if nl else []))
for k, e in enumerate(req.exceptions):
if k in b.consumed_exceptions:
continue
named = {h for s in e.ai_sites for h in HOST_ALIASES.get(s, [s])} | set(e.sites) | set(e.domains) | set(e.apps)
only_names = named and not (e.groups or e.unknown_groups or e.emails or e.serials or not e.data.empty())
# a category's sites are not known here, so a named site may well be in it
if only_names and not (named & blocked) and not body['activeWebCategories']:
b.consumed_exceptions.append(k)
d.issue('EXCEPTION_ALREADY_ALLOWED', 'info',
f'{", ".join(sorted(named))} is not on the blocked list, so it stays allowed.')
if c.ai_sites and web and ans != 'builtin_web_ai':
d.ask(Question('blocklist', 'Block only the sites you named, or every public AI chatbot '
'(the built-in "Public AI Chatbots" list)?',
[{'value': 'named', 'label': 'Only the sites I named'},
{'value': 'builtin_web_ai', 'label': 'All public AI chatbots'}], False, 'named'))
def _web_categories(d: Draft, body: dict, req: Request, ctx: PlatformContext, index: int, answers: dict,
applied: list[str]) -> None:
"""Kinds of sites with no built-in list ("adult", "shopping"): the tenant's Web Filtering categories."""
kinds = load().web_categories
norm = lambda s: re.sub(r'[^a-z]', '', s.lower())
picked, missing = [], []
for v in req.content.web_categories:
cat = ctx.web_category(v) # a category the admin named directly
if not cat and v in kinds:
names = [norm(n) for n in kinds[v]['names']]
cat = next((w for w in ctx.webCategories if any(n in norm(w.name) for n in names)), None)
if cat and cat not in picked:
picked.append(cat)
elif not cat:
missing.append(v)
for cat in picked:
if cat.name not in body['activeWebCategories']:
body['activeWebCategories'].append(cat.name)
if picked:
applied.append('webcategory')
empty = [w.name for w in picked if not w.domains]
if empty:
d.issue('WEB_CATEGORY_EMPTY', 'warning',
f'The {", ".join(empty)} categor{"y has" if len(empty) == 1 else "ies have"} no sites yet, so '
f'{"it blocks" if len(empty) == 1 else "they block"} nothing until filled (Web Filtering).',
categories=empty)
if missing and answer_for(answers, 'webcategory', index) != 'ignore':
what = ', '.join(missing)
why = ('there is no Web Filtering category for them' if ctx.webCategoriesKnown
else 'I cannot see your Web Filtering categories')
d.issue('WEB_CATEGORY_MISSING', 'blocking',
f'{what[0].upper()}{what[1:]} sites have no built-in block list, and {why}. Create the category in '
f'Web Filtering (it can fill itself with sites), or name the sites to block.', categories=missing)
d.ask(Question('webcategory', f'There is no category for {what} sites. Continue without them?',
[{'value': 'ignore', 'label': 'Yes, without them'}, {'value': 'cancel', 'label': 'Cancel'}],
True, None))
def _glob(p: str) -> str:
"""'.env' -> '**/.env', '*.pem' -> '**/*.pem': a sensitive file anywhere, not only at the root."""
p = p.strip()
if p.startswith('./'):
p = p[2:]
if p.startswith('**/') or p.startswith('/') or ('/' in p and not p.startswith('*')):
return p
return f'**/{p}'
def _add_list(ac: dict, kind: str, lid: str) -> None:
key = 'webBlockListIds' if kind == 'web' else 'appBlockListIds'
if lid not in ac[key]:
ac[key].append(lid)
def _ide(d: Draft, body: dict, req: Request, rd: RuleDecision, data_on: bool, index: int, answers: dict,
applied: list[str]) -> None:
c, verb = req.content, req.verb
if verb == 'DISABLE' and not data_on and not c.ide_tools and not c.ide_providers:
d.issue('ALLOW_IS_DEFAULT', 'info', 'AI coding tool monitoring is off unless a policy turns it on, so there '
'is nothing to switch off.')
return
ide = copy.deepcopy(body['agentControl']['ideControl'])
ide['enabled'] = True
did = False
if data_on and (c.ide_any or c.ide_tools or c.ide_providers or not (c.ai_sites or c.ai_any)):
ide['detectionRuleIds'] = list(rd.rule_ids)
ide['promptAction'] = choose_action(d, req, index, answers, mapping=IDE_PROMPT_MAP,
allowed=[('block', 'Block'), ('warn', 'Warn'), ('redact', 'Remove the data'),
('log', 'Record only'), ('allow', 'Allow')],
default='warn', type_label='AI coding tool')
applied.append('ide_prompt')
did = True
if c.ide_tools and not data_on and verb in RESTRICT_VERBS and not (c.ide_features or c.paths):
ide['blockedAiTools'] = list(c.ide_tools)
did = True
elif c.ide_tools and not data_on and req.only and verb in ('ALLOW', 'ENABLE', None):
# "allow only Cursor": every other AI coding tool is blocked
tools = catalog.ide()['tools']
ide['blockedAiTools'] = [t['key'] for t in tools if t['key'] not in c.ide_tools]
d.issue('IDE_TOOL_ALLOWLIST', 'info',
'Every other AI coding tool is blocked: '
+ ', '.join(t['name'] for t in tools if t['key'] not in c.ide_tools) + '.')
did = True
elif c.ide_tools and (c.ide_features or c.paths or data_on):
# "block MCP servers in Cursor": the tool is where, not what; these settings cover every tool
names = {t['key']: t['name'] for t in catalog.ide()['tools']}
d.issue('IDE_SETTING_ALL_TOOLS', 'info',
f'AI coding settings apply to every AI coding tool, not only {", ".join(names.get(t, t) for t in c.ide_tools)}.',
tools=list(c.ide_tools))
if c.ide_providers and not data_on:
all_ids = [p['id'] for p in catalog.ide()['providers']]
if req.only or verb == 'ALLOW':
ide['providerControl'] = 'allowlist'
ide['allowedProviders'] = list(c.ide_providers)
did = True
elif verb in RESTRICT_VERBS:
ide['providerControl'] = 'allowlist'
ide['allowedProviders'] = [p for p in all_ids if p not in c.ide_providers]
d.issue('IDE_ALLOWLIST_FROM_BLOCK', 'info',
'AI coding tools are limited by an allow-list, so every other provider is allowed and '
f'{", ".join(c.ide_providers)} is left out.')
did = True
for feat in c.ide_features:
off = verb == 'DISABLE' or (req.negated and verb in ('BLOCK', 'ALLOW'))
if feat == 'store_prompts':
ide['storePromptContent'] = not off
if not off:
d.issue('PROMPT_CONTENT_STORED', 'warning', 'The full text of prompts will be stored. Prompts can '
'contain personal data; check this with your privacy team.')
did = True
continue
if feat in ('intercept', 'responses', 'claude_hook'):
key = {'intercept': 'interceptPrompts', 'responses': 'inspectResponses', 'claude_hook': 'claudeCodeHook'}[feat]
ide[key] = not off
if feat in ('intercept', 'responses') and not off:
d.issue('INTERCEPT_NEEDS_PROXY', 'info', 'This uses the agent\'s TLS proxy; the CLOAK certificate '
'must be trusted on the machines.')
did = True
continue
if feat == 'file_access':
ide['fileAccessAction'] = 'off' if off and verb == 'DISABLE' else {
'BLOCK': 'close', 'WARN': 'warn', 'LOG': 'log', 'ALERT': 'log'}.get(verb, 'log')
for mod, level in (('severity_high', 'HIGH'), ('severity_medium', 'MEDIUM')):
if mod in c.mods:
ide['fileAccessThreshold'] = level
if 'critical' in req.clause.split():
ide['fileAccessThreshold'] = 'CRITICAL'
did = True
continue
if feat in ('editor_alerts', 'discovery', 'config_files'):
key = {'editor_alerts': 'inEditorAlerts', 'discovery': 'discoverAiTools',
'config_files': 'protectAiConfigFiles'}[feat]
want = not (verb == 'DISABLE' or (req.negated and feat != 'config_files'))
if ide.get(key) == want:
d.issue('ALREADY_DEFAULT', 'info', f'This is already {"on" if want else "off"} by default.')
ide[key] = want
did = True
continue
if feat == 'mcp':
if verb in RESTRICT_VERBS:
servers = [{'type': 'url' if x.startswith('http') else 'name', 'value': x}
for x in (c.data.keywords + c.domains)]
ans = answer_for(answers, 'mcp', index)
if isinstance(ans, list):
servers = [{'type': 'url' if str(x).startswith('http') else 'name', 'value': str(x)}
for x in ans if str(x).strip()]
if servers:
ide['mcpControl'] = 'denylist'
ide['deniedMcpServers'] = servers
else:
d.ask(Question('mcp', 'Which MCP servers should be blocked? (names or URLs)', [], True, None))
did = True
elif verb in ('LOG', 'ALERT', None):
ide['mcpControl'] = 'monitor'
did = True
elif feat == 'commands' and verb in ACCESS_MAP:
ide['commandAction'] = ACCESS_MAP[verb]
did = True
elif feat == 'context' and verb in ACCESS_MAP:
ide['contextAction'] = ACCESS_MAP[verb]
did = True
if c.paths:
ide['sensitivePaths'] = [_glob(p) for p in c.paths]
if verb in ACCESS_MAP and 'context' not in c.ide_features:
ide['contextAction'] = ACCESS_MAP[verb]
did = True
if 'monitor_only' in c.mods or (not did and verb in ('LOG', 'ALERT', None)):
ide['enforce'] = False
did = True
if not did and c.ide_any and verb in RESTRICT_VERBS and not data_on:
ans = answer_for(answers, 'ide', index)
if ans == 'block_tools':
ide['blockedAiTools'] = [t['key'] for t in catalog.ide()['tools']]
elif ans == 'monitor':
ide['enforce'] = False
else:
d.ask(Question('ide', 'Block AI coding tools how? (To keep certain data out of their prompts instead, '
'say which data.)',
[{'value': 'block_tools', 'label': 'Block every AI coding tool'},
{'value': 'monitor', 'label': 'Only monitor them'}], True, 'block_tools'))
did = True
if did:
body['agentControl']['ideControl'] = ide
body['ideControl'] = copy.deepcopy(ide)
applied.append('ide')