lib.PathFilter: Generalize the package filter into path filters #170

Merged
Jan Lindemann merged 12 commits from jan/feature/20261107-lib-execapp-rename-pkg-filter-to-path-filter into master 2026-11-07 19:52:11 +01:00 AGit
Owner

This PR generalizes the JW_DEFAULT_PKG_FILTER / --pkg-filter framework into loadable path filters that can be attached to any FileContext. The series adds the PathFilter base class and the lib.pf module for concrete filters, teaches FileContext to check path actions against an attached filter, and threads the filter through copies and package selection. The existing package filter becomes the first loadable filter, and the CLI option and environment variable are renamed from --pkg-filter to --path-filter.

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.

lib.FileContext: Limit accessible addresses

Allow to attach a default path filter to a FileContext, given in the constructor as default_path_filter, or in the URI query as path-filter. Passing False as default_path_filter will make the FileContext ignore any path-filter in the query.

The filter is created lazily on first use through PathFilter.create(), so a string spec is loaded from the lib.pf module like any other filter.

Every path based action evaluates the filter before it is carried out. An IGNORE verdict is logged and the action is skipped: reads and stats raise FileNotFoundError, writes, unlinks, erases, renames, and mkdirs are no-ops, and readdir() drops the filtered entries. An ABORT verdict raises PermissionError.

Every path-driven method also takes a flag parameter, filtered, defaulting to True, so a caller that must work regardless of the context's filter can opt out of the check.

Add a unit test that exercises the verdicts, the filtered bypass, and the distribution probe on a Local context.

lib.CopyContext: Add optional default_path_filter

CopyContext creates its src and dst contexts from URIs when they are not given as instances. Pass an optional default_path_filter into that creation, so a copy made under a filter limits the addresses it reads and writes. A context given as an instance keeps the filter it already carries.

lib.util.copy(): Add optional default_path_filter

Hand the new default_path_filter parameter to the CopyContext copy() opens, and to the recursive calls it makes for a list of URIs, so an app can limit the addresses a copy touches the same way it limits its own contexts.

lib.PackageFilter: Derive from PathFilter

This commit moves package-based filtering from the package level to the path level, using the new path filter framework. Derive PackageFilter from a new FileContextFilter base, which binds a PathFilter to the FileContext it guards, and implements _access().

The filter's space is the file list of the packages it selects, taken from the backing distro's pkg_files(). An address is allowed only if it names one of those files.

The backing distro is taken from the file context, if the context carries one, and is otherwise autodetected from the host the context executes on, so the filter also works on a bare exec context. A context that is not an exec context and carries no distro rejects path queries with an error.

The filter string now follows the loader syntax, pkg:url=~ regex, and a bare url=~regex like the old url=~jw.dev form is rejected; update JW_DEFAULT_PKG_FILTER values accordingly.

PackageFilter's match() is renamed match_pkg(), to make clear that it matches packages, and Distro._select() calls the new name.

ExecApp.path_filter builds the filter through PathFilter.create() and binds it to the exec context. CmdSelect passes its argument to the new constructor, CmdCopy hands the app's filter to the copy, and the App distro picks it up as the default package filter.

Extend the unit test to cover the space membership and the distro lookup.

lib.pf.Pkg: Add module

Add the Pkg path filter to the lib.pf module, the loader-visible home of the package filter spec. A spec of the form pkg:criterion=~regex instantiates Pkg through PathFilter.create(). The parsing duplicates the one of PackageFilterString for now; a later commit consolidates the two.

lib.Distro: Widen the package filter to PathFilter

The default package filter is typed PackageFilter, but it is handed over from file contexts that carry a PathFilter. Widen the default_pkg_filter parameter.

cmds.pkg.CmdSelect: Load path filter dynamically

The select command names PackageFilterString directly, but the filter is a spec string and the class is resolved dynamically by the loader. Build the filter through PathFilter.create() so the concrete class no longer appears at the call site.

