policyshield 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. policyshield-0.5.0/.gitignore +50 -0
  2. policyshield-0.5.0/CHANGELOG.md +96 -0
  3. policyshield-0.5.0/CODE_OF_CONDUCT.md +28 -0
  4. policyshield-0.5.0/CONTRIBUTING.md +88 -0
  5. policyshield-0.5.0/Dockerfile +12 -0
  6. policyshield-0.5.0/LICENSE +21 -0
  7. policyshield-0.5.0/PKG-INFO +434 -0
  8. policyshield-0.5.0/README.md +373 -0
  9. policyshield-0.5.0/ROADMAP.md +123 -0
  10. policyshield-0.5.0/VISION.md +85 -0
  11. policyshield-0.5.0/docker-compose.yml +20 -0
  12. policyshield-0.5.0/docs/api/core.md +10 -0
  13. policyshield-0.5.0/docs/api/engine.md +13 -0
  14. policyshield-0.5.0/docs/api/linter.md +8 -0
  15. policyshield-0.5.0/docs/getting-started/configuration.md +43 -0
  16. policyshield-0.5.0/docs/getting-started/installation.md +43 -0
  17. policyshield-0.5.0/docs/getting-started/quickstart.md +61 -0
  18. policyshield-0.5.0/docs/github-action.md +50 -0
  19. policyshield-0.5.0/docs/guides/cli.md +65 -0
  20. policyshield-0.5.0/docs/guides/presets.md +48 -0
  21. policyshield-0.5.0/docs/guides/testing-rules.md +53 -0
  22. policyshield-0.5.0/docs/guides/writing-rules.md +71 -0
  23. policyshield-0.5.0/docs/index.md +53 -0
  24. policyshield-0.5.0/docs/integrations/crewai.md +21 -0
  25. policyshield-0.5.0/docs/integrations/fastapi.md +33 -0
  26. policyshield-0.5.0/docs/integrations/langchain.md +27 -0
  27. policyshield-0.5.0/docs/integrations/nanobot.md +33 -0
  28. policyshield-0.5.0/examples/async_demo.py +29 -0
  29. policyshield-0.5.0/examples/fastapi_agent/README.md +33 -0
  30. policyshield-0.5.0/examples/fastapi_agent/app.py +92 -0
  31. policyshield-0.5.0/examples/fastapi_agent/policies/rules.yaml +30 -0
  32. policyshield-0.5.0/examples/fastapi_agent/policies/test_rules.yaml +31 -0
  33. policyshield-0.5.0/examples/github-actions/policy-check.yml +24 -0
  34. policyshield-0.5.0/examples/langchain_demo.py +83 -0
  35. policyshield-0.5.0/examples/nanobot_rules.yaml +37 -0
  36. policyshield-0.5.0/examples/nanobot_shield_agentloop.py +80 -0
  37. policyshield-0.5.0/examples/nanobot_shield_example.py +81 -0
  38. policyshield-0.5.0/examples/policies/compliance.yaml +37 -0
  39. policyshield-0.5.0/examples/policies/full.yaml +60 -0
  40. policyshield-0.5.0/examples/policies/minimal.yaml +42 -0
  41. policyshield-0.5.0/examples/policies/rules_test.yaml +43 -0
  42. policyshield-0.5.0/examples/policies/security.yaml +54 -0
  43. policyshield-0.5.0/examples/policyshield.yaml +48 -0
  44. policyshield-0.5.0/mkdocs.yml +73 -0
  45. policyshield-0.5.0/policyshield/__init__.py +3 -0
  46. policyshield-0.5.0/policyshield/approval/__init__.py +23 -0
  47. policyshield-0.5.0/policyshield/approval/base.py +80 -0
  48. policyshield-0.5.0/policyshield/approval/cache.py +88 -0
  49. policyshield-0.5.0/policyshield/approval/cli_backend.py +78 -0
  50. policyshield-0.5.0/policyshield/approval/memory.py +63 -0
  51. policyshield-0.5.0/policyshield/approval/telegram.py +210 -0
  52. policyshield-0.5.0/policyshield/approval/webhook.py +259 -0
  53. policyshield-0.5.0/policyshield/cli/__init__.py +0 -0
  54. policyshield-0.5.0/policyshield/cli/init_scaffold.py +459 -0
  55. policyshield-0.5.0/policyshield/cli/main.py +593 -0
  56. policyshield-0.5.0/policyshield/config/__init__.py +15 -0
  57. policyshield-0.5.0/policyshield/config/loader.py +326 -0
  58. policyshield-0.5.0/policyshield/config/schema.json +188 -0
  59. policyshield-0.5.0/policyshield/core/__init__.py +29 -0
  60. policyshield-0.5.0/policyshield/core/exceptions.py +15 -0
  61. policyshield-0.5.0/policyshield/core/models.py +154 -0
  62. policyshield-0.5.0/policyshield/core/parser.py +189 -0
  63. policyshield-0.5.0/policyshield/integrations/__init__.py +1 -0
  64. policyshield-0.5.0/policyshield/integrations/crewai/__init__.py +11 -0
  65. policyshield-0.5.0/policyshield/integrations/crewai/wrapper.py +135 -0
  66. policyshield-0.5.0/policyshield/integrations/langchain/__init__.py +5 -0
  67. policyshield-0.5.0/policyshield/integrations/langchain/wrapper.py +90 -0
  68. policyshield-0.5.0/policyshield/integrations/nanobot/__init__.py +21 -0
  69. policyshield-0.5.0/policyshield/integrations/nanobot/cli_wrapper.py +77 -0
  70. policyshield-0.5.0/policyshield/integrations/nanobot/context.py +8 -0
  71. policyshield-0.5.0/policyshield/integrations/nanobot/installer.py +58 -0
  72. policyshield-0.5.0/policyshield/integrations/nanobot/monkey_patch.py +145 -0
  73. policyshield-0.5.0/policyshield/integrations/nanobot/registry.py +212 -0
  74. policyshield-0.5.0/policyshield/lint/__init__.py +5 -0
  75. policyshield-0.5.0/policyshield/lint/differ.py +144 -0
  76. policyshield-0.5.0/policyshield/lint/linter.py +198 -0
  77. policyshield-0.5.0/policyshield/py.typed +0 -0
  78. policyshield-0.5.0/policyshield/shield/__init__.py +19 -0
  79. policyshield-0.5.0/policyshield/shield/async_engine.py +247 -0
  80. policyshield-0.5.0/policyshield/shield/base_engine.py +401 -0
  81. policyshield-0.5.0/policyshield/shield/engine.py +83 -0
  82. policyshield-0.5.0/policyshield/shield/matcher.py +252 -0
  83. policyshield-0.5.0/policyshield/shield/pii.py +293 -0
  84. policyshield-0.5.0/policyshield/shield/rate_limiter.py +163 -0
  85. policyshield-0.5.0/policyshield/shield/sanitizer.py +184 -0
  86. policyshield-0.5.0/policyshield/shield/session.py +139 -0
  87. policyshield-0.5.0/policyshield/shield/verdict.py +144 -0
  88. policyshield-0.5.0/policyshield/shield/watcher.py +106 -0
  89. policyshield-0.5.0/policyshield/testing/__init__.py +5 -0
  90. policyshield-0.5.0/policyshield/testing/runner.py +219 -0
  91. policyshield-0.5.0/policyshield/trace/__init__.py +0 -0
  92. policyshield-0.5.0/policyshield/trace/analyzer.py +189 -0
  93. policyshield-0.5.0/policyshield/trace/exporter.py +180 -0
  94. policyshield-0.5.0/policyshield/trace/otel.py +173 -0
  95. policyshield-0.5.0/policyshield/trace/recorder.py +138 -0
  96. policyshield-0.5.0/pyproject.toml +82 -0
