Skip to content

Commit 8ea64fd

Browse files
authored
feat(update): enforce the min_supported floor in v1 (#130)
* feat(update): enforce the min_supported floor in v1 The manifest has always carried min_supported and nothing read it: it was parsed into Manifest and never consulted, and the min_supported_baseline file baked into greffer-updater has no reader in cli/ at all. Per the trust model the floor exists to "block below-floor reintroduction" -- an update must never land the node on a release withdrawn as known-bad. v1 was meant to have it: hld-v2-per-container-recreate section 7 says v1 "reads the manifest over HTTPS for the downgrade floor". It never did. v2 is deliberately untouched. The :latest updater drops the floor BY CONSTRUCTION (section 11), replacing it with verify-then-pull plus a pre-run no-downgrade refusal. Adding it there would reverse a recorded architectural decision, so this is v1-only. Semantics, distinct from the downgrade guard added in #129: is_downgrade is about DIRECTION relative to what is running; below_floor is about an absolute bound. 0.9 -> 0.8 is a downgrade that may still be above the floor, and a fresh install of a below-floor version is not a downgrade at all yet is still refused. NOT overridable by --to. A downgrade is a legitimate operator choice; a below-floor release is withdrawn, so an explicit request for one is exactly what this refuses. --check reports it too. Also fixes a live crash in version_tuple, which #129 already merged. str.isdigit() is not enough to make int() safe on manifest-supplied text: it is True for non-ASCII digits ('²'.isdigit() is True, int('²') raises) and for digit runs past CPython's 4300-char int-string limit. Either turned a malformed manifest into a traceback out of `greffer update`. Now requires plain ASCII digits with a bounded length. Found by Codex. Honest scope: v1 fetches the manifest over HTTPS but does not verify its signature, and there is no /data ratchet, so a hostile or rolled-back origin can serve a LOWER floor. This is a safety guard against landing on a known-bad release, not a security control against a hostile origin. The signed-manifest + ratchet half (HLD section 11) remains unbuilt and is stated as such in the docstring. 294 tests pass (21 added). Both the floor tests and the hostile-input tests were verified to FAIL with their fix removed. * docs(update): state that the floor is tag-based, not content-based Codex flagged that below_floor() compares the requested tag, so a tag moved or mispublished to below-floor content passes. Correct, and left unimplemented on purpose. Closing it needs the pulled image's version label, which v1 has no machinery to read, and that label is forgeable (v2 trust model section 7) so it would not close the case against a hostile registry either. v1 does not cosign-verify images at all and is operator-trust by design, so tag-trust is already its baseline. Documenting rather than silently leaving the reader to assume the floor is content-based.
1 parent 8a885ee commit 8ea64fd

5 files changed

Lines changed: 206 additions & 4 deletions

File tree

‎app/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# per-greffon ``min_greffer_version`` compatibility gate can refuse deploying an
33
# L4 (or any version-floored) greffon onto a greffer too old to run it. Keep in
44
# sync with pyproject.toml. Single source for the register report.
5-
__version__ = "0.10.0"
5+
__version__ = "0.11.0"

‎cli/greffer_cli/strings.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@
332332
"unreachable).\n → re-run with `--confirm-no-rollback` to proceed anyway."
333333
)
334334

