ADR-013 Phase 2: import guard round-4 fix (init-less dir .py sibling)
This commit is contained in:
@@ -167,6 +167,36 @@ def test_planted_sourceless_pyc_refused(guarded):
|
||||
guard.find_spec(f'plugins.{name}.models.evil')
|
||||
|
||||
|
||||
def test_tampered_py_with_sibling_dir_refused(guarded):
|
||||
"""Round 4: a signed foo.py tampered + an empty foo/ dir next to it must be
|
||||
refused. FileFinder would load the file over the init-less dir, so the guard
|
||||
must hash-gate the sibling .py instead of returning None for the dir."""
|
||||
guard, plugins_dir, priv, name = guarded
|
||||
pdir = _write_pkg_plugin(plugins_dir, name)
|
||||
(pdir / 'models' / 'device.py').write_text('MARKER = "signed"\n')
|
||||
_stamp(pdir, priv, name)
|
||||
|
||||
# attacker tampers the signed device.py and adds an empty sibling device/
|
||||
(pdir / 'models' / 'device.py').write_text('MARKER = "evil"\n')
|
||||
(pdir / 'models' / 'device').mkdir()
|
||||
|
||||
with pytest.raises(PluginVerificationError):
|
||||
guard.find_spec(f'plugins.{name}.models.device')
|
||||
|
||||
|
||||
def test_unsigned_py_with_sibling_dir_refused(guarded):
|
||||
"""Symmetric variant: a never-signed evil.py next to evil/ is refused too."""
|
||||
guard, plugins_dir, priv, name = guarded
|
||||
pdir = _write_pkg_plugin(plugins_dir, name)
|
||||
_stamp(pdir, priv, name)
|
||||
|
||||
(pdir / 'models' / 'evil.py').write_text('MARKER = "evil"\n')
|
||||
(pdir / 'models' / 'evil').mkdir()
|
||||
|
||||
with pytest.raises(PluginVerificationError):
|
||||
guard.find_spec(f'plugins.{name}.models.evil')
|
||||
|
||||
|
||||
def test_absent_module_defers(guarded):
|
||||
"""A genuinely-absent module returns None (normal ModuleNotFoundError)."""
|
||||
guard, plugins_dir, priv, name = guarded
|
||||
|
||||
Reference in New Issue
Block a user