修复邮件协议认证错误分类并补齐真实响应验收

This commit is contained in:
xuans committed 2026-09-17 13:05:39 +08:00
1 parent 0169676923
commit b04dce7bcf
8 files changed
+166 -14

No files matched your search

+15 -3
View File
@@ -193,6 +193,13 @@ def main():
codes[protocol] = client.success(base + 'generate', {'protocol': protocol})['code']
assert protocol_operation(protocol, context, codes[protocol])() == 'success'
result['protocol_login'] = list(codes)
with smtp_connect(context) as smtp:
try:
smtp.auth('PLAIN', lambda challenge=None: '\0' + ADDRESS + '\0invalid-code')
raise AssertionError('SMTP 错误凭据未被拒绝')
except smtplib.SMTPAuthenticationError as error:
assert error.smtp_code == 535 and error.smtp_error.startswith(b'5.7.8 ')
result['smtp_invalid_credentials'] = 535
result['stage'] = 'smtp'
message = EmailMessage()
message['From'] = 'probe@runtime.invalid'
@@ -218,7 +225,10 @@ def main():
result['smtp'] = {'message_id': message_id, 'stored': True, 'owners': 1, 'attachment': True}
result['stage'] = 'overload'
rounds = []
result['overload'] = rounds
for protocol in codes:
round_result = {'protocol': protocol, 'responses': [], 'recovered': False}
rounds.append(round_result)
operations = [protocol_operation(protocol, context, codes[protocol]) for _ in range(4)]
barrier = threading.Barrier(8, timeout=20)
@@ -236,14 +246,16 @@ def main():
return {'outcome': operation(), 'seconds': round(time.monotonic() - began, 3)}
with ThreadPoolExecutor(max_workers=8) as pool:
responses = list(pool.map(execute, operations + [http_operation] * 4))
for response in pool.map(execute, operations + [http_operation] * 4):
round_result['responses'].append(response)
responses = round_result['responses']
assert any(item['outcome'] == 'busy' for item in responses[:4]), protocol + ' 未观察到繁忙响应'
assert any(item['outcome'] == 'busy' for item in responses[4:]), 'HTTP 未观察到繁忙响应'
assert any(item['outcome'] == 'success' for item in responses), '所有请求均失败'
assert protocol_operation(protocol, context, codes[protocol])() == 'success'
assert client.success('/api/v1/login', login).get('account') == ACCOUNT
rounds.append({'protocol': protocol, 'responses': responses, 'recovered': True})
result['overload'] = rounds
round_result['recovered'] = True
(root.parent / 'probe-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
result['stage'] = 'complete'
result['passed'] = True
finally:
+21 -5
View File
@@ -23,12 +23,24 @@ def service_state():
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--main-sha', required=True)
parser.add_argument('--binary', type=Path, default=Path('/opt/smail/smail'))
args = parser.parse_args()
assert os.geteuid() == 0 and run('hostname') == 'ikun2'
os.umask(0o027)
live = Path('/opt/smail')
with (live / 'smail').open('rb') as stream:
binary = args.binary.resolve(strict=True)
assert binary == args.binary and binary.is_file()
if binary != live / 'smail':
assert binary.parent.parent == Path('/var/tmp') and binary.parent.name.startswith('smail-review-')
assert binary.stat().st_uid == 0 and binary.stat().st_mode & 0o022 == 0
assert binary.parent.stat().st_uid == 0 and binary.parent.stat().st_mode & 0o022 == 0
with binary.open('rb') as stream:
assert hashlib.file_digest(stream, 'sha256').hexdigest() == args.main_sha
stream.seek(0)
header = stream.read(20)
assert header[:5] == b'\x7fELF\x02' and header[18:20] == b'\x3e\x00'
with (live / 'smail').open('rb') as stream:
live_sha = hashlib.file_digest(stream, 'sha256').hexdigest()
memory = dict(line.split(':', 1) for line in Path('/proc/meminfo').read_text().splitlines())
assert int(memory['MemAvailable'].split()[0]) >= 272 * 1024, '可用内存不足,暂停隔离验收'
stamp = time.strftime('%Y%m%d-%H%M%S')
@@ -40,7 +52,7 @@ def main():
for directory in (root, control, root / 'ssl'):
directory.mkdir(mode=0o750)
shutil.chown(directory, user='smail', group='smail')
shutil.copy2(live / 'smail', root / 'smail')
shutil.copy2(binary, root / 'smail')
(root / 'smail').chmod(0o755)
shutil.copy2(Path(__file__).with_name('runtime_probe.py'), target / 'runtime_probe.py')
subprocess.run(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-days', '2',
@@ -56,7 +68,8 @@ def main():
before = service_state()
unit = 'smail-runtime-review-' + stamp
probe_unit = 'smail-runtime-probe-' + stamp
result = {'target': str(target), 'unit': unit, 'main_sha256': args.main_sha, 'before': before}
result = {'target': str(target), 'unit': unit, 'main_sha256': args.main_sha,
'source': str(binary), 'live_sha256': live_sha, 'before': before}
started = time.strftime('%Y-%m-%dT%H:%M:%S')
try:
subprocess.run([
@@ -94,8 +107,9 @@ def main():
], text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=270)
(target / 'probe.log').write_text(probe.stdout, encoding='utf-8')
result['probe_code'] = probe.returncode
if (target / 'probe-result.json').exists():
result['probe'] = json.loads((target / 'probe-result.json').read_text())
assert probe.returncode == 0, '隔离验收失败,请检查保留的 probe.log'
result['probe'] = json.loads((target / 'probe-result.json').read_text())
assert result['probe']['passed']
with sqlite3.connect(f'file:{live / "data/smail.db"}?mode=ro', uri=True) as database:
message_id = result['probe']['smtp']['message_id']
@@ -107,7 +121,9 @@ def main():
subprocess.run(['systemctl', 'stop', unit], check=True)
subprocess.run(['systemctl', 'stop', probe_unit], check=False, stderr=subprocess.DEVNULL)
result['after'] = service_state()
result['live_unchanged'] = result['after'] == before
with (live / 'smail').open('rb') as stream:
result['live_unchanged'] = (result['after'] == before
and hashlib.file_digest(stream, 'sha256').hexdigest() == live_sha)
result['integrity'] = {}
for name in ('smail.db', 'smail-state.db'):
path = root / 'data' / name