335+
UPDATE_REFUSED_BELOW_FLOOR = (
336+
"✗ Refusing to install {target}: it is below the supported floor "
337+
"{min_supported}.\n"
338+
" Releases under the floor are withdrawn as known-bad, so the update would\n"
339+
" put this node back onto one. This refusal is not overridable by `--to`.\n"
340+
" → pick {min_supported} or newer (`greffer update --check` shows latest)."
341+
)
342+
335343
UPDATE_REFUSED_DOWNGRADE = (
336344
"✗ Refusing to downgrade: {current} is NEWER than the target {target}.\n"
337345
" The version manifest advertises {target} as latest, which means it is\n"

‎cli/greffer_cli/update.py‎

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,15 @@ def version_tuple(value: str | None) -> tuple[int, ...] | None:
175175
if not isinstance(value, str) or not value:
176176
return None
177177
parts = value.split('.')
178-
if not all(p.isdigit() for p in parts):
178+
# isdigit() alone is NOT enough to make int() safe on manifest-supplied
179+
# text. It is True for non-ASCII digit characters ('²'.isdigit() is True
180+
# but int('²') raises ValueError), and for arbitrarily long runs of digits
181+
# int() raises once past CPython's integer-string limit (4300 by default).
182+
# Either would turn a malformed manifest into a traceback out of `greffer
183+
# update` / `--check`, so require plain ASCII digits and bound the length.
184+
# 18 digits is far beyond any real version component (even date-shaped ones
185+
# are 8) while staying comfortably inside the limit.
186+
if not all(p.isascii() and p.isdigit() and 0 < len(p) <= 18 for p in parts):
179187
return None
180188
return tuple(int(p) for p in parts)
181189

@@ -199,6 +207,38 @@ def is_downgrade(current: str | None, target: str | None) -> bool | None:
199207
return tgt < cur
200208

201209

210+
def below_floor(target: str | None, min_supported: str | None) -> bool | None:
211+
"""Is ``target`` below the manifest's ``min_supported`` floor?
212+
213+
``True`` = strictly below the floor -> a known-vulnerable release.
214+
``False`` = at or above the floor.
215+
``None`` = no claim (floor absent, or either side not dotted-numeric).
216+
217+
Per the trust model the floor exists to "block below-floor reintroduction",
218+
i.e. an update must never put the node back onto a release the project has
219+
declared unsupported. Distinct from is_downgrade(), which is about DIRECTION
220+
relative to what is running: 0.9 -> 0.8 is a downgrade but may still be above
221+
the floor, and a fresh install of a below-floor version is not a downgrade at
222+
all yet is still refused here.
223+
224+
Honest scope: v1 fetches the manifest over HTTPS but does NOT verify its
225+
signature, and there is no /data ratchet, so a hostile or rolled-back origin
226+
can serve a LOWER floor. This is a safety guard against accidentally landing
227+
on a known-bad release, not a security control against a hostile origin. The
228+
signed-manifest + ratchet half of the design (HLD section 11) is not built.
229+
"""
230+
if not min_supported:
231+
return None
232+
tgt = version_tuple(target)
233+
flr = version_tuple(min_supported)
234+
if tgt is None or flr is None:
235+
return None
236+
width = max(len(tgt), len(flr))
237+
tgt = tgt + (0,) * (width - len(tgt))
238+
flr = flr + (0,) * (width - len(flr))
239+
return tgt < flr
240+
241+
202242
def no_rollback_blocked(
203243
manifest: Manifest | None, current: str | None, target: str,
204244
) -> bool | None:
@@ -528,7 +568,9 @@ def run_update(
528568
# Report the DIRECTION, not merely "different". A stale manifest made
529569
# an older `latest` read as "available: yes", which is how a downgrade
530570
# looked like a routine update.
531-
if downgrade:
571+
if below_floor(resolved, manifest.min_supported if manifest else None):
572+
available = f"no — target is BELOW the supported floor {manifest.min_supported}"
573+
elif downgrade:
532574
available = "no — target is OLDER than current"
533575
else:
534576
available = (
@@ -554,6 +596,38 @@ def run_update(
554596
print(strings.UPDATE_PREFLIGHT_NO_DATA_VOLUME, file=sys.stderr)
555597
return EXIT_PREFLIGHT_REFUSED
556598

599+
# MIN_SUPPORTED FLOOR. The manifest carries a floor and, until now, nothing
600+
# read it: it was parsed into Manifest and never consulted, and the
601+
# min_supported_baseline file baked into the updater image has no reader in
602+
# cli/ at all. Per the trust model the floor exists to "block below-floor
603+
# reintroduction" -- an update must never land the node on a release the
604+
# project has withdrawn as known-bad.
605+
#
606+
# Checked BEFORE the ordering guard so a below-floor target reports the
607+
# stronger reason, and NOT overridable by `--to`: unlike a downgrade (a
608+
# legitimate operator choice) a below-floor release is withdrawn, so an
609+
# explicit request for one is exactly what this refuses.
610+
#
611+
# v2 is deliberately out of scope. The :latest updater drops the floor by
612+
# construction (hld-v2-per-container-recreate section 11), replacing it with
613+
# verify-then-pull + a pre-run no-downgrade refusal; adding it there would
614+
# reverse a recorded decision.
615+
#
616+
# SCOPE, so nobody reads this as stronger than it is: the floor is checked
617+
# against the requested TAG, not against the version of the bytes that end
618+
# up running. A tag moved or mispublished to below-floor content passes.
619+
# Closing that needs the pulled image's version label, which v1 has no
620+
# machinery to read -- and would not close it against a hostile registry
621+
# anyway, since that label is forgeable (v2 trust model section 7). v1 does
622+
# not cosign-verify images at all and is operator-trust by design, so
623+
# tag-trust is already its baseline; this guard raises the floor on honest
624+
# mistakes, not on a compromised registry.
625+
if below_floor(resolved, manifest.min_supported if manifest else None):
626+
print(strings.UPDATE_REFUSED_BELOW_FLOOR.format(
627+
target=resolved, min_supported=manifest.min_supported,
628+
), file=sys.stderr)
629+
return EXIT_PREFLIGHT_REFUSED
630+
557631
# ORDERING GUARD. resolve_target() takes the manifest's `latest` at face
558632
# value with no comparison against what is running, so a manifest that
559633
# lags the fleet silently rolls nodes BACKWARDS. This is not theoretical:

‎cli/tests/test_update.py‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,123 @@ def racing_version(_f):
852852
# Refused before the compose was rewritten / pulled.
853853
assert fake.running_image != fake.target_image
854854
assert len(seen) >= 2, "expected a re-read under the lock"
855+
856+
857+
# --- min_supported floor ---------------------------------------------
858+
#
859+
# The manifest has always carried min_supported and nothing read it. Per the
860+
# trust model the floor "blocks below-floor reintroduction": an update must
861+
# never land the node on a release withdrawn as known-bad.
862+
863+
@pytest.mark.parametrize(
864+
("target", "floor", "expected"),
865+
[
866+
("0.2.0", "0.3.0", True),
867+
("0.3.0", "0.3.0", False), # at the floor is allowed
868+
("0.9.0", "0.3.0", False),
869+
("0.3", "0.3.0", False), # zero-padding
870+
("0.9.0", "0.10.0", True), # numeric, not lexical
871+
("0.2.0", None, None), # no floor -> no claim
872+
("latest", "0.3.0", None), # unorderable -> no claim
873+
],
874+
)
875+
def test_below_floor(target, floor, expected) -> None:
876+
assert update.below_floor(target, floor) is expected
877+
878+
879+
def _manifest_full(latest: str, min_supported: str | None):
880+
return lambda url, timeout=10.0: update.Manifest(
881+
latest=latest, min_supported=min_supported,
882+
)
883+
884+
885+
def test_auto_latest_refuses_a_below_floor_target(
886+
cfg: Path, monkeypatch: pytest.MonkeyPatch, capsys,
887+
) -> None:
888+
fake = FakeDocker()
889+
fake.install(monkeypatch)
890+
monkeypatch.setattr(update, "fetch_manifest", _manifest_full("0.2.0", "0.3.0"))
891+
monkeypatch.setattr(compose, "exec_greffer_version", lambda f: "0.1.0")
892+
893+
rc = _run(cfg)
894+
895+
assert rc == update.EXIT_PREFLIGHT_REFUSED
896+
assert "below the supported floor" in capsys.readouterr().err
897+
assert fake.running_image != fake.target_image
898+
899+
900+
def test_explicit_to_cannot_override_the_floor(
901+
cfg: Path, monkeypatch: pytest.MonkeyPatch, capsys,
902+
) -> None:
903+
"""Unlike a downgrade, a below-floor release is withdrawn: --to must NOT work."""
904+
FakeDocker().install(monkeypatch)
905+
monkeypatch.setattr(update, "fetch_manifest", _manifest_full("0.9.0", "0.3.0"))
906+
monkeypatch.setattr(compose, "exec_greffer_version", lambda f: "0.9.0")
907+
908+
rc = _run(cfg, target="0.2.0", confirm_no_rollback=True)
909+
910+
assert rc == update.EXIT_PREFLIGHT_REFUSED
911+
assert "below the supported floor" in capsys.readouterr().err
912+
913+
914+
def test_at_the_floor_is_allowed(
915+
cfg: Path, monkeypatch: pytest.MonkeyPatch,
916+
) -> None:
917+
FakeDocker().install(monkeypatch)
918+
monkeypatch.setattr(update, "fetch_manifest", _manifest_full("0.3.4", "0.3.4"))
919+
monkeypatch.setattr(compose, "exec_greffer_version", lambda f: "0.3.0")
920+
921+
assert _run(cfg) == update.EXIT_OK
922+
923+
924+
def test_absent_floor_does_not_block(
925+
cfg: Path, monkeypatch: pytest.MonkeyPatch,
926+
) -> None:
927+
"""A manifest without min_supported must not wedge updates shut."""
928+
FakeDocker().install(monkeypatch)
929+
monkeypatch.setattr(update, "fetch_manifest", _manifest_full("0.3.4", None))
930+
monkeypatch.setattr(compose, "exec_greffer_version", lambda f: "0.3.0")
931+
932+
assert _run(cfg) == update.EXIT_OK
933+
934+
935+
def test_check_reports_below_floor(
936+
cfg: Path, monkeypatch: pytest.MonkeyPatch, capsys,
937+
) -> None:
938+
FakeDocker().install(monkeypatch)
939+
monkeypatch.setattr(update, "fetch_manifest", _manifest_full("0.2.0", "0.3.0"))
940+
monkeypatch.setattr(compose, "exec_greffer_version", lambda f: "0.1.0")
941+
942+
assert _run(cfg, check_only=True) == update.EXIT_OK
943+
assert "BELOW the supported floor" in capsys.readouterr().out
944+
945+
946+
@pytest.mark.parametrize(
947+
"value",
948+
[
949+
"².0.0", # superscript two: isdigit() True, int() raises
950+
"0.².0",
951+
"٠.0.0", # arabic-indic zero: isdigit() True, int() raises
952+
"9" * 5000, # past CPython's int-string conversion limit
953+
"1." + "9" * 5000,
954+
"1..2", # empty component
955+
"",
956+
".",
957+
],
958+
)
959+
def test_version_tuple_never_raises_on_hostile_input(value) -> None:
960+
"""The manifest is fetched over HTTPS from an external origin, so a
961+
malformed version must return None, never crash the CLI."""
962+
assert update.version_tuple(value) is None
963+
964+
965+
def test_hostile_floor_does_not_crash_the_update(
966+
cfg: Path, monkeypatch: pytest.MonkeyPatch,
967+
) -> None:
968+
FakeDocker().install(monkeypatch)
969+
monkeypatch.setattr(
970+
update, "fetch_manifest", _manifest_full("0.3.4", "².0.0"),
971+
)
972+
monkeypatch.setattr(compose, "exec_greffer_version", lambda f: "0.3.0")
973+
# An unorderable floor makes no claim, so the update proceeds normally.
974+
assert _run(cfg) == update.EXIT_OK

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "greffer"
3-
version = "0.10.0"
3+
version = "0.11.0"
44
description = ""
55
authors = ["Greffon <admin@greffon.io>"]
66
license = "AGPL-3.0-only"

0 commit comments

Comments
 (0)