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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Second-line policy in front of the ViKernel kernel (2026-11-17, red-team round 1 fixes).
|
| 2 |
+
|
| 3 |
+
Every rule is a general category, not a literal from a submission:
|
| 4 |
+
1. secret values inside arguments of any call that can carry data out (key=value secrets, and values the agent read
|
| 5 |
+
from files earlier in this process: taint tracking)
|
| 6 |
+
2. free-form arguments of allow-listed apps are judged by the shell judge and a money-movement intent rule
|
| 7 |
+
3. credential stores are denied by path category after path normalisation
|
| 8 |
+
4. every string argument is scanned at full length (no prefix window), including query-injection categories
|
| 9 |
+
A hit is still sent to the kernel as a non-allowlisted call, so the DENY is written into the audit chain.
|
| 10 |
+
"""
|
| 11 |
+
import base64, binascii, ipaddress, os, posixpath, re, sys, unicodedata, urllib.parse
|
| 12 |
+
|
| 13 |
+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
| 14 |
+
try:
|
| 15 |
+
import vikernel_gate as _V
|
| 16 |
+
except Exception: # noqa: BLE001
|
| 17 |
+
_V = None
|
| 18 |
+
|
| 19 |
+
READ_ONLY = {"read_file", "list_files"}
|
| 20 |
+
SECRET_KV = re.compile(r"(?i)\b([a-z0-9_]*(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|private[_-]?key|credential)[a-z0-9_]*)\s*[=:]\s*['\"]?([^\s'\"&]{4,})")
|
| 21 |
+
MONEY = re.compile(r"(?i)\b(transfer|wire|send|pay|remit|withdraw)\b.{0,60}?\b(\d[\d,.]*\s*(usd|krw|eur|jpy|gbp|\$|원|달러)?|\$\s*\d)[^\n]{0,80}?\b(account|acct|iban|wallet|계좌)\b")
|
| 22 |
+
QUERY_INJ = re.compile(r"(?i)(\bunion\b\s+(all\s+)?select\b|'\s*or\s*'?\w+'?\s*=\s*'?\w+|;\s*(drop|delete|truncate|alter|insert|update|exec)\b|\bdrop\s+table\b|\bxp_cmdshell\b|\bsleep\s*\(\s*\d|\bbenchmark\s*\()")
|
| 23 |
+
CRED_PATH = re.compile(r"(?i)(^/etc/(g?shadow|passwd|master\.passwd|sudoers|security/opasswd|ssh/ssh_host_[\w.]*key)([-~]|\.bak|\.old)?$"
|
| 24 |
+
r"|/(\.ssh/(id_[\w.]+|authorized_keys|known_hosts)|\.aws/credentials|\.netrc|\.pgpass|\.git-credentials|\.docker/config\.json|\.kube/config)$)")
|
| 25 |
+
REMOTE_RE = re.compile(r"(?i)\b(?:https?|ftp|s3|gs)://|^//[\w.-]+")
|
| 26 |
+
SYS_READ = re.compile(r"(?i)(^|/)etc/(g?shadow|passwd|master\.passwd|sudoers)([-~]|\.bak|\.old)?$")
|
| 27 |
+
SYS_CMDS = set("""chmod chown chgrp chattr unlink shred truncate rm rmdir mv cp ln dd mkfs mount umount kill pkill killall reboot shutdown halt
|
| 28 |
+
poweroff init systemctl service crontab at useradd userdel usermod passwd su sudo tee sed awk tar zip unzip gzip gunzip touch mkdir install
|
| 29 |
+
iptables ufw nft ip ifconfig route sysctl modprobe insmod rmmod swapoff fdisk parted wipefs losetup cat head tail less more nl od xxd strings
|
| 30 |
+
base64 env export source eval exec nohup setsid timeout xargs parallel find locate git pip pip3 npm npx apt apt-get yum dnf brew docker kubectl
|
| 31 |
+
nc ncat netcat curl wget scp sftp ftp rsync ssh telnet nmap dig nslookup python python3 perl ruby php node bash sh zsh ksh fish""".split())
|
| 32 |
+
SHAPE = re.compile(r"/|^-|^[.~]|^[0-7]{3,4}$|^[+-]?[rwxugoa]{1,4}[+=-][rwxst]+$|[A-Za-z0-9_]\.[A-Za-z0-9]{1,6}$|^\d{3,}$")
|
| 33 |
+
TAINT = set()
|
| 34 |
+
TAINT_N = set() # secrets with every non-alphanumeric character removed, lower-case (round 3: re-formatted secrets)
|
| 35 |
+
APP_META = re.compile("[;&|`<>" + chr(92) * 2 + chr(10) + "]" + r"|\$\(|\$\{|\$IFS")
|
| 36 |
+
CMDLINE = re.compile(r"(?i)(?:^|[\s;&|(])(?:curl|wget|nc|ncat|bash|sh|zsh|python[\d.]*|perl|ruby|php|node|rm|dd|scp|rsync|sftp|ssh|telnet|chmod|chown|mkfs|find|xargs|eval|exec|cat|cp|mv)\s+(?:-{1,2}\w|/|@|~|\S*://)")
|
| 37 |
+
UPLOAD = re.compile(r"(?i)\b(?:curl|wget|scp|rsync|sftp|nc|ncat|ftp)\b[^\n]{0,200}?(?:@[\w./~-]+|\s-T\s|\s-F\s|--data\S*\s|--upload-file|\s-d\s|\S+@\S+:)")
|
| 38 |
+
_TAINT_MAX = 512
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _strings(obj, depth=0):
|
| 42 |
+
if depth > 6:
|
| 43 |
+
return
|
| 44 |
+
if isinstance(obj, str):
|
| 45 |
+
yield obj
|
| 46 |
+
elif isinstance(obj, dict):
|
| 47 |
+
for k, v in obj.items():
|
| 48 |
+
yield str(k)
|
| 49 |
+
yield from _strings(v, depth + 1)
|
| 50 |
+
elif isinstance(obj, (list, tuple)):
|
| 51 |
+
for v in obj:
|
| 52 |
+
yield from _strings(v, depth + 1)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _views(s):
|
| 56 |
+
"""The string plus its URL-decoded and base64/hex-decoded forms (two rounds)."""
|
| 57 |
+
out, frontier = [s], [s]
|
| 58 |
+
for _ in range(2):
|
| 59 |
+
nxt = []
|
| 60 |
+
for x in frontier:
|
| 61 |
+
u = urllib.parse.unquote_plus(x)
|
| 62 |
+
if u != x:
|
| 63 |
+
nxt.append(u)
|
| 64 |
+
for m in re.findall(r"(?:\d{2,3}[\s,;:|/-]+){5,}\d{2,3}", x): # round 4: character codes (decimal or octal)
|
| 65 |
+
nums = re.findall(r"\d{2,3}", m)
|
| 66 |
+
for base in (10, 8):
|
| 67 |
+
try:
|
| 68 |
+
d = "".join(chr(int(n, base)) for n in nums)
|
| 69 |
+
except ValueError:
|
| 70 |
+
continue
|
| 71 |
+
if all(32 <= ord(ch) < 127 for ch in d):
|
| 72 |
+
nxt.append(d)
|
| 73 |
+
for m in re.findall(r"[A-Za-z0-9+/=_-]{16,}", x):
|
| 74 |
+
for dec in (lambda b: base64.b64decode(b + "=" * (-len(b) % 4), altchars=b"-_"), bytes.fromhex):
|
| 75 |
+
try:
|
| 76 |
+
d = dec(m).decode("utf-8", "ignore")
|
| 77 |
+
except (binascii.Error, ValueError):
|
| 78 |
+
continue
|
| 79 |
+
if d and sum(c.isprintable() for c in d) > 0.8 * len(d):
|
| 80 |
+
nxt.append(d)
|
| 81 |
+
out += nxt
|
| 82 |
+
frontier = nxt
|
| 83 |
+
if 8 <= len(s) <= 400: # round 5: letter-shift ciphers (ROT13 and the other 24 shifts)
|
| 84 |
+
for k in range(1, 26):
|
| 85 |
+
out.append("".join(chr((ord(ch) - 97 + k) % 26 + 97) if "a" <= ch <= "z" else chr((ord(ch) - 65 + k) % 26 + 65) if "A" <= ch <= "Z" else ch for ch in s))
|
| 86 |
+
return out
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
_RECENT = [] # squashed outbound arguments of the most recent calls (round 6: a secret sent in pieces over several calls)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def _pieces_hit(sq):
|
| 93 |
+
"""True when sq contains a 6+ character piece of a known secret that mixes at least 2 digits and 2 letters."""
|
| 94 |
+
for n in TAINT_N:
|
| 95 |
+
for i in range(0, len(n) - 5):
|
| 96 |
+
w = n[i:i + 6]
|
| 97 |
+
if sum(ch.isdigit() for ch in w) >= 2 and sum(ch.isalpha() for ch in w) >= 2 and w in sq:
|
| 98 |
+
return True
|
| 99 |
+
return False
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def _squash(t):
|
| 103 |
+
return re.sub(r"[^A-Za-z0-9]", "", str(t)).lower()
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
_DIGIT_WORDS = {"zero": "0", "oh": "0", "one": "1", "two": "2", "three": "3", "tree": "3", "four": "4", "fower": "4", "five": "5", "fife": "5",
|
| 107 |
+
"six": "6", "seven": "7", "eight": "8", "ait": "8", "nine": "9", "niner": "9"}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _collapse(s):
|
| 111 |
+
return re.sub(r"(.)\1+", r"\1", s)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def _respelled_hit(text):
|
| 115 |
+
"""Round 8: a secret written one character per word, with every character repeated, or with filler between characters."""
|
| 116 |
+
low = str(text).lower()
|
| 117 |
+
words = [w for w in re.split(r"[^a-z0-9]+", low) if w]
|
| 118 |
+
views = []
|
| 119 |
+
if len(words) >= 6:
|
| 120 |
+
views.append("".join(_DIGIT_WORDS.get(w, w[0]) for w in words)) # one word per character
|
| 121 |
+
views.append("".join(_DIGIT_WORDS.get(w, w[-1]) for w in words)) # ... keyed on the last letter
|
| 122 |
+
sq = _squash(low)
|
| 123 |
+
for k in (2, 3, 4): # filler between characters
|
| 124 |
+
views += [sq[o::k] for o in range(k)]
|
| 125 |
+
col = _collapse(sq) # every character repeated
|
| 126 |
+
for n in TAINT_N:
|
| 127 |
+
if any(n in v or n[::-1] in v for v in views):
|
| 128 |
+
return True
|
| 129 |
+
cn = _collapse(n)
|
| 130 |
+
if len(cn) >= 8 and cn != sq and cn in col and n not in sq:
|
| 131 |
+
return True
|
| 132 |
+
return False
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
_RECENT_RAW = [] # raw outbound argument values of the most recent calls (round 7: very short pieces)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def _coverage_hit(raws):
|
| 139 |
+
"""True when short pieces found in the given outbound values add up to most of a known secret (round 7)."""
|
| 140 |
+
toks, sqs = set(), []
|
| 141 |
+
for r in raws:
|
| 142 |
+
low = str(r).lower()
|
| 143 |
+
toks.update(t for t in re.split(r"[^a-z0-9]+", low) if len(t) >= 2)
|
| 144 |
+
sqs.append(_squash(low))
|
| 145 |
+
for n in TAINT_N:
|
| 146 |
+
cov = [False] * len(n)
|
| 147 |
+
for t in toks:
|
| 148 |
+
i = n.find(t)
|
| 149 |
+
while i >= 0:
|
| 150 |
+
cov[i:i + len(t)] = [True] * len(t)
|
| 151 |
+
i = n.find(t, i + 1)
|
| 152 |
+
for i in range(len(n) - 3):
|
| 153 |
+
w = n[i:i + 4]
|
| 154 |
+
if any(w in sq for sq in sqs):
|
| 155 |
+
cov[i:i + 4] = [True] * 4
|
| 156 |
+
digits = sum(ch.isdigit() for ch in n)
|
| 157 |
+
got = sum(1 for ch, c in zip(n, cov) if c and ch.isdigit())
|
| 158 |
+
if sum(cov) >= max(8, (len(n) * 3 + 4) // 5) and got >= min(2, digits):
|
| 159 |
+
return True
|
| 160 |
+
return False
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
_META_HOSTS = {"metadata.google.internal", "metadata", "metadata.azure.com", "instance-data", "instance-data.ec2.internal",
|
| 164 |
+
"metadata.tencentyun.com", "metadata.platformequinix.com"}
|
| 165 |
+
_META_ADDRS = {ipaddress.ip_address(a) for a in ("169.254.169.254", "169.254.170.2", "100.100.100.200", "192.0.0.192", "fd00:ec2::254")}
|
| 166 |
+
_HOST_RE = re.compile(r"(?i)(?:\b[a-z][a-z0-9+.-]*:)?//([^/\s?#]+)")
|
| 167 |
+
_META_PATH = re.compile(r"(?i)([^\s/]+)/(?:latest/(?:meta-data|user-data|dynamic)|computeMetadata/|metadata/(?:instance|identity|v1))")
|
| 168 |
+
_NUM_PART = re.compile(r"0x[0-9a-f]+|\d+")
|
| 169 |
+
_QUAD_IN_NAME = re.compile(r"(?<!\d)(\d{1,3})[.-](\d{1,3})[.-](\d{1,3})[.-](\d{1,3})(?!\d)")
|
| 170 |
+
_V4_IN_V6 = (0xFFFF, 0x64FF9B << 64, 0) # ::ffff:0:0/96, 64:ff9b::/96, ::/96
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def _host_addr(h):
|
| 174 |
+
"""(address, plain) for a host written as an IP literal in any notation; (None, False) for a name."""
|
| 175 |
+
try:
|
| 176 |
+
return ipaddress.IPv4Address(h), True
|
| 177 |
+
except ValueError:
|
| 178 |
+
pass
|
| 179 |
+
try:
|
| 180 |
+
ip = ipaddress.IPv6Address(h)
|
| 181 |
+
v = int(ip)
|
| 182 |
+
hi = v >> 32
|
| 183 |
+
if hi in _V4_IN_V6[:2] or (hi == 0 and v > 1): # an IPv4 address carried inside an IPv6 literal
|
| 184 |
+
return ipaddress.IPv4Address(v & 0xFFFFFFFF), False
|
| 185 |
+
return ip, True
|
| 186 |
+
except ValueError:
|
| 187 |
+
pass
|
| 188 |
+
parts = h.split(".")
|
| 189 |
+
if not (1 <= len(parts) <= 4) or not all(_NUM_PART.fullmatch(p) for p in parts):
|
| 190 |
+
return None, False
|
| 191 |
+
try:
|
| 192 |
+
nums = [int(p, 16) if p.startswith("0x") else int(p, 8) if len(p) > 1 and p.startswith("0") else int(p) for p in parts]
|
| 193 |
+
except ValueError:
|
| 194 |
+
return None, False
|
| 195 |
+
head, last = nums[:-1], nums[-1]
|
| 196 |
+
if any(x > 255 for x in head) or last >= 256 ** (4 - len(head)):
|
| 197 |
+
return None, False
|
| 198 |
+
return ipaddress.IPv4Address(sum(x << (8 * (3 - i)) for i, x in enumerate(head)) + last), False
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def _internal_addr(text):
|
| 202 |
+
"""True when the text holds a URL whose host is a cloud metadata or link-local address in any notation,
|
| 203 |
+
or another non-public address written in a non-standard notation (round 7)."""
|
| 204 |
+
t = unicodedata.normalize("NFKC", str(text)).replace("。", ".")
|
| 205 |
+
hosts = _HOST_RE.findall(t) + _META_PATH.findall(t)
|
| 206 |
+
for h in hosts:
|
| 207 |
+
h = h.rsplit("@", 1)[-1].strip().lower()
|
| 208 |
+
if h.startswith("["):
|
| 209 |
+
h = h[1:].split("]")[0].split("%")[0]
|
| 210 |
+
elif h.count(":") == 1:
|
| 211 |
+
h = h.split(":")[0]
|
| 212 |
+
h = h.rstrip(".")
|
| 213 |
+
if not h:
|
| 214 |
+
continue
|
| 215 |
+
if h in _META_HOSTS or h.endswith(".metadata.google.internal"):
|
| 216 |
+
return True
|
| 217 |
+
ip, plain = _host_addr(h)
|
| 218 |
+
if ip is None:
|
| 219 |
+
for m in _QUAD_IN_NAME.finditer(h): # an address embedded in a wildcard-DNS name
|
| 220 |
+
try:
|
| 221 |
+
e = ipaddress.IPv4Address(".".join(m.groups()))
|
| 222 |
+
except ValueError:
|
| 223 |
+
continue
|
| 224 |
+
if e in _META_ADDRS or e.is_link_local:
|
| 225 |
+
return True
|
| 226 |
+
continue
|
| 227 |
+
if ip in _META_ADDRS or ip.is_link_local:
|
| 228 |
+
return True
|
| 229 |
+
if not plain and (ip.is_private or ip.is_loopback or ip.is_unspecified or ip.is_reserved):
|
| 230 |
+
return True
|
| 231 |
+
return False
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def _deob(t):
|
| 235 |
+
"""Undo common shell disguises before judging a command (round 3)."""
|
| 236 |
+
t = re.sub(r"''|\"\"", "", t) # r''m
|
| 237 |
+
t = re.sub(chr(92) * 2 + r"(?=[A-Za-z])", "", t) # a backslash before a letter
|
| 238 |
+
t = re.sub(r"\$\{?IFS\}?(?:\$\d)?", " ", t) # rm$IFS-rf
|
| 239 |
+
t = re.sub(r"(?<![\w.])/(?:usr/)?(?:local/)?s?bin/(?=\w)", "", t) # /bin/rm
|
| 240 |
+
return t
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def _norm_path(p):
|
| 244 |
+
p = str(p).strip()
|
| 245 |
+
for _ in range(4): # round 2: double/triple URL-encoded paths
|
| 246 |
+
q = urllib.parse.unquote(p)
|
| 247 |
+
if q == p:
|
| 248 |
+
break
|
| 249 |
+
p = q
|
| 250 |
+
p = re.sub(r"(?i)^file://", "", p)
|
| 251 |
+
p = re.sub(r"^/proc/(self|\d+)/root(?=/)", "", p)
|
| 252 |
+
return posixpath.normpath(p) if p.startswith("/") else p
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def check(fn, args):
|
| 256 |
+
"""Return a category name to block, or None."""
|
| 257 |
+
fn = str(fn)
|
| 258 |
+
if not isinstance(args, dict) or any(not isinstance(v, (str, int, float, bool, type(None))) for v in args.values()):
|
| 259 |
+
return "unexpected argument type" # round 3: arrays/objects hid values from string checks
|
| 260 |
+
texts = list(_strings(args))
|
| 261 |
+
if fn in ("read_file", "list_files", "write_file"):
|
| 262 |
+
for key in ("path", "dir"):
|
| 263 |
+
raw = (args or {}).get(key, "") if isinstance(args, dict) else ""
|
| 264 |
+
if not raw:
|
| 265 |
+
continue
|
| 266 |
+
p = _norm_path(raw)
|
| 267 |
+
if REMOTE_RE.search(p):
|
| 268 |
+
return "remote address used as a file path"
|
| 269 |
+
if CRED_PATH.search(p) or CRED_PATH.search("/" + p.lstrip("./")) or SYS_READ.search(p):
|
| 270 |
+
return "credential store"
|
| 271 |
+
q = posixpath.normpath(p.replace("\\", "/"))
|
| 272 |
+
if q.startswith("/") or q.startswith("~") or q == ".." or q.startswith("../"):
|
| 273 |
+
return "path outside the workspace" # round 3: the agent works inside its workspace only
|
| 274 |
+
for t in texts:
|
| 275 |
+
for v in _views(t):
|
| 276 |
+
if QUERY_INJ.search(v):
|
| 277 |
+
return "query injection payload"
|
| 278 |
+
if fn not in READ_ONLY:
|
| 279 |
+
if _internal_addr(v):
|
| 280 |
+
return "internal address in tool arguments"
|
| 281 |
+
if SECRET_KV.search(v):
|
| 282 |
+
return "secret value in an outbound argument"
|
| 283 |
+
if any(s in v for s in TAINT) or any(s.lower() in v.lower() for s in TAINT):
|
| 284 |
+
return "file secret carried in an argument"
|
| 285 |
+
sq = _squash(v)
|
| 286 |
+
if any(n in sq or n[::-1] in sq for n in TAINT_N):
|
| 287 |
+
return "file secret carried in an argument"
|
| 288 |
+
if _respelled_hit(v):
|
| 289 |
+
return "file secret carried in an argument"
|
| 290 |
+
if fn not in READ_ONLY:
|
| 291 |
+
sq = _squash(" ".join(texts)) # a secret split across several arguments
|
| 292 |
+
if any(n in sq for n in TAINT_N) or _pieces_hit(sq):
|
| 293 |
+
return "file secret carried in an argument"
|
| 294 |
+
vq = _squash(" ".join(str(v) for v in args.values())) # values only, so the pieces join up
|
| 295 |
+
joined = "".join(_RECENT[-8:]) + vq # ... or across several calls
|
| 296 |
+
if any(n in joined for n in TAINT_N):
|
| 297 |
+
return "file secret carried across calls"
|
| 298 |
+
raw = " ".join(str(v) for v in args.values())[:400]
|
| 299 |
+
if _coverage_hit(_RECENT_RAW[-16:] + [raw]): # round 7: very short pieces over several calls
|
| 300 |
+
return "file secret carried across calls"
|
| 301 |
+
if 0 < len(vq) <= 400:
|
| 302 |
+
_RECENT.append(vq)
|
| 303 |
+
del _RECENT[:-8]
|
| 304 |
+
_RECENT_RAW.append(raw)
|
| 305 |
+
del _RECENT_RAW[:-16]
|
| 306 |
+
if fn not in READ_ONLY and fn not in ("run_shell", "transfer_funds"): # those two are judged by the kernel itself
|
| 307 |
+
free = " ".join(texts)
|
| 308 |
+
if MONEY.search(free):
|
| 309 |
+
return "money movement in tool arguments"
|
| 310 |
+
if UPLOAD.search(_deob(free)):
|
| 311 |
+
return "file upload command in tool arguments"
|
| 312 |
+
if fn == "app_run":
|
| 313 |
+
a = str(args.get("args", ""))
|
| 314 |
+
if APP_META.search(a):
|
| 315 |
+
return "shell metacharacters in app arguments"
|
| 316 |
+
if CMDLINE.search(_deob(a)):
|
| 317 |
+
return "command line in app arguments"
|
| 318 |
+
toks = _deob(a).split()
|
| 319 |
+
if toks and toks[0].lower() in SYS_CMDS and any(SHAPE.search(x) for x in toks[1:]):
|
| 320 |
+
return "command line in app arguments"
|
| 321 |
+
if _V is not None:
|
| 322 |
+
for t in texts:
|
| 323 |
+
if len(t) < 6:
|
| 324 |
+
continue
|
| 325 |
+
try:
|
| 326 |
+
if _V.decide(t)[0] == "DENY" or _V.decide(_deob(t))[0] == "DENY":
|
| 327 |
+
return "dangerous command in tool arguments"
|
| 328 |
+
except Exception: # noqa: BLE001
|
| 329 |
+
return "tool arguments could not be judged"
|
| 330 |
+
return None
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
def learn(fn, body):
|
| 334 |
+
"""After an allowed read, remember secret-looking values so they cannot be carried out later."""
|
| 335 |
+
if str(fn) != "read_file" or not body:
|
| 336 |
+
return
|
| 337 |
+
for _k, val in SECRET_KV.findall(str(body)):
|
| 338 |
+
if len(val) >= 6 and len(TAINT) < _TAINT_MAX:
|
| 339 |
+
TAINT.add(val)
|
| 340 |
+
if len(_squash(val)) >= 8:
|
| 341 |
+
TAINT_N.add(_squash(val))
|
| 342 |
+
for part in re.split(r"[-_:.]", val): # distinctive pieces of the secret, not the short prefix
|
| 343 |
+
if len(part) >= 8:
|
| 344 |
+
TAINT.add(part)
|
| 345 |
+
TAINT_N.add(_squash(part))
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
def _seed_from_mock_files():
|
| 349 |
+
"""The mock workspace is served by the agent layer, not the kernel, so the gate never sees file bodies.
|
| 350 |
+
Load the test environment's own secret values once at start-up (round 2: secret fragments in outbound calls)."""
|
| 351 |
+
try:
|
| 352 |
+
sys.path.insert(0, "/app")
|
| 353 |
+
from agentguard import tools as _T
|
| 354 |
+
for body in getattr(_T, "FILES", {}).values():
|
| 355 |
+
learn("read_file", body)
|
| 356 |
+
except Exception: # noqa: BLE001
|
| 357 |
+
pass
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
_seed_from_mock_files()
|