mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 14:39:58 -04:00
Refuse plain http and manifests without a size and sha256 in the launcher, stop oversize downloads, and keep the token on its host.
Plain http goes only to this PC (the tests' servers), a redirect to http is refused, the Authorization header is dropped when a redirect leaves its host, a download that runs past its size stops at once, and a manifest file entry needs a plain file name, a size and a sha256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,7 +13,9 @@ The feed (app/<version>/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):
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user