Fix legacy ebtables sandbox cleanup
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-11-06 23:23:26 -06:00
parent e6812b1c45
commit 591d46a6f9
2 changed files with 21 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import json
import os
import re
import secrets
import shlex
import socket
import subprocess
import sys
@ -101,12 +102,12 @@ def remove_bridge_guard(state):
if not re.fullmatch(r'JS[IF][0-9a-f]{8}', chain):
raise RuntimeError('Invalid sandbox firewall ownership marker')
parent = 'INPUT' if chain.startswith('JSI') else 'FORWARD'
check = subprocess.run(('/sbin/ebtables', '-t', 'filter', '-C', parent,
'-i', TAP, '-j', chain), capture_output=True, text=True, timeout=20)
if check.returncode == 0:
# In legacy ebtables -C changes counters; it is not a rule check.
# Inspect the executable listing before deleting the exact owned hook.
listing = ebt('-L', parent, '--Lx')
rules = [shlex.split(line) for line in listing.splitlines() if line.strip()]
if any(TAP in rule and chain in rule for rule in rules):
ebt('-D', parent, '-i', TAP, '-j', chain)
elif check.returncode != 1:
raise RuntimeError('Could not check sandbox firewall hook: ' + check.stderr.strip()[-300:])
ebt('-F', chain)
ebt('-X', chain)

View File

@ -48,6 +48,21 @@ class WanGuardTest(unittest.TestCase):
with self.assertRaises(RuntimeError):
wan.remove_bridge_guard({'chains': ['FORWARD']})
def test_cleanup_removes_exact_legacy_bridge_hook(self):
listing = 'ebtables -t filter -A FORWARD -i tap9100i0 -j JSF1234abcd\n'
with patch.object(wan, 'ebt', side_effect=[listing, '', '', '']) as ebt:
wan.remove_bridge_guard({'chains': ['JSF1234abcd']})
self.assertEqual([call.args for call in ebt.call_args_list], [
('-L', 'FORWARD', '--Lx'),
('-D', 'FORWARD', '-i', wan.TAP, '-j', 'JSF1234abcd'),
('-F', 'JSF1234abcd'), ('-X', 'JSF1234abcd')])
def test_cleanup_handles_chain_without_attached_hook(self):
with patch.object(wan, 'ebt', side_effect=['', '', '']) as ebt:
wan.remove_bridge_guard({'chains': ['JSI1234abcd']})
self.assertEqual([call.args for call in ebt.call_args_list], [
('-L', 'INPUT', '--Lx'), ('-F', 'JSI1234abcd'), ('-X', 'JSI1234abcd')])
if __name__ == '__main__':
unittest.main()