@raytrace-cli/cli 0.12.0 → 0.14.1
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/README.md +240 -498
- package/THIRD_PARTY_NOTICES.md +51 -0
- package/dist/dashboard/assets/index-CM-3AX0c.css +1 -0
- package/dist/dashboard/assets/index-DF_klLF4.js +38 -0
- package/dist/dashboard/favicon.svg +4 -6
- package/dist/dashboard/index.html +3 -3
- package/package.json +8 -2
- package/proxy/evidence-store.mjs +7 -2
- package/proxy/evidence-store.test.mjs +11 -0
- package/proxy/exec-narrative.mjs +21 -2
- package/proxy/exec-narrative.test.mjs +23 -0
- package/proxy/gvisor-forwarder.mjs +3 -1
- package/proxy/gvisor-forwarder.test.mjs +2 -0
- package/src/lima.mjs +5 -2
- package/worker/sandbox/SYSCALL_CATALOG.md +2 -2
- package/worker/sandbox/claude-hook.mjs +3 -4
- package/worker/sandbox/collector.py +99 -0
- package/worker/sandbox/engine.py +84 -11
- package/worker/sandbox/setup.py +9 -0
- package/worker/sandbox/test_capture.py +215 -0
- package/dist/dashboard/assets/index-DepcI8Fi.js +0 -38
- package/dist/dashboard/assets/index-H97VLD5l.css +0 -1
|
@@ -0,0 +1,215 @@
|
|
|
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
|
+
if __name__ == '__main__':
|
|
215
|
+
unittest.main()
|