Add invocation-scoped TLS verification bypass for self-signed servers
This commit is contained in:
1 parent
7fa71eceb1
commit
59ab4c5b26
9 files changed
+206
-15
No files matched your search
@@ -17,6 +17,7 @@ import posixpath
|
||||
import re
|
||||
import secrets
|
||||
import shlex
|
||||
import ssl
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -28,7 +29,7 @@ import urllib.parse as url
|
||||
import urllib.request as http
|
||||
import webbrowser
|
||||
|
||||
VERSION = '0.1.0'
|
||||
VERSION = '0.2.0'
|
||||
|
||||
class Failure(Exception):
|
||||
def __init__(self, message, status=0, payload=None):
|
||||
@@ -75,9 +76,18 @@ class NoRedirect(http.HTTPRedirectHandler):
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, base, token='', lang='auto'):
|
||||
def __init__(self, base, token='', lang='auto', *, insecure=False):
|
||||
self.base, self.token, self.lang = origin(base), token, lang
|
||||
self.opener = http.build_opener(NoRedirect())
|
||||
self.insecure = insecure
|
||||
handlers = [NoRedirect()]
|
||||
if insecure:
|
||||
# This client's TLS policy only; never change Python's global defaults.
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
context.check_hostname = False
|
||||
context.verify_mode = ssl.CERT_NONE
|
||||
context.set_alpn_protocols(['http/1.1'])
|
||||
handlers.append(http.HTTPSHandler(context=context))
|
||||
self.opener = http.build_opener(*handlers)
|
||||
|
||||
def request(self, route, method='GET', data=None, form=None, limit=4 << 20, etag=None, content_type=None):
|
||||
if not route.startswith('/') or route.startswith('//'):
|
||||
@@ -671,7 +681,7 @@ def execute(client, profile, words):
|
||||
route = addr[5:] if addr.startswith('path:') else '/o/' + addr if addr.startswith('uuid:') else '/r/view/' + addr[4:]
|
||||
webbrowser.open(client.base + url.quote(route, safe='/:'), new=2)
|
||||
elif cmd == 'edit':
|
||||
editing = Client(client.base, client.token, language(args.lang)) if args.lang else client
|
||||
editing = Client(client.base, client.token, language(args.lang), insecure=client.insecure) if args.lang else client
|
||||
editor(editing, addr, args.format)
|
||||
elif cmd == 'put':
|
||||
state = client.snapshot(addr)
|
||||
@@ -743,11 +753,13 @@ def main(argv=None):
|
||||
parser.add_argument('--version', action='version', version='pages-cli ' + VERSION)
|
||||
parser.add_argument('--profile', help='Use another configured connection for this invocation')
|
||||
parser.add_argument('--lang', help='auto or an exact language tag for this invocation')
|
||||
parser.add_argument('-k', '--insecure', action='store_true', help='Skip TLS certificate and hostname verification for this invocation only (unsafe; place before COMMAND)')
|
||||
parser.add_argument('command', nargs='?', default='shell')
|
||||
parser.add_argument('args', nargs=argparse.REMAINDER)
|
||||
args = parser.parse_args(argv)
|
||||
if args.command == 'configure':
|
||||
if args.profile or args.lang: parser.error('Use pages configure set --profile NAME --lang TAG.')
|
||||
if args.insecure: parser.error('--insecure is invocation-only; configure does not connect or save a TLS bypass.')
|
||||
configure(args.args); return
|
||||
if args.command == 'help':
|
||||
parser.print_help(); return
|
||||
@@ -769,10 +781,12 @@ def main(argv=None):
|
||||
lang = language(args.lang or spec.get('lang', 'auto'))
|
||||
# Hold the profile lock before releasing the configuration lock.
|
||||
profile = stack.enter_context(Profile(name).locked())
|
||||
if args.insecure:
|
||||
print('WARNING: --insecure disables TLS certificate and hostname verification for this invocation. Credentials and data can be intercepted; this option is not saved.', file=sys.stderr)
|
||||
if args.command == 'login':
|
||||
if not login_args.oidc and not spec.get('username'): raise Failure('Set your account with pages configure --username NAME, or use pages login --oidc.')
|
||||
if profile.data.get('token'): raise Failure('Log out this profile before replacing its session.')
|
||||
client = Client(spec['origin'], lang=lang)
|
||||
client = Client(spec['origin'], lang=lang, insecure=args.insecure)
|
||||
if login_args.oidc:
|
||||
expires, username = client.login_oidc(login_args.no_browser, spec.get('username', ''))
|
||||
else:
|
||||
@@ -801,7 +815,7 @@ def main(argv=None):
|
||||
if args.command != 'logout' and (profile.data.get('origin') != spec['origin'] or profile.data.get('configured_username', profile.data.get('username')) != spec.get('username', '')):
|
||||
raise Failure('Session and configured identity differ. Log out before reconnecting.')
|
||||
if args.command not in ('logout', 'status') and profile.data.get('expires', 0) <= time.time(): raise Failure('Session expired; log out and log in again.')
|
||||
client = Client(profile.data['origin'], profile.data['token'], lang)
|
||||
client = Client(profile.data['origin'], profile.data['token'], lang, insecure=args.insecure)
|
||||
if args.command == 'shell': shell(client, profile)
|
||||
else: execute(client, profile, [args.command] + args.args)
|
||||
|
||||
|
||||
Reference in new issue
Block a user