lib.ExecApp: Move .path_filter into the app's ExecContext

Create an app's exec context with the user-specified path filter, and drop the ExecApp.path_filter property, now a duplicate of exec_context.path_filter. Use the latter in the two call sites.

lib.pf.Pkg: Consolidate PackageFilter, drop PackageFilterString

PackageFilterString and Pkg are duplicates. The PackageFilter and FileContextFilter base classes live in lib/PackageFilter.py, imported by Pkg, Distro, and the tests.

Move the two base classes into lib/pf/Pkg.py (where Pkg already lives), drop PackageFilterString, delete lib/PackageFilter.py, and repoint Distro and the tests to the new location. Move the test suite with the module to test/.../pf/Pkg/.

lib.ec.secret_store.KvFile: Init filter in FC-style

Drop the re-read of JW_DEFAULT_PKG_FILTER, and derive the filter from the ctor and URI like all other FileContext instances would. Use that to filter the backend for the packages provinding secrets.

lib.ExecApp: Rename --pkg-filter to --path-filter

The exec context's default filter selects paths, not just packages, so the pkg spelling of the option and environment variable that set it no longer fit. Rename both and some variables accordingly:

  • ExecApp: --pkg-filter becomes --path-filter, and the fallback environment variable JW_DEFAULT_PKG_FILTER becomes JW_DEFAULT_PATH_FILTER
  • Distro: the default_pkg_filter constructor argument and property become default_path_filter, matching FileContext, ExecContext, and CopyContext; App and KvFile pass the renamed argument
  • Adapt the help test's expected output and the unit tests
