Skip to content

Commit c97bc86

Browse files
fix: keep checkpoint publication within recovery bounds
1 parent 9e8904b commit c97bc86

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎docs/REFERENCE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ python wordpress_check.py --recover report.html.partial.json --format html --out
301301
python wordpress_check.py --recover report.html.partial.json --format json --output recovered.json
302302
```
303303

304-
Recovery returns `130` for an incomplete checkpoint, even if all targets were checkpointed before final publication. The conservative checkpoint is intentionally retained after success and is not final-output attestation. Keep the final report as the completion receipt. Recovery requires a regular non-symlink checkpoint, validates its structure/counts/result types and reads at most 64 MiB (100,000 result rows). Keep both the original checkpoint and any reconstructed artifact; continuation requires a separately authorized new run of pending targets only. The tool never resumes automatically.
304+
Recovery returns `130` for an incomplete checkpoint, even if all targets were checkpointed before final publication. The conservative checkpoint is intentionally retained after success and is not final-output attestation. Keep the final report as the completion receipt. Runs accept at most 100,000 targets; split larger lists explicitly. Checkpoint publication refuses content beyond the 64 MiB recovery bound and preserves the previous checkpoint, with an error rather than an unrecoverable larger artifact. Recovery requires a regular non-symlink checkpoint, validates its structure/counts/result types and reads at most 64 MiB (100,000 result rows). Keep both the original checkpoint and any reconstructed artifact; continuation requires a separately authorized new run of pending targets only. The tool never resumes automatically.
305305

306306
Without `--output`, results accumulate in memory and stdout receives one well-formed report on completion or a handled interrupt. Stdout/pipes do not provide durable recovery after forced termination; use `--output` when preserving evidence matters. Final output/partial names and report/input paths must differ.
307307

‎test_output_durability.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ def test_success_and_explicit_force_with_conservative_retained_journal(self):
116116
check.assert_not_called()
117117
self.assertEqual(json.loads(target.read_text()), report)
118118

119+
def test_checkpoint_limit_preserves_previous_committed_state(self):
120+
with tempfile.TemporaryDirectory() as directory:
121+
target = Path(directory) / 'limited.json'
122+
initial_size = len(m.checkpoint_json(m.run_report([], 1)).encode('utf-8'))
123+
with patch.object(m, 'MAX_CHECKPOINT_BYTES', initial_size + 1), patch.object(m.Checker, 'check', return_value=m.Result(input_url='one.example')):
124+
self.assertEqual(self.call(['one.example', '--output', str(target)]), 2)
125+
self.assertFalse(target.exists())
126+
report = m.load_checkpoint(Path(str(target) + '.partial.json'))
127+
self.assertEqual((report['completed_targets'], report['pending_targets']), (0, 1))
128+
119129
def test_empty_incomplete_stdout_and_invalid_recovery(self):
120130
with patch.object(m.Checker, 'check', side_effect=KeyboardInterrupt), redirect_stdout(io.StringIO()) as stdout, redirect_stderr(io.StringIO()):
121131
self.assertEqual(m.main(['one.example', '--format', 'json']), 130)

‎wordpress_check.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,17 @@ def esc(value):
634634

635635

636636

637+
MAX_CHECKPOINT_BYTES = 64 * 1024 * 1024
638+
MAX_CHECKPOINT_ROWS = 100000
639+
640+
641+
def checkpoint_json(report):
642+
text = json.dumps(report, ensure_ascii=False)
643+
if len(text.encode("utf-8")) > MAX_CHECKPOINT_BYTES:
644+
raise ValueError("Checkpoint exceeds recovery byte limit; last committed checkpoint retained")
645+
return text
646+
647+
637648
def publish_report(path, text, *, replace=False):
638649
"""Publish one fully fsynced artifact; no-clobber is enforced at publication."""
639650
descriptor, temporary = tempfile.mkstemp(prefix=".php2shell-", dir=path.parent)
@@ -663,15 +674,15 @@ def load_checkpoint(path):
663674
with path.open("rb") as stream:
664675
if not stat.S_ISREG(os.fstat(stream.fileno()).st_mode):
665676
raise ValueError("Recovery requires a regular checkpoint file")
666-
raw = stream.read(64 * 1024 * 1024 + 1)
667-
if len(raw) > 64 * 1024 * 1024:
677+
raw = stream.read(MAX_CHECKPOINT_BYTES + 1)
678+
if len(raw) > MAX_CHECKPOINT_BYTES:
668679
raise ValueError("Checkpoint exceeds 64 MiB recovery limit")
669680
report = json.loads(raw)
670681
if not isinstance(report, dict) or set(report) != {"journal_schema", "complete", "completed_targets", "pending_targets", "total_targets", "results"}:
671682
raise ValueError("Invalid checkpoint fields")
672683
rows = report["results"]
673684
if (report["journal_schema"] != "php2shell-run-v1" or type(report["complete"]) is not bool
674-
or not isinstance(rows, list) or len(rows) > 100000
685+
or not isinstance(rows, list) or len(rows) > MAX_CHECKPOINT_ROWS
675686
or any(type(report[k]) is not int or report[k] < 0 for k in ("completed_targets", "pending_targets", "total_targets"))
676687
or report["completed_targets"] != len(rows)
677688
or report["completed_targets"] + report["pending_targets"] != report["total_targets"]
@@ -742,6 +753,8 @@ def main(argv=None) -> int:
742753
urls = list(dict.fromkeys(urls))
743754
if not urls and not args.recover:
744755
parser.error("provide website URLs or --input websites.txt")
756+
if len(urls) > MAX_CHECKPOINT_ROWS:
757+
parser.error("At most 100,000 targets per run; split inputs explicitly")
745758
if args.recover and (urls or args.input):
746759
parser.error("--recover cannot be combined with targets or --input")
747760
if args.force and not args.output:
@@ -761,14 +774,14 @@ def main(argv=None) -> int:
761774
report = run_report([], len(urls))
762775
if checkpoint:
763776
# Exclusive creation protects a previous interrupted run from accidental reuse.
764-
publish_report(checkpoint, json.dumps(report, ensure_ascii=False))
777+
publish_report(checkpoint, checkpoint_json(report))
765778
checker = Checker(timeout=args.timeout, delay=args.delay, contact=args.contact, authorized_origins=args.authorized_origin)
766779
try:
767780
for index, url in enumerate(urls):
768781
result = checker.check(url)
769782
report = run_report([*report["results"], asdict(result)], len(urls))
770783
if checkpoint:
771-
publish_report(checkpoint, json.dumps(report, ensure_ascii=False), replace=True)
784+
publish_report(checkpoint, checkpoint_json(report), replace=True)
772785
print(f"[{index + 1}/{len(urls)}] {url}: {result.status}", file=sys.stderr)
773786
report["complete"] = True
774787
except KeyboardInterrupt:

0 commit comments

Comments
 (0)