From 41c8413fdadf536e2bdc741d42e8aea5be2eeb0d Mon Sep 17 00:00:00 2001
From: Caleb Connolly <caleb@postmarketos.org>
Date: Mon, 28 Oct 2024 02:33:28 +0100
Subject: [PATCH] helpers: apk: sanity check final command (MR 2463)

When running "apk add" we must always have --cache-dir set! We also
expect --no-interactive to be set.

Add some asserts so we don't regress here.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
---
 pmb/helpers/apk.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/pmb/helpers/apk.py b/pmb/helpers/apk.py
index 4f2d3d608..f72027bb2 100644
--- a/pmb/helpers/apk.py
+++ b/pmb/helpers/apk.py
@@ -205,6 +205,18 @@ def run(command: Sequence[PathString], chroot: Chroot, with_progress: bool = Tru
     """
     _command = _prepare_cmd(command, chroot)
 
+    # Sanity checks. We should avoid accidentally writing to
+    # /var/cache/apk on the host!
+    if "add" in command:
+        if "--no-interactive" not in _command:
+            raise RuntimeError(
+                "Encountered an 'apk add' command without --no-interactive! This is a bug."
+            )
+        if "--cache-dir" not in _command:
+            raise RuntimeError(
+                "Encountered an 'apk add' command without --cache-dir! This is a bug."
+            )
+
     if with_progress:
         _apk_with_progress(_command)
     else:
-- 
GitLab