Skip to content

Commit 1675ad7

Browse files
committed
Fix regex filter PR review issues
Signed-off-by: lucarlig <luca.carlig@ibm.com>
1 parent b294d1d commit 1675ad7

13 files changed

Lines changed: 1224 additions & 130 deletions

File tree

.github/workflows/release-rust-python-package.yaml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ jobs:
5454
with:
5555
python-version: "3.12"
5656

57-
- name: Validate plugin catalog
58-
run: python3 tools/plugin_catalog.py validate .
59-
6057
- id: resolve
6158
shell: bash
6259
env:
@@ -79,12 +76,15 @@ jobs:
7976
tag_ref="${GITHUB_REF}"
8077
fi
8178
82-
if git merge-base --is-ancestor "${tag_ref}" "refs/remotes/origin/main"; then
79+
tag_commit="$(git rev-list -n 1 "${tag_ref}")"
80+
if git merge-base --is-ancestor "${tag_commit}" "refs/remotes/origin/main"; then
8381
tag_on_main=true
8482
else
8583
tag_on_main=false
8684
fi
8785
86+
git checkout --detach "${checkout_ref}"
87+
python3 tools/plugin_catalog.py validate .
8888
release_info="$(python3 tools/plugin_catalog.py release-info . "${tag}")"
8989
plugin="$(printf '%s' "${release_info}" | python3 -c 'import json, sys; print(json.load(sys.stdin)["slug"])')"
9090
plugin_path="$(printf '%s' "${release_info}" | python3 -c 'import json, sys; print(json.load(sys.stdin)["path"])')"
@@ -255,7 +255,7 @@ jobs:
255255
fi
256256
257257
publish:
258-
if: ${{ (github.event_name != 'workflow_call' || inputs.publish_enabled) && (needs.resolve.outputs.publish_env != 'pypi' || needs.resolve.outputs.tag_on_main == 'true') }}
258+
if: ${{ github.event_name != 'workflow_call' || inputs.publish_enabled }}
259259
needs: [resolve, build-wheel, build-sdist]
260260
runs-on: ubuntu-latest
261261
environment: ${{ needs.resolve.outputs.publish_env }}
@@ -276,8 +276,14 @@ jobs:
276276
repository-url: https://test.pypi.org/legacy/
277277
skip-existing: true
278278

279+
- name: Verify PyPI tag is on main
280+
if: needs.resolve.outputs.publish_env == 'pypi' && needs.resolve.outputs.tag_on_main != 'true'
281+
run: |
282+
echo "Refusing to publish to PyPI because the release tag is not reachable from origin/main" >&2
283+
exit 1
284+
279285
- name: Publish distributions to PyPI
280-
if: needs.resolve.outputs.publish_env == 'pypi'
286+
if: needs.resolve.outputs.publish_env == 'pypi' && needs.resolve.outputs.tag_on_main == 'true'
281287
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e
282288
with:
283289
packages-dir: dist/

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Current plugins:
1010

1111
- `rate_limiter`
1212
- `pii_filter`
13+
- `encoded_exfil_detection`
14+
- `regex_filter`
15+
- `retry_with_backoff`
16+
- `secrets_detection`
17+
- `url_reputation`
1318

1419
Each managed plugin must include:
1520

plugins/rust/python-package/regex_filter/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ name = "stub_gen"
1616
path = "src/bin/stub_gen.rs"
1717

1818
[dependencies]
19-
cpex_framework_bridge = { path = "../../../../crates/framework_bridge" }
19+
cpex_framework_bridge = { workspace = true }
2020
log = { workspace = true }
2121
pyo3 = { workspace = true }
2222
pyo3-log = { workspace = true }
2323
pyo3-stub-gen = { workspace = true }
24-
regex = "1.12"
24+
regex = { workspace = true }
2525

2626
[dev-dependencies]
27-
criterion = { version = "0.8", features = ["html_reports"] }
27+
criterion = { workspace = true }
2828

2929
[[bench]]
3030
name = "regex_filter"

plugins/rust/python-package/regex_filter/README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Regex Filter Plugin for CPEX
22