@@ -0,0 +1,50 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ *.egg-info/
7
+ *.egg
8
+ dist/
9
+ build/
10
+
11
+ # Virtual environments
12
+ .venv/
13
+ venv/
14
+ ENV/
15
+
16
+ # Testing
17
+ .pytest_cache/
18
+ htmlcov/
19
+ .coverage
20
+ .coverage.*
21
+
22
+ # Linting
23
+ .ruff_cache/
24
+
25
+ # IDE
26
+ .idea/
27
+ .vscode/
28
+ *.swp
29
+ *.swo
30
+ *~
31
+
32
+ # OS
33
+ .DS_Store
34
+ Thumbs.db
35
+
36
+ # Traces (local only)
37
+ traces/
38
+ *.jsonl
39
+
40
+ # Local development files (not for repo)
41
+ CLAUDE.md
42
+ TECHNICAL_SPEC.md
43
+ INTEGRATION_SPEC.md
44
+ *.docx
45
+ /nanobot/
46
+ prompts/
47
+ demo.py
48
+ demo_traces/
49
+ .gemini/
50
+ .antigravityignore
@@ -0,0 +1,96 @@
1
+ # Changelog
2
+
3
+ ## [0.5.0] - 2025-02-12
4
+
5
+ ### Added
6
+ - **CLI `policyshield init`**: Scaffold new projects with presets (`minimal`, `security`, `compliance`), nanobot support, auto-generated test cases
7
+ - **CLI nanobot wrapper**: Extracted `cli_wrapper.py` with `patch_agent_loop_class()` and `run_nanobot_cli()`
8
+ - **PyPI packaging**: Updated metadata, Beta status, 7 optional dependency groups (`langchain`, `crewai`, `otel`, `nanobot`, `docs`, `dev`, `all`)
9
+ - **GitHub Actions CI**: Enhanced with format check, coverage XML artifact, build job with twine check
10
+ - **Release workflow**: Automated PyPI publishing on version tags
11
+ - **Reusable GitHub Action**: `.github/actions/lint-rules/` for validating and linting rules in CI
12
+ - **MkDocs documentation site**: Material theme, 14 pages covering getting started, guides, integrations, API reference
13
+ - **GitHub Pages deploy**: Automatic docs deployment workflow
14
+ - **FastAPI example**: Complete agent service with `/evaluate` and `/rules` endpoints
15
+ - **Docker quickstart**: Dockerfile and docker-compose.yml with validate/lint/test services
16
+ - **Contributing guide**: Updated with format checks, project structure, commit conventions
17
+ - **GitHub templates**: PR template, bug report and feature request issue templates
18
+ - **Code of Conduct**: Contributor Covenant v2.1
19
+ - 109 new tests (prompts 19–28), bringing total to 570
20
+
21
+ ## [0.4.0] - 2025-02-12
22
+
23
+ ### Added
24
+ - Session ID propagation from `AgentLoop` to `ShieldEngine` for per-session rate limiting
25
+ - Post-call PII scan: tool results are scanned and tainted PII types are recorded
26
+ - `get_definitions()` override: unconditionally blocked tools are hidden from LLM context
27
+ - Context enrichment: active policy constraints are injected into the LLM system prompt
28
+ - Subagent shield propagation via `SubagentManager.shield_config`
29
+ - `approval_backend` parameter in `install_shield()` for CLI/Telegram/Webhook approval flows
30
+ - Comprehensive nanobot integration guide (`docs/nanobot_integration.md`)
31
+ - Working examples: `nanobot_shield_example.py`, `nanobot_shield_agentloop.py`, `nanobot_rules.yaml`
32
+ - Integration tests with real nanobot `Tool` objects (`test_nanobot_real_tools.py`)
33
+ - 26 new tests, bringing total to 461
34
+
35
+ ## [0.3.1] - 2025-02-11
36
+
37
+ ### Fixed
38
+ - Session increment no longer fires on BLOCK/APPROVE verdicts (both sync and async engines)
39
+ - `_parse_rule` now preserves `approval_strategy` field from YAML rules
40
+ - `AsyncShieldEngine.reload_rules` protected with `threading.Lock` to prevent race conditions
41
+ - ReDoS protection: regex patterns in rules capped at 500 characters
42
+ - `redact_dict` now recursively redacts PII in nested dicts and lists
43
+ - `TraceRecorder.record()` / `flush()` protected with `threading.Lock` for thread safety
44
+ - LangChain `_arun` uses `asyncio.to_thread` instead of blocking sync call
45
+ - IP address regex validates octet range (0–255), rejects `999.999.999.999`
46
+ - Passport regex narrowed from 6–9 to 7–9 digits to reduce false positives
47
+
48
+ ### Added
49
+ - Nanobot integration: `ShieldedToolRegistry` extends nanobot's `ToolRegistry` with async support
50
+ - `install_shield()` helper to wrap existing nanobot registries
51
+ - `AgentLoop.shield_config` parameter for optional PolicyShield enablement
52
+ - 23 audit regression tests (`test_audit_fixes.py`), bringing total to 437
53
+
54
+ ## [0.3.0] - 2025-02-11
55
+
56
+ ### Added
57
+ - AsyncShieldEngine with full async/await support
58
+ - CrewAI BaseTool adapter (CrewAIShieldTool, shield_all_crewai_tools)
59
+ - OpenTelemetry exporter (OTLP spans + metrics)
60
+ - Webhook approval backend with HMAC-SHA256 signing
61
+ - YAML-based rule testing framework (`policyshield test`)
62
+ - Policy diff tool (`policyshield diff`)
63
+ - Trace export: CSV and HTML report (`policyshield trace export`)
64
+ - Input sanitizer with prompt injection protection
65
+ - Unified config file (policyshield.yaml) with JSON Schema
66
+ - 14 new E2E test scenarios for v0.3 features
67
+
68
+ ## [0.2.0] - 2025-02-11
69
+
70
+ ### Added
71
+ - Rule linter with 6 static checks (`policyshield lint`)
72
+ - Hot reload of YAML rules (file watcher)
73
+ - RU PII patterns: INN, SNILS, passport, phone (with checksum validation)
74
+ - Custom PII patterns from YAML
75
+ - Sliding window rate limiter with YAML config
76
+ - Human-in-the-loop APPROVE verdict
77
+ - Approval backends: InMemory, CLI, Telegram
78
+ - Batch approve with caching strategies (once, per_session, per_rule, per_tool)
79
+ - Trace stats aggregation (`policyshield trace stats`)
80
+ - LangChain BaseTool adapter (`PolicyShieldTool`, `shield_all_tools`)
81
+ - 12 new E2E test scenarios for v0.2 features
82
+ - CHANGELOG
83
+
84
+ ## [0.1.0] - 2025-02-11
85
+
86
+ ### Added
87
+ - Core models (Verdict, RuleConfig, ShieldResult, etc.)
88
+ - YAML rule parser with includes and env vars
89
+ - PII detector (EMAIL, PHONE, CREDIT_CARD, SSN, IBAN, IP, PASSPORT, DOB)
90
+ - Rule matcher with regex, glob, and exact match
91
+ - ShieldEngine orchestrator
92
+ - Session manager with tool call tracking
93
+ - Trace recorder (JSONL)
94
+ - CLI: validate, trace show, trace violations
95
+ - Nanobot integration
96
+ - 10 E2E test scenarios
@@ -0,0 +1,28 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We pledge to make participation in our community a harassment-free experience for everyone.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to a positive environment:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+
16
+ Examples of unacceptable behavior:
17
+
18
+ * Trolling, insulting/derogatory comments, and personal attacks
19
+ * Public or private harassment
20
+ * Publishing others' private information without explicit permission
21
+
22
+ ## Enforcement
23
+
24
+ Instances of abusive behavior may be reported to the project maintainers. All complaints will be reviewed and investigated promptly and fairly.
25
+
26
+ ## Attribution
27
+
28
+ This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/), version 2.1.
@@ -0,0 +1,88 @@
1
+ # Contributing to PolicyShield
2
+
3
+ Thanks for your interest in PolicyShield! Here's how to get started.
4
+
5
+ ## Setup
6
+
7
+ ```bash
8
+ git clone https://github.com/mishabar410/PolicyShield.git
9
+ cd PolicyShield
10
+ python -m venv .venv && source .venv/bin/activate
11
+ pip install -e ".[dev,langchain]"
12
+ ```
13
+
14
+ ## Development Workflow
15
+
16
+ 1. **Create a branch** from `main`
17
+ 2. **Write code + tests** — every feature must include tests
18
+ 3. **Lint**: `ruff check policyshield/ tests/`
19
+ 4. **Format**: `ruff format policyshield/ tests/`
20
+ 5. **Test**: `pytest tests/ -v --cov=policyshield --cov-fail-under=85`
21
+ 6. **Open a PR** against `main`
22
+
23
+ ## Code Style
24
+
25
+ - Python 3.10+ with type hints
26
+ - Formatted with `ruff`
27
+ - All public APIs must have docstrings
28
+ - Maximum line length: 120 characters
29
+
30
+ ## Testing
31
+
32
+ ```bash
33
+ # Run all tests
34
+ pytest tests/ -v
35
+
36
+ # With coverage
37
+ pytest tests/ --cov=policyshield --cov-report=term-missing
38
+
39
+ # Target coverage: ≥85%
40
+ ```
41
+
42
+ ## Project Structure
43
+
44
+ ```
45
+ policyshield/
46
+ ├── core/ # Data models, YAML parser
47
+ ├── shield/ # ShieldEngine, PII detector, matcher
48
+ ├── approval/ # Approval backends (CLI, Telegram, Webhook)
49
+ ├── integrations/ # LangChain, CrewAI, Nanobot adapters
50
+ ├── trace/ # JSONL recorder, OpenTelemetry exporter
51
+ ├── lint/ # Rule linter
52
+ ├── cli/ # CLI commands (validate, lint, test, init, nanobot)
53
+ └── config/ # Config file loader, JSON schema
54
+ ```
55
+
56
+ ## Adding a new rule check
57
+
58
+ 1. Add the check method to `policyshield/lint/linter.py`
59
+ 2. Add tests in `tests/test_linter.py`
60
+ 3. Document the check in `docs/api/linter.md`
61
+
62
+ ## Adding an integration
63
+
64
+ 1. Create a new module in `policyshield/integrations/`
65
+ 2. Add optional dependency group in `pyproject.toml`
66
+ 3. Add integration docs in `docs/integrations/`
67
+ 4. Write tests in `tests/`
68
+
69
+ ## Commit Messages
70
+
71
+ Use [Conventional Commits](https://www.conventionalcommits.org/):
72
+
73
+ ```
74
+ feat: add new feature
75
+ fix: fix a bug
76
+ docs: update documentation
77
+ test: add tests
78
+ chore: maintenance tasks
79
+ ```
80
+
81
+ ## Reporting Issues
82
+
83
+ - Use GitHub Issues
84
+ - Include: Python version, PolicyShield version, minimal reproduction
85
+
86
+ ## License
87
+
88
+ By contributing, you agree that your contributions will be licensed under the MIT License.
@@ -0,0 +1,12 @@
1
+ FROM python:3.12-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY pyproject.toml README.md LICENSE ./
6
+ COPY policyshield/ policyshield/
7
+
8
+ RUN pip install --no-cache-dir .
9
+
10
+ # Default: run the CLI help
11
+ ENTRYPOINT ["policyshield"]
12
+ CMD ["--help"]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 PolicyShield Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.