Files
Agent-Workbench/scripts/verify-desktop-credentials.py
T
2026-11-12 09:01:34 +08:00

207 lines
12 KiB
Python

"""Owned Windows DPAPI restart and real browser acceptance; no user credentials."""
import argparse
import hashlib
import http.server
import json
import os
from pathlib import Path
import re
import shutil
import socket
import sqlite3
import subprocess
import threading
import time
import urllib.request
import uuid
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--node', required=True)
args = parser.parse_args()
from playwright.sync_api import sync_playwright, expect
workspace = Path.cwd()
root = workspace / '.qa' / ('desktop-credentials-' + str(uuid.uuid4()))
app_root, data_root = root / 'app', root / 'data'
app_root.mkdir(parents=True)
for name in ['dist-client', 'dist-server']:
shutil.copytree(workspace / name, app_root / name)
shutil.copy2(workspace / 'package.json', app_root / 'package.json')
with socket.socket() as probe:
probe.bind(('127.0.0.1', 0))
port = probe.getsockname()[1]
url = 'http://127.0.0.1:' + str(port)
node = str(Path(args.node).resolve())
allowed = re.compile(r'^(path|pathext|systemroot|systemdrive|windir|comspec|temp|tmp|userprofile|homedrive|homepath|home|appdata|localappdata|programfiles(?:\(x86\))?|commonprogramfiles(?:\(x86\))?|lang|lc_all)$', re.I)
environment = {key: value for key, value in os.environ.items() if allowed.match(key)}
environment['PATH'] = str(Path(node).parent) + os.pathsep + environment.get('PATH', environment.get('Path', ''))
for key in list(environment):
if key.lower() == 'path' and key != 'PATH':
del environment[key]
keys = ['OWNED_UI_DPAPI_' + uuid.uuid4().hex, 'OWNED_UI_LEGACY_' + uuid.uuid4().hex, 'OWNED_UI_VOLATILE_' + uuid.uuid4().hex]
requests = []
class ModelFixture(http.server.BaseHTTPRequestHandler):
def respond(self):
if self.command == 'POST':
self.rfile.read(int(self.headers.get('Content-Length', 0)))
valid = self.headers.get('Authorization') in ['Bearer ' + key for key in keys]
requests.append({'path': self.path, 'authorizedOwnedKey': valid})
result = {'data': [{'id': 'owned-fixture'}]} if self.path.endswith('/models') else {'choices': [{'message': {'role': 'assistant', 'content': 'Owned response OK'}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 5, 'completion_tokens': 4, 'total_tokens': 9}}
encoded = json.dumps(result if valid else {'error': {'message': 'Owned fixture credential rejected'}}).encode()
self.send_response(200 if valid else 401)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
do_GET = respond
do_POST = respond
def log_message(self, *unused):
pass
fixture = http.server.ThreadingHTTPServer(('127.0.0.1', 0), ModelFixture)
threading.Thread(target=fixture.serve_forever, daemon=True).start()
endpoint = 'http://127.0.0.1:' + str(fixture.server_port) + '/v1'
marks, errors, images = [], [], []
started, token, stage = False, None, 'host start'
def host(command):
child = subprocess.run([node, str(app_root / 'dist-server' / 'host-launcher.mjs'), command, '--app-root', str(app_root), '--data-root', str(data_root), '--node', node, '--port', str(port)], cwd=str(app_root), env=environment, capture_output=True, encoding='utf-8', timeout=75, creationflags=subprocess.CREATE_NO_WINDOW)
if child.returncode:
raise RuntimeError('Owned host operation failed: ' + child.stderr)
return json.loads(child.stdout)
def api(route, payload=None):
req = urllib.request.Request(url + '/api' + route, data=None if payload is None else json.dumps(payload).encode(), headers={'Content-Type': 'application/json', 'X-Workbench-Token': token or '', 'Connection': 'close'})
with urllib.request.urlopen(req, timeout=30) as response:
return json.load(response)
def mark(name):
marks.append(name)
print('PASS ' + name, flush=True)
def snapshot_rows():
with sqlite3.connect((data_root / 'workbench.sqlite').as_uri() + '?mode=ro', uri=True) as db:
result = {table: db.execute('SELECT * FROM ' + table + ' ORDER BY rowid').fetchall() for table in ['metadata', 'projects', 'sessions', 'records', 'events', 'outbox']}
result['metadata'] = [row for row in result['metadata'] if row[0] != 'runtimeOwner']
return result
def save(success, detail=None):
result = {'success': success, 'root': str(root), 'url': url, 'stage': stage, 'originalHost': globals().get('original'), 'freshHost': globals().get('fresh'), 'groups': marks, 'requests': requests, 'images': images, 'pageErrors': errors, 'detail': detail, 'scope': 'Owned CurrentUser DPAPI and local HTTP fixture; no external model request or existing credential is used.'}
(root / 'result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
(workspace / '.qa' / 'desktop-credentials-latest.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
try:
original = host('start'); started = True
token = api('/bootstrap')['token']
api('/config', {'mode': 'demo', 'baseUrl': endpoint, 'model': 'owned-legacy'})
with sync_playwright() as pw:
browser = pw.chromium.launch(channel='msedge', headless=True)
page = browser.new_page(viewport={'width': 1440, 'height': 1000})
page.on('pageerror', lambda error: errors.append(str(error)))
page.goto(url)
page.wait_for_load_state('networkidle')
assert page.get_by_role('button', name='打开模型中心', exact=True).count() == 1
page.get_by_role('button', name='打开模型中心', exact=True).click()
dialog = page.get_by_role('dialog', name='模型中心', exact=True)
expect(dialog).to_be_visible()
stage = 'real connection form and explicit encrypted save'
dialog.get_by_role('button', name='手动填写(兼容)', exact=True).click()
editor = dialog.get_by_role('form', name='编辑模型连接', exact=True)
editor.get_by_label('连接名称', exact=True).fill('Owned Saved Primary')
editor.get_by_label('接口根地址', exact=True).fill(endpoint)
editor.get_by_label('实际模型名称', exact=True).fill('owned-primary')
editor.get_by_label('加密保存到本机,重启后自动恢复', exact=True).check()
expect(editor.get_by_role('button', name='保存模型连接', exact=True)).to_be_disabled()
editor.get_by_label('API Key', exact=True).fill(keys[0])
editor.get_by_role('button', name='保存模型连接', exact=True).click()
expect(dialog.locator('.mc-connection').filter(has_text='Owned Saved Primary')).to_contain_text('可从本机加密保存加载')
assert keys[0] not in page.content()
mark(stage)
initial_image = root / 'credentials-new-connection.png'
page.screenshot(path=str(initial_image), full_page=True)
images.append(str(initial_image))
center = api('/model-center')
saved = next(value for value in center['connections'] if value['name'] == 'Owned Saved Primary')
volatile = api('/model-center/connections', {'name': 'Owned Volatile', 'model': 'owned-volatile', 'baseUrl': endpoint, 'apiKey': keys[2]})
stage = 'real legacy credential save without exposing an existing key'
dialog.get_by_role('button', name='凭据保存', exact=True).click()
form = dialog.get_by_role('form', name='保存加密凭据', exact=True)
expect(form).to_be_visible()
form.get_by_label('选择保存连接', exact=True).select_option('legacy-default')
form.get_by_label('重新填写 API Key', exact=True).fill(keys[1])
form.get_by_role('button', name='加密保存凭据', exact=True).click()
expect(form.get_by_label('重新填写 API Key', exact=True)).to_have_value('')
expect(dialog.locator('.mc-connection').filter(has_text='原模型连接')).to_contain_text('本机加密保存')
mark(stage)
for width in [1440, 390]:
page.set_viewport_size({'width': width, 'height': 1000})
expect(dialog.get_by_role('heading', name='本机加密凭据', exact=True)).to_be_visible()
assert page.evaluate('document.documentElement.scrollWidth <= innerWidth + 1')
image = root / ('credentials-' + str(width) + '.png')
page.screenshot(path=str(image), full_page=True)
images.append(str(image))
mark('real desktop and 390px saved-state controls and layout')
browser.close()
stage = 'actual host exit and fresh process restores encrypted revisions'
api('/model-center/probe', {'id': saved['id']})
before_rows = snapshot_rows()
before_credentials = api('/model-center/credentials')
stopped = host('stop'); started = False
assert stopped['processExited']
fresh = host('start'); started = True
assert fresh['identity']['instanceId'] != original['identity']['instanceId']
token = api('/bootstrap')['token']
restored = api('/model-center')
current = next(value for value in restored['connections'] if value['id'] == saved['id'])
assert current['hasKey'] and current['credentialRevision'] == saved['credentialRevision']
assert next(value for value in restored['connections'] if value['id'] == volatile['id'])['hasKey'] is False
assert next(value for value in restored['connections'] if value['id'] == 'legacy-default')['hasKey']
assert snapshot_rows() == before_rows
assert api('/model-center/credentials')['connections'] == before_credentials['connections']
mark(stage)
api('/model-center/probe', {'id': saved['id']})
api('/model-center/probe', {'id': 'legacy-default'})
assert len(requests) == 3 and all(value['authorizedOwnedKey'] for value in requests)
mark('fresh host actually uses both DPAPI-restored keys with owned HTTP responses')
stage = 'disable, delete and forget through real controls'
with sync_playwright() as pw:
browser = pw.chromium.launch(channel='msedge', headless=True)
page = browser.new_page(viewport={'width': 1280, 'height': 900})
page.goto(url); page.wait_for_load_state('networkidle')
page.get_by_role('button', name='打开模型中心', exact=True).click()
dialog = page.get_by_role('dialog', name='模型中心', exact=True)
dialog.get_by_role('button', name='凭据保存', exact=True).click()
card = dialog.locator('.mc-connection').filter(has_text='Owned Saved Primary')
card.get_by_role('button', name='关闭自动加载', exact=True).click()
expect(card).to_contain_text('关闭自动加载')
card.get_by_role('button', name='启用自动加载', exact=True).click()
expect(card.get_by_role('button', name='关闭自动加载', exact=True)).to_be_visible()
card.get_by_role('button', name='删除本机保存', exact=True).click()
expect(card).to_contain_text('仅本次内存')
assert next(value for value in api('/model-center')['connections'] if value['id'] == saved['id'])['hasKey']
card.get_by_role('button', name='清除该连接凭据', exact=True).click()
deadline = time.time() + 10
while next(value for value in api('/model-center')['connections'] if value['id'] == saved['id'])['hasKey']:
if time.time() > deadline:
raise AssertionError('Credential clearing did not settle')
time.sleep(0.1)
browser.close()
mark(stage)
stage = 'plaintext leak scan over actual database, WAL, exports and host logs'
needles = [needle for key in keys for needle in [key.encode(), key.encode('utf-16le')]]
for file in data_root.rglob('*'):
if file.is_file():
data = file.read_bytes()
assert not any(needle in data for needle in needles), str(file)
assert not any(key in json.dumps(api('/model-center')) for key in keys)
mark(stage)
assert not errors
save(True)
except Exception as error:
save(False, str(error))
raise
finally:
fixture.shutdown(); fixture.server_close()
if started:
host('stop')