补充三种数据库的HTTP旧会话撤销实机验收与证据记录
This commit is contained in:
1 parent
30e86af15d
commit
5d6931fbcd
3 files changed
+186
-7
No files matched your search
@@ -0,0 +1,61 @@
|
||||
"""HTTP 撤销探针必须重放原 Cookie,不能把客户端清空 Cookie 视为服务端失效。"""
|
||||
|
||||
from http.cookies import SimpleCookie
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools'))
|
||||
import runtime_probe
|
||||
|
||||
|
||||
class RuntimeSessionTests(unittest.TestCase):
|
||||
def test_session_snapshot_preserves_cookie_and_does_not_follow_logout(self):
|
||||
original = runtime_probe.Client()
|
||||
original.cookies = SimpleCookie('session=test-session; smail_csrf=test-csrf')
|
||||
replay = runtime_probe.copy_session(original)
|
||||
original.cookies.clear()
|
||||
self.assertEqual(replay.cookies['session'].value, 'test-session')
|
||||
self.assertEqual(replay.cookies['smail_csrf'].value, 'test-csrf')
|
||||
|
||||
def test_only_explicit_need_login_is_accepted(self):
|
||||
client = MagicMock()
|
||||
for response in [(200, {'errorNo': 0}, None), (500, None, None),
|
||||
(200, {'errorNo': 500}, None), (403, None, None)]:
|
||||
with self.subTest(response=response):
|
||||
client.request.return_value = response
|
||||
with self.assertRaises(AssertionError):
|
||||
runtime_probe.require_session_denied(client)
|
||||
client.request.return_value = (200, {'errorNo': 403}, None)
|
||||
runtime_probe.require_session_denied(client)
|
||||
|
||||
def test_real_actions_are_required_and_passwords_are_not_in_result(self):
|
||||
clients = []
|
||||
|
||||
def create_client(*args, **kwargs):
|
||||
client = MagicMock()
|
||||
client.cookies = SimpleCookie('session=test-session')
|
||||
client.success.return_value = {'account': 'sessionreview'}
|
||||
client.request.side_effect = lambda path, *args: (200, {'errorNo': 100 if path.endswith('/login') else 403}, None)
|
||||
clients.append(client)
|
||||
return client
|
||||
|
||||
admin = MagicMock()
|
||||
admin.success.return_value = {'account': runtime_probe.ACCOUNT}
|
||||
with patch.object(runtime_probe, 'Client', side_effect=create_client), \
|
||||
patch.object(runtime_probe.secrets, 'token_urlsafe', side_effect=['TestPass1234', 'NewPass12345']):
|
||||
result = runtime_probe.verify_http_session_revocation(admin, None)
|
||||
self.assertTrue(all(result.values()))
|
||||
self.assertEqual(len(result), 8)
|
||||
edits = [call.args[1] for call in admin.success.call_args_list if call.args[0].endswith('/edit')]
|
||||
self.assertEqual([item['disabled'] for item in edits], [1, 0])
|
||||
paths = [call.args[0] for client in clients for call in client.success.call_args_list]
|
||||
self.assertIn('/api/v1/logout', paths)
|
||||
self.assertIn('/api/v1/settings/modify_password', paths)
|
||||
self.assertNotIn('TestPass1234', str(result))
|
||||
self.assertNotIn('NewPass12345', str(result))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -4,6 +4,7 @@ import argparse
|
||||
import base64
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import ExitStack
|
||||
from copy import deepcopy
|
||||
from email.message import EmailMessage
|
||||
from email.parser import BytesParser
|
||||
from email.policy import default as email_policy
|
||||
@@ -73,6 +74,66 @@ class Client:
|
||||
return data['data']
|
||||
|
||||
|
||||
def copy_session(client):
|
||||
replay = Client(client.context, client.port)
|
||||
replay.cookies = deepcopy(client.cookies)
|
||||
return replay
|
||||
|
||||
|
||||
def require_session_denied(client):
|
||||
status, data, _ = client.request('/api/v1/user/info')
|
||||
assert status == 200 and data is not None and data.get('errorNo') == 403, '旧会话未明确返回需要登录'
|
||||
|
||||
|
||||
def verify_http_session_revocation(admin, context):
|
||||
# 普通验收账号与超管分离,不改变备份恢复所需的超管密码。
|
||||
account = 'sessionreview'
|
||||
password = secrets.token_urlsafe(9)
|
||||
new_password = secrets.token_urlsafe(9)
|
||||
admin.success('/api/v1/user/create', {
|
||||
'account': account, 'username': '隔离会话撤销验收', 'password': password,
|
||||
'disabled': 0, 'role': 'user',
|
||||
})
|
||||
|
||||
def login(value):
|
||||
client = Client(context)
|
||||
identity = client.success('/api/v1/login', {'account': account, 'password': value})
|
||||
assert identity.get('account') == account and not identity.get('pending')
|
||||
assert client.success('/api/v1/user/info').get('account') == account
|
||||
return client
|
||||
|
||||
first, independent = login(password), login(password)
|
||||
replay = copy_session(first)
|
||||
assert replay.success('/api/v1/user/info').get('account') == account
|
||||
first.success('/api/v1/logout')
|
||||
require_session_denied(replay)
|
||||
assert independent.success('/api/v1/user/info').get('account') == account
|
||||
result = {'logout_cookie_replay_denied': True, 'independent_session_preserved': True}
|
||||
|
||||
# 此独立会话在禁用期间不发请求,避免只验证已被销毁的会话记录。
|
||||
replay_after_enable = login(password)
|
||||
admin.success('/api/v1/user/edit', {'account': account, 'disabled': 1})
|
||||
require_session_denied(independent)
|
||||
admin.success('/api/v1/user/edit', {'account': account, 'disabled': 0})
|
||||
require_session_denied(replay_after_enable)
|
||||
result.update(disabled_session_denied=True, reenabled_old_session_denied=True)
|
||||
|
||||
current, other = login(password), login(password)
|
||||
replay = copy_session(current)
|
||||
current.success('/api/v1/settings/modify_password', {
|
||||
'current_password': password, 'password': new_password,
|
||||
})
|
||||
require_session_denied(replay)
|
||||
require_session_denied(other)
|
||||
status, data, _ = Client(context).request('/api/v1/login', {'account': account, 'password': password})
|
||||
assert status == 200 and data is not None and data.get('errorNo') == 100, '修改密码后旧密码未明确拒绝'
|
||||
login(new_password)
|
||||
assert admin.success('/api/v1/user/info').get('account') == ACCOUNT
|
||||
result.update(password_change_sessions_denied=True, old_password_denied=True,
|
||||
new_password_accepted=True, admin_session_preserved=True)
|
||||
return result
|
||||
|
||||
|
||||
def smtp_connect(context):
|
||||
client = smtplib.SMTP('127.0.0.1', 25, timeout=30)
|
||||
client.ehlo('runtime.invalid')
|
||||
@@ -530,6 +591,8 @@ def main():
|
||||
revoke = lambda: client.success('/api/v1/security/credentials/clients/revoke', {'client_id': credential['client']['id']})
|
||||
result['client_revocation'] = verify_protocol_revocation(client, context, client_codes, revoke)
|
||||
assert all(result['client_revocation'].values()), '撤销客户端授权码后旧协议连接仍可访问业务数据'
|
||||
result['stage'] = 'http_session_revocation'
|
||||
result['http_session_revocation'] = verify_http_session_revocation(client, context)
|
||||
result['stage'] = 'complete'
|
||||
result['passed'] = True
|
||||
finally:
|
||||
|
||||
Reference in new issue
Block a user