This PR generalizes the `JW_DEFAULT_PKG_FILTER` / `--pkg-filter` framework into loadable path filters that can be attached to any FileContext. The series adds the PathFilter base class and the lib.pf module for concrete filters, teaches FileContext to check path actions against an attached filter, and threads the filter through copies and package selection. The existing package filter becomes the first loadable filter, and the CLI option and environment variable are renamed from `--pkg-filter` to `--path-filter`. #### 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. #### lib.FileContext: Limit accessible addresses Allow to attach a default path filter to a FileContext, given in the constructor as `default_path_filter`, or in the URI query as `path-filter`. Passing False as `default_path_filter` will make the FileContext ignore any path-filter in the query. The filter is created lazily on first use through `PathFilter.create()`, so a string spec is loaded from the lib.pf module like any other filter. Every path based action evaluates the filter before it is carried out. An `IGNORE` verdict is logged and the action is skipped: reads and stats raise FileNotFoundError, writes, unlinks, erases, renames, and mkdirs are no-ops, and `readdir()` drops the filtered entries. An `ABORT` verdict raises PermissionError. Every path-driven method also takes a flag parameter, `filtered`, defaulting to True, so a caller that must work regardless of the context's filter can opt out of the check. Add a unit test that exercises the verdicts, the filtered bypass, and the distribution probe on a Local context. #### lib.CopyContext: Add optional default_path_filter CopyContext creates its src and dst contexts from URIs when they are not given as instances. Pass an optional `default_path_filter` into that creation, so a copy made under a filter limits the addresses it reads and writes. A context given as an instance keeps the filter it already carries. #### lib.util.copy(): Add optional default_path_filter Hand the new `default_path_filter` parameter to the CopyContext `copy()` opens, and to the recursive calls it makes for a list of URIs, so an app can limit the addresses a copy touches the same way it limits its own contexts. #### lib.PackageFilter: Derive from PathFilter This commit moves package-based filtering from the package level to the path level, using the new path filter framework. Derive PackageFilter from a new FileContextFilter base, which binds a PathFilter to the FileContext it guards, and implements `_access()`. The filter's space is the file list of the packages it selects, taken from the backing distro's `pkg_files()`. An address is allowed only if it names one of those files. The backing distro is taken from the file context, if the context carries one, and is otherwise autodetected from the host the context executes on, so the filter also works on a bare exec context. A context that is not an exec context and carries no distro rejects path queries with an error. The filter string now follows the loader syntax, pkg:url=~ regex, and a bare url=~regex like the old url=~jw.dev form is rejected; update `JW_DEFAULT_PKG_FILTER` values accordingly. PackageFilter's `match()` is renamed `match_pkg()`, to make clear that it matches packages, and `Distro._select()` calls the new name. ExecApp.`path_filter` builds the filter through `PathFilter.create()` and binds it to the exec context. CmdSelect passes its argument to the new constructor, CmdCopy hands the app's filter to the copy, and the App distro picks it up as the default package filter. Extend the unit test to cover the space membership and the distro lookup. #### lib.pf.Pkg: Add module Add the Pkg path filter to the lib.pf module, the loader-visible home of the package filter spec. A spec of the form pkg:criterion=~regex instantiates Pkg through `PathFilter.create()`. The parsing duplicates the one of PackageFilterString for now; a later commit consolidates the two. #### lib.Distro: Widen the package filter to PathFilter The default package filter is typed PackageFilter, but it is handed over from file contexts that carry a PathFilter. Widen the `default_pkg_filter` parameter. #### cmds.pkg.CmdSelect: Load path filter dynamically The select command names PackageFilterString directly, but the filter is a spec string and the class is resolved dynamically by the loader. Build the filter through `PathFilter.create()` so the concrete class no longer appears at the call site. #### lib.ExecApp: Move .path_filter into the app's ExecContext Create an app's exec context with the user-specified path filter, and drop the ExecApp.`path_filter` property, now a duplicate of `exec_context`.`path_filter`. Use the latter in the two call sites. #### lib.pf.Pkg: Consolidate PackageFilter, drop PackageFilterString PackageFilterString and Pkg are duplicates. The PackageFilter and FileContextFilter base classes live in `lib/PackageFilter.py`, imported by Pkg, Distro, and the tests. Move the two base classes into `lib/pf/Pkg.py` (where Pkg already lives), drop PackageFilterString, delete `lib/PackageFilter.py`, and repoint Distro and the tests to the new location. Move the test suite with the module to test/.../pf/Pkg/. #### lib.ec.secret_store.KvFile: Init filter in FC-style Drop the re-read of `JW_DEFAULT_PKG_FILTER`, and derive the filter from the ctor and URI like all other FileContext instances would. Use that to filter the backend for the packages provinding secrets. #### lib.ExecApp: Rename --pkg-filter to --path-filter The exec context's default filter selects paths, not just packages, so the pkg spelling of the option and environment variable that set it no longer fit. Rename both and some variables accordingly: - ExecApp: `--pkg-filter` becomes `--path-filter`, and the fallback environment variable `JW_DEFAULT_PKG_FILTER` becomes `JW_DEFAULT_PATH_FILTER` - Distro: the `default_pkg_filter` constructor argument and property become `default_path_filter`, matching FileContext, ExecContext, and CopyContext; App and KvFile pass the renamed argument - Adapt the help test's expected output and the unit tests
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
Allow to attach a default path filter to a FileContext, given in the
constructor as default_path_filter, or in the URI query as "path-filter".
Passing False as default_path_filter will make the FileContext ignore any
path-filter in the query.

The filter is created lazily on first use through PathFilter.create(), so a
string spec is loaded from the lib.pf module like any other filter.

Every path based action evaluates the filter before it is carried out.
An IGNORE verdict is logged and the action is skipped: reads and stats
raise FileNotFoundError, writes, unlinks, erases, renames, and mkdirs are
no-ops, and readdir() drops the filtered entries. An ABORT verdict raises
PermissionError.

Every path-driven method also takes a flag parameter, "filtered",
defaulting to True, so a caller that must work regardless of the context's
filter can opt out of the check.