33
Rust-backed regex search and replace for prompt arguments, prompt messages, tool arguments, and tool results.
4+
Patterns use Rust `regex` syntax, which does not support look-around or backreferences.
5+
Replacement strings use Rust `regex` expansion syntax (`$0`, `$1`, `$name`, `${name}`, and `$$` for a literal dollar).
6+
Recursive filtering covers strings inside dicts and lists, plus Python tuples and sets; custom object attributes are left unchanged.
47

58
This package follows the same layout as the other Rust+Python CPEX plugins in this repository:
69

@@ -11,12 +14,30 @@ This package follows the same layout as the other Rust+Python CPEX plugins in th
1114
## Configuration
1215

1316
```yaml
14-
config:
15-
words:
16-
- search: "\\bsecret\\b"
17-
replace: "[REDACTED]"
18-
- search: "\\d{3}-\\d{2}-\\d{4}"
19-
replace: "XXX-XX-XXXX"
17+
plugins:
18+
- name: regex_filter
19+
kind: cpex_regex_filter.regex_filter.SearchReplacePlugin
20+
hooks:
21+
- prompt_pre_fetch
22+
- prompt_post_fetch
23+
- tool_pre_invoke
24+
- tool_post_invoke
25+
mode: enforce
26+
config:
27+
words:
28+
- search: "\\bsecret\\b"
29+
replace: "[REDACTED]"
30+
- search: "\\d{3}-\\d{2}-\\d{4}"
31+
replace: "XXX-XX-XXXX"
32+
max_text_bytes: 10485760
33+
max_total_text_bytes: 10485760
34+
max_nested_depth: 64
35+
max_collection_items: 4096
36+
max_total_items: 65536
37+
max_patterns: 1024
38+
max_search_bytes: 1048576
39+
max_replace_bytes: 1048576
40+
max_output_bytes: 10485760
2041
```
2142
2243
## Development

plugins/rust/python-package/regex_filter/benches/regex_filter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ fn bench_apply_patterns(c: &mut Criterion) {
2020
},
2121
],
2222
pattern_set: RegexSet::new([r"\bsecret\b", r"\d{3}-\d{2}-\d{4}"]).ok(),
23+
max_text_bytes: 10 * 1024 * 1024,
24+
max_total_text_bytes: 10 * 1024 * 1024,
25+
max_nested_depth: 64,
26+
max_collection_items: 4096,
27+
max_total_items: 65_536,
28+
max_output_bytes: 10 * 1024 * 1024,
2329
};
2430
let plugin = SearchReplacePluginRust { config };
2531
let text = "The secret number is 123-45-6789";
2632

2733
c.bench_function("regex_filter_apply_patterns", |b| {
28-
b.iter(|| plugin.apply_patterns(text))
34+
b.iter(|| plugin.apply_patterns(text).unwrap())
2935
});
3036
}
3137

plugins/rust/python-package/regex_filter/cpex_regex_filter/plugin-manifest.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
description: "Rust-backed regex search and replace for prompt arguments, prompt messages, tool inputs, and tool outputs"
22
author: "ContextForge Contributors"
3+
kind: "cpex_regex_filter.regex_filter.SearchReplacePlugin"
34
version: "0.1.0"
45
available_hooks:
56
- "prompt_pre_fetch"
@@ -8,3 +9,12 @@ available_hooks:
89
- "tool_post_invoke"
910
default_configs:
1011
words: []
12+
max_text_bytes: 10485760
13+
max_total_text_bytes: 10485760
14+
max_nested_depth: 64
15+
max_collection_items: 4096
16+
max_total_items: 65536
17+
max_patterns: 1024
18+
max_search_bytes: 1048576
19+
max_replace_bytes: 1048576
20+
max_output_bytes: 10485760

plugins/rust/python-package/regex_filter/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ classifiers = [
1818
"Programming Language :: Python :: 3.13",
1919
]
2020

21+
[project.entry-points."cpex.plugins"]
22+
regex_filter = "cpex_regex_filter.regex_filter:SearchReplacePlugin"
23+
2124
[tool.maturin]
2225
module-name = "cpex_regex_filter.regex_filter_rust"
2326
python-source = "."

0 commit comments

Comments
 (0)