sovereign-shield-ch 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.
- sovereign_shield_ch-0.1.0/.gitignore +45 -0
- sovereign_shield_ch-0.1.0/LICENSE +201 -0
- sovereign_shield_ch-0.1.0/NOTICE +16 -0
- sovereign_shield_ch-0.1.0/PKG-INFO +159 -0
- sovereign_shield_ch-0.1.0/README.md +129 -0
- sovereign_shield_ch-0.1.0/RELEASING.md +54 -0
- sovereign_shield_ch-0.1.0/pyproject.toml +131 -0
- sovereign_shield_ch-0.1.0/scripts/gen_gateway_corpus.py +231 -0
- sovereign_shield_ch-0.1.0/scripts/gen_shield_vectors.py +104 -0
- sovereign_shield_ch-0.1.0/src/sovereign_shield/__init__.py +67 -0
- sovereign_shield_ch-0.1.0/src/sovereign_shield/core.py +237 -0
- sovereign_shield_ch-0.1.0/src/sovereign_shield/gateway.py +114 -0
- sovereign_shield_ch-0.1.0/src/sovereign_shield/leak.py +37 -0
- sovereign_shield_ch-0.1.0/src/sovereign_shield/pii.py +225 -0
- sovereign_shield_ch-0.1.0/src/sovereign_shield/shield.py +94 -0
- sovereign_shield_ch-0.1.0/tests/test_core.py +140 -0
- sovereign_shield_ch-0.1.0/tests/test_leak.py +35 -0
- sovereign_shield_ch-0.1.0/tests/test_pii.py +100 -0
- sovereign_shield_ch-0.1.0/tests/test_shield.py +61 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# --- secrets ---
|
|
2
|
+
.env
|
|
3
|
+
.env.*
|
|
4
|
+
!.env.example
|
|
5
|
+
*.pem
|
|
6
|
+
*.key
|
|
7
|
+
|
|
8
|
+
# --- python ---
|
|
9
|
+
__pycache__/
|
|
10
|
+
*.py[cod]
|
|
11
|
+
*$py.class
|
|
12
|
+
*.egg-info/
|
|
13
|
+
*.egg
|
|
14
|
+
build/
|
|
15
|
+
dist/
|
|
16
|
+
.Python
|
|
17
|
+
.venv/
|
|
18
|
+
venv/
|
|
19
|
+
.python-version
|
|
20
|
+
|
|
21
|
+
# --- python tooling caches ---
|
|
22
|
+
.ruff_cache/
|
|
23
|
+
.mypy_cache/
|
|
24
|
+
.pytest_cache/
|
|
25
|
+
.coverage
|
|
26
|
+
.coverage.*
|
|
27
|
+
htmlcov/
|
|
28
|
+
coverage.xml
|
|
29
|
+
|
|
30
|
+
# --- node / next.js (web/ demo) ---
|
|
31
|
+
node_modules/
|
|
32
|
+
.next/
|
|
33
|
+
out/
|
|
34
|
+
.vercel/
|
|
35
|
+
*.tsbuildinfo
|
|
36
|
+
next-env.d.ts
|
|
37
|
+
|
|
38
|
+
# --- editor / OS / agent ---
|
|
39
|
+
.vscode/
|
|
40
|
+
.idea/
|
|
41
|
+
.claude/
|
|
42
|
+
.DS_Store
|
|
43
|
+
*.swp
|
|
44
|
+
*.swo
|
|
45
|
+
*~
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Sovereign Shield
|
|
2
|
+
Copyright 2026 acoseac
|
|
3
|
+
|
|
4
|
+
This product includes software developed by acoseac and contributors
|
|
5
|
+
under the Apache License, Version 2.0.
|
|
6
|
+
|
|
7
|
+
Sovereign Shield is an engineering utility for programmatic privacy
|
|
8
|
+
mitigation (deterministic Swiss/EU PII detection and reversible
|
|
9
|
+
tokenization). It is NOT an automated guarantee of regulatory compliance
|
|
10
|
+
under the Swiss Federal Act on Data Protection (FADP) or the EU GDPR, and
|
|
11
|
+
it is not legal advice. Structural, context-independent detection cannot
|
|
12
|
+
catch every leak vector. Use it alongside audit logs, a DPIA where
|
|
13
|
+
required, and human review. See the README for the full disclaimer.
|
|
14
|
+
|
|
15
|
+
The deterministic detection core is extracted from the K.E.V.I.N.
|
|
16
|
+
adversarial-testing research project.
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sovereign-shield-ch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministic, offline Swiss/EU (FADP/GDPR) PII gateway: tokenize personal data before a cloud LLM sees it, restore it on the way back.
|
|
5
|
+
Project-URL: Homepage, https://github.com/acoseac/sovereign-shield
|
|
6
|
+
Project-URL: Repository, https://github.com/acoseac/sovereign-shield
|
|
7
|
+
Project-URL: Issues, https://github.com/acoseac/sovereign-shield/issues
|
|
8
|
+
Project-URL: Live demo, https://shield.ars.md
|
|
9
|
+
Author: acoseac
|
|
10
|
+
License: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
License-File: NOTICE
|
|
13
|
+
Keywords: anonymization,compliance,data-sovereignty,fadp,gdpr,llm,pii,privacy,swiss,tokenization
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Security
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
26
|
+
Provides-Extra: gateway
|
|
27
|
+
Requires-Dist: langchain-core>=0.3; extra == 'gateway'
|
|
28
|
+
Requires-Dist: langchain-google-genai>=2.0; extra == 'gateway'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# Sovereign Shield
|
|
32
|
+
|
|
33
|
+
**Use any cloud LLM. Keep the personal data in Switzerland.**
|
|
34
|
+
|
|
35
|
+
A deterministic, offline gateway for Swiss/EU (FADP / GDPR) personal data. It
|
|
36
|
+
tokenizes structured identifiers **locally** — turning `756.1234.5678.97` into
|
|
37
|
+
`[AHV_1]` — so a prompt can go to Gemini, Claude, or any model without a real
|
|
38
|
+
identifier ever crossing the border, then restores the real values in the reply
|
|
39
|
+
on the way back. Detection is regex + checksum, not ML: zero dependencies, zero
|
|
40
|
+
latency, and it **cannot be talked out of a match**.
|
|
41
|
+
|
|
42
|
+
> ⚠️ **Disclaimer.** Sovereign Shield is an engineering utility that aids
|
|
43
|
+
> programmatic privacy mitigation. It is **not** an automated guarantee of
|
|
44
|
+
> regulatory compliance under the Swiss Federal Act on Data Protection (FADP) or
|
|
45
|
+
> the EU GDPR, and it is **not legal advice**. Context-dependent leak vectors
|
|
46
|
+
> (free-text names, encoded data, semantics) can still slip past a structural,
|
|
47
|
+
> deterministic layer. Use it alongside a DPIA where required, audit logs, and
|
|
48
|
+
> human review — as the outer, deliberately-dumb layer of a defence-in-depth stack.
|
|
49
|
+
|
|
50
|
+
[**See it live → shield.ars.md**](https://shield.ars.md) · deterministic, in-browser, no API key.
|
|
51
|
+
|
|
52
|
+
## Install
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install sovereign-shield-ch # core: stdlib-only, zero dependencies
|
|
56
|
+
pip install "sovereign-shield-ch[gateway]" # + the optional LangChain proxy
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Requires Python 3.12+.
|
|
60
|
+
|
|
61
|
+
## Quickstart
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from sovereign_shield import SovereignShield
|
|
65
|
+
|
|
66
|
+
shield = SovereignShield()
|
|
67
|
+
|
|
68
|
+
raw = ("Guten Tag. Meine AHV-Nummer ist 756.1234.5678.97. Bitte die Praemie auf "
|
|
69
|
+
"IBAN CH9300762011623852957 zurueckerstatten. Erreichbar unter "
|
|
70
|
+
"+41 79 214 88 03 oder hans.muster@bluewin.ch.")
|
|
71
|
+
|
|
72
|
+
# 1. De-identify locally. `safe` is all that crosses the border.
|
|
73
|
+
safe, ctx = shield.sanitize(raw)
|
|
74
|
+
# safe -> "Guten Tag. Meine AHV-Nummer ist [AHV_1]. Bitte die Praemie auf
|
|
75
|
+
# IBAN [IBAN_1] zurueckerstatten. Erreichbar unter [PHONE_1] oder [EMAIL_1]."
|
|
76
|
+
print(ctx.audit()) # {'ch_ahv': 1, 'iban': 1, 'ch_phone': 1, 'email': 1}
|
|
77
|
+
|
|
78
|
+
# 2. Call any cloud LLM on the placeholders (it never sees a real value).
|
|
79
|
+
answer = call_your_llm(safe)
|
|
80
|
+
|
|
81
|
+
# 3. Restore the real values locally before serving the user.
|
|
82
|
+
result = shield.rehydrate(answer, ctx)
|
|
83
|
+
print(result.text) # real AHV / IBAN / phone / email swapped back in
|
|
84
|
+
print(result.clean) # True if the model didn't mangle a placeholder
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`sanitize` is **fail-closed**: if any structured identifier would survive into
|
|
88
|
+
`safe`, it raises `DataLeakError` instead of leaking. `rehydrate` is strict and
|
|
89
|
+
deterministic, and reports any placeholder the model mangled or invented
|
|
90
|
+
(`result.leftover`) so you never ship a broken `[AHV_1` to a user.
|
|
91
|
+
|
|
92
|
+
### Transparent LangChain proxy
|
|
93
|
+
|
|
94
|
+
With the `[gateway]` extra, wrap any LangChain chat model and call it as usual —
|
|
95
|
+
sanitize-out and rehydrate-in happen under the hood:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
99
|
+
from sovereign_shield.gateway import ShieldedChatModel
|
|
100
|
+
|
|
101
|
+
llm = ShieldedChatModel(ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.3))
|
|
102
|
+
|
|
103
|
+
reply = llm.invoke("Refund AHV 756.1234.5678.97 to IBAN CH9300762011623852957.")
|
|
104
|
+
print(reply.content) # real values restored
|
|
105
|
+
print(reply.additional_kwargs["sovereign_shield"]) # {'kept_on_shore': 2, 'leftover': []}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## What it detects
|
|
109
|
+
|
|
110
|
+
Deterministic *shape regex + checksum* — the checksum rejects look-alikes so the
|
|
111
|
+
guard never trips on a random 13-digit string. Separators are stripped first, so
|
|
112
|
+
`756.1234.5678.97` and `756 1234 5678 97` validate identically.
|
|
113
|
+
|
|
114
|
+
| Category | Identifier | Validation |
|
|
115
|
+
|---|---|---|
|
|
116
|
+
| `ch_ahv` | Swiss AHV / AVS number | EAN-13 check digit |
|
|
117
|
+
| `iban` | CH / LI IBAN | ISO-7064 mod-97 |
|
|
118
|
+
| `credit_card` | Card PAN | Luhn |
|
|
119
|
+
| `ch_phone` | Swiss phone | shape only |
|
|
120
|
+
| `email` | Email | shape only |
|
|
121
|
+
| `dob` | Date of birth | off by default (bare dates false-positive) |
|
|
122
|
+
|
|
123
|
+
**Scope: structured identifiers only.** Person names and street addresses are
|
|
124
|
+
*not* detected — they need an NER model, which would forfeit the deterministic,
|
|
125
|
+
zero-dependency guarantee. Plug your own via `SovereignShield(extra_detectors=[...])`
|
|
126
|
+
(see `SpanDetector`); overlapping spans are dropped fail-closed.
|
|
127
|
+
|
|
128
|
+
**Not encoding-robust.** A model that base64s or ciphers an identifier defeats the
|
|
129
|
+
regex. Separator/whitespace reformatting is handled; encoding is not.
|
|
130
|
+
|
|
131
|
+
## How it works
|
|
132
|
+
|
|
133
|
+
The thesis, proven in the [K.E.V.I.N.](https://github.com/acoseac/kevin) red-team
|
|
134
|
+
research this is extracted from: you can't close a data leak from *inside* the
|
|
135
|
+
model — a jailbreak, a pretext, or a forced output schema will make it disclose.
|
|
136
|
+
So you put a **deterministic, offline boundary** around the model instead. Sovereign
|
|
137
|
+
Shield is that boundary, as a library: detect → tokenize → (model) → restore.
|
|
138
|
+
|
|
139
|
+
The browser demo ships a TypeScript port of the exact same detectors, kept
|
|
140
|
+
**byte-for-byte in parity** with this Python source by a generated vector suite —
|
|
141
|
+
so redaction on the client and on the server can never silently drift.
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
pip install -e ".[dev]"
|
|
147
|
+
pytest # unit + round-trip suite
|
|
148
|
+
ruff check . && ruff format --check . && mypy
|
|
149
|
+
python scripts/gen_shield_vectors.py --check # Python parity vectors current
|
|
150
|
+
cd web && npm install && npm run parity # TS shield reproduces them exactly
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The `web/` directory is the live demo (Next.js). See [web/README.md](web/README.md).
|
|
154
|
+
|
|
155
|
+
## Credits & license
|
|
156
|
+
|
|
157
|
+
Extracted from the [K.E.V.I.N.](https://github.com/acoseac/kevin) adversarial-testing
|
|
158
|
+
project; background in the [FADP AI-gateway write-up](https://www.ars.md/blog).
|
|
159
|
+
Licensed under [Apache-2.0](LICENSE).
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Sovereign Shield
|
|
2
|
+
|
|
3
|
+
**Use any cloud LLM. Keep the personal data in Switzerland.**
|
|
4
|
+
|
|
5
|
+
A deterministic, offline gateway for Swiss/EU (FADP / GDPR) personal data. It
|
|
6
|
+
tokenizes structured identifiers **locally** — turning `756.1234.5678.97` into
|
|
7
|
+
`[AHV_1]` — so a prompt can go to Gemini, Claude, or any model without a real
|
|
8
|
+
identifier ever crossing the border, then restores the real values in the reply
|
|
9
|
+
on the way back. Detection is regex + checksum, not ML: zero dependencies, zero
|
|
10
|
+
latency, and it **cannot be talked out of a match**.
|
|
11
|
+
|
|
12
|
+
> ⚠️ **Disclaimer.** Sovereign Shield is an engineering utility that aids
|
|
13
|
+
> programmatic privacy mitigation. It is **not** an automated guarantee of
|
|
14
|
+
> regulatory compliance under the Swiss Federal Act on Data Protection (FADP) or
|
|
15
|
+
> the EU GDPR, and it is **not legal advice**. Context-dependent leak vectors
|
|
16
|
+
> (free-text names, encoded data, semantics) can still slip past a structural,
|
|
17
|
+
> deterministic layer. Use it alongside a DPIA where required, audit logs, and
|
|
18
|
+
> human review — as the outer, deliberately-dumb layer of a defence-in-depth stack.
|
|
19
|
+
|
|
20
|
+
[**See it live → shield.ars.md**](https://shield.ars.md) · deterministic, in-browser, no API key.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install sovereign-shield-ch # core: stdlib-only, zero dependencies
|
|
26
|
+
pip install "sovereign-shield-ch[gateway]" # + the optional LangChain proxy
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Requires Python 3.12+.
|
|
30
|
+
|
|
31
|
+
## Quickstart
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from sovereign_shield import SovereignShield
|
|
35
|
+
|
|
36
|
+
shield = SovereignShield()
|
|
37
|
+
|
|
38
|
+
raw = ("Guten Tag. Meine AHV-Nummer ist 756.1234.5678.97. Bitte die Praemie auf "
|
|
39
|
+
"IBAN CH9300762011623852957 zurueckerstatten. Erreichbar unter "
|
|
40
|
+
"+41 79 214 88 03 oder hans.muster@bluewin.ch.")
|
|
41
|
+
|
|
42
|
+
# 1. De-identify locally. `safe` is all that crosses the border.
|
|
43
|
+
safe, ctx = shield.sanitize(raw)
|
|
44
|
+
# safe -> "Guten Tag. Meine AHV-Nummer ist [AHV_1]. Bitte die Praemie auf
|
|
45
|
+
# IBAN [IBAN_1] zurueckerstatten. Erreichbar unter [PHONE_1] oder [EMAIL_1]."
|
|
46
|
+
print(ctx.audit()) # {'ch_ahv': 1, 'iban': 1, 'ch_phone': 1, 'email': 1}
|
|
47
|
+
|
|
48
|
+
# 2. Call any cloud LLM on the placeholders (it never sees a real value).
|
|
49
|
+
answer = call_your_llm(safe)
|
|
50
|
+
|
|
51
|
+
# 3. Restore the real values locally before serving the user.
|
|
52
|
+
result = shield.rehydrate(answer, ctx)
|
|
53
|
+
print(result.text) # real AHV / IBAN / phone / email swapped back in
|
|
54
|
+
print(result.clean) # True if the model didn't mangle a placeholder
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`sanitize` is **fail-closed**: if any structured identifier would survive into
|
|
58
|
+
`safe`, it raises `DataLeakError` instead of leaking. `rehydrate` is strict and
|
|
59
|
+
deterministic, and reports any placeholder the model mangled or invented
|
|
60
|
+
(`result.leftover`) so you never ship a broken `[AHV_1` to a user.
|
|
61
|
+
|
|
62
|
+
### Transparent LangChain proxy
|
|
63
|
+
|
|
64
|
+
With the `[gateway]` extra, wrap any LangChain chat model and call it as usual —
|
|
65
|
+
sanitize-out and rehydrate-in happen under the hood:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
69
|
+
from sovereign_shield.gateway import ShieldedChatModel
|
|
70
|
+
|
|
71
|
+
llm = ShieldedChatModel(ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.3))
|
|
72
|
+
|
|
73
|
+
reply = llm.invoke("Refund AHV 756.1234.5678.97 to IBAN CH9300762011623852957.")
|
|
74
|
+
print(reply.content) # real values restored
|
|
75
|
+
print(reply.additional_kwargs["sovereign_shield"]) # {'kept_on_shore': 2, 'leftover': []}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## What it detects
|
|
79
|
+
|
|
80
|
+
Deterministic *shape regex + checksum* — the checksum rejects look-alikes so the
|
|
81
|
+
guard never trips on a random 13-digit string. Separators are stripped first, so
|
|
82
|
+
`756.1234.5678.97` and `756 1234 5678 97` validate identically.
|
|
83
|
+
|
|
84
|
+
| Category | Identifier | Validation |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| `ch_ahv` | Swiss AHV / AVS number | EAN-13 check digit |
|
|
87
|
+
| `iban` | CH / LI IBAN | ISO-7064 mod-97 |
|
|
88
|
+
| `credit_card` | Card PAN | Luhn |
|
|
89
|
+
| `ch_phone` | Swiss phone | shape only |
|
|
90
|
+
| `email` | Email | shape only |
|
|
91
|
+
| `dob` | Date of birth | off by default (bare dates false-positive) |
|
|
92
|
+
|
|
93
|
+
**Scope: structured identifiers only.** Person names and street addresses are
|
|
94
|
+
*not* detected — they need an NER model, which would forfeit the deterministic,
|
|
95
|
+
zero-dependency guarantee. Plug your own via `SovereignShield(extra_detectors=[...])`
|
|
96
|
+
(see `SpanDetector`); overlapping spans are dropped fail-closed.
|
|
97
|
+
|
|
98
|
+
**Not encoding-robust.** A model that base64s or ciphers an identifier defeats the
|
|
99
|
+
regex. Separator/whitespace reformatting is handled; encoding is not.
|
|
100
|
+
|
|
101
|
+
## How it works
|
|
102
|
+
|
|
103
|
+
The thesis, proven in the [K.E.V.I.N.](https://github.com/acoseac/kevin) red-team
|
|
104
|
+
research this is extracted from: you can't close a data leak from *inside* the
|
|
105
|
+
model — a jailbreak, a pretext, or a forced output schema will make it disclose.
|
|
106
|
+
So you put a **deterministic, offline boundary** around the model instead. Sovereign
|
|
107
|
+
Shield is that boundary, as a library: detect → tokenize → (model) → restore.
|
|
108
|
+
|
|
109
|
+
The browser demo ships a TypeScript port of the exact same detectors, kept
|
|
110
|
+
**byte-for-byte in parity** with this Python source by a generated vector suite —
|
|
111
|
+
so redaction on the client and on the server can never silently drift.
|
|
112
|
+
|
|
113
|
+
## Development
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
pip install -e ".[dev]"
|
|
117
|
+
pytest # unit + round-trip suite
|
|
118
|
+
ruff check . && ruff format --check . && mypy
|
|
119
|
+
python scripts/gen_shield_vectors.py --check # Python parity vectors current
|
|
120
|
+
cd web && npm install && npm run parity # TS shield reproduces them exactly
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The `web/` directory is the live demo (Next.js). See [web/README.md](web/README.md).
|
|
124
|
+
|
|
125
|
+
## Credits & license
|
|
126
|
+
|
|
127
|
+
Extracted from the [K.E.V.I.N.](https://github.com/acoseac/kevin) adversarial-testing
|
|
128
|
+
project; background in the [FADP AI-gateway write-up](https://www.ars.md/blog).
|
|
129
|
+
Licensed under [Apache-2.0](LICENSE).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Releasing
|
|
2
|
+
|
|
3
|
+
`sovereign-shield-ch` publishes to PyPI via **Trusted Publishing** (OIDC) — no API
|
|
4
|
+
token or secret is stored in the repo. The [`Release`](.github/workflows/release.yml)
|
|
5
|
+
workflow builds the sdist + wheel, runs `twine check --strict`, and uploads
|
|
6
|
+
whenever a **GitHub Release** is published.
|
|
7
|
+
|
|
8
|
+
## One-time setup (before the first release)
|
|
9
|
+
|
|
10
|
+
The PyPI project does not exist yet, so register a **pending publisher** at
|
|
11
|
+
<https://pypi.org/manage/account/publishing/> with these exact values:
|
|
12
|
+
|
|
13
|
+
| Field | Value |
|
|
14
|
+
|---|---|
|
|
15
|
+
| PyPI Project Name | `sovereign-shield-ch` |
|
|
16
|
+
| Owner | `acoseac` |
|
|
17
|
+
| Repository name | `sovereign-shield` |
|
|
18
|
+
| Workflow name | `release.yml` |
|
|
19
|
+
| Environment name | `pypi` |
|
|
20
|
+
|
|
21
|
+
Then, in the GitHub repo, create an **environment** named `pypi`
|
|
22
|
+
(Settings → Environments → New environment). Optionally add yourself as a
|
|
23
|
+
required reviewer so a publish can't run without a manual approval — the workflow
|
|
24
|
+
already references `environment: pypi`.
|
|
25
|
+
|
|
26
|
+
(Optional) Register the same as a pending publisher on
|
|
27
|
+
<https://test.pypi.org/manage/account/publishing/> if you want to rehearse
|
|
28
|
+
against TestPyPI first.
|
|
29
|
+
|
|
30
|
+
## Cutting a release
|
|
31
|
+
|
|
32
|
+
1. Bump the version — **single source of truth** is `__version__` in
|
|
33
|
+
[`src/sovereign_shield/__init__.py`](src/sovereign_shield/__init__.py)
|
|
34
|
+
(hatchling reads it; `pyproject.toml` has no separate version to sync).
|
|
35
|
+
2. Commit and merge to `main` (CI green).
|
|
36
|
+
3. Tag and publish a GitHub Release matching the version:
|
|
37
|
+
```bash
|
|
38
|
+
gh release create v0.1.0 --title v0.1.0 --generate-notes
|
|
39
|
+
```
|
|
40
|
+
4. The `Release` workflow runs automatically: build → `twine check --strict` →
|
|
41
|
+
publish to PyPI via OIDC. Watch it under the **Actions** tab (and approve the
|
|
42
|
+
`pypi` environment if you enabled a required reviewer).
|
|
43
|
+
|
|
44
|
+
## Local dry run
|
|
45
|
+
|
|
46
|
+
Reproduce exactly what CI does, without publishing:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install -e ".[dev]" build twine
|
|
50
|
+
python -m build
|
|
51
|
+
twine check --strict dist/*
|
|
52
|
+
# inspect the wheel is package-only (no web/ demo):
|
|
53
|
+
python -m zipfile -l dist/*.whl
|
|
54
|
+
```
|