cognexus 0.1.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.
- cognexus-0.1.0/.github/workflows/publish.yml +54 -0
- cognexus-0.1.0/.gitignore +30 -0
- cognexus-0.1.0/LICENSE +27 -0
- cognexus-0.1.0/PKG-INFO +267 -0
- cognexus-0.1.0/README.md +206 -0
- cognexus-0.1.0/pyproject.toml +77 -0
- cognexus-0.1.0/src/cognexus/__init__.py +108 -0
- cognexus-0.1.0/src/cognexus/_helpers.py +492 -0
- cognexus-0.1.0/src/cognexus/events.py +200 -0
- cognexus-0.1.0/src/cognexus/prompt_defense.py +594 -0
- cognexus-0.1.0/src/cognexus/prompt_injection.py +814 -0
- cognexus-0.1.0/tests/__init__.py +0 -0
- cognexus-0.1.0/tests/test_prompt_defense.py +90 -0
- cognexus-0.1.0/tests/test_prompt_injection.py +224 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
name: Run tests
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- uses: actions/setup-python@v5
|
|
20
|
+
with:
|
|
21
|
+
python-version: ${{ matrix.python-version }}
|
|
22
|
+
|
|
23
|
+
- name: Install package and dev dependencies
|
|
24
|
+
run: pip install -e ".[dev]"
|
|
25
|
+
|
|
26
|
+
- name: Run tests
|
|
27
|
+
run: pytest
|
|
28
|
+
|
|
29
|
+
build-and-publish:
|
|
30
|
+
name: Build and publish to PyPI
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
needs: test
|
|
33
|
+
environment: pypi
|
|
34
|
+
permissions:
|
|
35
|
+
id-token: write # required for Trusted Publisher OIDC (no token needed)
|
|
36
|
+
|
|
37
|
+
steps:
|
|
38
|
+
- uses: actions/checkout@v4
|
|
39
|
+
|
|
40
|
+
- uses: actions/setup-python@v5
|
|
41
|
+
with:
|
|
42
|
+
python-version: "3.11"
|
|
43
|
+
|
|
44
|
+
- name: Install build tools
|
|
45
|
+
run: pip install build twine
|
|
46
|
+
|
|
47
|
+
- name: Build distributions
|
|
48
|
+
run: python -m build
|
|
49
|
+
|
|
50
|
+
- name: Validate distributions
|
|
51
|
+
run: twine check dist/*
|
|
52
|
+
|
|
53
|
+
- name: Publish to PyPI
|
|
54
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Build artefacts
|
|
2
|
+
dist/
|
|
3
|
+
build/
|
|
4
|
+
*.egg-info/
|
|
5
|
+
__pycache__/
|
|
6
|
+
*.pyc
|
|
7
|
+
*.pyo
|
|
8
|
+
*.pyd
|
|
9
|
+
|
|
10
|
+
# Test / coverage
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.coverage
|
|
13
|
+
htmlcov/
|
|
14
|
+
|
|
15
|
+
# Virtual environments
|
|
16
|
+
.venv/
|
|
17
|
+
venv/
|
|
18
|
+
env/
|
|
19
|
+
|
|
20
|
+
# Audit output (generated at runtime)
|
|
21
|
+
*.jsonl
|
|
22
|
+
|
|
23
|
+
# Editor
|
|
24
|
+
.vscode/
|
|
25
|
+
.idea/
|
|
26
|
+
*.swp
|
|
27
|
+
|
|
28
|
+
# OS
|
|
29
|
+
.DS_Store
|
|
30
|
+
Thumbs.db
|
cognexus-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CogNexus Labs
|
|
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.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
Portions of this software are derived from microsoft/agent-governance-toolkit
|
|
26
|
+
(https://github.com/microsoft/agent-governance-toolkit), also licensed under
|
|
27
|
+
the MIT License. Copyright (c) Microsoft Corporation.
|
cognexus-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cognexus
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OWASP-aligned LLM prompt defence, injection detection, and audit logging
|
|
5
|
+
Project-URL: Homepage, https://github.com/CogNexusLabs/cognexus
|
|
6
|
+
Project-URL: Source, https://github.com/CogNexusLabs/cognexus
|
|
7
|
+
Project-URL: Issues, https://github.com/CogNexusLabs/cognexus/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/CogNexusLabs/cognexus/releases
|
|
9
|
+
Author: Odenthal
|
|
10
|
+
Maintainer: Odenthal
|
|
11
|
+
License: MIT License
|
|
12
|
+
|
|
13
|
+
Copyright (c) 2026 CogNexus Labs
|
|
14
|
+
|
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
in the Software without restriction, including without limitation the rights
|
|
18
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all
|
|
23
|
+
copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
Portions of this software are derived from microsoft/agent-governance-toolkit
|
|
36
|
+
(https://github.com/microsoft/agent-governance-toolkit), also licensed under
|
|
37
|
+
the MIT License. Copyright (c) Microsoft Corporation.
|
|
38
|
+
License-File: LICENSE
|
|
39
|
+
Keywords: ai-safety,audit,guardrails,llm,owasp,prompt-defense,prompt-injection,security
|
|
40
|
+
Classifier: Development Status :: 4 - Beta
|
|
41
|
+
Classifier: Intended Audience :: Developers
|
|
42
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
43
|
+
Classifier: Operating System :: OS Independent
|
|
44
|
+
Classifier: Programming Language :: Python :: 3
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
46
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
47
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
48
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
49
|
+
Classifier: Topic :: Security
|
|
50
|
+
Classifier: Typing :: Typed
|
|
51
|
+
Requires-Python: >=3.10
|
|
52
|
+
Provides-Extra: dev
|
|
53
|
+
Requires-Dist: build; extra == 'dev'
|
|
54
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
55
|
+
Requires-Dist: pyyaml>=6.0; extra == 'dev'
|
|
56
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
57
|
+
Requires-Dist: twine; extra == 'dev'
|
|
58
|
+
Provides-Extra: yaml
|
|
59
|
+
Requires-Dist: pyyaml>=6.0; extra == 'yaml'
|
|
60
|
+
Description-Content-Type: text/markdown
|
|
61
|
+
|
|
62
|
+
# cognexus
|
|
63
|
+
|
|
64
|
+
**OWASP-aligned prompt defence, injection detection, and audit logging for LLM applications.**
|
|
65
|
+
|
|
66
|
+
`cognexus` gives you two complementary security layers and a tamper-evident audit trail — all in pure Python with zero mandatory dependencies.
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
pip install cognexus
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Features
|
|
75
|
+
|
|
76
|
+
| Layer | What it does |
|
|
77
|
+
|---|---|
|
|
78
|
+
| **Static prompt defence** | Grades system prompts A–F against 12 OWASP LLM Top-10 attack vectors before deployment |
|
|
79
|
+
| **Runtime injection detection** | Screens user input, RAG content, and tabular payloads at request time |
|
|
80
|
+
| **Audit events** | Append-only JSONL trail for every detected injection — no raw text stored |
|
|
81
|
+
|
|
82
|
+
### Detection coverage
|
|
83
|
+
|
|
84
|
+
- Direct instruction override
|
|
85
|
+
- Delimiter and context-boundary attacks
|
|
86
|
+
- Base64 / hex / ROT13 encoding attacks
|
|
87
|
+
- Role-play and jailbreak language (DAN mode, developer mode, etc.)
|
|
88
|
+
- Context manipulation ("your real instructions are…")
|
|
89
|
+
- Canary token leak detection
|
|
90
|
+
- Multi-turn escalation
|
|
91
|
+
- Cross-plugin / tool-chaining attacks (OWASP ASI04)
|
|
92
|
+
- Markup injection (XSS gadgets in model-visible text)
|
|
93
|
+
- Zero-width / token-smuggling unicode attacks
|
|
94
|
+
- Credential exfiltration requests
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Quick-start
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from cognexus import (
|
|
102
|
+
augment_system_prompt,
|
|
103
|
+
evaluate_system_prompt,
|
|
104
|
+
screen_user_input,
|
|
105
|
+
should_block,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
# 1. Augment your system prompt so it scores grade A before inference
|
|
109
|
+
system = augment_system_prompt("You are a helpful customer support agent.")
|
|
110
|
+
report = evaluate_system_prompt(system)
|
|
111
|
+
print(report.grade) # "A"
|
|
112
|
+
print(report.score) # 100
|
|
113
|
+
print(report.missing) # []
|
|
114
|
+
|
|
115
|
+
# 2. Screen every user message at request time
|
|
116
|
+
result = screen_user_input(user_message, source="chat")
|
|
117
|
+
|
|
118
|
+
if should_block(result):
|
|
119
|
+
raise PermissionError(f"Injection blocked: {result.explanation}")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Screening helpers
|
|
125
|
+
|
|
126
|
+
Three presets cover the most common LLM input surfaces:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from cognexus import (
|
|
130
|
+
screen_user_input, # balanced sensitivity — direct chat messages
|
|
131
|
+
screen_external_content, # strict sensitivity — RAG / web / API content
|
|
132
|
+
screen_tabular_payload, # permissive — CSV / dataframe blobs
|
|
133
|
+
should_block,
|
|
134
|
+
wrap_untrusted_content,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# Wrap RAG content before inserting into a prompt
|
|
138
|
+
safe_chunk = wrap_untrusted_content("web_search", raw_text)
|
|
139
|
+
|
|
140
|
+
# Screen it too
|
|
141
|
+
result = screen_external_content(raw_text, source="web_search", user_id=user.id)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Sensitivity presets
|
|
145
|
+
|
|
146
|
+
| Preset | Threshold | Min threat flagged | Use for |
|
|
147
|
+
|---|---|---|---|
|
|
148
|
+
| `strict` | 0.3 | LOW | External / RAG content |
|
|
149
|
+
| `balanced` | 0.5 | LOW | Direct user input |
|
|
150
|
+
| `permissive` | 0.7 | HIGH | CSV / tabular payloads |
|
|
151
|
+
|
|
152
|
+
Override via environment variables:
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
COGNEXUS_PROMPT_INJECTION_USER_SENSITIVITY=balanced
|
|
156
|
+
COGNEXUS_PROMPT_INJECTION_EXTERNAL_SENSITIVITY=strict
|
|
157
|
+
COGNEXUS_PROMPT_INJECTION_TABULAR_SENSITIVITY=permissive
|
|
158
|
+
COGNEXUS_PROMPT_INJECTION_BLOCK=0 # set to 1 to block any hit, not just CRITICAL
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Using the core classes directly
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from cognexus import PromptInjectionDetector, DetectionConfig, InjectionType
|
|
167
|
+
|
|
168
|
+
detector = PromptInjectionDetector(
|
|
169
|
+
config=DetectionConfig(
|
|
170
|
+
sensitivity="strict",
|
|
171
|
+
blocklist=["my-internal-keyword"],
|
|
172
|
+
allowlist=["safe phrase"],
|
|
173
|
+
)
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
result = detector.detect(text, source="api_gateway")
|
|
177
|
+
print(result.is_injection) # True / False
|
|
178
|
+
print(result.threat_level) # ThreatLevel.HIGH
|
|
179
|
+
print(result.injection_type) # InjectionType.DIRECT_OVERRIDE
|
|
180
|
+
print(result.confidence) # 0.9
|
|
181
|
+
print(result.matched_patterns) # ["direct_override:..."]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## Audit events
|
|
187
|
+
|
|
188
|
+
Detections are automatically written to a JSONL file (no raw input stored):
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
# Events go to $COGNEXUS_PROMPT_DEFENSE_EVENTS_DIR/prompt_defense_events.jsonl
|
|
192
|
+
# (falls back to $REPORTS_DIR, then /tmp)
|
|
193
|
+
|
|
194
|
+
from cognexus.events import read_recent_events
|
|
195
|
+
|
|
196
|
+
rows = read_recent_events(user_id=42, limit=20)
|
|
197
|
+
# [{"ts": "...", "kind": "prompt_injection", "threat": "high", ...}, ...]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Custom event sink (database, queue, dashboard)
|
|
201
|
+
|
|
202
|
+
Pass an `on_event` callback to mirror records into your own store:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
def save_to_db(record: dict) -> None:
|
|
206
|
+
db.execute("INSERT INTO security_events ...", record)
|
|
207
|
+
|
|
208
|
+
screen_user_input(text, source="chat", user_id=user.id, on_event=save_to_db)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Static prompt defence — standalone
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
from cognexus import PromptDefenseEvaluator, PromptDefenseConfig
|
|
217
|
+
|
|
218
|
+
evaluator = PromptDefenseEvaluator(
|
|
219
|
+
config=PromptDefenseConfig(min_grade="B")
|
|
220
|
+
)
|
|
221
|
+
report = evaluator.evaluate(my_system_prompt)
|
|
222
|
+
|
|
223
|
+
print(report.grade) # "C"
|
|
224
|
+
print(report.score) # 58
|
|
225
|
+
print(report.missing) # ["unicode-attack", "context-overflow"]
|
|
226
|
+
|
|
227
|
+
if report.is_blocking():
|
|
228
|
+
print("System prompt is below minimum grade — fix before deploying.")
|
|
229
|
+
|
|
230
|
+
# Evaluate a file
|
|
231
|
+
report = evaluator.evaluate_file("prompts/assistant.txt")
|
|
232
|
+
|
|
233
|
+
# Batch evaluation
|
|
234
|
+
reports = evaluator.evaluate_batch({
|
|
235
|
+
"chat": chat_prompt,
|
|
236
|
+
"analyst": analyst_prompt,
|
|
237
|
+
})
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Environment variables
|
|
243
|
+
|
|
244
|
+
| Variable | Default | Purpose |
|
|
245
|
+
|---|---|---|
|
|
246
|
+
| `COGNEXUS_PROMPT_DEFENSE_EVENTS_DIR` | `/tmp` | JSONL audit file directory |
|
|
247
|
+
| `COGNEXUS_PROMPT_INJECTION_LOG` | `1` | Log clean scans at DEBUG |
|
|
248
|
+
| `COGNEXUS_PROMPT_INJECTION_BLOCK` | `0` | Block any injection (not just CRITICAL) |
|
|
249
|
+
| `COGNEXUS_PROMPT_INJECTION_USER_SENSITIVITY` | `balanced` | User-input preset |
|
|
250
|
+
| `COGNEXUS_PROMPT_INJECTION_EXTERNAL_SENSITIVITY` | `strict` | External/RAG preset |
|
|
251
|
+
| `COGNEXUS_PROMPT_INJECTION_TABULAR_SENSITIVITY` | `permissive` | CSV/tabular preset |
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Security notes
|
|
256
|
+
|
|
257
|
+
- All detection is **pure regex** — deterministic, zero LLM calls, zero network access, < 5 ms per input.
|
|
258
|
+
- Audit records store a **SHA-256 hash** and a **96-character redacted preview** of the input. Raw user text is never written to disk.
|
|
259
|
+
- The package ships **sample rules** that cover common attack patterns. Review and extend them for your production threat model using `DetectionConfig.custom_patterns` or a YAML config file loaded with `load_prompt_injection_config()`.
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## License
|
|
264
|
+
|
|
265
|
+
MIT — see [LICENSE](LICENSE).
|
|
266
|
+
|
|
267
|
+
Detection rules and evaluator logic originally derived from [microsoft/agent-governance-toolkit](https://github.com/microsoft/agent-governance-toolkit) (MIT).
|
cognexus-0.1.0/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# cognexus
|
|
2
|
+
|
|
3
|
+
**OWASP-aligned prompt defence, injection detection, and audit logging for LLM applications.**
|
|
4
|
+
|
|
5
|
+
`cognexus` gives you two complementary security layers and a tamper-evident audit trail — all in pure Python with zero mandatory dependencies.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
pip install cognexus
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
| Layer | What it does |
|
|
16
|
+
|---|---|
|
|
17
|
+
| **Static prompt defence** | Grades system prompts A–F against 12 OWASP LLM Top-10 attack vectors before deployment |
|
|
18
|
+
| **Runtime injection detection** | Screens user input, RAG content, and tabular payloads at request time |
|
|
19
|
+
| **Audit events** | Append-only JSONL trail for every detected injection — no raw text stored |
|
|
20
|
+
|
|
21
|
+
### Detection coverage
|
|
22
|
+
|
|
23
|
+
- Direct instruction override
|
|
24
|
+
- Delimiter and context-boundary attacks
|
|
25
|
+
- Base64 / hex / ROT13 encoding attacks
|
|
26
|
+
- Role-play and jailbreak language (DAN mode, developer mode, etc.)
|
|
27
|
+
- Context manipulation ("your real instructions are…")
|
|
28
|
+
- Canary token leak detection
|
|
29
|
+
- Multi-turn escalation
|
|
30
|
+
- Cross-plugin / tool-chaining attacks (OWASP ASI04)
|
|
31
|
+
- Markup injection (XSS gadgets in model-visible text)
|
|
32
|
+
- Zero-width / token-smuggling unicode attacks
|
|
33
|
+
- Credential exfiltration requests
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Quick-start
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from cognexus import (
|
|
41
|
+
augment_system_prompt,
|
|
42
|
+
evaluate_system_prompt,
|
|
43
|
+
screen_user_input,
|
|
44
|
+
should_block,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# 1. Augment your system prompt so it scores grade A before inference
|
|
48
|
+
system = augment_system_prompt("You are a helpful customer support agent.")
|
|
49
|
+
report = evaluate_system_prompt(system)
|
|
50
|
+
print(report.grade) # "A"
|
|
51
|
+
print(report.score) # 100
|
|
52
|
+
print(report.missing) # []
|
|
53
|
+
|
|
54
|
+
# 2. Screen every user message at request time
|
|
55
|
+
result = screen_user_input(user_message, source="chat")
|
|
56
|
+
|
|
57
|
+
if should_block(result):
|
|
58
|
+
raise PermissionError(f"Injection blocked: {result.explanation}")
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Screening helpers
|
|
64
|
+
|
|
65
|
+
Three presets cover the most common LLM input surfaces:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from cognexus import (
|
|
69
|
+
screen_user_input, # balanced sensitivity — direct chat messages
|
|
70
|
+
screen_external_content, # strict sensitivity — RAG / web / API content
|
|
71
|
+
screen_tabular_payload, # permissive — CSV / dataframe blobs
|
|
72
|
+
should_block,
|
|
73
|
+
wrap_untrusted_content,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
# Wrap RAG content before inserting into a prompt
|
|
77
|
+
safe_chunk = wrap_untrusted_content("web_search", raw_text)
|
|
78
|
+
|
|
79
|
+
# Screen it too
|
|
80
|
+
result = screen_external_content(raw_text, source="web_search", user_id=user.id)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Sensitivity presets
|
|
84
|
+
|
|
85
|
+
| Preset | Threshold | Min threat flagged | Use for |
|
|
86
|
+
|---|---|---|---|
|
|
87
|
+
| `strict` | 0.3 | LOW | External / RAG content |
|
|
88
|
+
| `balanced` | 0.5 | LOW | Direct user input |
|
|
89
|
+
| `permissive` | 0.7 | HIGH | CSV / tabular payloads |
|
|
90
|
+
|
|
91
|
+
Override via environment variables:
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
COGNEXUS_PROMPT_INJECTION_USER_SENSITIVITY=balanced
|
|
95
|
+
COGNEXUS_PROMPT_INJECTION_EXTERNAL_SENSITIVITY=strict
|
|
96
|
+
COGNEXUS_PROMPT_INJECTION_TABULAR_SENSITIVITY=permissive
|
|
97
|
+
COGNEXUS_PROMPT_INJECTION_BLOCK=0 # set to 1 to block any hit, not just CRITICAL
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Using the core classes directly
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from cognexus import PromptInjectionDetector, DetectionConfig, InjectionType
|
|
106
|
+
|
|
107
|
+
detector = PromptInjectionDetector(
|
|
108
|
+
config=DetectionConfig(
|
|
109
|
+
sensitivity="strict",
|
|
110
|
+
blocklist=["my-internal-keyword"],
|
|
111
|
+
allowlist=["safe phrase"],
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
result = detector.detect(text, source="api_gateway")
|
|
116
|
+
print(result.is_injection) # True / False
|
|
117
|
+
print(result.threat_level) # ThreatLevel.HIGH
|
|
118
|
+
print(result.injection_type) # InjectionType.DIRECT_OVERRIDE
|
|
119
|
+
print(result.confidence) # 0.9
|
|
120
|
+
print(result.matched_patterns) # ["direct_override:..."]
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Audit events
|
|
126
|
+
|
|
127
|
+
Detections are automatically written to a JSONL file (no raw input stored):
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
# Events go to $COGNEXUS_PROMPT_DEFENSE_EVENTS_DIR/prompt_defense_events.jsonl
|
|
131
|
+
# (falls back to $REPORTS_DIR, then /tmp)
|
|
132
|
+
|
|
133
|
+
from cognexus.events import read_recent_events
|
|
134
|
+
|
|
135
|
+
rows = read_recent_events(user_id=42, limit=20)
|
|
136
|
+
# [{"ts": "...", "kind": "prompt_injection", "threat": "high", ...}, ...]
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Custom event sink (database, queue, dashboard)
|
|
140
|
+
|
|
141
|
+
Pass an `on_event` callback to mirror records into your own store:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
def save_to_db(record: dict) -> None:
|
|
145
|
+
db.execute("INSERT INTO security_events ...", record)
|
|
146
|
+
|
|
147
|
+
screen_user_input(text, source="chat", user_id=user.id, on_event=save_to_db)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## Static prompt defence — standalone
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from cognexus import PromptDefenseEvaluator, PromptDefenseConfig
|
|
156
|
+
|
|
157
|
+
evaluator = PromptDefenseEvaluator(
|
|
158
|
+
config=PromptDefenseConfig(min_grade="B")
|
|
159
|
+
)
|
|
160
|
+
report = evaluator.evaluate(my_system_prompt)
|
|
161
|
+
|
|
162
|
+
print(report.grade) # "C"
|
|
163
|
+
print(report.score) # 58
|
|
164
|
+
print(report.missing) # ["unicode-attack", "context-overflow"]
|
|
165
|
+
|
|
166
|
+
if report.is_blocking():
|
|
167
|
+
print("System prompt is below minimum grade — fix before deploying.")
|
|
168
|
+
|
|
169
|
+
# Evaluate a file
|
|
170
|
+
report = evaluator.evaluate_file("prompts/assistant.txt")
|
|
171
|
+
|
|
172
|
+
# Batch evaluation
|
|
173
|
+
reports = evaluator.evaluate_batch({
|
|
174
|
+
"chat": chat_prompt,
|
|
175
|
+
"analyst": analyst_prompt,
|
|
176
|
+
})
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Environment variables
|
|
182
|
+
|
|
183
|
+
| Variable | Default | Purpose |
|
|
184
|
+
|---|---|---|
|
|
185
|
+
| `COGNEXUS_PROMPT_DEFENSE_EVENTS_DIR` | `/tmp` | JSONL audit file directory |
|
|
186
|
+
| `COGNEXUS_PROMPT_INJECTION_LOG` | `1` | Log clean scans at DEBUG |
|
|
187
|
+
| `COGNEXUS_PROMPT_INJECTION_BLOCK` | `0` | Block any injection (not just CRITICAL) |
|
|
188
|
+
| `COGNEXUS_PROMPT_INJECTION_USER_SENSITIVITY` | `balanced` | User-input preset |
|
|
189
|
+
| `COGNEXUS_PROMPT_INJECTION_EXTERNAL_SENSITIVITY` | `strict` | External/RAG preset |
|
|
190
|
+
| `COGNEXUS_PROMPT_INJECTION_TABULAR_SENSITIVITY` | `permissive` | CSV/tabular preset |
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Security notes
|
|
195
|
+
|
|
196
|
+
- All detection is **pure regex** — deterministic, zero LLM calls, zero network access, < 5 ms per input.
|
|
197
|
+
- Audit records store a **SHA-256 hash** and a **96-character redacted preview** of the input. Raw user text is never written to disk.
|
|
198
|
+
- The package ships **sample rules** that cover common attack patterns. Review and extend them for your production threat model using `DetectionConfig.custom_patterns` or a YAML config file loaded with `load_prompt_injection_config()`.
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT — see [LICENSE](LICENSE).
|
|
205
|
+
|
|
206
|
+
Detection rules and evaluator logic originally derived from [microsoft/agent-governance-toolkit](https://github.com/microsoft/agent-governance-toolkit) (MIT).
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cognexus"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "OWASP-aligned LLM prompt defence, injection detection, and audit logging"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Odenthal", email = "" },
|
|
14
|
+
]
|
|
15
|
+
maintainers = [
|
|
16
|
+
{ name = "Odenthal", email = "" },
|
|
17
|
+
]
|
|
18
|
+
keywords = [
|
|
19
|
+
"llm",
|
|
20
|
+
"security",
|
|
21
|
+
"prompt-injection",
|
|
22
|
+
"prompt-defense",
|
|
23
|
+
"owasp",
|
|
24
|
+
"ai-safety",
|
|
25
|
+
"audit",
|
|
26
|
+
"guardrails",
|
|
27
|
+
]
|
|
28
|
+
classifiers = [
|
|
29
|
+
"Development Status :: 4 - Beta",
|
|
30
|
+
"Intended Audience :: Developers",
|
|
31
|
+
"License :: OSI Approved :: MIT License",
|
|
32
|
+
"Operating System :: OS Independent",
|
|
33
|
+
"Programming Language :: Python :: 3",
|
|
34
|
+
"Programming Language :: Python :: 3.10",
|
|
35
|
+
"Programming Language :: Python :: 3.11",
|
|
36
|
+
"Programming Language :: Python :: 3.12",
|
|
37
|
+
"Topic :: Security",
|
|
38
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
39
|
+
"Typing :: Typed",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
# Zero mandatory runtime dependencies — pure stdlib.
|
|
43
|
+
dependencies = []
|
|
44
|
+
|
|
45
|
+
[project.optional-dependencies]
|
|
46
|
+
# Required only if you load custom detection patterns from YAML files via
|
|
47
|
+
# load_prompt_injection_config().
|
|
48
|
+
yaml = ["pyyaml>=6.0"]
|
|
49
|
+
|
|
50
|
+
dev = [
|
|
51
|
+
"pytest>=8.0",
|
|
52
|
+
"ruff>=0.4",
|
|
53
|
+
"build",
|
|
54
|
+
"twine",
|
|
55
|
+
"pyyaml>=6.0",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[project.urls]
|
|
59
|
+
Homepage = "https://github.com/CogNexusLabs/cognexus"
|
|
60
|
+
Source = "https://github.com/CogNexusLabs/cognexus"
|
|
61
|
+
Issues = "https://github.com/CogNexusLabs/cognexus/issues"
|
|
62
|
+
Changelog = "https://github.com/CogNexusLabs/cognexus/releases"
|
|
63
|
+
|
|
64
|
+
[tool.hatch.build.targets.wheel]
|
|
65
|
+
packages = ["src/cognexus"]
|
|
66
|
+
|
|
67
|
+
[tool.pytest.ini_options]
|
|
68
|
+
testpaths = ["tests"]
|
|
69
|
+
addopts = "-v"
|
|
70
|
+
|
|
71
|
+
[tool.ruff]
|
|
72
|
+
line-length = 100
|
|
73
|
+
target-version = "py310"
|
|
74
|
+
|
|
75
|
+
[tool.ruff.lint]
|
|
76
|
+
select = ["E", "F", "W", "I", "UP"]
|
|
77
|
+
ignore = ["E501"]
|