stacktrace-cli 0.22.0__py3-none-any.whl → 0.23.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stacktrace/__init__.py +1 -1
- stacktrace/agentspec/author.py +1 -1
- stacktrace/agentspec/checks.py +222 -21
- stacktrace/agentspec/effects/__init__.py +0 -7
- stacktrace/agentspec/engine.py +5 -3
- stacktrace/agentspec/judge.py +32 -6
- stacktrace/agentspec/redact.py +286 -2
- stacktrace/agentspec/triggers.py +2 -1
- stacktrace/daemon/config.py +19 -0
- stacktrace/daemon/policy_inputs.py +13 -11
- stacktrace/daemon/policy_runtime.py +22 -12
- stacktrace/daemon/presentation.py +73 -8
- stacktrace/daemon/runtime.py +4 -0
- stacktrace/daemon/store.py +9 -2
- stacktrace/daemon/usage.py +16 -3
- stacktrace/detector/deterministic.py +80 -6
- stacktrace/detector/inline.py +29 -5
- stacktrace/detector/personal_data.py +45 -10
- stacktrace/detector/rules.py +36 -25
- stacktrace/hooks/codex.py +23 -2
- stacktrace/hydrant_cli.py +42 -8
- stacktrace/remote/cli.py +2 -1
- stacktrace/remote/sync.py +17 -1
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.23.0.dist-info}/METADATA +4 -4
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.23.0.dist-info}/RECORD +27 -27
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.23.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.23.0.dist-info}/entry_points.txt +0 -0
stacktrace/agentspec/redact.py
CHANGED
|
@@ -14,14 +14,27 @@ the model still knows a secret was there and what kind. Four passes:
|
|
|
14
14
|
A `$NAME` reference is kept: it names a variable, not a value, and the model
|
|
15
15
|
needs it to see a secret travel.
|
|
16
16
|
|
|
17
|
+
`SessionValues` adds what one string cannot know: every value those passes
|
|
18
|
+
recognise anywhere in the session, plus personal data (`personal_data.py`),
|
|
19
|
+
each with an indexed placeholder (`<BEARER_TOKEN#1>`, `<EMAIL_ADDRESS#2>`).
|
|
20
|
+
Every view of one judged call replaces every occurrence of every known value,
|
|
21
|
+
so a token first seen in a log line and later pasted bare is still taken out,
|
|
22
|
+
and the judge can see it is the same token (ADR-0224).
|
|
23
|
+
|
|
17
24
|
`assert_clean` is the boundary check. The client calls it on every request
|
|
18
25
|
and refuses to send one that still carries a credential shape.
|
|
19
26
|
"""
|
|
20
27
|
|
|
21
28
|
from __future__ import annotations
|
|
22
29
|
|
|
30
|
+
import hashlib
|
|
31
|
+
import json
|
|
23
32
|
import re
|
|
33
|
+
import threading
|
|
34
|
+
from collections import OrderedDict
|
|
35
|
+
from collections.abc import Iterable, Iterator, Mapping
|
|
24
36
|
|
|
37
|
+
from stacktrace.detector.personal_data import personal_data_values, unescaped
|
|
25
38
|
from stacktrace.detector.secret_paths import kind_of
|
|
26
39
|
from stacktrace.detector.secrets import (
|
|
27
40
|
_PATTERNS, # pyright: ignore[reportPrivateUsage]
|
|
@@ -37,9 +50,27 @@ _ASSIGNMENT = re.compile(
|
|
|
37
50
|
r"""(?P<name>["']?[A-Za-z_][A-Za-z0-9_.-]*["']?)(?P<sep>\s*[:=]\s*)"""
|
|
38
51
|
r"""(?P<value>"[^"\n]*"|'[^'\n]*'|[^\s"',;&|)]+)"""
|
|
39
52
|
)
|
|
53
|
+
#: `_ASSIGNMENT` tried only where a name can start: the same matches over a
|
|
54
|
+
#: session's text, without re-reading every word from each of its letters.
|
|
55
|
+
_ASSIGNMENT_AT_A_NAME = re.compile(r"(?<![A-Za-z0-9_])" + _ASSIGNMENT.pattern)
|
|
40
56
|
_AUTH_HEADER = re.compile(r"(?i)(authorization\s*:\s*(?:bearer|basic|token)\s+)([^\s\"']+)")
|
|
41
57
|
_SCHEMES = frozenset({"bearer", "basic", "token"})
|
|
42
58
|
_DOTENV_LINE = re.compile(r"(?m)^(\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*)(.+)$")
|
|
59
|
+
#: A credential file's line as a step shows it, perhaps numbered (`cat -n`,
|
|
60
|
+
#: `grep -n`, a reading tool).
|
|
61
|
+
_NUMBERED_DOTENV_LINE = re.compile(
|
|
62
|
+
r"(?m)^(\s*(?:\d+[\t:])?\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*)(.+)$"
|
|
63
|
+
)
|
|
64
|
+
#: A placeholder, indexed or not: what a view keeps when it shortens text.
|
|
65
|
+
PLACEHOLDER = re.compile(r"<[A-Z][A-Z0-9_]*(?:#\d+)?>")
|
|
66
|
+
_INDEXED = re.compile(r"<[A-Z][A-Z0-9_]*#\d+>")
|
|
67
|
+
#: RFC 6750's b64token: every character a bearer credential can hold.
|
|
68
|
+
_TOKEN_RUN = re.compile(r"[A-Za-z0-9._~+/-]+=*")
|
|
69
|
+
#: A bearer value the writer already masked (`pl_live_[REDACTED]`, `sk-****`,
|
|
70
|
+
#: `abc…`): a few token characters, then one no token can hold.
|
|
71
|
+
_MASKED = re.compile(r"[A-Za-z0-9._~+/-]{0,15}[\[*\u2026<{]")
|
|
72
|
+
#: A JSON escape inside a string: where a value written in escaped text ends.
|
|
73
|
+
_ESCAPE = re.compile(r"\\[nrt\"]")
|
|
43
74
|
|
|
44
75
|
|
|
45
76
|
def _placeholder(kind: str) -> str:
|
|
@@ -47,8 +78,20 @@ def _placeholder(kind: str) -> str:
|
|
|
47
78
|
|
|
48
79
|
|
|
49
80
|
def _is_reference(value: str) -> bool:
|
|
81
|
+
"""A variable, a placeholder, or nothing: no value to take out. A value
|
|
82
|
+
that starts with an indexed placeholder already has been."""
|
|
50
83
|
bare = value.strip("\"'")
|
|
51
|
-
return
|
|
84
|
+
return (
|
|
85
|
+
bare.startswith("$")
|
|
86
|
+
or (bare.startswith("<") and bare.endswith(">"))
|
|
87
|
+
or not bare
|
|
88
|
+
or bool(_INDEXED.match(bare))
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _bearer(value: str) -> bool:
|
|
93
|
+
"""Whether an Authorization header's value is a credential to take out."""
|
|
94
|
+
return not _is_reference(value) and not _MASKED.match(value)
|
|
52
95
|
|
|
53
96
|
|
|
54
97
|
def _secret_name(name: str) -> bool:
|
|
@@ -65,7 +108,7 @@ def redact_text(text: str) -> str:
|
|
|
65
108
|
text = text[: found.start()] + _placeholder(kind) + text[found.end() :]
|
|
66
109
|
|
|
67
110
|
text = _AUTH_HEADER.sub(
|
|
68
|
-
lambda m: m.group(
|
|
111
|
+
lambda m: f"{m.group(1)}<BEARER_TOKEN>" if _bearer(m.group(2)) else m.group(0),
|
|
69
112
|
text,
|
|
70
113
|
)
|
|
71
114
|
|
|
@@ -101,6 +144,247 @@ def redact_value(value: object, depth: int = 0) -> object:
|
|
|
101
144
|
return value
|
|
102
145
|
|
|
103
146
|
|
|
147
|
+
# --- one session's values ----------------------------------------------------
|
|
148
|
+
|
|
149
|
+
#: Named only by the name it was assigned to, so it must look like a value
|
|
150
|
+
#: before every copy of it is taken out: not a path, not code, not a word.
|
|
151
|
+
_CODE = re.compile(r"[()\[\]{}<>\s]")
|
|
152
|
+
_PERSONAL = frozenset({"payment_card", "iban", "us_ssn", "email_address", "phone_number"})
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _recognised(text: str, path: str | None) -> Iterator[tuple[str, str]]:
|
|
156
|
+
"""(kind, value) for each value the four passes would take out of `text`,
|
|
157
|
+
then each piece of personal data, normalized."""
|
|
158
|
+
for kind, found in _claimed_matches(text, _PATTERNS):
|
|
159
|
+
yield kind, found.group(0)
|
|
160
|
+
for m in _AUTH_HEADER.finditer(text):
|
|
161
|
+
run = _TOKEN_RUN.match(m.group(2))
|
|
162
|
+
if _bearer(m.group(2)) and run:
|
|
163
|
+
# The token itself, not the escaped text after it (`TOKEN\nHost:`).
|
|
164
|
+
yield "bearer_token", run.group(0)
|
|
165
|
+
for m in _ASSIGNMENT_AT_A_NAME.finditer(text):
|
|
166
|
+
name, value = m.group("name"), m.group("value")
|
|
167
|
+
if _secret_name(name) and not _is_reference(value) and value.lower() not in _SCHEMES:
|
|
168
|
+
yield "secret", _ESCAPE.split(value.strip("\"'"), maxsplit=1)[0]
|
|
169
|
+
if path is not None and kind_of(path) is not None:
|
|
170
|
+
for m in _NUMBERED_DOTENV_LINE.finditer(text):
|
|
171
|
+
value = m.group(2).strip()
|
|
172
|
+
if not _is_reference(value):
|
|
173
|
+
bare = value[1:-1] if value[:1] in "\"'" and value[-1:] == value[:1] else None
|
|
174
|
+
yield "secret", bare if bare is not None else value.split()[0]
|
|
175
|
+
for kind, values in personal_data_values(text).items():
|
|
176
|
+
for value in sorted(values):
|
|
177
|
+
yield kind, value
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _linkable(kind: str, value: str) -> bool:
|
|
181
|
+
"""Whether every copy of `value` can be taken out without taking ordinary
|
|
182
|
+
text with it."""
|
|
183
|
+
if kind in _PERSONAL:
|
|
184
|
+
return True
|
|
185
|
+
if len(value) < 8 or _is_reference(value):
|
|
186
|
+
return False
|
|
187
|
+
if kind == "secret":
|
|
188
|
+
return (
|
|
189
|
+
not value.startswith(("/", "~", "."))
|
|
190
|
+
and not _CODE.search(value)
|
|
191
|
+
and (any(c.isdigit() for c in value) or len(value) >= 16)
|
|
192
|
+
)
|
|
193
|
+
return True
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
#: What each text recognised, by digest: a session's map is rebuilt for every
|
|
197
|
+
#: judged call, and a long session's earlier steps are the same text each time.
|
|
198
|
+
_FOUND: OrderedDict[bytes, tuple[tuple[str, str], ...]] = OrderedDict()
|
|
199
|
+
_FOUND_LIMIT = 100_000
|
|
200
|
+
_FOUND_LOCK = threading.Lock()
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _found(text: str, path: str | None) -> tuple[tuple[str, str], ...]:
|
|
204
|
+
"""The linkable (kind, value) pairs in `text`, read once per distinct text."""
|
|
205
|
+
credential_file = path is not None and kind_of(path) is not None
|
|
206
|
+
key = hashlib.blake2b(text.encode(errors="surrogatepass"), digest_size=16).digest()
|
|
207
|
+
key += b"f" if credential_file else b"t"
|
|
208
|
+
with _FOUND_LOCK:
|
|
209
|
+
hit = _FOUND.get(key)
|
|
210
|
+
if hit is not None:
|
|
211
|
+
_FOUND.move_to_end(key)
|
|
212
|
+
return hit
|
|
213
|
+
found = tuple(
|
|
214
|
+
(kind, value)
|
|
215
|
+
for kind, value in _recognised(text, path if credential_file else None)
|
|
216
|
+
if _linkable(kind, value)
|
|
217
|
+
)
|
|
218
|
+
with _FOUND_LOCK:
|
|
219
|
+
_FOUND[key] = found
|
|
220
|
+
while len(_FOUND) > _FOUND_LIMIT:
|
|
221
|
+
_FOUND.popitem(last=False)
|
|
222
|
+
return found
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _written(kind: str, value: str) -> re.Pattern[str] | None:
|
|
226
|
+
"""A pattern for `value` as text writes it, or None where text writes it
|
|
227
|
+
exactly. Personal data is found normalized, so a card or phone number
|
|
228
|
+
matches with or without its separators, and an address in any case."""
|
|
229
|
+
if kind not in ("payment_card", "iban", "phone_number", "email_address"):
|
|
230
|
+
return None
|
|
231
|
+
return re.compile(_written_source(kind, value))
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _written_source(kind: str, value: str) -> str:
|
|
235
|
+
if kind in ("payment_card", "iban"):
|
|
236
|
+
return r"(?<![0-9A-Za-z])" + "[ -]?".join(map(re.escape, value)) + r"(?![0-9A-Za-z])"
|
|
237
|
+
if kind == "phone_number":
|
|
238
|
+
# A North American number is found with its country code, and is
|
|
239
|
+
# often written without it.
|
|
240
|
+
national = len(value) == 11 and value[0] == "1"
|
|
241
|
+
lead = r"(?:\+?1[ .-]?)?" if national else r"\+?"
|
|
242
|
+
digits = value[1:] if national else value
|
|
243
|
+
return r"(?<![\w+])" + lead + r"\(?" + r"[ ().-]{0,2}".join(digits) + r"(?!\w)"
|
|
244
|
+
return "(?i:" + re.escape(value) + ")"
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _could_hold(kind: str, value: str, text: str) -> bool:
|
|
248
|
+
"""A cheap test that `text` may hold `value` as `_written` matches it."""
|
|
249
|
+
if kind == "email_address":
|
|
250
|
+
return "@" in text
|
|
251
|
+
if kind == "iban":
|
|
252
|
+
return value[:2] in text
|
|
253
|
+
return value[-2:] in text
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class SessionValues:
|
|
257
|
+
"""Every sensitive value seen in one session, each with a typed, indexed
|
|
258
|
+
placeholder, numbered per kind in the order the session first showed it.
|
|
259
|
+
|
|
260
|
+
Built for one judged call and used by every view of it, so one value has
|
|
261
|
+
one placeholder across them all. Holds values only in memory: nothing here
|
|
262
|
+
is written, logged or put in a finding.
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
def __init__(self) -> None:
|
|
266
|
+
self._tags: dict[str, str] = {}
|
|
267
|
+
self._kinds: dict[str, str] = {}
|
|
268
|
+
self._per_kind: dict[str, int] = {}
|
|
269
|
+
self._order: list[tuple[str, str, str, re.Pattern[str] | None]] | None = None
|
|
270
|
+
|
|
271
|
+
def __len__(self) -> int:
|
|
272
|
+
return len(self._tags)
|
|
273
|
+
|
|
274
|
+
def learn(self, text: str, path: str | None = None) -> None:
|
|
275
|
+
"""Every value redaction recognises in `text`. `path` is the file the
|
|
276
|
+
text came from, when it did: a credential file loses every value."""
|
|
277
|
+
if not text:
|
|
278
|
+
return
|
|
279
|
+
for kind, value in _found(text, path):
|
|
280
|
+
if value in self._tags:
|
|
281
|
+
continue
|
|
282
|
+
n = self._per_kind[kind] = self._per_kind.get(kind, 0) + 1
|
|
283
|
+
self._tags[value] = f"<{kind.upper()}#{n}>"
|
|
284
|
+
self._kinds[value] = kind
|
|
285
|
+
self._order = None
|
|
286
|
+
|
|
287
|
+
def learn_value(self, value: object, path: str | None = None, depth: int = 0) -> None:
|
|
288
|
+
"""Every string in a JSON-shaped value, read as `learn` reads text, and
|
|
289
|
+
the value serialized whole: `{"api_key": "..."}` names its secret only
|
|
290
|
+
through its key, which a leaf alone does not carry."""
|
|
291
|
+
if depth > 24:
|
|
292
|
+
return
|
|
293
|
+
if depth == 0 and isinstance(value, (dict, list, tuple)):
|
|
294
|
+
self.learn(json.dumps(value, default=str), path)
|
|
295
|
+
if isinstance(value, str):
|
|
296
|
+
self.learn(value, path)
|
|
297
|
+
elif isinstance(value, dict):
|
|
298
|
+
# A key is text too: a mapping keyed by an address holds that address.
|
|
299
|
+
for key, item in value.items(): # pyright: ignore[reportUnknownVariableType]
|
|
300
|
+
if isinstance(key, str):
|
|
301
|
+
self.learn(key, path)
|
|
302
|
+
self.learn_value(item, path, depth + 1)
|
|
303
|
+
elif isinstance(value, (list, tuple)):
|
|
304
|
+
for item in value: # pyright: ignore[reportUnknownVariableType]
|
|
305
|
+
self.learn_value(item, path, depth + 1)
|
|
306
|
+
|
|
307
|
+
def _entries(self) -> list[tuple[str, str, str, re.Pattern[str] | None]]:
|
|
308
|
+
"""(value, kind, placeholder, pattern), longest value first: where one
|
|
309
|
+
value holds another, the whole is taken. One pass per value, each a
|
|
310
|
+
substring test first: a single pattern of every value is far slower."""
|
|
311
|
+
if self._order is None:
|
|
312
|
+
self._order = [
|
|
313
|
+
(v, self._kinds[v], self._tags[v], _written(self._kinds[v], v))
|
|
314
|
+
for v in sorted(self._tags, key=len, reverse=True)
|
|
315
|
+
]
|
|
316
|
+
return self._order
|
|
317
|
+
|
|
318
|
+
def replace(self, text: str) -> str:
|
|
319
|
+
"""`text` with every known value replaced by its placeholder. Learning
|
|
320
|
+
reads text with JSON's escapes undone; where a value still hides behind
|
|
321
|
+
an escape (`ana\\u0040acme.io`), the text is replaced in that form."""
|
|
322
|
+
if not text:
|
|
323
|
+
return text
|
|
324
|
+
out = self._replace(text)
|
|
325
|
+
if "\\" in out and self.kinds_in([out]):
|
|
326
|
+
out = self._replace(unescaped(out))
|
|
327
|
+
return out
|
|
328
|
+
|
|
329
|
+
def _replace(self, text: str) -> str:
|
|
330
|
+
for value, kind, tag, pattern in self._entries():
|
|
331
|
+
if pattern is None:
|
|
332
|
+
if value in text:
|
|
333
|
+
text = text.replace(value, tag)
|
|
334
|
+
elif _could_hold(kind, value, text):
|
|
335
|
+
text = pattern.sub(tag, text)
|
|
336
|
+
return text
|
|
337
|
+
|
|
338
|
+
def redact(self, text: str) -> str:
|
|
339
|
+
"""Known values first, so each keeps its index, then `redact_text`."""
|
|
340
|
+
return redact_text(self.replace(text))
|
|
341
|
+
|
|
342
|
+
def redact_file(self, path: str, text: str) -> str:
|
|
343
|
+
return redact_file(path, self.replace(text))
|
|
344
|
+
|
|
345
|
+
def redact_value(self, value: object, depth: int = 0) -> object:
|
|
346
|
+
"""A JSON-shaped value with every string in it redacted."""
|
|
347
|
+
if depth > 24:
|
|
348
|
+
return "<TOO_DEEP>"
|
|
349
|
+
if isinstance(value, str):
|
|
350
|
+
return self.redact(value)
|
|
351
|
+
if isinstance(value, dict):
|
|
352
|
+
out: dict[str, object] = {}
|
|
353
|
+
for k, v in value.items(): # pyright: ignore[reportUnknownVariableType]
|
|
354
|
+
key = self.redact(str(k)) # pyright: ignore[reportUnknownArgumentType]
|
|
355
|
+
# Two spellings of one value redact to one key; keep both entries.
|
|
356
|
+
n = 2
|
|
357
|
+
while key in out:
|
|
358
|
+
key, n = f"{self.redact(str(k))} ({n})", n + 1 # pyright: ignore[reportUnknownArgumentType]
|
|
359
|
+
out[key] = self.redact_value(v, depth + 1)
|
|
360
|
+
return out
|
|
361
|
+
if isinstance(value, (list, tuple)):
|
|
362
|
+
return [self.redact_value(v, depth + 1) for v in value] # pyright: ignore[reportUnknownVariableType]
|
|
363
|
+
return value
|
|
364
|
+
|
|
365
|
+
def guard(self, state: Mapping[str, object]) -> dict[str, object]:
|
|
366
|
+
"""A judge request's state as it may leave: every value in it learned,
|
|
367
|
+
then every string and key redacted. The one boundary every request to
|
|
368
|
+
the judge passes (`judge.JevJudge.ask`), whatever built the state."""
|
|
369
|
+
self.learn_value(dict(state))
|
|
370
|
+
out = self.redact_value(dict(state))
|
|
371
|
+
assert isinstance(out, dict)
|
|
372
|
+
return {str(k): v for k, v in out.items()} # pyright: ignore[reportUnknownVariableType]
|
|
373
|
+
|
|
374
|
+
def kinds_in(self, texts: Iterable[str]) -> list[str]:
|
|
375
|
+
"""The kinds of known values still in `texts`, raw. Never a value."""
|
|
376
|
+
found: set[str] = set()
|
|
377
|
+
for text in map(unescaped, texts):
|
|
378
|
+
for value, kind, _, pattern in self._entries():
|
|
379
|
+
if pattern is None:
|
|
380
|
+
hit = value in text
|
|
381
|
+
else:
|
|
382
|
+
hit = _could_hold(kind, value, text) and pattern.search(text) is not None
|
|
383
|
+
if hit:
|
|
384
|
+
found.add(kind)
|
|
385
|
+
return sorted(found)
|
|
386
|
+
|
|
387
|
+
|
|
104
388
|
class SecretLeak(ValueError):
|
|
105
389
|
"""A request still carried a credential shape after redaction."""
|
|
106
390
|
|
stacktrace/agentspec/triggers.py
CHANGED
|
@@ -199,7 +199,8 @@ _CONTEXT = (
|
|
|
199
199
|
"`call` is one tool call an AI coding agent is about to make. `recent_steps` are the "
|
|
200
200
|
"calls before it with short results, `person_said` is what the person who owns the agent "
|
|
201
201
|
"typed, and `project_config` holds small config and readme files beside what the call "
|
|
202
|
-
"touches. Secret values appear as placeholders such as
|
|
202
|
+
"touches. Secret values and personal data appear as placeholders such as "
|
|
203
|
+
"<GITHUB_TOKEN#1>; one <KIND#n> marks the same value wherever it appears. "
|
|
203
204
|
)
|
|
204
205
|
|
|
205
206
|
|
stacktrace/daemon/config.py
CHANGED
|
@@ -42,6 +42,10 @@ SETTINGS: tuple[tuple[str, str, str], ...] = (
|
|
|
42
42
|
# Environment only: the detector reads the same variable wherever it runs,
|
|
43
43
|
# so the daemon, a detached `start` and `guard serve` cannot disagree.
|
|
44
44
|
("company_domains", "STACKTRACE_COMPANY_DOMAINS", ""),
|
|
45
|
+
# ADR-0220: where the enforced policy comes from. `local` never fetches
|
|
46
|
+
# the Fleet org's policy and enforces only the custom rules file; findings
|
|
47
|
+
# still upload. Environment only, as the engine's own switch is.
|
|
48
|
+
("policy_source", "STACKTRACE_POLICY_SOURCE", "fleet"),
|
|
45
49
|
)
|
|
46
50
|
|
|
47
51
|
_UNITS: dict[str, int] = {"s": 1, "m": 60, "h": 3600, "d": 86400}
|
|
@@ -157,6 +161,14 @@ def parse_gateway_mode(value: str) -> str:
|
|
|
157
161
|
return value
|
|
158
162
|
|
|
159
163
|
|
|
164
|
+
def parse_policy_source(value: str) -> str:
|
|
165
|
+
# Refused, never read as `fleet`: a mistyped `local` would otherwise
|
|
166
|
+
# enforce the org's policy on a machine told to ignore it.
|
|
167
|
+
if value not in ("fleet", "local"):
|
|
168
|
+
raise ValueError(f"policy source must be 'fleet' or 'local', not {value!r}")
|
|
169
|
+
return value
|
|
170
|
+
|
|
171
|
+
|
|
160
172
|
@dataclass(frozen=True)
|
|
161
173
|
class Setting:
|
|
162
174
|
"""One resolved value, the text it was resolved from, and where that came from."""
|
|
@@ -180,6 +192,7 @@ class DaemonConfig:
|
|
|
180
192
|
gateway_listen: Setting
|
|
181
193
|
gateway_mode: Setting
|
|
182
194
|
company_domains: Setting
|
|
195
|
+
policy_source: Setting
|
|
183
196
|
|
|
184
197
|
@property
|
|
185
198
|
def scan_interval_seconds(self) -> float:
|
|
@@ -212,6 +225,10 @@ class DaemonConfig:
|
|
|
212
225
|
def reasoning_enabled(self) -> bool:
|
|
213
226
|
return parse_reasoning(self.reasoning.value)
|
|
214
227
|
|
|
228
|
+
@property
|
|
229
|
+
def local_policy(self) -> bool:
|
|
230
|
+
return parse_policy_source(self.policy_source.value) == "local"
|
|
231
|
+
|
|
215
232
|
@property
|
|
216
233
|
def company_domain_set(self) -> frozenset[str]:
|
|
217
234
|
return parse_company_domains(self.company_domains.value)
|
|
@@ -295,6 +312,7 @@ class DaemonConfig:
|
|
|
295
312
|
"gateway_listen": None,
|
|
296
313
|
"gateway_mode": None,
|
|
297
314
|
"company_domains": None,
|
|
315
|
+
"policy_source": None,
|
|
298
316
|
}
|
|
299
317
|
resolved: dict[str, Setting] = {}
|
|
300
318
|
for name, variable, default in SETTINGS:
|
|
@@ -324,6 +342,7 @@ class DaemonConfig:
|
|
|
324
342
|
parse_listen(config.gateway_listen.value),
|
|
325
343
|
parse_gateway_mode(config.gateway_mode.value),
|
|
326
344
|
config.company_domain_set,
|
|
345
|
+
config.local_policy,
|
|
327
346
|
)
|
|
328
347
|
if not config.kinds:
|
|
329
348
|
raise ValueError("--agent-kind must name at least one agent kind")
|
|
@@ -41,7 +41,7 @@ from stacktrace.remote.config import (
|
|
|
41
41
|
load_remote_config,
|
|
42
42
|
save_remote_config,
|
|
43
43
|
)
|
|
44
|
-
from stacktrace.remote.sync import _asset_transition_lock
|
|
44
|
+
from stacktrace.remote.sync import _asset_transition_lock, _enrollment_lock
|
|
45
45
|
|
|
46
46
|
_MAX_DOCUMENT_BYTES = 2 * 1024 * 1024
|
|
47
47
|
#: The served fields this build leaves out of the whole-envelope digest: the
|
|
@@ -720,22 +720,24 @@ class PolicyFetcher:
|
|
|
720
720
|
config = replace(
|
|
721
721
|
config, org_id=org_id, enrollment_epoch=config.enrollment_epoch + 1
|
|
722
722
|
)
|
|
723
|
-
|
|
723
|
+
with _enrollment_lock(self._config_path):
|
|
724
|
+
save_remote_config(config, self._config_path)
|
|
724
725
|
self._binding = (config.api_url.rstrip("/"), org_id, config.enrollment_epoch)
|
|
725
726
|
assert config.org_id is not None
|
|
726
727
|
tenant = Tenant(config.api_url, config.org_id)
|
|
727
728
|
update = client.get_policy_update(etag=self._etag)
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
729
|
+
with _enrollment_lock(self._config_path):
|
|
730
|
+
if update.envelope is None:
|
|
731
|
+
state = self._store.read(tenant)
|
|
732
|
+
if state.desired_revision is None:
|
|
733
|
+
self._etag = None
|
|
734
|
+
raise PolicyInputError("policy_unchanged_without_inputs")
|
|
735
|
+
self._on_state(config, state)
|
|
736
|
+
return state
|
|
737
|
+
state = self._store.ingest(tenant, update.envelope)
|
|
733
738
|
self._on_state(config, state)
|
|
739
|
+
self._etag = update.etag
|
|
734
740
|
return state
|
|
735
|
-
state = self._store.ingest(tenant, update.envelope)
|
|
736
|
-
self._on_state(config, state)
|
|
737
|
-
self._etag = update.etag
|
|
738
|
-
return state
|
|
739
741
|
except PolicyInputError:
|
|
740
742
|
self._etag = None
|
|
741
743
|
raise
|
|
@@ -39,7 +39,7 @@ from stacktrace.detector.rules import (
|
|
|
39
39
|
from stacktrace.package_logger import get_logger
|
|
40
40
|
from stacktrace.policy_display import default_description
|
|
41
41
|
from stacktrace.remote.config import ConfigError, RemoteConfig, load_remote_config
|
|
42
|
-
from stacktrace.remote.sync import
|
|
42
|
+
from stacktrace.remote.sync import _enrollment_lock
|
|
43
43
|
|
|
44
44
|
if TYPE_CHECKING:
|
|
45
45
|
from stacktrace.daemon.mcp_records import AccountRecords
|
|
@@ -497,7 +497,12 @@ def configuration_digest(scope: Scope, watch_paths: tuple[str, ...]) -> str:
|
|
|
497
497
|
}
|
|
498
498
|
if scope.agent_kind == "codex":
|
|
499
499
|
paths = {
|
|
500
|
-
|
|
500
|
+
# Desktop replaces runtime state in this root during normal tool use.
|
|
501
|
+
# Watch configuration and discovery directories, not their parent.
|
|
502
|
+
str(root / "plugins"),
|
|
503
|
+
str(root / "plugins" / "cache"),
|
|
504
|
+
str(root / "cache" / "codex_apps_tools"),
|
|
505
|
+
*(str(path) for path in root.glob("*.config.toml")),
|
|
501
506
|
str(root / "config.toml"),
|
|
502
507
|
str(root / "hooks.json"),
|
|
503
508
|
str(project / ".codex"),
|
|
@@ -559,8 +564,13 @@ class PolicyRuntime:
|
|
|
559
564
|
catch_up: Callable[[str, str, frozenset[str]], None] | None = None,
|
|
560
565
|
catch_up_timeout: float = CATCH_UP_TIMEOUT,
|
|
561
566
|
engine: PolicyEngine | None = None,
|
|
567
|
+
local: bool = False,
|
|
562
568
|
) -> None:
|
|
563
569
|
self.config_path = config_path
|
|
570
|
+
#: Enforce no Fleet policy (ADR-0220): never fetch it, never read a
|
|
571
|
+
#: stored one, so only the engine's custom rules decide. The
|
|
572
|
+
#: enrollment still holds the token the findings upload with.
|
|
573
|
+
self.local = local
|
|
564
574
|
#: The AgentSpec engine, when it is on (ADR-0214). None keeps every
|
|
565
575
|
#: decision exactly as the released hook makes it.
|
|
566
576
|
self.engine = engine
|
|
@@ -646,7 +656,7 @@ class PolicyRuntime:
|
|
|
646
656
|
if binding != self.cache.binding:
|
|
647
657
|
state = (
|
|
648
658
|
self.store.read(Tenant(config.api_url, config.org_id))
|
|
649
|
-
if config.org_id
|
|
659
|
+
if config.org_id and not self.local
|
|
650
660
|
else PolicyState()
|
|
651
661
|
)
|
|
652
662
|
self._publish_inputs(config, state)
|
|
@@ -655,10 +665,10 @@ class PolicyRuntime:
|
|
|
655
665
|
try:
|
|
656
666
|
config = load_remote_config(self.config_path)
|
|
657
667
|
if not config.token:
|
|
658
|
-
with
|
|
668
|
+
with _enrollment_lock(self.config_path, timeout=0.1):
|
|
659
669
|
self._read_inputs(load_remote_config(self.config_path))
|
|
660
670
|
return
|
|
661
|
-
if self.clock() - self._fetch_at >= self.poll_interval:
|
|
671
|
+
if not self.local and self.clock() - self._fetch_at >= self.poll_interval:
|
|
662
672
|
self._fetch_at = self.clock()
|
|
663
673
|
try:
|
|
664
674
|
self.fetcher.refresh()
|
|
@@ -673,7 +683,7 @@ class PolicyRuntime:
|
|
|
673
683
|
self.cache.invalidate(scope)
|
|
674
684
|
self._digests[scope] = digest
|
|
675
685
|
# Offline restart loads desired inputs, never executable decisions.
|
|
676
|
-
with
|
|
686
|
+
with _enrollment_lock(self.config_path, timeout=0.1):
|
|
677
687
|
config = load_remote_config(self.config_path)
|
|
678
688
|
self._read_inputs(config)
|
|
679
689
|
with self.cache.guard:
|
|
@@ -707,7 +717,7 @@ class PolicyRuntime:
|
|
|
707
717
|
# join the next pass's watch set only after successful publication.
|
|
708
718
|
digest = configuration_digest(scope, self._paths.get(scope, ()))
|
|
709
719
|
discovered_digest = configuration_digest(scope, collected.watch_paths)
|
|
710
|
-
with
|
|
720
|
+
with _enrollment_lock(self.config_path, timeout=0.1):
|
|
711
721
|
config = load_remote_config(self.config_path)
|
|
712
722
|
self._read_inputs(config)
|
|
713
723
|
if digest != self._digests[scope] or discovered_digest != configuration_digest(
|
|
@@ -768,7 +778,7 @@ class PolicyRuntime:
|
|
|
768
778
|
str(value["global_config"]),
|
|
769
779
|
str(value["agent_kind"]),
|
|
770
780
|
)
|
|
771
|
-
with
|
|
781
|
+
with _enrollment_lock(self.config_path, timeout=0.05):
|
|
772
782
|
config = load_remote_config(self.config_path)
|
|
773
783
|
self._read_inputs(config)
|
|
774
784
|
with self.cache.guard:
|
|
@@ -853,7 +863,7 @@ class PolicyRuntime:
|
|
|
853
863
|
str(action["global_config"]),
|
|
854
864
|
str(action["agent_kind"]),
|
|
855
865
|
)
|
|
856
|
-
with
|
|
866
|
+
with _enrollment_lock(self.config_path, timeout=0.05):
|
|
857
867
|
config = load_remote_config(self.config_path)
|
|
858
868
|
self._read_inputs(config)
|
|
859
869
|
if not config.token:
|
|
@@ -1270,7 +1280,7 @@ class PolicyRuntime:
|
|
|
1270
1280
|
def detection_entry(self, rule_id: str) -> DetectionEntry | None:
|
|
1271
1281
|
"""The current policy's entry for `rule_id`, if it has one."""
|
|
1272
1282
|
try:
|
|
1273
|
-
with
|
|
1283
|
+
with _enrollment_lock(self.config_path, timeout=0.05):
|
|
1274
1284
|
config = load_remote_config(self.config_path)
|
|
1275
1285
|
self._read_inputs(config)
|
|
1276
1286
|
if not config.token:
|
|
@@ -1306,7 +1316,7 @@ class PolicyRuntime:
|
|
|
1306
1316
|
|
|
1307
1317
|
def status(self) -> dict[str, object]:
|
|
1308
1318
|
try:
|
|
1309
|
-
with
|
|
1319
|
+
with _enrollment_lock(self.config_path, timeout=0.05):
|
|
1310
1320
|
config = load_remote_config(self.config_path)
|
|
1311
1321
|
self._read_inputs(config)
|
|
1312
1322
|
return self._status(bool(config.token))
|
|
@@ -1314,7 +1324,7 @@ class PolicyRuntime:
|
|
|
1314
1324
|
return self._status(False)
|
|
1315
1325
|
|
|
1316
1326
|
def view(self, project_root: str | None = None) -> dict[str, object]:
|
|
1317
|
-
with
|
|
1327
|
+
with _enrollment_lock(self.config_path, timeout=0.05):
|
|
1318
1328
|
config = load_remote_config(self.config_path)
|
|
1319
1329
|
self._read_inputs(config)
|
|
1320
1330
|
with self.cache.guard:
|