Add a unit test that exercises the verdicts, the filtered bypass, and the
distribution probe on a Local context.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
CopyContext creates its src and dst contexts from URIs when they are
not given as instances. Pass an optional default_path_filter into that
creation, so a copy made under a filter limits the addresses it reads and
writes. A context given as an instance keeps the filter it already carries.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Hand the new default_path_filter parameter to the CopyContext copy() opens,
and to the recursive calls it makes for a list of URIs, so an app can limit
the addresses a copy touches the same way it limits its own contexts.

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 moves package-based filtering from the package level to the
path level, using the new path filter framework. Derive PackageFilter from
a new FileContextFilter base, which binds a PathFilter to the FileContext
it guards, and implements _access().

The filter's space is the file list of the packages it selects, taken from
the backing distro's pkg_files(). An address is allowed only if it names
one of those files.

The backing distro is taken from the file context, if the context carries
one, and is otherwise autodetected from the host the context executes on,
so the filter also works on a bare exec context. A context that is not an
exec context and carries no distro rejects path queries with an error.

The filter string now follows the loader syntax, pkg:url=~ regex, and
a bare url=~regex like the old url=~jw.dev form is rejected; update
JW_DEFAULT_PKG_FILTER values accordingly.

PackageFilter's match() is renamed match_pkg(), to make clear that it
matches packages, and Distro._select() calls the new name.

ExecApp.path_filter builds the filter through PathFilter.create() and
binds it to the exec context. CmdSelect passes its argument to the new
constructor, CmdCopy hands the app's filter to the copy, and the App distro
picks it up as the default package filter.

Extend the unit test to cover the space membership and the distro lookup.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Add the Pkg path filter to the lib.pf module, the loader-visible home
of the package filter spec. A spec of the form pkg:criterion=~regex
instantiates Pkg through PathFilter.create(). The parsing duplicates the
one of PackageFilterString for now; a later commit consolidates the two.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
The default package filter is typed PackageFilter, but it is handed over
from file contexts that carry a PathFilter. Widen the default_pkg_filter
parameter.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
The select command names PackageFilterString directly, but the filter is a
spec string and the class is resolved dynamically by the loader. Build the
filter through PathFilter.create() so the concrete class no longer appears
at the call site.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Create an app's exec context with the user-specified path filter, and drop
the ExecApp.path_filter property, now a duplicate of
exec_context.path_filter. Use the latter in the two call sites.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
PackageFilterString and Pkg are duplicates. The PackageFilter and
FileContextFilter base classes live in lib/PackageFilter.py, imported by
Pkg, Distro, and the tests.

Move the two base classes into lib/pf/Pkg.py (where Pkg already lives),
drop PackageFilterString, delete lib/PackageFilter.py, and repoint Distro
and the tests to the new location. Move the test suite with the module to
test/.../pf/Pkg/.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Drop the re-read of JW_DEFAULT_PKG_FILTER, and derive the filter from the
ctor and URI like all other FileContext instances would. Use that to filter
the backend for the packages provinding secrets.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
lib.ExecApp: Rename --pkg-filter to --path-filter
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m17s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m27s
CI / Packaging test (pull_request) Successful in 0s
0373ae3134
The exec context's default filter selects paths, not just packages, so the
pkg spelling of the option and environment variable that set it no longer
fit. Rename both and some variables accordingly:

- ExecApp: --pkg-filter becomes --path-filter, and the fallback environment
  variable JW_DEFAULT_PKG_FILTER becomes JW_DEFAULT_PATH_FILTER
- Distro: the default_pkg_filter constructor argument and property become
  default_path_filter, matching FileContext, ExecContext, and CopyContext;
  App and KvFile pass the renamed argument
- Adapt the help test's expected output and the unit tests

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Jan Lindemann scheduled this pull request to auto merge when all checks succeed 2026-11-07 19:41:24 +01:00
Jan Lindemann changed title from lib.ExecApp: Rename --pkg-filter to --path-filter to lib.PathFilter: Generalize the package filter into path filters 2026-11-07 19:42:33 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
janware/jw-pkg!170
No description provided.