extras carry a manifest; the unpacked tree is checked against it

tools/make-extra.sh writes ./manifest (sha256 of every file). extra_unpack checks the unpacked tree, after the
walk, against it: every plain file listed, nothing else, each with its hash (required for lang, checked for vpn
when present - older vpn extras have none). A downloaded extra's manifest is covered by the release's SHA256SUMS.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DqKKtBQZCfxYnHuBCuDqqn
This commit is contained in:
Abdurrahman Kaan DikeçandClaude Fable 5.1 committed 2026-10-06 13:13:50 +03:00
1 parent 0ed4d71d7f
commit d7a89d6be9
3 files changed
+78 -16

No files matched your search

+18 -2
View File
@@ -392,7 +392,7 @@ VPN_ENGINES="xray hev-socks5-tunnel sing-box"
extra_desc() {
case $1 in
vpn) echo "the VPN engines for mu300-vpn: Xray-core, hev-socks5-tunnel, sing-box (about 40 MB to download, 120 MB installed)" ;;
lang) echo "LuCI (the web interface) in 40 more languages, the MU300 panel in 29 of them; OpenWrt only (about 3 MB to download, 9 MB installed)" ;;
lang) echo "LuCI (the web interface) in 40 more languages, the MU300 panel in 29 of them; OpenWrt only (about 2 MB to download, 6 MB installed)" ;;
esac
}
extra_asset() { echo "mu300-extra-$1.tar.gz"; }
@@ -451,6 +451,19 @@ extra_tree_ok() {
_t=$(find "$2/i18n" -mindepth 1 ! -type f) || return 1
[ -z "$_t" ]
}
# extra_manifest_ok NAME DIR: ./manifest (sha256sum lines, "HASH ./path") lists every plain file of the tree but
# itself, and each one has that hash. The lang extra must carry one; for the others it is checked when there is one
# (extras from before it have none). With a download the manifest is covered by the release's SHA256SUMS, which
# the whole tarball was checked against.
extra_manifest_ok() {
_m=$2/manifest
[ -f "$_m" ] || { [ "$1" != lang ]; return; }
! LC_ALL=C grep -Evxq -- '[0-9a-f]{64} \./[A-Za-z0-9._+/-]+' "$_m" || return 1
_want=$(cut -c67- "$_m" | LC_ALL=C sort) || return 1
_have=$(cd "$2" && find . -type f ! -path ./manifest | LC_ALL=C sort) || return 1
[ -n "$_want" ] && [ "$_want" = "$_have" ] || return 1
(cd "$2" && sha256sum -c manifest >/dev/null 2>&1)
}
# extra_size_ok NAME TARBALL: unpacked, it fits (a small archive must not fill the Linux partition every system
# lives on): at most 32 MB and 1000 entries for lang, 600 MB for the others. Counted by decompressing, which every
# gzip does alike, not from a listing format.
@@ -461,7 +474,7 @@ extra_size_ok() {
[ "$1" != lang ] || [ "$(untar -tzf "$2" | wc -l)" -le 1000 ]
}
# lang_tar_ok TARBALL: the lang extra holds only its four files and catalogs
LANG_ENTRY='\.|\./|\./(name|release|components|languages|i18n/?|i18n/[a-z0-9-]+\.[a-z]{2,3}(-[a-z]{2})?\.lmo)'
LANG_ENTRY='\.|\./|\./(name|release|components|languages|manifest|i18n/?|i18n/[a-z0-9-]+\.[a-z]{2,3}(-[a-z]{2})?\.lmo)'
lang_tar_ok() {
_l=$(untar -tzf "$1") || return 1
[ -n "$_l" ] && ! printf '%s\n' "$_l" | LC_ALL=C grep -Evxq -- "$LANG_ENTRY"
@@ -500,6 +513,9 @@ extra_unpack() {
if ! extra_tree_ok "$1" "$_new"; then
rm -rf "$_new"; echo "extra $1: $2 unpacked to links or unexpected files; not installed" >&2; return 1
fi
if ! extra_manifest_ok "$1" "$_new"; then
rm -rf "$_new"; echo "extra $1: $2 does not match its manifest; not installed" >&2; return 1
fi
# modes are ours too: the disk is shared with Ubuntu's users and mounted without nosuid. Catalogs are plain
# read-only files; programs keep their execute bits, never set-id or writable by others.
chown -R 0:0 "$_new" 2>/dev/null
+46 -12
View File
@@ -329,9 +329,18 @@ LANGS = {'de': 'Deutsch (German)', 'pt_br': 'Português do Brasil (Brazilian Por
LMOS = ('base.de.lmo', 'firewall.de.lmo', 'mu300.de.lmo', 'base.pt-br.lmo', 'base.ja.lmo', 'mu300.ja.lmo')
def lang_tarball(path, release='v2026.10.10', langs=None, lmos=LMOS, extra=(), lines=None):
"""the lang extra: ./name ./release ./components ./languages ./i18n/*.lmo; EXTRA: (name, data, type) more"""
def lang_tarball(path, release='v2026.10.10', langs=None, lmos=LMOS, extra=(), lines=None, manifest=True,
tamper=None):
"""the lang extra: ./name ./release ./components ./languages ./manifest ./i18n/*.lmo; EXTRA: (name, data, type)
more; TAMPER: a file whose bytes differ from what the manifest says"""
langs = LANGS if langs is None else langs
text = lines if lines is not None else ''.join(f'{k}\t{v}\n' for k, v in langs.items())
files = {'./name': b'lang\n', './release': (release + '\n').encode(), './components': b'luci-i18n-base-de 26.275\n',
'./languages': text.encode()}
files.update({f'./i18n/{n}': f'lmo {n} {release}'.encode() for n in lmos})
man = ''.join(f'{hashlib.sha256(d).hexdigest()} {n}\n' for n, d in sorted(files.items()))
if tamper:
files[tamper] = b'other bytes'
with tarfile.open(path, 'w:gz') as t:
def add(fn, data=b'', typ=tarfile.REGTYPE, link=''):
ti = tarfile.TarInfo(fn)
@@ -339,14 +348,13 @@ def lang_tarball(path, release='v2026.10.10', langs=None, lmos=LMOS, extra=(), l
ti.size = len(data) if typ == tarfile.REGTYPE else 0
ti.mode = 0o755 if typ == tarfile.DIRTYPE else 0o644
t.addfile(ti, io.BytesIO(data) if typ == tarfile.REGTYPE else None)
add('./name', b'lang\n')
add('./release', (release + '\n').encode())
add('./components', b'luci-i18n-base-de 26.275\n')
text = lines if lines is not None else ''.join(f'{k}\t{v}\n' for k, v in langs.items())
add('./languages', text.encode())
for n in ('./name', './release', './components', './languages'):
add(n, files[n])
if manifest:
add('./manifest', man.encode())
add('./i18n', typ=tarfile.DIRTYPE)
for n in lmos:
add(f'./i18n/{n}', f'lmo {n} {release}'.encode())
add(f'./i18n/{n}', files[f'./i18n/{n}'])
for e in extra:
add(*e)
return path
@@ -448,6 +456,9 @@ class LangExtra(ExtrasBase):
('ampersand in a name', dict(lines='de\tA &amp; B\n')),
('markup as the release', dict(release='<img src=x onerror=alert(1)>')),
('a catalog that is a directory', dict(extra=[('./i18n/base.fr.lmo', b'', tarfile.DIRTYPE)])),
('a catalog the manifest does not list', dict(extra=[('./i18n/base.fr.lmo', b'x')])),
('a catalog with other bytes than the manifest says', dict(tamper='./i18n/mu300.de.lmo')),
('no manifest', dict(manifest=False)),
]
for shell in self.each_shell():
self.clean(shell)
@@ -489,6 +500,26 @@ class LangExtra(ExtrasBase):
self.assertEqual(list(out.iterdir()), [], (shell, name))
self.assertFalse((self.disk / 'extra' / name).exists(), (shell, name))
def test_a_vpn_extra_with_a_manifest_is_held_to_it(self):
# the vpn extra's manifest (tools/make-extra.sh writes one) is checked as well; one without (older releases)
# is still taken
for shell in self.each_shell():
self.clean(shell)
for manifest, ok in (('good', True), ('bad', False), (None, True)):
f = self.tmp / 'vpn.tar.gz'
files = {'./name': b'vpn\n', './release': b'v1\n', './bin/xray': b'#!/bin/sh\n'}
with tarfile.open(f, 'w:gz') as t:
for n, d in list(files.items()) + ([('./manifest', ''.join(
f'{hashlib.sha256(d if manifest == "good" else b"x").hexdigest()} {n}\n'
for n, d in sorted(files.items())).encode())] if manifest else []):
ti = tarfile.TarInfo(n)
ti.size, ti.mode = len(d), 0o755
t.addfile(ti, io.BytesIO(d))
self.up(shell, f'rm -rf "{self.disk}/extra/vpn"')
r = self.up(shell, f'extra_unpack vpn "{f}"; echo "rc=$?"')
self.assertIn('rc=0' if ok else 'rc=1', r.stdout, (shell, manifest, r.stderr))
self.assertEqual((self.disk / 'extra' / 'vpn').exists(), ok, (shell, manifest))
def test_a_pack_that_unpacks_too_large_is_refused(self):
# 40 MB of zeros compress to about 40 kB: refused before anything is unpacked
for shell in self.each_shell():
@@ -507,9 +538,11 @@ class LangExtra(ExtrasBase):
self.clean(shell)
f = self.tmp / 'modes.tar.gz'
with tarfile.open(f, 'w:gz') as t:
for fn, data, mode in (('./name', b'lang\n', 0o666), ('./release', b'v1\n', 0o644),
('./languages', b'de\tDeutsch (German)\n', 0o666), ('./i18n', None, 0o777),
('./i18n/base.de.lmo', b'x', 0o4777)):
members = [('./name', b'lang\n', 0o666), ('./release', b'v1\n', 0o644),
('./languages', b'de\tDeutsch (German)\n', 0o666), ('./i18n', None, 0o777),
('./i18n/base.de.lmo', b'x', 0o4777)]
man = ''.join(f'{hashlib.sha256(d).hexdigest()} {n}\n' for n, d, _ in members if d is not None)
for fn, data, mode in members + [('./manifest', man.encode(), 0o666)]:
ti = tarfile.TarInfo(fn)
ti.mode = mode
if data is None:
@@ -519,7 +552,8 @@ class LangExtra(ExtrasBase):
t.addfile(ti, io.BytesIO(data) if data is not None else None)
r = self.up(shell, f'extra_unpack lang "{f}"; echo "rc=$?"')
self.assertIn('rc=0', r.stdout, (shell, r.stderr))
for rel, want in (('name', 0o644), ('languages', 0o644), ('i18n', 0o755), ('i18n/base.de.lmo', 0o644)):
for rel, want in (('name', 0o644), ('languages', 0o644), ('manifest', 0o644), ('i18n', 0o755),
('i18n/base.de.lmo', 0o644)):
self.assertEqual(oct((self.lang() / rel).stat().st_mode & 0o7777), oct(want), (shell, rel))
def test_the_tarball_is_read_once(self):
+14 -2
View File
@@ -7,7 +7,8 @@
# LuCI app of the images, from the luci-i18n-* packages (apk add in the OpenWrt base image that
# openwrt/build-rootfs.sh imports, so apk checks every package against the feed's signed index), plus the
# MU300 panel's catalogs (openwrt/luci-app-mu300/po, tools/po2lmo.py). Needs docker with arm64 support.
# Layout (what mu300-update's extra_unpack checks): ./name ./release ./components, then ./bin/<programs>, or for lang
# Layout (what mu300-update's extra_unpack checks): ./name ./release ./components ./manifest (sha256 of every file),
# then ./bin/<programs>, or for lang
# ./languages (uci key, tab, name) and ./i18n/<component>.<code>.lmo; owned by root.
set -eu
NAME=${1:?usage: tools/make-extra.sh NAME OUT.tar.gz [TAG]}
@@ -103,8 +104,19 @@ def root(ti):
ti.uid = ti.gid = 0
ti.uname = ti.gname = 'root'
return ti
# ./manifest: the sha256 of every file, as sha256sum writes it (mu300-update checks the unpacked tree against it)
import hashlib
lines = []
for d, _, fs in os.walk(src):
for f in fs:
path = os.path.join(d, f)
rel = './' + os.path.relpath(path, src)
if rel != './manifest':
lines.append('%s %s\n' % (hashlib.sha256(open(path, 'rb').read()).hexdigest(), rel))
with open(os.path.join(src, 'manifest'), 'w') as m:
m.writelines(sorted(lines))
with tarfile.open(out, 'w:gz', format=tarfile.GNU_FORMAT) as t:
for name in ('name', 'release', 'components', 'languages', 'bin', 'i18n'):
for name in ('name', 'release', 'components', 'languages', 'manifest', 'bin', 'i18n'):
if os.path.exists(os.path.join(src, name)):
t.add(os.path.join(src, name), arcname='./' + name, filter=root)
PY