diff --git a/docs/launcher.md b/docs/launcher.md index c186a86d..5c099935 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -102,7 +102,9 @@ A release carries `NX2navi-update.json` (version, notes, and the size and sha256 Preview channel) that has the manifest. A private repository answers only with a token (`github_token` in `data/launcher.json`); players need a public one. `feed_repo` in `data/launcher.json` points one launcher at another repository for testing; `{"type": "url", "url": ".../NX2navi-update.json"}` in feed.json reads one manifest -directly. +directly. Every address is https (plain http only to this PC), a redirect to http is refused, and the token is not +sent on when a redirect leaves the host it was meant for (`launcher/net.py`). A manifest that does not give a file's +size and sha256 is refused. `jobs.UpdateDownloadJob`, before the restart (`launcher/updater.py`): diff --git a/launcher/feed.py b/launcher/feed.py index dc469080..368dc579 100644 --- a/launcher/feed.py +++ b/launcher/feed.py @@ -13,7 +13,9 @@ The feed (app//feed.json, or the player's override in settings) is eith {"type": "url", "url": "https://.../NX2navi-update.json"} one manifest; its files sit beside it """ import json +import re import urllib.parse +from pathlib import Path from . import net, versions @@ -42,10 +44,19 @@ class Release: entry = self.manifest.get(key) if not entry: raise FeedError(f'the {self.version} manifest has no {key} file') - url = self.assets.get(entry['name']) + name, size, sha = entry.get('name'), entry.get('size'), entry.get('sha256') + # The name becomes a file in downloads/: a bare file name only. Every file is checked against its size and + # sha256, so a manifest without them is refused rather than downloaded unchecked. + if (not isinstance(name, str) or not name or name in ('.', '..') or name != Path(name).name + or '/' in name or '\\' in name or ':' in name): + raise FeedError(f'the {self.version} manifest names its {key} file {name!r}, not a plain file name') + if (not isinstance(size, int) or isinstance(size, bool) or size <= 0 or not isinstance(sha, str) + or not re.fullmatch(r'[0-9a-fA-F]{64}', sha)): + raise FeedError(f'the {self.version} manifest gives no size and sha256 for {name}') + url = self.assets.get(name) if not url: - raise FeedError(f'the {self.version} release lacks {entry["name"]}') - return url, self.headers, entry.get('size'), entry.get('sha256') + raise FeedError(f'the {self.version} release lacks {name}') + return url, self.headers, size, sha def load(layout, settings): diff --git a/launcher/net.py b/launcher/net.py index c758fbeb..5c228148 100644 --- a/launcher/net.py +++ b/launcher/net.py @@ -88,13 +88,42 @@ def _checks(): return _CHECKS +LOOPBACK = ('127.0.0.1', 'localhost', '::1') + + +def _plain_ok(url): + """Plain HTTP only to this PC (the tests' local servers): anything fetched from elsewhere comes over TLS.""" + return urllib.parse.urlsplit(url).hostname in LOOPBACK + + +class _Redirect(urllib.request.HTTPRedirectHandler): + """Follows a redirect only to https (or to this PC), and drops the Authorization header when the redirect leaves + the host it was meant for: urllib copies every header given to the Request onto the redirected one, and GitHub + sends asset downloads on to its file servers.""" + def redirect_request(self, req, fp, code, msg, headers, newurl): + if urllib.parse.urlsplit(newurl).scheme.lower() != 'https' and not _plain_ok(newurl): + raise urllib.error.HTTPError(newurl, code, f'redirect to {newurl} refused: not https', headers, fp) + new = super().redirect_request(req, fp, code, msg, headers, newurl) + if new is not None and urllib.parse.urlsplit(newurl).hostname != urllib.parse.urlsplit(req.full_url).hostname: + new.remove_header('Authorization') + return new + + +def _open(url, h, timeout, ctx=None): + handlers = [_Redirect()] + ([urllib.request.HTTPSHandler(context=ctx)] if ctx is not None else []) + return urllib.request.build_opener(*handlers).open(urllib.request.Request(url, headers=h), timeout=timeout) + + def request(url, headers=None, timeout=30, version='dev'): """The open response to a GET of url. An https URL is tried with each certificate check in turn, first the one - that accepted the host last, until one accepts the server; CertificateError when none does.""" + that accepted the host last, until one accepts the server; CertificateError when none does. Plain http is + refused but for this PC's own address.""" h = {'User-Agent': user_agent(version)} h.update(headers or {}) - if not url.startswith('https:'): - return urllib.request.urlopen(urllib.request.Request(url, headers=h), timeout=timeout) + if urllib.parse.urlsplit(url).scheme.lower() != 'https': + if not _plain_ok(url): + raise DownloadError(f'{url}: not an https address') + return _open(url, h, timeout) host = urllib.parse.urlsplit(url).hostname or '' checks = _checks() first = min(_ACCEPTED.get(host, 0), len(checks) - 1) @@ -102,7 +131,7 @@ def request(url, headers=None, timeout=30, version='dev'): for i in [first] + [i for i in range(len(checks)) if i != first]: name, ctx = checks[i] try: - r = urllib.request.urlopen(urllib.request.Request(url, headers=h), timeout=timeout, context=ctx) + r = _open(url, h, timeout, ctx) except urllib.error.URLError as e: if not isinstance(e.reason, ssl.SSLCertVerificationError): raise @@ -218,17 +247,22 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head break f.write(b) done += len(b) + if size and done > size: # stop a server that sends more than the file: it would fill the disk + raise DownloadError(f'{url}: more than the expected {size} bytes') if progress: progress(done, total or done) break except (Cancelled, CertificateError): raise + except DownloadError: + part.unlink(missing_ok=True) + raise except urllib.error.HTTPError as e: if e.code == 416 and have: # the part file is complete or longer than the file: start over part.unlink(missing_ok=True) last_error = e continue - if e.code in (401, 403, 404, 410): + if e.code in (401, 403, 404, 410) or 300 <= e.code < 400: # 3xx: a redirect _Redirect refused raise DownloadError(f'{url}: HTTP {e.code}') from e last_error = e except (urllib.error.URLError, OSError, TimeoutError) as e: diff --git a/tests/launcher_test.py b/tests/launcher_test.py index bec2cb71..a349c52f 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -439,6 +439,27 @@ class NetTest(Tmp): with self.assertRaises(net.DownloadError): net.download(srv.url + 'missing.bin', self.tmp / 'out' / 'm.bin') + def test_plain_http_redirects_and_oversize(self): + with self.assertRaises(net.DownloadError): # refused before any connection + net.request('http://example.invalid/f.bin') + (self.tmp / 'f.bin').write_bytes(b'0123456789') + routes = {} + srv = Server(self.tmp, routes) + self.addCleanup(srv.close) + other = srv.url.replace('127.0.0.1', 'localhost') # the same server under another host name + routes['/same'] = (302, {'Location': srv.url + 'f.bin'}, b'') + routes['/away'] = (302, {'Location': other + 'f.bin'}, b'') + token = {'Authorization': 'Bearer secret'} + self.assertEqual(net.get_bytes(srv.url + 'same', token), b'0123456789') + self.assertEqual(srv.hits[-1][1].get('Authorization'), 'Bearer secret') + self.assertEqual(net.get_bytes(srv.url + 'away', token), b'0123456789') + self.assertEqual(srv.hits[-2][1].get('Authorization'), 'Bearer secret') + self.assertIsNone(srv.hits[-1][1].get('Authorization')) # not sent on to another host + dest = self.tmp / 'out' / 'o.bin' + with self.assertRaises(net.DownloadError): # more than the expected size + net.download(srv.url + 'f.bin', dest, size=4) + self.assertFalse(dest.exists() or dest.with_name('o.bin.part').exists()) + def test_server_clock(self): srv = Server(self.tmp) self.addCleanup(srv.close) @@ -536,6 +557,16 @@ def manifest_for(z, ver): class FeedTest(Tmp): + def test_asset_needs_a_plain_name_size_and_sha256(self): + good = {'name': 'a.zip', 'size': 10, 'sha256': '0' * 64} + r = feed.Release({'format': 1, 'version': '1.0.0', 'app': good}, assets={'a.zip': 'https://x/a.zip'}) + self.assertEqual(r.asset('app')[2:], (10, '0' * 64)) + for bad in ({'name': 'a.zip'}, dict(good, sha256='x'), dict(good, size=0), dict(good, size='10'), + dict(good, name='../a.zip'), dict(good, name='C:a.zip'), dict(good, name='sub\\a.zip')): + r = feed.Release({'format': 1, 'version': '1.0.0', 'app': bad}, assets={bad['name']: 'https://x/a'}) + with self.assertRaises(feed.FeedError, msg=repr(bad)): + r.asset('app') + def serve_github(self): self.routes = {} srv = Server(self.tmp, self.routes)