385 lines
22 KiB
Python
385 lines
22 KiB
Python
import contextlib
|
|
import base64
|
|
from email.message import Message
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import runpy
|
|
import stat
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
import unittest
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
from unittest.mock import patch
|
|
|
|
m = runpy.run_path(str(Path(__file__).with_name('pages')))
|
|
Client, Failure, Profile = m['Client'], m['Failure'], m['Profile']
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def server(callback):
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_GET(self): callback(self)
|
|
def do_POST(self): callback(self)
|
|
def log_message(self, *args): pass
|
|
instance = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
|
|
thread = threading.Thread(target=instance.serve_forever, daemon=True)
|
|
thread.start()
|
|
try: yield 'http://127.0.0.1:%d' % instance.server_port
|
|
finally: instance.shutdown(); instance.server_close(); thread.join()
|
|
|
|
|
|
def reply(h, status, body, mime='application/json'):
|
|
raw = json.dumps(body).encode() if isinstance(body, dict) else body
|
|
h.send_response(status); h.send_header('Content-Type', mime); h.end_headers(); h.wfile.write(raw)
|
|
|
|
|
|
class RemoteTests(unittest.TestCase):
|
|
def test_oidc_polling_pkce_and_no_secret_output(self):
|
|
now = [0]
|
|
waits, calls = [], []
|
|
started = {'device_code': 'D'*43, 'verification_uri': '/_/auth/cli/verify?request='+'R'*43,
|
|
'user_code': 'ABCD-1234', 'expires_in': 300, 'interval': 5}
|
|
client = Client('https://wiki.example')
|
|
def sleep(seconds): waits.append(seconds); now[0] += seconds
|
|
def request(route, **kwargs):
|
|
calls.append((route, kwargs))
|
|
if route.endswith('/start'): return started
|
|
polls = len(calls)-1
|
|
if polls == 1: raise Failure('pending',400,{'error':'authorization_pending'})
|
|
if polls == 2: raise Failure('slow',400,{'error':'slow_down'})
|
|
return {'access_token':'pw_api_'+'S'*43,'token_type':'Bearer','expires_in':3600,'username':'anya'}
|
|
with patch.object(client,'json',side_effect=request), patch.object(m['webbrowser'],'open') as browser, contextlib.redirect_stdout(io.StringIO()) as out:
|
|
expires, name = client.login_oidc(True,sleep=sleep,clock=lambda:now[0])
|
|
self.assertEqual(name,'anya'); self.assertGreater(expires,time.time())
|
|
self.assertEqual(waits,[5,5,10]); browser.assert_not_called()
|
|
proof = calls[1][1]['form']['code_verifier']
|
|
expected=base64.urlsafe_b64encode(hashlib.sha256(proof.encode()).digest()).decode().rstrip('=')
|
|
self.assertEqual(calls[0][1]['form']['code_challenge'],expected)
|
|
self.assertNotIn(proof,out.getvalue()); self.assertNotIn(started['device_code'],out.getvalue()); self.assertNotIn(client.token,out.getvalue())
|
|
self.assertIn('ABCD-1234',out.getvalue())
|
|
|
|
def test_oidc_unavailable_server_explained_without_retry(self):
|
|
for status in (404, 405, 501):
|
|
with self.subTest(status=status):
|
|
client = Client('https://wiki.example')
|
|
with patch.object(client, 'json', side_effect=Failure('Not found or not readable.', status)) as api, patch.object(m['webbrowser'], 'open') as browser, contextlib.redirect_stdout(io.StringIO()) as out:
|
|
with self.assertRaisesRegex(Failure, 'CLI OIDC login is unavailable') as caught:
|
|
client.login_oidc(True)
|
|
self.assertEqual(caught.exception.status, status)
|
|
api.assert_called_once()
|
|
browser.assert_not_called()
|
|
self.assertEqual(out.getvalue(), '')
|
|
self.assertEqual(client.token, '')
|
|
|
|
def test_oidc_disabled_and_other_start_errors(self):
|
|
client = Client('https://wiki.example')
|
|
with patch.object(client, 'json', side_effect=Failure('Access denied.', 403, {'error': 'oidc_disabled'})):
|
|
with self.assertRaisesRegex(Failure, 'not enabled for this domain'):
|
|
client.login_oidc(True)
|
|
for error in (Failure('Connection/TLS failure.'), Failure('Access denied.', 403), Failure('Unavailable.', 503)):
|
|
with patch.object(client, 'json', side_effect=error) as api:
|
|
with self.assertRaises(Failure) as caught:
|
|
client.login_oidc(True)
|
|
self.assertIs(caught.exception, error)
|
|
api.assert_called_once()
|
|
|
|
def test_oidc_rejects_external_url_and_wrong_account(self):
|
|
client=Client('https://wiki.example')
|
|
started={'device_code':'D'*43,'verification_uri':'https://evil.example/','user_code':'ABCD-1234','expires_in':300,'interval':5}
|
|
with patch.object(client,'json',return_value=started),patch.object(m['webbrowser'],'open') as browser:
|
|
with self.assertRaises(Failure): client.login_oidc()
|
|
browser.assert_not_called()
|
|
started['verification_uri']='/_/auth/cli/verify?request='+'R'*43
|
|
token={'access_token':'pw_api_'+'S'*43,'token_type':'Bearer','expires_in':3600,'username':'other'}
|
|
with patch.object(client,'json',side_effect=[started,token,{}]) as api, contextlib.redirect_stdout(io.StringIO()):
|
|
with self.assertRaises(Failure): client.login_oidc(True,'anya',sleep=lambda _:None,clock=lambda:0)
|
|
self.assertEqual(api.call_args.args[0],'/_/auth/logout')
|
|
self.assertEqual(client.token,'')
|
|
|
|
def test_oidc_cancellation_denial_and_ambiguous_exchange(self):
|
|
started={'device_code':'D'*43,'verification_uri':'/_/auth/cli/verify?request='+'R'*43,'user_code':'ABCD-1234','expires_in':300,'interval':5}
|
|
for error in [Failure('denied',403,{'error':'access_denied'}),Failure('expired',400,{'error':'expired_token'}),Failure('network failure')]:
|
|
client=Client('https://wiki.example')
|
|
with patch.object(client,'json',side_effect=[started,error]) as api,contextlib.redirect_stdout(io.StringIO()):
|
|
with self.assertRaises(Failure): client.login_oidc(True,sleep=lambda _:None,clock=lambda:0)
|
|
self.assertEqual(api.call_count,2)
|
|
client=Client('https://wiki.example')
|
|
with patch.object(client,'json',return_value=started) as api,contextlib.redirect_stdout(io.StringIO()):
|
|
def interrupt(_): raise KeyboardInterrupt()
|
|
with self.assertRaises(KeyboardInterrupt): client.login_oidc(True,sleep=interrupt,clock=lambda:0)
|
|
self.assertEqual(api.call_count,1)
|
|
|
|
def test_oidc_main_without_configured_username(self):
|
|
with tempfile.TemporaryDirectory() as root, patch.dict(os.environ,{'XDG_CONFIG_HOME':root}):
|
|
self.configure(None,'--origin','https://wiki.example')
|
|
def login(client,*args,**kwargs): client.token='pw_api_'+'s'*43; return time.time()+600,'anya'
|
|
with patch.object(Client,'login_oidc',login),contextlib.redirect_stdout(io.StringIO()):
|
|
m['main'](['login','--oidc','--no-browser'])
|
|
m['main'](['status'])
|
|
self.configure(None,'--lang','ja')
|
|
with Profile('default').locked() as profile:
|
|
self.assertEqual(profile.data['username'],'anya')
|
|
self.assertEqual(profile.data['configured_username'],'')
|
|
with self.assertRaises(Failure): self.configure(None,'--username','other')
|
|
|
|
def configure(self, root, *args):
|
|
with contextlib.redirect_stdout(io.StringIO()) as output:
|
|
m['configure'](list(args), root)
|
|
return output.getvalue()
|
|
|
|
def test_configuration_defaults_and_switch(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
self.configure(root,'--origin','https://one.example','--username','anya')
|
|
self.configure(root,'set','--profile','work','--origin','https://two.example','--username','worker','--lang','ja')
|
|
self.configure(root,'use','work')
|
|
settings=json.loads(self.configure(root,'show'))
|
|
self.assertEqual(settings['default_profile'],'work')
|
|
self.assertEqual(settings['profiles']['default']['lang'],'und')
|
|
self.assertEqual(settings['profiles']['work']['lang'],'ja')
|
|
self.configure(root,'--lang','en-US')
|
|
settings=json.loads(self.configure(root,'show'))
|
|
self.assertEqual(settings['profiles']['work']['lang'],'en-US')
|
|
with m['Configuration'](root).locked() as cfg:
|
|
self.assertEqual(stat.S_IMODE(cfg.path.stat().st_mode),0o600)
|
|
with self.assertRaises(Failure): self.configure(root,'use','missing')
|
|
with self.assertRaises(Failure): self.configure(root,'--lang','ja_JP')
|
|
|
|
def test_active_credentials_cannot_be_retargeted_or_shown(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
self.configure(root,'--origin','https://one.example','--username','anya')
|
|
with Profile('default',root).locked() as profile:
|
|
profile.data={'origin':'https://one.example','username':'anya','token':'private-token'}; profile.save()
|
|
for args in [('--origin','https://other.example'),('--username','other')]:
|
|
with self.assertRaises(Failure): self.configure(root,*args)
|
|
self.assertNotIn('private-token',self.configure(root,'show'))
|
|
self.assertNotIn('private-token',self.configure(root,'list'))
|
|
self.configure(root,'--lang','ja')
|
|
with Profile('default',root).locked():
|
|
with self.assertRaises(Failure): self.configure(root,'--lang','en')
|
|
with Profile('default',root).locked() as profile:
|
|
profile.data.pop('token'); profile.save()
|
|
self.configure(root,'--origin','https://other.example')
|
|
|
|
def test_main_uses_saved_defaults_and_rejects_manual_retarget(self):
|
|
with tempfile.TemporaryDirectory() as root, patch.dict(os.environ,{'XDG_CONFIG_HOME':root}):
|
|
self.configure(root,'set','--profile','work','--origin','https://work.example','--username','worker','--lang','ja')
|
|
with Profile('work',root).locked() as profile:
|
|
profile.data={'origin':'https://work.example','username':'worker','token':'session-token','expires':time.time()+100,'cwd':'/notes'}; profile.save()
|
|
# Patch the globals used by main rather than running a real request.
|
|
globals=m['main'].__globals__
|
|
with patch.dict(globals,{'execute':lambda client,profile,args: self.assertEqual((client.base,client.lang,args),('https://work.example','ja',['ls']))}):
|
|
m['main'](['ls'])
|
|
with patch.dict(globals,{'execute':lambda client,profile,args: self.assertEqual(client.lang,'en')}):
|
|
m['main'](['--lang','en','ls'])
|
|
with m['Configuration'](root).locked() as cfg:
|
|
cfg.data['profiles']['work']['origin']='https://evil.example'; cfg.save()
|
|
with self.assertRaises(Failure): m['main'](['ls'])
|
|
|
|
def test_bad_configuration_is_not_printed(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
with m['Configuration'](root).locked() as cfg:
|
|
cfg.data={'version':1,'default_profile':'default','profiles':{'default':{'origin':'https://example.test','token':'secret'}}}; cfg.save()
|
|
with self.assertRaises(Failure): self.configure(root,'show')
|
|
for invalid in [None,42,[],{}]:
|
|
with self.assertRaises(Failure): m['profile_name'](invalid)
|
|
with self.assertRaises(Failure): m['language'](invalid)
|
|
with self.assertRaises(Failure): m['origin'](invalid)
|
|
|
|
def test_origin_and_paths(self):
|
|
for value in ['http://example.com', 'https://user:pass@example.com', 'https://example.com/a', 'https://example.com?token=x']:
|
|
with self.assertRaises(Failure): m['origin'](value)
|
|
self.assertEqual(m['address']('../日本語 #?%', '/docs/deep'), 'path:/docs/日本語 #?%')
|
|
self.assertEqual(m['semantic']('../../..', '/'), '/')
|
|
for value in ['https://evil.test', 'asset:x:y', 'rev:bad', '/bad\x1b[31m']:
|
|
with self.assertRaises(Failure): m['address'](value)
|
|
self.assertNotIn('\x1b', m['safe_text']('\x1b]52;clipboard\x07'))
|
|
self.assertNotIn('\u202e', m['safe_text']('abc\u202e'))
|
|
|
|
def test_redirect_never_receives_bearer(self):
|
|
seen = []
|
|
with server(lambda h: (seen.append(h.headers.get('Authorization')), reply(h, 200, {}))) as sink:
|
|
def redirect(h):
|
|
h.send_response(302); h.send_header('Location', sink); h.end_headers()
|
|
with server(redirect) as base:
|
|
with self.assertRaises(Failure) as error: Client(base, 'secret').json('/test')
|
|
self.assertEqual(error.exception.status, 302)
|
|
self.assertEqual(seen, [])
|
|
|
|
def test_protocol_failures_and_limits(self):
|
|
for status, body, mime in [(200,b'<html>login</html>','text/html'), (200,b'not-json','application/json'), (403,b'private internal error','text/plain')]:
|
|
with server(lambda h: reply(h,status,body,mime)) as base:
|
|
with self.assertRaises(Failure) as error: Client(base).json('/test')
|
|
self.assertNotIn('private internal', str(error.exception))
|
|
with server(lambda h: reply(h,200,b'x'*100)) as base:
|
|
with self.assertRaises(Failure): Client(base).request('/large',limit=10)
|
|
|
|
def test_password_totp_contract(self):
|
|
requests = []
|
|
def handle(h):
|
|
requests.append((h.path, h.rfile.read(int(h.headers['Content-Length'])).decode()))
|
|
if h.path.endswith('/login'): reply(h,401,{'mfa_required':True,'challenge_token':'short-lived'})
|
|
else: reply(h,200,{'access_token':'a'*32,'token_type':'Bearer','expires_in':3600})
|
|
with server(handle) as base:
|
|
client = Client(base)
|
|
client.login('someone','secret',lambda prompt:'123456')
|
|
self.assertEqual(client.token,'a'*32)
|
|
self.assertIn('challenge_token=short-lived',requests[1][1])
|
|
self.assertNotIn('password',requests[1][1])
|
|
|
|
def test_private_profile_lock_and_symlinks(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
with Profile('work',root).locked() as profile:
|
|
profile.data={'token':'secret'}; profile.save()
|
|
self.assertEqual(stat.S_IMODE(profile.path.stat().st_mode),0o600)
|
|
self.assertEqual(stat.S_IMODE(profile.root.stat().st_mode),0o700)
|
|
with self.assertRaises(Failure):
|
|
with Profile('work',root).locked(): pass
|
|
with Profile('work',root).locked() as loaded: self.assertEqual(loaded.data['token'],'secret')
|
|
profile.path.chmod(0o644)
|
|
with self.assertRaises(Failure):
|
|
with Profile('work',root).locked(): pass
|
|
profile.path.unlink(); profile.path.symlink_to(Path(root)/'missing')
|
|
with self.assertRaises(Failure):
|
|
with Profile('work',root).locked(): pass
|
|
|
|
def test_cas_and_authoring_contract(self):
|
|
state={'object_uuid':'object','head_revision_uuid':'base','principal_uuid':'person','lang':'ja',
|
|
'content_type':'text/html','filename':'a.html','body':'old'}
|
|
client=Client('https://example.test')
|
|
calls=[]
|
|
def api(route, **kwargs):
|
|
calls.append((route,kwargs['data']))
|
|
return {'html':'<p>new</p>'} if route.endswith('/markdown') else {'revision_uuid':'new'}
|
|
with patch.object(client,'json',side_effect=api): client.save(state,'new',True)
|
|
self.assertTrue(calls[0][1]['authoring']); self.assertFalse(calls[0][1]['projection'])
|
|
self.assertEqual(calls[1][1]['base_revision_uuid'],'base')
|
|
self.assertEqual(calls[1][1]['editor_principal_uuid'],'person')
|
|
self.assertEqual(calls[1][1]['lang'],'ja')
|
|
with patch.object(client,'json',side_effect=Failure('conflict',409)):
|
|
with self.assertRaises(Failure): client.save(state,'new')
|
|
|
|
def test_download_private_and_no_clobber(self):
|
|
headers=Message(); headers['X-PagesWiki-Revision-UUID']='rev'
|
|
with tempfile.TemporaryDirectory() as root:
|
|
profile=Profile('test',root)
|
|
client=Client('https://example.test')
|
|
file=str(Path(root)/'download')
|
|
with patch.object(client,'raw',return_value=(b'data',headers)), contextlib.redirect_stdout(io.StringIO()):
|
|
m['execute'](client,profile,['get','/path',file])
|
|
self.assertEqual(stat.S_IMODE(Path(file).stat().st_mode),0o600)
|
|
with self.assertRaises(FileExistsError): m['execute'](client,profile,['get','/path',file])
|
|
|
|
def test_editor_conflict_retains_private_recovery(self):
|
|
state={'object_uuid':'object','head_revision_uuid':'base','principal_uuid':'person','lang':'und',
|
|
'content_type':'text/plain','filename':'x.txt','body':'old','markdown':'old'}
|
|
client=Client('https://example.test','secret-token')
|
|
with tempfile.TemporaryDirectory() as root:
|
|
directory=Path(root)/'working'; directory.mkdir(mode=0o700)
|
|
def edit(args, **kwargs):
|
|
Path(args[-1]).write_text('my edit')
|
|
class Result: returncode=0
|
|
return Result()
|
|
with patch('tempfile.mkdtemp',return_value=str(directory)), patch('subprocess.run',side_effect=edit), patch.object(client,'snapshot',return_value=state), patch.object(client,'save',side_effect=Failure('conflict',409)), contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
|
|
with self.assertRaises(Failure): m['editor'](client,'path:/x','auto')
|
|
self.assertEqual((directory/'document.txt').read_text(),'my edit')
|
|
recovery=(directory/'recovery.json').read_text()
|
|
self.assertNotIn('secret-token',recovery)
|
|
self.assertIn('base',recovery)
|
|
self.assertEqual(stat.S_IMODE((directory/'recovery.json').stat().st_mode),0o600)
|
|
|
|
|
|
def oidc_integration():
|
|
fixture=json.load(sys.stdin)
|
|
original=Client.json
|
|
grant={}
|
|
def capture(client,route,**kwargs):
|
|
result=original(client,route,**kwargs)
|
|
if route=='/_/auth/cli/start': grant.update(result)
|
|
return result
|
|
def browser(target,**kwargs):
|
|
ident=m['url'].parse_qs(m['url'].urlsplit(target).query)['request'][0]
|
|
original(Client(fixture['origin']),'/__test/cli-approve',method='POST',form={'request':ident,'code':grant['user_code']})
|
|
return True
|
|
with tempfile.TemporaryDirectory() as root,patch.dict(os.environ,{'XDG_CONFIG_HOME':root}),contextlib.redirect_stdout(io.StringIO()) as output:
|
|
m['main'](['configure','--origin',fixture['origin']])
|
|
with patch.object(Client,'json',capture),patch.object(m['webbrowser'],'open',browser): m['main'](['login','--oidc'])
|
|
with Profile('default',root).locked() as session:
|
|
token=session.data['token']; assert session.data['username']=='admin'
|
|
m['main'](['ls']); m['main'](['status'])
|
|
client=Client(fixture['origin'],token)
|
|
state=client.snapshot('path:/editor-fixture')
|
|
client.save(state,'# OIDC edit\n\n{{children}}\n',True)
|
|
assert '{{children}}' in client.snapshot('path:/editor-fixture')['markdown']
|
|
m['main'](['logout'])
|
|
assert token not in output.getvalue()
|
|
try: client.snapshot('path:/editor-fixture')
|
|
except Failure as e: assert e.status in (401,403,303)
|
|
else: raise AssertionError('revoked CLI session accepted')
|
|
print('OIDC CLI configure/login/list/authoring/logout integration passed')
|
|
|
|
|
|
def integration():
|
|
fixture=json.load(sys.stdin)
|
|
# Exercise the public entry point with persisted defaults, not only Client.
|
|
with tempfile.TemporaryDirectory() as root, patch.dict(os.environ,{'XDG_CONFIG_HOME':root}), contextlib.redirect_stdout(io.StringIO()) as output:
|
|
m['main'](['configure','--origin',fixture['origin'],'--username','admin'])
|
|
with patch('getpass.getpass',return_value='a long test password'): m['main'](['login'])
|
|
with Profile('default',root).locked() as session: session_token=session.data['token']
|
|
m['main'](['ls'])
|
|
m['main'](['status'])
|
|
def local_editor(args, **kwargs):
|
|
file=Path(args[-1]); file.write_text(file.read_text()+'\nConfigured CLI edit\n')
|
|
class Result: returncode=0
|
|
return Result()
|
|
with patch('subprocess.run',side_effect=local_editor): m['main'](['edit','/editor-fixture'])
|
|
check=Client(fixture['origin'],session_token)
|
|
assert 'Configured CLI edit' in check.snapshot('path:/editor-fixture')['body']
|
|
m['main'](['logout'])
|
|
assert session_token not in output.getvalue()
|
|
with Profile('default',root).locked() as session: assert 'token' not in session.data
|
|
client=Client(fixture['origin'])
|
|
client.login('admin','a long test password')
|
|
addr='path:/editor-fixture'
|
|
state=client.snapshot(addr)
|
|
assert state['lang']=='und' and state['principal_uuid']
|
|
missing=Client(fixture['origin'], client.token, 'ja')
|
|
try: missing.snapshot(addr)
|
|
except Failure: pass
|
|
else: raise AssertionError('missing language silently fell back')
|
|
assert client.listing('/')['entries']
|
|
assert client.save(state,'# Remote\n\n{{children}}\n',True)['revision_uuid'] != state['head_revision_uuid']
|
|
latest=client.snapshot(addr)
|
|
assert '{{children}}' in latest['markdown'] and '<h1' in latest['body']
|
|
try: client.save(state,'stale edit')
|
|
except Failure as e: assert e.status==409
|
|
else: raise AssertionError('stale save accepted')
|
|
raw,headers=client.raw(addr)
|
|
assert hashlib.sha256(raw).hexdigest()==headers['X-PagesWiki-Document-SHA256']
|
|
assert client.raw('rev:'+latest['revision_uuid'])[0]==raw
|
|
try: client.snapshot('rev:'+latest['revision_uuid'])
|
|
except Failure: pass
|
|
else: raise AssertionError('immutable edit accepted')
|
|
with tempfile.TemporaryDirectory() as root:
|
|
profile=Profile('integration',root)
|
|
profile.data={'cwd':'/'}
|
|
file=Path(root)/'edit.txt'; file.write_text('local replacement')
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
m['execute'](client,profile,['put',str(file),addr,'--base',latest['head_revision_uuid']])
|
|
assert client.snapshot(addr)['body']=='local replacement'
|
|
client.json('/_/auth/logout',method='POST')
|
|
try: client.snapshot(addr)
|
|
except Failure as e: assert e.status in (401,403,303)
|
|
else: raise AssertionError('revoked session accepted')
|
|
print('Remote login/list/read/authoring/CAS/put/logout integration passed')
|
|
|
|
|
|
if __name__=='__main__':
|
|
if sys.argv[1:] == ['--integration']: integration()
|
|
elif sys.argv[1:] == ['--oidc-integration']: oidc_integration()
|
|
else: unittest.main()
|