@raytrace-cli/cli 0.17.17 → 0.19.0
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.
- package/bin/raytrace.mjs +9 -2
- package/package.json +5 -1
- package/src/auth.mjs +3 -3
- package/src/beta.mjs +12 -10
- package/src/local-sandbox.mjs +21 -5
- package/src/mcp.mjs +11 -12
- package/src/sandbox.mjs +19 -20
- package/src/update-check.mjs +113 -0
- package/worker/sandbox/CORE_API.md +167 -0
- package/worker/sandbox/Dockerfile +7 -1
- package/worker/sandbox/agents.json +4 -0
- package/worker/sandbox/catalog_numbers.py +144 -0
- package/worker/sandbox/core/__init__.py +36 -0
- package/worker/sandbox/core/mechanics.py +882 -0
- package/worker/sandbox/core/release.py +90 -0
- package/worker/sandbox/engine.py +20 -7
- package/worker/sandbox/network.py +36 -4
- package/worker/sandbox/projects.py +8 -726
- package/worker/sandbox/setup.py +149 -21
- package/worker/sandbox/test_capture.py +0 -439
- package/worker/sandbox/test_dns_asker.py +0 -102
- package/worker/sandbox/transcript-delivery.test.mjs +0 -153
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
"""Raw-call context and failed execs. Run: python3 -B -m unittest discover -s worker/sandbox -p 'test_*.py'"""
|
|
2
|
-
import json
|
|
3
|
-
import os
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
import sqlite3
|
|
6
|
-
import sys
|
|
7
|
-
import tempfile
|
|
8
|
-
import time
|
|
9
|
-
import unittest
|
|
10
|
-
|
|
11
|
-
sys.path.insert(0, str(Path(__file__).parent))
|
|
12
|
-
import collector # noqa: E402
|
|
13
|
-
import engine # noqa: E402
|
|
14
|
-
import setup # noqa: E402
|
|
15
|
-
|
|
16
|
-
HERE = Path(__file__).parent
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def attempt(pid, executable, argv):
|
|
20
|
-
return {'kind': 'exec_attempt', 'pid': pid, 'ppid': 1, 'executable': executable, 'argv': argv,
|
|
21
|
-
'timestamp_ns': '1794866886430727989', 'process_start_ns': '1794866886430553658', 'container_id': 'c' * 64,
|
|
22
|
-
'markers': {'RAYTRACE_CALL_ID': 'call_1'}}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class TraceConfig(unittest.TestCase):
|
|
26
|
-
def test_a_number_only_call_has_its_enter_point_with_the_same_context(self):
|
|
27
|
-
config = setup.trace_config('aarch64', (HERE / 'syscall_catalog.sql').read_text(), '/run/x.sock')
|
|
28
|
-
points = {point['name']: point for point in config['trace_session']['points']}
|
|
29
|
-
for number in (34, 35, 38, 45, 46, 52, 53, 276): # mkdirat unlinkat renameat truncate ftruncate fchmod fchmodat renameat2
|
|
30
|
-
enter, leave = points[f'syscall/sysno/{number}/enter'], points[f'syscall/sysno/{number}/exit']
|
|
31
|
-
self.assertEqual(enter['context_fields'], leave['context_fields'])
|
|
32
|
-
self.assertIn('container_id', enter['context_fields'])
|
|
33
|
-
self.assertIn('group_id', enter['context_fields'])
|
|
34
|
-
# Typed points carry their own context, and the exec points are as they were.
|
|
35
|
-
self.assertNotIn('syscall/openat/enter', points)
|
|
36
|
-
self.assertNotIn('syscall/sysno/221/enter', points)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class RawCalls(unittest.TestCase):
|
|
40
|
-
def setUp(self):
|
|
41
|
-
self.engine = engine.Engine(':memory:', arch='aarch64', watch=False)
|
|
42
|
-
|
|
43
|
-
def test_the_enter_event_is_taken_and_not_queued(self):
|
|
44
|
-
self.assertTrue(self.engine.accept({'kind': 'syscall_enter', 'sysno': 35, 'container_id': 'c' * 64}))
|
|
45
|
-
self.assertTrue(self.engine.inbox.empty())
|
|
46
|
-
self.assertTrue(self.engine.accept({'kind': 'syscall_return', 'sysno': 35, 'container_id': 'c' * 64}))
|
|
47
|
-
self.assertEqual(self.engine.inbox.qsize(), 1)
|
|
48
|
-
|
|
49
|
-
def test_a_refused_delete_with_its_process_is_an_attempt_by_that_process(self):
|
|
50
|
-
box = engine.Sandbox('c' * 64)
|
|
51
|
-
box.workspace, box.guarded = '/nowhere', {'config/keys.js': 'watched'}
|
|
52
|
-
box.processes[41] = {'start_ns': '5', 'ppid': 1, 'name': 'rm', 'argv': ['rm', 'config/keys.js'], 'call_id': 'call_9'}
|
|
53
|
-
self.engine.numbered_call(box, {'kind': 'syscall_return', 'sysno': 35, 'errno': 13, 'pid': 41, 'ppid': 1,
|
|
54
|
-
'process_start_ns': '5', 'timestamp_ns': '100', 'cwd': '/workspace'})
|
|
55
|
-
(fact,) = self.engine.facts.values()
|
|
56
|
-
self.assertEqual((fact['path'], fact['effect'], fact['intent'], fact['pid'], fact['call_id']),
|
|
57
|
-
('config/keys.js', 'attempted', 'delete', 41, 'call_9'))
|
|
58
|
-
self.assertEqual(box.raw[0]['who']['pid'], 41)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
class WhoChangedIt(unittest.TestCase):
|
|
62
|
-
"""engine.look(): a change goes to the process that made it, not to
|
|
63
|
-
whoever opened that name before."""
|
|
64
|
-
def setUp(self):
|
|
65
|
-
self.now = 1000.0
|
|
66
|
-
self.engine = engine.Engine(':memory:', arch='aarch64', watch=False, clock=lambda: self.now)
|
|
67
|
-
self.folder = tempfile.TemporaryDirectory()
|
|
68
|
-
self.addCleanup(self.folder.cleanup)
|
|
69
|
-
self.box = engine.Sandbox('c' * 64)
|
|
70
|
-
self.box.workspace, self.box.ip = self.folder.name, '172.30.60.3'
|
|
71
|
-
self.write('keys.js', 'one')
|
|
72
|
-
self.box.guarded = {'keys.js': 'watched'}
|
|
73
|
-
self.box.photos['keys.js'] = engine.photo(self.folder.name, 'keys.js')
|
|
74
|
-
for pid, name in ((10, 'python3'), (20, 'sed'), (30, 'truncate')):
|
|
75
|
-
self.box.processes[pid] = {'start_ns': str(pid), 'ppid': 1, 'name': name, 'argv': [name], 'call_id': f'call_{pid}'}
|
|
76
|
-
|
|
77
|
-
def write(self, name, text):
|
|
78
|
-
(Path(self.folder.name) / name).write_text(text)
|
|
79
|
-
|
|
80
|
-
def opened(self, pid, name='keys.js', flags=engine.O_TRUNC | 1):
|
|
81
|
-
self.engine.opened(self.box, {'kind': 'open', 'pathname': '/workspace/' + name, 'flags': flags, 'errno': 0, 'pid': pid,
|
|
82
|
-
'ppid': 1, 'process_start_ns': str(pid), 'timestamp_ns': str(time.time_ns()), 'sysno': 56})
|
|
83
|
-
|
|
84
|
-
def call(self, pid, sysno, errno=0):
|
|
85
|
-
self.engine.numbered_call(self.box, {'kind': 'syscall_return', 'sysno': sysno, 'errno': errno, 'pid': pid, 'ppid': 1,
|
|
86
|
-
'process_start_ns': str(pid), 'timestamp_ns': str(time.time_ns()), 'cwd': '/workspace'})
|
|
87
|
-
|
|
88
|
-
def changed(self, *words):
|
|
89
|
-
self.engine.noticed(self.box, 'keys.js', list(words))
|
|
90
|
-
self.engine.look(self.box, 'keys.js')
|
|
91
|
-
return [fact for fact in self.engine.facts.values() if fact['kind'] == 'file' and fact['effect'] not in ('read', 'opened_for_writing')]
|
|
92
|
-
|
|
93
|
-
def earlier_writer(self):
|
|
94
|
-
"""Process 10 writes the file, exits, and its change is recorded."""
|
|
95
|
-
self.opened(10)
|
|
96
|
-
self.write('keys.js', 'two')
|
|
97
|
-
self.engine.exited(self.box, {'pid': 10, 'process_start_ns': '10'})
|
|
98
|
-
(fact,) = self.changed('modify', 'close_write')
|
|
99
|
-
self.assertEqual((fact['pid'], fact['certainty']), (10, 'confirmed'))
|
|
100
|
-
self.engine.facts.clear()
|
|
101
|
-
self.now += 60
|
|
102
|
-
|
|
103
|
-
def test_a_writer_that_exited_is_not_credited_with_a_later_change(self):
|
|
104
|
-
self.earlier_writer()
|
|
105
|
-
self.write('keys.js', 'three')
|
|
106
|
-
(fact,) = self.changed('modify', 'close_write')
|
|
107
|
-
self.assertEqual((fact['pid'], fact['certainty']), (None, 'unattributed'))
|
|
108
|
-
|
|
109
|
-
def test_exit_brings_the_look_forward(self):
|
|
110
|
-
self.opened(10)
|
|
111
|
-
self.box.due.clear()
|
|
112
|
-
self.engine.exited(self.box, {'pid': 10, 'process_start_ns': '10'})
|
|
113
|
-
self.assertIn('keys.js', self.box.due)
|
|
114
|
-
|
|
115
|
-
def test_a_file_moved_over_it_is_the_movers(self):
|
|
116
|
-
self.earlier_writer()
|
|
117
|
-
self.opened(10) # another process still has the name open
|
|
118
|
-
self.write('keys.js', 'by sed')
|
|
119
|
-
self.call(20, 38) # renameat
|
|
120
|
-
(fact,) = self.changed('moved_to', 'close_write')
|
|
121
|
-
self.assertEqual((fact['pid'], fact['call_id'], fact['certainty'], fact['basis']['syscall']), (20, 'call_20', 'confirmed', 'renameat'))
|
|
122
|
-
|
|
123
|
-
def test_truncate_is_the_truncaters(self):
|
|
124
|
-
self.earlier_writer()
|
|
125
|
-
self.write('keys.js', '')
|
|
126
|
-
self.call(30, 45) # truncate
|
|
127
|
-
(fact,) = self.changed('modify')
|
|
128
|
-
self.assertEqual((fact['effect'], fact['pid'], fact['certainty']), ('emptied', 30, 'confirmed'))
|
|
129
|
-
|
|
130
|
-
def test_a_write_through_a_symbolic_link_or_a_hard_link(self):
|
|
131
|
-
for make in (lambda: os.symlink('keys.js', Path(self.folder.name) / 'alias'),
|
|
132
|
-
lambda: os.link(Path(self.folder.name) / 'keys.js', Path(self.folder.name) / 'alias')):
|
|
133
|
-
with self.subTest():
|
|
134
|
-
make()
|
|
135
|
-
self.opened(20, 'alias')
|
|
136
|
-
(Path(self.folder.name) / 'alias').write_text('through ' + str(self.now))
|
|
137
|
-
(fact,) = self.changed('modify', 'close_write')
|
|
138
|
-
self.assertEqual((fact['pid'], fact['certainty'], fact['basis']['resolution'], fact['basis']['via']), (20, 'confirmed', 'link', 'alias'))
|
|
139
|
-
self.engine.facts.clear()
|
|
140
|
-
self.box.aliases.clear()
|
|
141
|
-
os.unlink(Path(self.folder.name) / 'alias')
|
|
142
|
-
self.now += 60
|
|
143
|
-
|
|
144
|
-
def test_a_link_out_of_the_project_is_not_followed(self):
|
|
145
|
-
os.symlink('/etc/hostname', Path(self.folder.name) / 'alias')
|
|
146
|
-
self.opened(20, 'alias')
|
|
147
|
-
self.write('keys.js', 'someone')
|
|
148
|
-
(fact,) = self.changed('modify', 'close_write')
|
|
149
|
-
self.assertEqual(fact['certainty'], 'unattributed')
|
|
150
|
-
|
|
151
|
-
def test_a_folder_argument_reaches_its_files_only_for_a_file_tool(self):
|
|
152
|
-
self.box.guarded = {'src/a.js': 'watched', 'src/b.js': 'watched'}
|
|
153
|
-
script = {'name': 'python3', 'argv': ['python3', 'run.py', 'src']}
|
|
154
|
-
self.assertEqual(self.engine.named(self.box, script, '/workspace'), [])
|
|
155
|
-
self.assertEqual(self.engine.named(self.box, {'name': 'rm', 'argv': ['rm', '-r', 'src']}, '/workspace'),
|
|
156
|
-
[('src/a.js', 'argv_folder'), ('src/b.js', 'argv_folder')])
|
|
157
|
-
self.assertEqual(self.engine.named(self.box, {'name': 'python3', 'argv': ['python3', 'run.py', 'src/a.js']}, '/workspace'), [('src/a.js', 'argv')])
|
|
158
|
-
|
|
159
|
-
def test_a_connection_to_its_own_address_is_the_sandbox_talking_to_itself(self):
|
|
160
|
-
event = {'kind': 'connect', 'family': 'inet', 'ip': '172.30.60.3', 'port': 18082, 'errno': 0, 'pid': 10, 'ppid': 1, 'process_start_ns': '10'}
|
|
161
|
-
self.engine.connected(self.box, event)
|
|
162
|
-
self.assertEqual(self.engine.facts, {})
|
|
163
|
-
self.engine.db = sqlite3.connect(':memory:')
|
|
164
|
-
self.engine.db.executescript(engine.SCHEMA)
|
|
165
|
-
self.engine.connected(self.box, {**event, 'ip': '172.30.60.9'}) # another machine in that range
|
|
166
|
-
(fact,) = self.engine.facts.values()
|
|
167
|
-
self.assertEqual(fact['outcomes'], {'refused_by_policy': 1})
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
class FailedExecs(unittest.TestCase):
|
|
171
|
-
def setUp(self):
|
|
172
|
-
self.attempts = collector.ExecAttempts()
|
|
173
|
-
|
|
174
|
-
def test_an_attempt_no_program_follows_failed_when_the_process_exits(self):
|
|
175
|
-
self.assertEqual(self.attempts.see(attempt(7, '/workspace/missing', ['/workspace/missing'])), [])
|
|
176
|
-
self.assertEqual(self.attempts.see({'kind': 'exec_syscall_return', 'pid': 7, 'errno': 0}), [])
|
|
177
|
-
(failed,) = self.attempts.see({'kind': 'process_exit', 'pid': 7, 'exit_code': 255})
|
|
178
|
-
self.assertEqual((failed['kind'], failed['errno'], failed['argv'], failed['tried']),
|
|
179
|
-
('exec_failed', None, ['/workspace/missing'], ['/workspace/missing']))
|
|
180
|
-
self.assertEqual(failed['markers'], {'RAYTRACE_CALL_ID': 'call_1'})
|
|
181
|
-
self.assertIn('inferred', failed)
|
|
182
|
-
self.assertEqual(self.attempts.see({'kind': 'process_exit', 'pid': 7}), []) # gVisor's second exit event
|
|
183
|
-
|
|
184
|
-
def test_a_path_search_that_finds_the_program_is_no_failure(self):
|
|
185
|
-
for folder in ('/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin'):
|
|
186
|
-
self.assertEqual(self.attempts.see(attempt(9, folder + '/python3', ['python3', '-c', 'pass'])), [])
|
|
187
|
-
self.assertEqual(self.attempts.see({'kind': 'exec_succeeded', 'pid': 9}), [])
|
|
188
|
-
self.assertEqual(self.attempts.see({'kind': 'process_exit', 'pid': 9}), [])
|
|
189
|
-
|
|
190
|
-
def test_a_path_search_that_finds_nothing_is_one_failure(self):
|
|
191
|
-
for folder in ('/usr/local/bin', '/usr/bin', '/bin'):
|
|
192
|
-
self.attempts.see(attempt(9, folder + '/nope', ['nope']))
|
|
193
|
-
(failed,) = self.attempts.see({'kind': 'process_exit', 'pid': 9})
|
|
194
|
-
self.assertEqual(failed['tried'], ['/usr/local/bin/nope', '/usr/bin/nope', '/bin/nope'])
|
|
195
|
-
|
|
196
|
-
def test_a_failure_then_another_program_in_the_same_process(self):
|
|
197
|
-
self.attempts.see(attempt(3, '/a', ['/a']))
|
|
198
|
-
(failed,) = self.attempts.see(attempt(3, '/bin/b', ['/bin/b']))
|
|
199
|
-
self.assertEqual(failed['argv'], ['/a'])
|
|
200
|
-
self.assertEqual(self.attempts.see({'kind': 'exec_succeeded', 'pid': 3}), [])
|
|
201
|
-
self.assertEqual(self.attempts.see({'kind': 'process_exit', 'pid': 3}), [])
|
|
202
|
-
|
|
203
|
-
def test_a_failure_gvisor_reported_is_not_said_twice(self):
|
|
204
|
-
self.attempts.see(attempt(4, '/a', ['/a']))
|
|
205
|
-
self.assertEqual(self.attempts.see({'kind': 'exec_failed', 'pid': 4, 'errno': 14}), [])
|
|
206
|
-
self.assertEqual(self.attempts.see({'kind': 'process_exit', 'pid': 4}), [])
|
|
207
|
-
|
|
208
|
-
def test_the_failure_is_json_and_carries_no_raw_packet(self):
|
|
209
|
-
self.attempts.see(attempt(5, '/a', ['/a']))
|
|
210
|
-
(failed,) = self.attempts.see({'kind': 'process_exit', 'pid': 5})
|
|
211
|
-
self.assertNotIn('raw_packet_base64', json.loads(json.dumps(failed)))
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
class EveryProjectFile(unittest.TestCase):
|
|
216
|
-
"""A project kept whole: what happens to any of its files is a fact, not
|
|
217
|
-
only to the files on a list."""
|
|
218
|
-
SID = 'rtp-' + 'a' * 32
|
|
219
|
-
|
|
220
|
-
def setUp(self):
|
|
221
|
-
self.now = 1000.0
|
|
222
|
-
self.engine = engine.Engine(':memory:', arch='aarch64', watch=False, clock=lambda: self.now)
|
|
223
|
-
self.folder = tempfile.TemporaryDirectory()
|
|
224
|
-
self.state = tempfile.TemporaryDirectory()
|
|
225
|
-
self.addCleanup(self.folder.cleanup)
|
|
226
|
-
self.addCleanup(self.state.cleanup)
|
|
227
|
-
for name in ('CONTAINERS', 'GUARDRAILS', 'PROJECT_LIMIT'):
|
|
228
|
-
self.addCleanup(setattr, engine, name, getattr(engine, name))
|
|
229
|
-
engine.CONTAINERS = engine.GUARDRAILS = Path(self.state.name)
|
|
230
|
-
for name, text in (('src/app.js', 'one'), ('config/payment-keys.js', 'key'), ('node_modules/x/index.js', 'x')):
|
|
231
|
-
self.write(name, text)
|
|
232
|
-
(Path(self.state.name) / ('c' * 64 + '.json')).write_text(json.dumps({'id': self.SID, 'workspace': self.folder.name}))
|
|
233
|
-
self.box = engine.Sandbox('c' * 64)
|
|
234
|
-
self.box.processes[10] = {'start_ns': '10', 'ppid': 1, 'name': 'sed', 'argv': ['sed'], 'call_id': 'call_10'}
|
|
235
|
-
|
|
236
|
-
def write(self, name, text):
|
|
237
|
-
path = Path(self.folder.name) / name
|
|
238
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
239
|
-
path.write_text(text)
|
|
240
|
-
|
|
241
|
-
def opened(self, name, flags=engine.O_TRUNC | 1):
|
|
242
|
-
self.engine.opened(self.box, {'kind': 'open', 'pathname': '/workspace/' + name, 'flags': flags, 'errno': 0, 'pid': 10,
|
|
243
|
-
'ppid': 1, 'process_start_ns': '10', 'timestamp_ns': str(time.time_ns()), 'sysno': 56})
|
|
244
|
-
|
|
245
|
-
def looked(self, name, *words, final=False):
|
|
246
|
-
if words:
|
|
247
|
-
self.engine.noticed(self.box, name, list(words))
|
|
248
|
-
self.engine.look(self.box, name, final=final)
|
|
249
|
-
return [(fact['path'], fact['effect'], fact['guard'], fact['call_id']) for fact in self.engine.facts.values() if fact['kind'] == 'file']
|
|
250
|
-
|
|
251
|
-
def test_every_file_outside_the_skipped_folders_is_kept(self):
|
|
252
|
-
self.engine.refresh(self.box)
|
|
253
|
-
self.assertEqual(self.box.guarded, {'src/app.js': 'project', 'config/payment-keys.js': 'sensitive'})
|
|
254
|
-
|
|
255
|
-
def test_the_lists_can_turn_it_off_and_a_huge_project_is_not_kept_whole(self):
|
|
256
|
-
(Path(self.state.name) / (self.SID + '.json')).write_text(json.dumps({'restricted': [], 'watched': ['src/app.js'], 'project': False}))
|
|
257
|
-
self.engine.refresh(self.box)
|
|
258
|
-
self.assertEqual(self.box.guarded, {'src/app.js': 'watched'})
|
|
259
|
-
engine.PROJECT_LIMIT = 1
|
|
260
|
-
other = engine.Sandbox('c' * 64)
|
|
261
|
-
(Path(self.state.name) / (self.SID + '.json')).unlink()
|
|
262
|
-
self.engine.refresh(other)
|
|
263
|
-
self.assertEqual(other.guarded, {})
|
|
264
|
-
|
|
265
|
-
def test_an_edit_is_a_fact_and_a_read_is_counted(self):
|
|
266
|
-
self.engine.refresh(self.box)
|
|
267
|
-
self.opened('src/app.js', flags=0)
|
|
268
|
-
self.assertEqual(self.looked('src/app.js'), [])
|
|
269
|
-
self.opened('src/app.js')
|
|
270
|
-
self.write('src/app.js', 'two')
|
|
271
|
-
self.assertEqual(self.looked('src/app.js', 'modify', 'close_write'), [('src/app.js', 'changed', 'project', 'call_10')])
|
|
272
|
-
# A sensitive file that was there from the start: its read is a fact too.
|
|
273
|
-
self.opened('config/payment-keys.js', flags=0)
|
|
274
|
-
self.assertIn(('config/payment-keys.js', 'read', 'sensitive', 'call_10'), self.looked('config/payment-keys.js'))
|
|
275
|
-
|
|
276
|
-
def test_a_new_file_is_created_and_a_deleted_one_is_let_go(self):
|
|
277
|
-
self.engine.refresh(self.box)
|
|
278
|
-
self.opened('src/new.js', flags=engine.O_CREAT | 1)
|
|
279
|
-
self.write('src/new.js', 'new')
|
|
280
|
-
self.assertEqual(self.looked('src/new.js', 'create', 'close_write'), [('src/new.js', 'created', 'project', 'call_10')])
|
|
281
|
-
(Path(self.folder.name) / 'src/new.js').unlink()
|
|
282
|
-
self.assertIn(('src/new.js', 'deleted', 'project', None), self.looked('src/new.js', 'delete'))
|
|
283
|
-
self.assertNotIn('src/new.js', self.box.guarded)
|
|
284
|
-
self.assertNotIn('src/new.js', self.box.known)
|
|
285
|
-
|
|
286
|
-
def test_the_temporary_file_of_a_save_leaves_no_line(self):
|
|
287
|
-
self.engine.refresh(self.box)
|
|
288
|
-
self.opened('src/sedAbC', flags=engine.O_CREAT | 1)
|
|
289
|
-
self.write('src/sedAbC', 'two')
|
|
290
|
-
os.rename(Path(self.folder.name) / 'src/sedAbC', Path(self.folder.name) / 'src/app.js')
|
|
291
|
-
self.engine.numbered_call(self.box, {'kind': 'syscall_return', 'sysno': 38, 'errno': 0, 'pid': 10, 'ppid': 1,
|
|
292
|
-
'process_start_ns': '10', 'timestamp_ns': str(time.time_ns()), 'cwd': '/workspace'})
|
|
293
|
-
self.assertEqual(self.looked('src/app.js', 'moved_to'), [('src/app.js', 'changed', 'project', 'call_10')])
|
|
294
|
-
self.assertEqual(len(self.looked('src/sedAbC', 'moved_from', final=True)), 1)
|
|
295
|
-
self.assertNotIn('src/sedAbC', self.box.guarded)
|
|
296
|
-
|
|
297
|
-
def test_a_file_or_a_folder_moved_in_is_kept(self):
|
|
298
|
-
self.engine.refresh(self.box)
|
|
299
|
-
self.write('src/moved.js', 'm')
|
|
300
|
-
self.engine.appeared(self.box, 'src/moved.js', False)
|
|
301
|
-
self.assertEqual(self.looked('src/moved.js', 'moved_to'), [('src/moved.js', 'created', 'project', None)])
|
|
302
|
-
self.write('lib/deep/a.js', 'a')
|
|
303
|
-
self.write('lib/node_modules/b.js', 'b')
|
|
304
|
-
self.engine.appeared(self.box, 'lib', True)
|
|
305
|
-
self.assertEqual({path for path in self.box.guarded if path.startswith('lib/')}, {'lib/deep/a.js'})
|
|
306
|
-
# A kept file under a new name is the same file: no second line for it.
|
|
307
|
-
os.rename(Path(self.folder.name) / 'src/app.js', Path(self.folder.name) / 'src/main.js')
|
|
308
|
-
self.engine.appeared(self.box, 'src/main.js', False, 'src/app.js')
|
|
309
|
-
self.assertEqual([fact for fact in self.looked('src/main.js', 'moved_to') if fact[0] == 'src/main.js'], [])
|
|
310
|
-
|
|
311
|
-
RENAME = {'kind': 'syscall_return', 'sysno': 38, 'errno': 0, 'ppid': 1, 'cwd': '/workspace'}
|
|
312
|
-
|
|
313
|
-
def test_a_new_file_saved_under_a_temporary_name_is_created_by_the_mover(self):
|
|
314
|
-
"""How an agent's Write tool saves: it writes a temporary file and
|
|
315
|
-
renames it, while the snapshot hook's git renames its own index."""
|
|
316
|
-
self.engine.refresh(self.box)
|
|
317
|
-
self.engine.executed(self.box, {'pid': 30, 'ppid': 1, 'process_start_ns': '30', 'executable': '/usr/local/bin/node',
|
|
318
|
-
'argv': ['node', engine.OWN_HOOK]})
|
|
319
|
-
self.engine.executed(self.box, {'pid': 31, 'ppid': 30, 'process_start_ns': '31', 'executable': '/usr/bin/git', 'argv': ['git', 'add', '-A']})
|
|
320
|
-
self.opened('src/new.js.tmp.1', flags=engine.O_CREAT | 1)
|
|
321
|
-
self.write('src/new.js.tmp.1', 'new')
|
|
322
|
-
os.rename(Path(self.folder.name) / 'src/new.js.tmp.1', Path(self.folder.name) / 'src/new.js')
|
|
323
|
-
for pid in (31, 10):
|
|
324
|
-
self.engine.numbered_call(self.box, {**self.RENAME, 'pid': pid, 'process_start_ns': str(pid), 'timestamp_ns': str(time.time_ns())})
|
|
325
|
-
self.engine.appeared(self.box, 'src/new.js', False, 'src/new.js.tmp.1')
|
|
326
|
-
self.looked('src/new.js', 'moved_to')
|
|
327
|
-
(fact,) = self.engine.facts.values()
|
|
328
|
-
self.assertEqual((fact['path'], fact['effect'], fact['certainty'], fact['pid']), ('src/new.js', 'created', 'confirmed', 10))
|
|
329
|
-
|
|
330
|
-
def test_the_snapshot_hook_reading_every_file_is_counted_not_listed(self):
|
|
331
|
-
self.engine.refresh(self.box)
|
|
332
|
-
self.engine.executed(self.box, {'pid': 30, 'ppid': 1, 'process_start_ns': '30', 'executable': '/usr/local/bin/node',
|
|
333
|
-
'argv': ['node', engine.OWN_HOOK]})
|
|
334
|
-
self.engine.executed(self.box, {'pid': 31, 'ppid': 30, 'process_start_ns': '31', 'executable': '/usr/bin/git', 'argv': ['git', 'add', '-A']})
|
|
335
|
-
read = {'kind': 'open', 'pathname': '/workspace/config/payment-keys.js', 'flags': 0, 'errno': 0, 'ppid': 30,
|
|
336
|
-
'timestamp_ns': str(time.time_ns()), 'sysno': 56}
|
|
337
|
-
self.engine.opened(self.box, {**read, 'pid': 31, 'process_start_ns': '31'})
|
|
338
|
-
self.assertEqual([fact for fact in self.engine.facts.values() if fact['kind'] == 'file'], [])
|
|
339
|
-
# Anything else named like the hook is not it, and its read is a fact.
|
|
340
|
-
self.engine.executed(self.box, {'pid': 40, 'ppid': 1, 'process_start_ns': '40', 'executable': '/usr/local/bin/node',
|
|
341
|
-
'argv': ['node', '/tmp/hook.mjs']})
|
|
342
|
-
self.engine.opened(self.box, {**read, 'pid': 40, 'ppid': 1, 'process_start_ns': '40'})
|
|
343
|
-
self.assertEqual([fact['pid'] for fact in self.engine.facts.values() if fact['kind'] == 'file'], [40])
|
|
344
|
-
|
|
345
|
-
def test_the_shell_of_a_tool_call_carries_the_call_in_its_own_text(self):
|
|
346
|
-
"""Claude Code runs `bash -c "... eval '<command>'"`: a redirect is the shell's own write."""
|
|
347
|
-
self.engine.refresh(self.box)
|
|
348
|
-
self.engine.executed(self.box, {'pid': 50, 'ppid': 1, 'process_start_ns': '50', 'executable': '/bin/bash', 'argv': [
|
|
349
|
-
'/bin/bash', '-c', "source /home/node/snap.sh || true && eval 'export RAYTRACE_CALL_ID=toolu_01ABC\necho hi > src/app.js' < /dev/null"]})
|
|
350
|
-
self.assertEqual(self.box.processes[50]['call_id'], 'toolu_01ABC')
|
|
351
|
-
def hook(self, pid, phase, call_id, path, at_ns):
|
|
352
|
-
"""The hook, started by the agent (pid 10), announcing a file tool's call."""
|
|
353
|
-
self.engine.executed(self.box, {'pid': pid, 'ppid': 10, 'process_start_ns': str(pid), 'executable': '/usr/local/bin/node',
|
|
354
|
-
'argv': ['node', engine.OWN_HOOK]})
|
|
355
|
-
self.engine.executed(self.box, {'pid': pid + 1, 'ppid': pid, 'process_start_ns': str(pid + 1), 'executable': '/usr/bin/true',
|
|
356
|
-
'argv': ['true', engine.TOOL_MARK, phase, 'Write', path], 'timestamp_ns': str(at_ns),
|
|
357
|
-
'markers': {'RAYTRACE_CALL_ID': call_id}})
|
|
358
|
-
|
|
359
|
-
def test_the_agents_own_file_tool_is_joined_to_the_call_the_hook_announced(self):
|
|
360
|
-
self.engine.refresh(self.box)
|
|
361
|
-
self.box.processes[10] = {'start_ns': '10', 'ppid': 1, 'name': 'claude.exe', 'argv': ['claude'], 'call_id': None}
|
|
362
|
-
read = {'kind': 'open', 'pathname': '/workspace/config/payment-keys.js', 'flags': 0, 'errno': 0, 'pid': 10, 'ppid': 1,
|
|
363
|
-
'process_start_ns': '10', 'sysno': 56}
|
|
364
|
-
now = time.time_ns()
|
|
365
|
-
# Before any call is announced, the agent's read names no call.
|
|
366
|
-
self.engine.opened(self.box, {**read, 'timestamp_ns': str(now - 60_000_000_000)})
|
|
367
|
-
self.hook(60, 'begin', 'toolu_read', 'config/payment-keys.js', now)
|
|
368
|
-
self.engine.opened(self.box, {**read, 'timestamp_ns': str(now + 1_000_000)})
|
|
369
|
-
self.hook(62, 'end', 'toolu_read', 'config/payment-keys.js', now + 2_000_000)
|
|
370
|
-
# Long after the call ended, it is not that call's read.
|
|
371
|
-
self.engine.opened(self.box, {**read, 'timestamp_ns': str(now + 30_000_000_000)})
|
|
372
|
-
self.assertEqual({(fact['call_id'], fact.get('call_from'), fact['count']) for fact in self.engine.facts.values()},
|
|
373
|
-
{(None, None, 2), ('toolu_read', 'tool_report', 1)})
|
|
374
|
-
self.engine.facts.clear()
|
|
375
|
-
# A file the Write tool saves: written under another name and moved here.
|
|
376
|
-
self.hook(64, 'begin', 'toolu_write', 'src/app.js', time.time_ns())
|
|
377
|
-
self.write('src/app.js', 'saved by the tool')
|
|
378
|
-
self.engine.numbered_call(self.box, {**self.RENAME, 'pid': 10, 'process_start_ns': '10', 'timestamp_ns': str(time.time_ns())})
|
|
379
|
-
self.looked('src/app.js', 'moved_to')
|
|
380
|
-
(fact,) = self.engine.facts.values()
|
|
381
|
-
self.assertEqual((fact['effect'], fact['certainty'], fact['pid'], fact['call_id']), ('changed', 'confirmed', 10, 'toolu_write'))
|
|
382
|
-
|
|
383
|
-
def test_only_the_hook_can_announce_a_call(self):
|
|
384
|
-
self.engine.refresh(self.box)
|
|
385
|
-
self.box.processes[10] = {'start_ns': '10', 'ppid': 1, 'name': 'claude.exe', 'argv': ['claude'], 'call_id': None}
|
|
386
|
-
self.engine.executed(self.box, {'pid': 70, 'ppid': 10, 'process_start_ns': '70', 'executable': '/usr/bin/true',
|
|
387
|
-
'argv': ['true', engine.TOOL_MARK, 'begin', 'Read', 'config/payment-keys.js'],
|
|
388
|
-
'timestamp_ns': str(time.time_ns()), 'markers': {'RAYTRACE_CALL_ID': 'made_up'}})
|
|
389
|
-
self.assertEqual(len(self.box.tools), 0)
|
|
390
|
-
def test_a_file_that_came_and_went_between_two_looks_is_a_line(self):
|
|
391
|
-
self.engine.refresh(self.box)
|
|
392
|
-
self.opened('src/scratch.txt', flags=engine.O_CREAT | 1)
|
|
393
|
-
self.assertEqual(self.looked('src/scratch.txt', 'create', 'modify', 'close_write', 'delete'),
|
|
394
|
-
[('src/scratch.txt', 'created_and_deleted', 'project', 'call_10')])
|
|
395
|
-
self.assertNotIn('src/scratch.txt', self.box.guarded)
|
|
396
|
-
self.assertEqual(len(self.looked('src/scratch.txt', final=True)), 1)
|
|
397
|
-
|
|
398
|
-
def test_a_file_written_with_the_same_bytes_is_said_to_be_written(self):
|
|
399
|
-
self.engine.refresh(self.box)
|
|
400
|
-
self.opened('src/app.js')
|
|
401
|
-
self.write('src/app.js', 'one') # what it already held
|
|
402
|
-
self.assertEqual(self.looked('src/app.js', 'modify', 'close_write'), [('src/app.js', 'rewritten', 'project', 'call_10')])
|
|
403
|
-
# Opened for writing and never written: still only "possible", at the last look.
|
|
404
|
-
self.engine.facts.clear()
|
|
405
|
-
self.opened('config/payment-keys.js', flags=1)
|
|
406
|
-
self.assertEqual(self.looked('config/payment-keys.js'), [])
|
|
407
|
-
self.assertEqual(self.looked('config/payment-keys.js', final=True), [('config/payment-keys.js', 'opened_for_writing', 'sensitive', 'call_10')])
|
|
408
|
-
|
|
409
|
-
def test_two_deletes_nobody_can_be_named_for_are_two_lines_when_minutes_apart(self):
|
|
410
|
-
self.engine.refresh(self.box)
|
|
411
|
-
body = {'path': 'src/app.js', 'effect': 'deleted', 'certainty': 'unattributed', 'basis': {}}
|
|
412
|
-
key = ('src/app.js', 'deleted', 'unattributed')
|
|
413
|
-
now = time.time_ns() // 60_000_000_000 * 60_000_000_000 + 1_000_000_000 # a second into a minute
|
|
414
|
-
self.engine.fact(self.box, 'file', key, {}, dict(body), now)
|
|
415
|
-
self.engine.fact(self.box, 'file', key, {}, dict(body), now + 1_000_000)
|
|
416
|
-
self.engine.fact(self.box, 'file', key, {}, dict(body), now + 600_000_000_000)
|
|
417
|
-
self.assertEqual(sorted(fact['count'] for fact in self.engine.facts.values()), [1, 2])
|
|
418
|
-
|
|
419
|
-
def test_a_write_through_a_link_is_still_the_writers(self):
|
|
420
|
-
os.symlink('app.js', Path(self.folder.name) / 'src/alias.js')
|
|
421
|
-
self.engine.refresh(self.box)
|
|
422
|
-
self.assertNotIn('src/alias.js', self.box.guarded)
|
|
423
|
-
self.opened('src/alias.js', flags=engine.O_CREAT | engine.O_TRUNC | 1)
|
|
424
|
-
self.assertNotIn('src/alias.js', self.box.guarded)
|
|
425
|
-
self.write('src/app.js', 'through the link')
|
|
426
|
-
self.looked('src/app.js', 'modify', 'close_write')
|
|
427
|
-
(fact,) = self.engine.facts.values()
|
|
428
|
-
self.assertEqual((fact['call_id'], fact['certainty'], fact['basis']['via']), ('call_10', 'confirmed', 'src/alias.js'))
|
|
429
|
-
self.engine.facts.clear()
|
|
430
|
-
os.link(Path(self.folder.name) / 'src/app.js', Path(self.folder.name) / 'src/hard.js')
|
|
431
|
-
self.opened('src/hard.js', flags=engine.O_CREAT | 1)
|
|
432
|
-
self.write('src/hard.js', 'through the hard link')
|
|
433
|
-
self.looked('src/app.js', 'modify', 'close_write')
|
|
434
|
-
(fact,) = [fact for fact in self.engine.facts.values() if fact['path'] == 'src/app.js']
|
|
435
|
-
self.assertEqual((fact['call_id'], fact['basis']['via']), ('call_10', 'src/hard.js'))
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
if __name__ == '__main__':
|
|
439
|
-
unittest.main()
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"""Which process asked a lookup: engine.py notes each connection to the
|
|
2
|
-
sandbox network's resolver, and dns_forwarder.py names the process whose
|
|
3
|
-
connection is closest in time to the lookup. Run: python3 -B -m unittest discover -s worker/sandbox -p 'test_*.py'"""
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
import sqlite3
|
|
6
|
-
import sys
|
|
7
|
-
import unittest
|
|
8
|
-
|
|
9
|
-
sys.path.insert(0, str(Path(__file__).parent))
|
|
10
|
-
import dns_forwarder # noqa: E402
|
|
11
|
-
import engine # noqa: E402
|
|
12
|
-
import network # noqa: E402
|
|
13
|
-
|
|
14
|
-
CONTAINER = 'c' * 64
|
|
15
|
-
AT = 1_800_000_000_000_000_000
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class EngineNotesResolverConnections(unittest.TestCase):
|
|
19
|
-
def setUp(self):
|
|
20
|
-
self.engine = engine.Engine(':memory:', arch='aarch64', watch=False)
|
|
21
|
-
self.engine.db = sqlite3.connect(':memory:')
|
|
22
|
-
self.engine.db.executescript(engine.SCHEMA)
|
|
23
|
-
self.box = engine.Sandbox(CONTAINER)
|
|
24
|
-
self.box.ip = '172.30.60.3'
|
|
25
|
-
self.box.processes[10] = {'start_ns': '10', 'ppid': 1, 'name': 'curl', 'argv': ['curl', 'https://example.com'], 'call_id': 'call_1'}
|
|
26
|
-
|
|
27
|
-
def connect(self, ip, port, errno=0):
|
|
28
|
-
self.engine.connected(self.box, {'kind': 'connect', 'family': 'inet', 'ip': ip, 'port': port, 'errno': errno,
|
|
29
|
-
'pid': 10, 'ppid': 1, 'process_start_ns': '10', 'timestamp_ns': str(AT)})
|
|
30
|
-
|
|
31
|
-
def test_a_connection_to_the_resolver_is_noted_with_its_process_and_call_and_is_no_network_fact(self):
|
|
32
|
-
self.connect(network.GATEWAY, 53)
|
|
33
|
-
self.engine.flush()
|
|
34
|
-
rows = self.engine.db.execute('SELECT container_id, at_ns, pid, process_start_ns, process, command, call_id FROM resolver_calls').fetchall()
|
|
35
|
-
self.assertEqual(rows, [(CONTAINER, AT, 10, '10', 'curl', 'curl https://example.com', 'call_1')])
|
|
36
|
-
self.assertEqual([fact for fact in self.engine.facts.values() if fact['kind'] == 'network'], [])
|
|
37
|
-
|
|
38
|
-
def test_a_refused_connection_to_the_resolver_and_other_connections_are_not_noted(self):
|
|
39
|
-
self.connect(network.GATEWAY, 53, errno=111)
|
|
40
|
-
self.connect('93.184.216.34', 443)
|
|
41
|
-
self.engine.flush()
|
|
42
|
-
self.assertEqual(self.engine.db.execute('SELECT count(*) FROM resolver_calls').fetchone()[0], 0)
|
|
43
|
-
self.assertEqual(len([fact for fact in self.engine.facts.values() if fact['kind'] == 'network']), 1)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
class ForwarderNamesTheAsker(unittest.TestCase):
|
|
47
|
-
def setUp(self):
|
|
48
|
-
self.db = sqlite3.connect(':memory:')
|
|
49
|
-
self.db.executescript(engine.SCHEMA)
|
|
50
|
-
dns_forwarder.facts.clear()
|
|
51
|
-
dns_forwarder.dirty.clear()
|
|
52
|
-
self.addCleanup(dns_forwarder.facts.clear)
|
|
53
|
-
self.addCleanup(dns_forwarder.dirty.clear)
|
|
54
|
-
|
|
55
|
-
def connected(self, pid, at_ns, call_id=None, container=CONTAINER):
|
|
56
|
-
self.db.execute('INSERT INTO resolver_calls VALUES (?,?,?,?,?,?,?)', (container, at_ns, pid, str(pid), f'proc{pid}', f'proc{pid} x', call_id))
|
|
57
|
-
|
|
58
|
-
def looked_up(self, name, at_ns=AT):
|
|
59
|
-
lookup = {'container': (CONTAINER, False), 'name': name, 'kind': 1, 'code': 3, 'addresses': [], 'at_ns': at_ns}
|
|
60
|
-
dns_forwarder.settle(lookup, dns_forwarder.asker(self.db, CONTAINER, at_ns))
|
|
61
|
-
return [fact for fact in dns_forwarder.facts.values() if fact['name'] == name]
|
|
62
|
-
|
|
63
|
-
def test_the_process_that_connected_just_before_is_named(self):
|
|
64
|
-
self.connected(10, AT - 2_000_000, 'call_1')
|
|
65
|
-
[fact] = self.looked_up('a.invalid')
|
|
66
|
-
self.assertEqual((fact['pid'], fact['call_id'], fact['process'], fact['certainty']), (10, 'call_1', 'proc10', 'likely'))
|
|
67
|
-
self.assertEqual(fact['basis']['gap_ms'], 2.0)
|
|
68
|
-
self.assertEqual(fact['failed'], 'NXDOMAIN')
|
|
69
|
-
|
|
70
|
-
def test_with_two_processes_at_it_nobody_is_named_and_both_are_listed(self):
|
|
71
|
-
# Measured: two processes asking within a millisecond are told apart by time only ~70% of the time.
|
|
72
|
-
self.connected(10, AT - 30_000_000, 'call_1')
|
|
73
|
-
self.connected(20, AT - 1_000_000, 'call_2')
|
|
74
|
-
[fact] = self.looked_up('b.invalid')
|
|
75
|
-
self.assertEqual((fact['pid'], fact['call_id']), (None, None))
|
|
76
|
-
self.assertNotIn('certainty', fact)
|
|
77
|
-
self.assertEqual(sorted(other['call_id'] for other in fact['basis']['candidates']), ['call_1', 'call_2'])
|
|
78
|
-
self.connected(30, AT + 9_000_000, 'call_3')
|
|
79
|
-
self.connected(40, AT + 9_500_000, 'call_4')
|
|
80
|
-
[fact] = self.looked_up('b.invalid', AT + 10_000_000)
|
|
81
|
-
self.assertEqual(fact['count'], 2)
|
|
82
|
-
self.assertEqual(len(fact['basis']['candidates']), 4)
|
|
83
|
-
|
|
84
|
-
def test_nobody_in_the_window_or_another_sandbox_leaves_it_unnamed_as_before(self):
|
|
85
|
-
self.connected(10, AT - dns_forwarder.ASKED_BEFORE - 1, 'call_1')
|
|
86
|
-
self.connected(20, AT - 1_000_000, 'call_2', container='d' * 64)
|
|
87
|
-
[fact] = self.looked_up('c.invalid')
|
|
88
|
-
self.assertEqual((fact['pid'], fact['call_id']), (None, None))
|
|
89
|
-
self.assertNotIn('certainty', fact)
|
|
90
|
-
|
|
91
|
-
def test_one_fact_per_name_and_step_with_a_count(self):
|
|
92
|
-
self.connected(10, AT - 1_000_000, 'call_1')
|
|
93
|
-
self.looked_up('d.invalid')
|
|
94
|
-
[fact] = self.looked_up('d.invalid', AT + 1_000)
|
|
95
|
-
self.assertEqual(fact['count'], 2)
|
|
96
|
-
self.connected(20, AT + 100_000_000, 'call_2')
|
|
97
|
-
facts = self.looked_up('d.invalid', AT + 101_000_000)
|
|
98
|
-
self.assertEqual(sorted(fact['call_id'] for fact in facts), ['call_1', 'call_2'])
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if __name__ == '__main__':
|
|
102
|
-
unittest.main()
|