lib.PathFilter / lib.pf: Add class + module
Add the abstract PathFilter class, designed to be attached to a FileContext in later commits. This is the first commit of a series that generalizes the JW_DEFAULT_PKG_FILTER / --pkg-filter framework into loadable path filters that can be attached to any FileContext. A filter answers an access check, _access(), with a bool. run() turns the answer into a verdict: ALLOW when the check passes, otherwise ABORT or IGNORE according to the filter's deny action, LOG or ABORT. The verdict is handled by the filter's handle, a pair of per-verdict gates: with the ABORT gate set, an ABORT verdict raises PermissionError; with the LOG gate set, an IGNORE verdict is logged and returned. A verdict whose gate is not set passes through unhandled. Also add the lib.pf module that hosts the concrete filters. A filter spec of the form foo:something is loaded by PathFilter.create() as the Foo class from the jw.pkg.lib.pf.Foo module. A spec without the name prefix, or a name that no module provides, is rejected with an error that names the expected class and module. Signed-off-by: Jan Lindemann <jan@janware.com> Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
This commit is contained in:
parent
bed3e90169
commit
013636fa38
2 changed files with 201 additions and 0 deletions
8
test/unit/python/jw/pkg/lib/PathFilter/Makefile
Normal file
8
test/unit/python/jw/pkg/lib/PathFilter/Makefile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
TOPDIR = ../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-run.mk
|
||||
|
||||
all:
|
||||
|
||||
test: run
|
||||
94
test/unit/python/jw/pkg/lib/PathFilter/test.py
Normal file
94
test/unit/python/jw/pkg/lib/PathFilter/test.py
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import asyncio
|
||||
import io
|
||||
import os
|
||||
|
||||
from contextlib import redirect_stderr
|
||||
from typing import override
|
||||
|
||||
from jw.pkg.lib.PathFilter import DenyAction, Handle, PathFilter, Verdict
|
||||
|
||||
class ConstFilter(PathFilter):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
allow: bool,
|
||||
deny_action: DenyAction = DenyAction.LOG,
|
||||
handle: Handle = Handle.LOG | Handle.ABORT,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
spec = 'const:test',
|
||||
deny_action = deny_action,
|
||||
handle = handle,
|
||||
)
|
||||
self.__allow = allow
|
||||
|
||||
@override
|
||||
async def _access(self, path: str, mode: int = os.F_OK) -> bool:
|
||||
return self.__allow
|
||||
|
||||
async def run_tests() -> None:
|
||||
# -- An allowed address passes through, unhandled
|
||||
assert await ConstFilter(True).run('p') is Verdict.ALLOW
|
||||
|
||||
# -- Default handle: a LOG denial is returned and logged, an ABORT
|
||||
# denial raises
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
assert await ConstFilter(
|
||||
False, deny_action = DenyAction.LOG
|
||||
).run('p') is Verdict.IGNORE
|
||||
assert 'is filtered, ignoring' in buf.getvalue()
|
||||
try:
|
||||
await ConstFilter(False, deny_action = DenyAction.ABORT).run('p')
|
||||
raise AssertionError('default handle: ABORT should raise')
|
||||
except PermissionError:
|
||||
pass
|
||||
|
||||
# -- ABORT only: a LOG denial passes through without logging, an
|
||||
# ABORT denial raises
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
assert await ConstFilter(
|
||||
False,
|
||||
deny_action = DenyAction.LOG,
|
||||
handle = Handle.ABORT,
|
||||
).run('p') is Verdict.IGNORE
|
||||
assert 'filtered' not in buf.getvalue()
|
||||
try:
|
||||
await ConstFilter(
|
||||
False,
|
||||
deny_action = DenyAction.ABORT,
|
||||
handle = Handle.ABORT,
|
||||
).run('p')
|
||||
raise AssertionError('ABORT handle: ABORT should raise')
|
||||
except PermissionError:
|
||||
pass
|
||||
|
||||
# -- LOG only: both denials are returned unraised; the LOG denial is
|
||||
# logged with the ignoring suffix, the ABORT denial is not logged
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
assert await ConstFilter(
|
||||
False,
|
||||
deny_action = DenyAction.LOG,
|
||||
handle = Handle.LOG,
|
||||
).run('p') is Verdict.IGNORE
|
||||
assert 'is filtered, ignoring' in buf.getvalue()
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
assert await ConstFilter(
|
||||
False,
|
||||
deny_action = DenyAction.ABORT,
|
||||
handle = Handle.LOG,
|
||||
).run('p') is Verdict.ABORT
|
||||
assert 'is filtered' not in buf.getvalue()
|
||||
|
||||
# -- No handle: denials pass through, unhandled
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
assert await ConstFilter(
|
||||
False, deny_action = DenyAction.ABORT, handle = Handle(0)
|
||||
).run('p') is Verdict.ABORT
|
||||
assert 'filtered' not in buf.getvalue()
|
||||
|
||||
asyncio.run(run_tests())
|
||||
Loading…
Reference in a new issue