Repository navigation
Expand file tree
/
Copy pathtest_output_durability.py
More file actions
227 lines (209 loc) · 12.6 KB
/
Copy pathtest_output_durability.py
File metadata and controls
227 lines (209 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import csv
import io
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import threading
import unittest
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from contextlib import redirect_stderr, redirect_stdout
from unittest.mock import patch
import wordpress_check as m
class DurableOutputTests(unittest.TestCase):
def call(self, args):
with redirect_stderr(io.StringIO()), redirect_stdout(io.StringIO()):
return m.main(args)
def test_generated_nonstandard_status_checkpoint_recovers_without_requests(self):
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(404 if self.path == '/robots.txt' else 999)
self.send_header('Content-Length', '0')
self.end_headers()
def log_message(self, *args):
pass
server = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
original_check = m.Checker.check
calls = []
def check_then_interrupt(checker, url):
calls.append(url)
if len(calls) == 2:
raise KeyboardInterrupt
return original_check(checker, url)
try:
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'interrupted.json'
origin = 'http://127.0.0.1:' + str(server.server_port)
with patch.object(m.Checker, 'check', check_then_interrupt):
self.assertEqual(self.call([origin + '/999', origin + '/pending', '--delay', '0.001',
'--output', str(target), '--format', 'json']), 130)
checkpoint = Path(str(target) + '.partial.json')
report = m.load_checkpoint(checkpoint)
self.assertEqual(report['results'][0]['http_status'], 999)
self.assertEqual((report['completed_targets'], report['pending_targets']), (1, 1))
for fmt in ('json', 'csv', 'html'):
output = Path(directory) / ('recovered.' + fmt)
with patch.object(m.Checker, 'check', side_effect=AssertionError('network')):
self.assertEqual(self.call(['--recover', str(checkpoint), '--output', str(output),
'--format', fmt]), 130)
text = output.read_text(encoding='utf-8')
self.assertIn('999', text)
if fmt == 'json':
recovered = json.loads(text)
self.assertFalse(recovered['complete'])
self.assertEqual(recovered['results'][0]['php2shell_exploitability'], 'not_tested')
finally:
server.shutdown()
server.server_close()
thread.join(timeout=5)
def test_checkpoint_status_domain_and_unsafe_types(self):
with tempfile.TemporaryDirectory() as directory:
checkpoint = Path(directory) / 'boundary.json'
for status in (None, 100, 599, 600, 999):
with self.subTest(accepted=status):
report = m.run_report([m.asdict(m.Result(input_url='one.example', http_status=status))], 2)
checkpoint.write_text(m.checkpoint_json(report), encoding='utf-8')
self.assertEqual(m.load_checkpoint(checkpoint)['results'][0]['http_status'], status)
for status in (99, 1000, -1, True, 999.0, '999', [], {}):
with self.subTest(rejected=status):
report = m.run_report([m.asdict(m.Result(input_url='one.example', http_status=status))], 2)
checkpoint.write_text(m.checkpoint_json(report), encoding='utf-8')
with self.assertRaisesRegex(ValueError, 'Invalid checkpoint result value'):
m.load_checkpoint(checkpoint)
report = m.run_report([m.asdict(m.Result(input_url='one.example', http_status=999,
php2shell_exploitability='confirmed'))], 2)
checkpoint.write_text(m.checkpoint_json(report), encoding='utf-8')
with self.assertRaisesRegex(ValueError, 'cannot attest exploitability'):
m.load_checkpoint(checkpoint)
def test_existing_output_and_failed_force_remain_unchanged(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'report.json'
for fmt in ('json', 'csv', 'html'):
target.write_text('PREVIOUS REPORT', encoding='utf-8')
with patch.object(m.Checker, 'check') as check, self.assertRaises(SystemExit):
self.call(['one.example', '--output', str(target), '--format', fmt])
check.assert_not_called()
self.assertEqual(target.read_text(), 'PREVIOUS REPORT')
with patch.object(m.Checker, 'check', side_effect=KeyboardInterrupt):
self.assertEqual(self.call(['one.example', '--output', str(target), '--force']), 130)
self.assertEqual(target.read_text(), 'PREVIOUS REPORT')
journal = json.loads(Path(str(target) + '.partial.json').read_text())
self.assertFalse(journal['complete'])
self.assertEqual(journal['completed_targets'], 0)
self.assertEqual(journal['pending_targets'], 1)
def test_interrupted_completed_rows_recover_all_formats_without_requests(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'initial.json'
result = m.Result(input_url='https://one.example', title='=SUM(1)', http_status=200)
with patch.object(m.Checker, 'check', side_effect=[result, KeyboardInterrupt]):
self.assertEqual(self.call(['one.example', 'two.example', '--output', str(target)]), 130)
self.assertFalse(target.exists())
journal = Path(str(target) + '.partial.json')
report = m.load_checkpoint(journal)
self.assertEqual((report['completed_targets'], report['pending_targets']), (1, 1))
for fmt in ('json', 'csv', 'html'):
recovered = Path(directory) / ('recovered.' + fmt)
with patch.object(m.Checker, 'check', side_effect=AssertionError('network')):
self.assertEqual(self.call(['--recover', str(journal), '--format', fmt, '--output', str(recovered)]), 130)
text = recovered.read_text(encoding='utf-8')
if fmt == 'json':
self.assertFalse(json.loads(text)['complete'])
elif fmt == 'csv':
row = next(csv.DictReader(io.StringIO(text)))
self.assertEqual(row['title'], "'=SUM(1)")
self.assertEqual(row['pending_targets'], '1')
else:
self.assertIn('incomplete / recovered', text)
self.assertIn('https://one.example', text)
self.assertTrue(journal.exists())
def test_forced_process_exit_preserves_checkpoint_for_html(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'killed.html'
script = """import os,sys
import wordpress_check as m
calls=0
def check(self,url):
global calls
calls+=1
if calls==2: os._exit(73)
return m.Result(input_url=url,title='<private>')
m.Checker.check=check
m.main(['https://one.example','https://two.example','--output',sys.argv[1],'--format','html'])
"""
run = subprocess.run([sys.executable, '-c', script, str(target)], capture_output=True, timeout=20)
self.assertEqual(run.returncode, 73)
self.assertFalse(target.exists())
recovered = Path(directory) / 'recovered.html'
self.assertEqual(self.call(['--recover', str(target) + '.partial.json', '--format', 'html', '--output', str(recovered)]), 130)
self.assertIn('one.example', recovered.read_text(encoding='utf-8'))
self.assertNotIn('<private>', recovered.read_text(encoding='utf-8'))
def test_atomic_publish_race_and_render_failure_preserve_output(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'out.json'
original_link = os.link
def race(source, destination):
if Path(destination) == target:
target.write_text('RACING REPORT', encoding='utf-8')
return original_link(source, destination)
with patch.object(m.Checker, 'check', return_value=m.Result(input_url='one.example')), patch.object(m.os, 'link', side_effect=race):
self.assertEqual(self.call(['one.example', '--output', str(target)]), 2)
self.assertEqual(target.read_text(), 'RACING REPORT')
self.assertEqual(m.load_checkpoint(Path(str(target) + '.partial.json'))['completed_targets'], 1)
target2 = Path(directory) / 'render.html'
target2.write_text('OLDER', encoding='utf-8')
with patch.object(m.Checker, 'check', return_value=m.Result(input_url='one.example')), patch.object(m, 'render_html', side_effect=ValueError('render failed')):
self.assertEqual(self.call(['one.example', '--output', str(target2), '--force', '--format', 'html']), 2)
self.assertEqual(target2.read_text(), 'OLDER')
def test_success_and_explicit_force_with_conservative_retained_journal(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'out.json'
target.write_text('OLD', encoding='utf-8')
with patch.object(m.Checker, 'check', return_value=m.Result(input_url='one.example')):
self.assertEqual(self.call(['one.example', '--output', str(target), '--format', 'json', '--force']), 0)
report = json.loads(target.read_text())
self.assertTrue(report['complete'])
self.assertEqual(report['pending_targets'], 0)
# Existing journal is never silently overwritten, including --force.
with patch.object(m.Checker, 'check') as check:
self.assertEqual(self.call(['one.example', '--output', str(target), '--force']), 2)
check.assert_not_called()
self.assertEqual(json.loads(target.read_text()), report)
@unittest.skipUnless(hasattr(os, 'mkfifo') and os.name == 'posix', 'POSIX FIFO fixture')
def test_fifo_recovery_rejected_without_blocking(self):
with tempfile.TemporaryDirectory() as directory:
fifo = Path(directory) / 'checkpoint.fifo'
os.mkfifo(fifo, 0o600)
run = subprocess.run([sys.executable, 'wordpress_check.py', '--recover', str(fifo)],
capture_output=True, timeout=10)
self.assertEqual(run.returncode, 2)
self.assertIn(b'regular checkpoint', run.stderr)
def test_directory_recovery_rejected(self):
with tempfile.TemporaryDirectory() as directory:
with patch.object(m.Checker, 'check') as check:
self.assertEqual(self.call(['--recover', directory]), 2)
check.assert_not_called()
def test_checkpoint_limit_preserves_previous_committed_state(self):
with tempfile.TemporaryDirectory() as directory:
target = Path(directory) / 'limited.json'
initial_size = len(m.checkpoint_json(m.run_report([], 1)).encode('utf-8'))
with patch.object(m, 'MAX_CHECKPOINT_BYTES', initial_size + 1), patch.object(m.Checker, 'check', return_value=m.Result(input_url='one.example')):
self.assertEqual(self.call(['one.example', '--output', str(target)]), 2)
self.assertFalse(target.exists())
report = m.load_checkpoint(Path(str(target) + '.partial.json'))
self.assertEqual((report['completed_targets'], report['pending_targets']), (0, 1))
def test_empty_incomplete_stdout_and_invalid_recovery(self):
with patch.object(m.Checker, 'check', side_effect=KeyboardInterrupt), redirect_stdout(io.StringIO()) as stdout, redirect_stderr(io.StringIO()):
self.assertEqual(m.main(['one.example', '--format', 'json']), 130)
self.assertEqual(json.loads(stdout.getvalue())['pending_targets'], 1)
with tempfile.TemporaryDirectory() as directory:
journal = Path(directory) / 'bad.json'
journal.write_text('{"results":[]}', encoding='utf-8')
with patch.object(m.Checker, 'check') as check:
self.assertEqual(self.call(['--recover', str(journal)]), 2)
check.assert_not_called()
if __name__ == '__main__':
unittest.main()