aliasgate 0.1.0__py3-none-any.whl
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.
- aliasgate/__init__.py +5 -0
- aliasgate/__main__.py +8 -0
- aliasgate/audit.py +84 -0
- aliasgate/cli.py +321 -0
- aliasgate/config.py +367 -0
- aliasgate/detectors/__init__.py +1 -0
- aliasgate/detectors/cache.py +57 -0
- aliasgate/detectors/checksums.py +88 -0
- aliasgate/detectors/chunking.py +44 -0
- aliasgate/detectors/dictionary.py +96 -0
- aliasgate/detectors/gliner.py +195 -0
- aliasgate/detectors/literal.py +17 -0
- aliasgate/detectors/rules.py +318 -0
- aliasgate/dialects/__init__.py +1 -0
- aliasgate/dialects/anthropic_messages.py +329 -0
- aliasgate/dialects/base.py +193 -0
- aliasgate/dialects/openai_chat.py +359 -0
- aliasgate/dialects/sse.py +108 -0
- aliasgate/engine.py +237 -0
- aliasgate/eval/__init__.py +4 -0
- aliasgate/eval/__main__.py +139 -0
- aliasgate/eval/corpus.py +105 -0
- aliasgate/eval/metrics.py +197 -0
- aliasgate/eval/report.py +103 -0
- aliasgate/factory.py +67 -0
- aliasgate/mapping.py +103 -0
- aliasgate/metrics.py +131 -0
- aliasgate/placeholders.py +41 -0
- aliasgate/proxy/__init__.py +1 -0
- aliasgate/proxy/app.py +602 -0
- aliasgate/proxy/errors.py +106 -0
- aliasgate/proxy/headers.py +50 -0
- aliasgate/py.typed +0 -0
- aliasgate/resolve.py +32 -0
- aliasgate/stream.py +50 -0
- aliasgate/terms.py +18 -0
- aliasgate/types.py +70 -0
- aliasgate-0.1.0.dist-info/METADATA +151 -0
- aliasgate-0.1.0.dist-info/RECORD +42 -0
- aliasgate-0.1.0.dist-info/WHEEL +4 -0
- aliasgate-0.1.0.dist-info/entry_points.txt +2 -0
- aliasgate-0.1.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Errors the gateway answers itself, with fixed messages chosen by the gateway."""
|
|
2
|
+
|
|
3
|
+
from starlette.responses import JSONResponse
|
|
4
|
+
|
|
5
|
+
from aliasgate.dialects.base import Dialect
|
|
6
|
+
from aliasgate.metrics import Outcome
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class GatewayError(Exception):
|
|
10
|
+
"""An error response produced by the gateway. Its message never contains request text.
|
|
11
|
+
|
|
12
|
+
`outcome` classifies it for metrics: "rejected" when the gateway refused the request,
|
|
13
|
+
"upstream_error" when the provider failed, "gateway_error" when the gateway itself did.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, status: int, code: str, message: str, outcome: Outcome) -> None:
|
|
17
|
+
super().__init__(code)
|
|
18
|
+
self.status = status
|
|
19
|
+
self.code = code
|
|
20
|
+
self.message = message
|
|
21
|
+
self.outcome: Outcome = outcome
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def error_response(dialect: Dialect, error: GatewayError) -> JSONResponse:
|
|
25
|
+
"""The error in the dialect's error shape, with the error's status."""
|
|
26
|
+
body = dialect.error_body(error.status, error.code, error.message)
|
|
27
|
+
return JSONResponse(body, status_code=error.status)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Each call builds a new exception: a shared instance would carry one request's traceback
|
|
31
|
+
# and context into another.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def request_too_large() -> GatewayError:
|
|
35
|
+
"""413 request_too_large."""
|
|
36
|
+
return GatewayError(
|
|
37
|
+
413, "request_too_large", "Request body is larger than the gateway allows.", "rejected"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def invalid_json() -> GatewayError:
|
|
42
|
+
"""400 invalid_json."""
|
|
43
|
+
return GatewayError(
|
|
44
|
+
400, "invalid_json", "Request body must be a JSON object encoded as UTF-8.", "rejected"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def pseudonymization_failed() -> GatewayError:
|
|
49
|
+
"""500 pseudonymization_failed."""
|
|
50
|
+
return GatewayError(
|
|
51
|
+
500,
|
|
52
|
+
"pseudonymization_failed",
|
|
53
|
+
"Pseudonymisation failed; the request was not forwarded.",
|
|
54
|
+
"gateway_error",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def leak_audit_failed() -> GatewayError:
|
|
59
|
+
"""500 leak_audit_failed: a detected value was still in the outbound body."""
|
|
60
|
+
return GatewayError(
|
|
61
|
+
500,
|
|
62
|
+
"leak_audit_failed",
|
|
63
|
+
"A detected value was still present after pseudonymisation; the request was not forwarded.",
|
|
64
|
+
"rejected",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def upstream_timeout() -> GatewayError:
|
|
69
|
+
"""504 upstream_timeout."""
|
|
70
|
+
return GatewayError(
|
|
71
|
+
504, "upstream_timeout", "The upstream provider did not answer in time.", "upstream_error"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def upstream_unreachable() -> GatewayError:
|
|
76
|
+
"""502 upstream_unreachable."""
|
|
77
|
+
return GatewayError(
|
|
78
|
+
502, "upstream_unreachable", "The upstream provider could not be reached.", "upstream_error"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def restoration_failed() -> GatewayError:
|
|
83
|
+
"""502 restoration_failed."""
|
|
84
|
+
return GatewayError(
|
|
85
|
+
502, "restoration_failed", "The upstream response could not be restored.", "gateway_error"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def unknown_endpoint() -> GatewayError:
|
|
90
|
+
"""404 unknown_endpoint."""
|
|
91
|
+
return GatewayError(404, "unknown_endpoint", "Unknown endpoint.", "rejected")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def internal_error() -> GatewayError:
|
|
95
|
+
"""500 internal_error."""
|
|
96
|
+
return GatewayError(500, "internal_error", "Internal gateway error.", "gateway_error")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def unsupported_content(kind: str) -> GatewayError:
|
|
100
|
+
"""422 for content the gateway cannot inspect; `kind` is a sanitised label."""
|
|
101
|
+
return GatewayError(422, "unsupported_content", f"Unsupported content: {kind}", "rejected")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def malformed_request(position: str) -> GatewayError:
|
|
105
|
+
"""400 for a body without the dialect's required shape; `position` names fields, not text."""
|
|
106
|
+
return GatewayError(400, "malformed_request", f"Malformed request: {position}", "rejected")
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Which headers cross the gateway in each direction (architecture §7)."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from typing import Final
|
|
5
|
+
|
|
6
|
+
_HOP_BY_HOP: Final = frozenset(
|
|
7
|
+
{
|
|
8
|
+
"connection",
|
|
9
|
+
"keep-alive",
|
|
10
|
+
"proxy-authenticate",
|
|
11
|
+
"proxy-authorization",
|
|
12
|
+
"te",
|
|
13
|
+
"trailer",
|
|
14
|
+
"transfer-encoding",
|
|
15
|
+
"upgrade",
|
|
16
|
+
}
|
|
17
|
+
)
|
|
18
|
+
# httpx sets host and content-length itself and negotiates its own encoding.
|
|
19
|
+
_REQUEST_DROPPED: Final = _HOP_BY_HOP | {"host", "content-length", "accept-encoding"}
|
|
20
|
+
# httpx has already decoded the body, and the gateway re-serialises it.
|
|
21
|
+
_RESPONSE_DROPPED: Final = _HOP_BY_HOP | {"content-length", "content-encoding"}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _filter(headers: Iterable[tuple[str, str]], dropped: frozenset[str]) -> list[tuple[str, str]]:
|
|
25
|
+
pairs = list(headers)
|
|
26
|
+
named = {
|
|
27
|
+
token.strip().lower()
|
|
28
|
+
for name, value in pairs
|
|
29
|
+
if name.lower() == "connection"
|
|
30
|
+
for token in value.split(",")
|
|
31
|
+
}
|
|
32
|
+
return [
|
|
33
|
+
(name, value)
|
|
34
|
+
for name, value in pairs
|
|
35
|
+
if name.lower() not in dropped and name.lower() not in named
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def filter_request_headers(headers: Iterable[tuple[str, str]]) -> list[tuple[str, str]]:
|
|
40
|
+
"""Client headers to forward upstream: hop-by-hop headers, any header named in `connection`,
|
|
41
|
+
`host`, `content-length` and `accept-encoding` removed, case-insensitively; the rest kept in
|
|
42
|
+
order, credentials included."""
|
|
43
|
+
return _filter(headers, _REQUEST_DROPPED)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def filter_response_headers(headers: Iterable[tuple[str, str]]) -> list[tuple[str, str]]:
|
|
47
|
+
"""Upstream headers to return: hop-by-hop headers, any header named in `connection`,
|
|
48
|
+
`content-length` and `content-encoding` removed, case-insensitively; the rest kept in
|
|
49
|
+
order."""
|
|
50
|
+
return _filter(headers, _RESPONSE_DROPPED)
|
aliasgate/py.typed
ADDED
|
File without changes
|
aliasgate/resolve.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Overlap resolution between the spans that detectors report for one text."""
|
|
2
|
+
|
|
3
|
+
from bisect import bisect_left
|
|
4
|
+
from collections.abc import Iterable
|
|
5
|
+
|
|
6
|
+
from aliasgate.types import Span
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _resolution_order(span: Span) -> tuple[int, float, int, int, str, str]:
|
|
10
|
+
# Architecture §3.3, step 2. The trailing keys only make the order total.
|
|
11
|
+
return (
|
|
12
|
+
-span.priority,
|
|
13
|
+
-span.score,
|
|
14
|
+
-(span.end - span.start),
|
|
15
|
+
span.start,
|
|
16
|
+
span.entity_type,
|
|
17
|
+
span.source,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def resolve_overlaps(spans: Iterable[Span]) -> list[Span]:
|
|
22
|
+
"""Return non-overlapping spans sorted by start, chosen as in architecture §3.3."""
|
|
23
|
+
accepted: list[Span] = [] # pairwise disjoint, sorted by start
|
|
24
|
+
starts: list[int] = []
|
|
25
|
+
for span in sorted((span for span in spans if span.start < span.end), key=_resolution_order):
|
|
26
|
+
# Only the accepted span with the greatest start before span.end can overlap it.
|
|
27
|
+
index = bisect_left(starts, span.end)
|
|
28
|
+
if index > 0 and accepted[index - 1].end > span.start:
|
|
29
|
+
continue
|
|
30
|
+
accepted.insert(index, span)
|
|
31
|
+
starts.insert(index, span.start)
|
|
32
|
+
return accepted
|
aliasgate/stream.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Restoration of text that arrives in chunks (architecture §5.4)."""
|
|
2
|
+
|
|
3
|
+
from aliasgate.engine import count_unresolved, restore
|
|
4
|
+
from aliasgate.mapping import Mapping
|
|
5
|
+
from aliasgate.placeholders import MAX_TOLERANT_LENGTH
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _hold_position(buffer: str) -> int:
|
|
9
|
+
"""Where the part of `buffer` that could still grow into a placeholder starts."""
|
|
10
|
+
i = buffer.rfind("[")
|
|
11
|
+
if i != -1 and "]" not in buffer[i:] and len(buffer) - i <= MAX_TOLERANT_LENGTH:
|
|
12
|
+
return i - 1 if i > 0 and buffer[i - 1] == "\\" else i
|
|
13
|
+
if buffer.endswith("\\"):
|
|
14
|
+
return len(buffer) - 1
|
|
15
|
+
return len(buffer)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class StreamRestorer:
|
|
19
|
+
"""Restores placeholders in streamed text, emitting everything that is safe as early as
|
|
20
|
+
possible and holding back only a tail that could still become a placeholder.
|
|
21
|
+
|
|
22
|
+
For any text and any way of cutting it into chunks, the concatenation of every `feed`
|
|
23
|
+
result and `flush` equals `restore(text)`, and nothing emitted is ever retracted.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
__slots__ = ("_buffer", "_json_escape", "_mapping", "unresolved")
|
|
27
|
+
|
|
28
|
+
def __init__(self, mapping: Mapping, *, json_escape: bool = False) -> None:
|
|
29
|
+
self._mapping = mapping
|
|
30
|
+
self._json_escape = json_escape
|
|
31
|
+
self._buffer = ""
|
|
32
|
+
self.unresolved = 0 # tolerant matches left unrestored in emitted text
|
|
33
|
+
|
|
34
|
+
def feed(self, chunk: str) -> str:
|
|
35
|
+
"""Add `chunk`; return the text that is safe to emit now (possibly "")."""
|
|
36
|
+
self._buffer += chunk
|
|
37
|
+
hold = _hold_position(self._buffer)
|
|
38
|
+
ready, self._buffer = self._buffer[:hold], self._buffer[hold:]
|
|
39
|
+
return self._restore(ready)
|
|
40
|
+
|
|
41
|
+
def flush(self) -> str:
|
|
42
|
+
"""Return the held text, restored, and empty the buffer. Call at the end of a stream."""
|
|
43
|
+
ready, self._buffer = self._buffer, ""
|
|
44
|
+
return self._restore(ready)
|
|
45
|
+
|
|
46
|
+
def _restore(self, text: str) -> str:
|
|
47
|
+
if not text:
|
|
48
|
+
return ""
|
|
49
|
+
self.unresolved += count_unresolved(text, self._mapping)
|
|
50
|
+
return restore(text, self._mapping, json_escape=self._json_escape)
|
aliasgate/terms.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Whole-term search patterns, shared by propagation, the dictionary and the leak audit."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def term_body(surface: str) -> str:
|
|
7
|
+
"""The regular expression for `surface` without word boundaries: its tokens, escaped and
|
|
8
|
+
joined by `\\s+`."""
|
|
9
|
+
tokens = surface.split()
|
|
10
|
+
if not tokens:
|
|
11
|
+
raise ValueError("term must contain at least one non-whitespace character")
|
|
12
|
+
return r"\s+".join(re.escape(token) for token in tokens)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def build_term_pattern(surface: str) -> re.Pattern[str]:
|
|
16
|
+
"""Pattern matching `surface` as a whole term, case-insensitively, with any whitespace run
|
|
17
|
+
between its tokens (architecture §4.3 step 3)."""
|
|
18
|
+
return re.compile(rf"(?<!\w)(?:{term_body(surface)})(?!\w)", re.IGNORECASE)
|
aliasgate/types.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Core vocabulary: entity types, detection priorities, spans and the detector protocol."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Final, Protocol
|
|
6
|
+
|
|
7
|
+
PRIORITY_LITERAL: Final = 100
|
|
8
|
+
PRIORITY_DICTIONARY: Final = 80
|
|
9
|
+
PRIORITY_RULE: Final = 60
|
|
10
|
+
PRIORITY_ML: Final = 40
|
|
11
|
+
PRIORITY_PROPAGATION: Final = 20
|
|
12
|
+
|
|
13
|
+
MAX_ENTITY_TYPE_LENGTH: Final = 40
|
|
14
|
+
|
|
15
|
+
PERSON: Final = "PERSON"
|
|
16
|
+
ORG: Final = "ORG"
|
|
17
|
+
ADDRESS: Final = "ADDRESS"
|
|
18
|
+
EMAIL: Final = "EMAIL"
|
|
19
|
+
PHONE: Final = "PHONE"
|
|
20
|
+
IBAN: Final = "IBAN"
|
|
21
|
+
CREDIT_CARD: Final = "CREDIT_CARD"
|
|
22
|
+
NATIONAL_ID: Final = "NATIONAL_ID"
|
|
23
|
+
TAX_ID: Final = "TAX_ID"
|
|
24
|
+
IP_ADDRESS: Final = "IP_ADDRESS"
|
|
25
|
+
URL: Final = "URL"
|
|
26
|
+
# An entity type name, not a credential.
|
|
27
|
+
SECRET: Final = "SECRET" # noqa: S105
|
|
28
|
+
LITERAL: Final = "LITERAL"
|
|
29
|
+
|
|
30
|
+
_ENTITY_TYPE: Final = re.compile(r"[A-Z][A-Z_]*")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def validate_entity_type(name: str) -> str:
|
|
34
|
+
"""Return `name` if it matches [A-Z][A-Z_]* and is at most 40 chars; else raise ValueError."""
|
|
35
|
+
if len(name) > MAX_ENTITY_TYPE_LENGTH or _ENTITY_TYPE.fullmatch(name) is None:
|
|
36
|
+
# The value itself is not echoed: callers may pass arbitrary text.
|
|
37
|
+
raise ValueError(
|
|
38
|
+
f"entity type must match [A-Z][A-Z_]* and be at most {MAX_ENTITY_TYPE_LENGTH} "
|
|
39
|
+
f"characters (got a string of length {len(name)})"
|
|
40
|
+
)
|
|
41
|
+
return name
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True, slots=True)
|
|
45
|
+
class Span:
|
|
46
|
+
"""A detected range of one text; construction guarantees valid offsets, score and type."""
|
|
47
|
+
|
|
48
|
+
start: int
|
|
49
|
+
end: int
|
|
50
|
+
entity_type: str
|
|
51
|
+
score: float
|
|
52
|
+
source: str
|
|
53
|
+
priority: int
|
|
54
|
+
|
|
55
|
+
def __post_init__(self) -> None:
|
|
56
|
+
if self.start < 0:
|
|
57
|
+
raise ValueError(f"span start must be non-negative (got {self.start})")
|
|
58
|
+
if self.end < self.start:
|
|
59
|
+
raise ValueError(f"span end must not precede start (got {self.start}..{self.end})")
|
|
60
|
+
if not 0.0 <= self.score <= 1.0: # also rejects NaN
|
|
61
|
+
raise ValueError("span score must be between 0 and 1")
|
|
62
|
+
validate_entity_type(self.entity_type)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class Detector(Protocol):
|
|
66
|
+
"""Finds spans in a text; the same text always yields the same spans, and failure raises."""
|
|
67
|
+
|
|
68
|
+
name: str
|
|
69
|
+
|
|
70
|
+
def detect(self, text: str) -> list[Span]: ...
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: aliasgate
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reversible pseudonymization gateway for LLM APIs: the model never sees real customer data, the user never sees placeholders.
|
|
5
|
+
Project-URL: Homepage, https://github.com/jcjmenez/aliasgate
|
|
6
|
+
Project-URL: Documentation, https://github.com/jcjmenez/aliasgate/blob/main/docs/usage.md
|
|
7
|
+
Project-URL: Changelog, https://github.com/jcjmenez/aliasgate/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Juan Carlos Jiménez
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: gateway,gdpr,llm,pii,privacy,proxy,pseudonymization
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Security
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Requires-Dist: httpx>=0.28.1
|
|
21
|
+
Requires-Dist: phonenumbers>=9.0.43
|
|
22
|
+
Requires-Dist: prometheus-client>=0.26.0
|
|
23
|
+
Requires-Dist: pydantic-settings>=2.16.3
|
|
24
|
+
Requires-Dist: pydantic>=2.14.3
|
|
25
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
26
|
+
Requires-Dist: starlette>=1.10.0
|
|
27
|
+
Requires-Dist: uvicorn>=0.54.0
|
|
28
|
+
Provides-Extra: ml
|
|
29
|
+
Requires-Dist: gliner>=0.2.30; extra == 'ml'
|
|
30
|
+
Requires-Dist: protobuf>=7.36.2; extra == 'ml'
|
|
31
|
+
Requires-Dist: torch>=2.15.0; extra == 'ml'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# aliasgate
|
|
35
|
+
|
|
36
|
+
[](https://github.com/jcjmenez/aliasgate/actions/workflows/ci.yml)
|
|
37
|
+
[](https://github.com/jcjmenez/aliasgate/blob/main/LICENSE)
|
|
38
|
+
|
|
39
|
+
A self-hosted gateway that sits between your users and an LLM API. It replaces customer data
|
|
40
|
+
with placeholders before the request leaves your network and puts the real values back in the
|
|
41
|
+
answer. **The model never sees real data. The user never sees placeholders.**
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
user / app aliasgate LLM provider
|
|
45
|
+
─────────── ───────── ────────────
|
|
46
|
+
"Email Juan Pérez ──▶ detect + replace ──▶ "Email [PERSON_1]
|
|
47
|
+
(juan@acme.es) about ([EMAIL_1]) about
|
|
48
|
+
the ACME contract" the [ORG_1] contract"
|
|
49
|
+
|
|
50
|
+
"Dear Juan Pérez, ..." ◀── restore ◀── "Dear [PERSON_1], ..."
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> **Status: alpha (0.1.0).** The gateway pseudonymises OpenAI Chat Completions and Anthropic
|
|
54
|
+
> Messages requests, streaming and tool calls included, with rules, company dictionaries and an
|
|
55
|
+
> optional local ML model, and restores the answers. It has not run in production yet. Start
|
|
56
|
+
> with the [usage guide](https://github.com/jcjmenez/aliasgate/blob/main/docs/usage.md).
|
|
57
|
+
|
|
58
|
+
## Quick start
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install aliasgate
|
|
62
|
+
echo "{}" > aliasgate.yaml # every key is optional: rules only, port 8080 on loopback
|
|
63
|
+
aliasgate check --config aliasgate.yaml
|
|
64
|
+
aliasgate serve --config aliasgate.yaml
|
|
65
|
+
export OPENAI_BASE_URL=http://localhost:8080/v1 ANTHROPIC_BASE_URL=http://localhost:8080
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`serve` keeps running. In another shell, the last line points OpenAI and Anthropic clients at
|
|
69
|
+
the gateway, which forwards their usual API key. Dictionaries for your customers' names, the ML model, Docker images, Azure
|
|
70
|
+
OpenAI, Claude in Microsoft Foundry and tuning are in [docs/usage.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/usage.md).
|
|
71
|
+
|
|
72
|
+
## Why
|
|
73
|
+
|
|
74
|
+
Many companies forbid sending customer data to external LLMs, and their staff still need LLMs to
|
|
75
|
+
work on documents full of customer data. aliasgate is a technical control for that conflict: you
|
|
76
|
+
point your OpenAI or Anthropic client at the gateway instead of the provider, and nothing else
|
|
77
|
+
changes.
|
|
78
|
+
|
|
79
|
+
## Design in brief
|
|
80
|
+
|
|
81
|
+
- **Four detection layers.** Rules with checksum validation (emails, IBANs, cards, national IDs,
|
|
82
|
+
phones, secrets), a company dictionary (your customers, projects, codenames), a pluggable local
|
|
83
|
+
ML model for names, organisations and addresses, and propagation of every detected value to
|
|
84
|
+
all its other occurrences.
|
|
85
|
+
- **Deterministic mapping.** Inside a request the same value always becomes the same placeholder,
|
|
86
|
+
and placeholders are numbered so the model can tell entities apart.
|
|
87
|
+
- **Reversible, including streams and tool calls.** Placeholders split across streamed chunks are
|
|
88
|
+
buffered and restored correctly. This is verified with property tests.
|
|
89
|
+
- **Nothing reversible at rest.** The placeholder-to-value mapping exists only in memory while a
|
|
90
|
+
request is being served. There is no vault to steal.
|
|
91
|
+
- **Fail closed.** If detection fails, or the request carries content the gateway cannot inspect,
|
|
92
|
+
the request is rejected instead of forwarded.
|
|
93
|
+
- **Self-hosted.** No data is sent anywhere except to the LLM provider you configure, and only
|
|
94
|
+
after pseudonymisation.
|
|
95
|
+
|
|
96
|
+
## What it does not guarantee
|
|
97
|
+
|
|
98
|
+
No detector finds everything, and text without any name can still identify a person through
|
|
99
|
+
context. aliasgate greatly reduces the personal and confidential data that reaches a provider.
|
|
100
|
+
It does not make data anonymous and it does not, by itself, make a deployment compliant with any
|
|
101
|
+
regulation. Read the [threat model](https://github.com/jcjmenez/aliasgate/blob/main/docs/threat-model.md) before relying on it.
|
|
102
|
+
|
|
103
|
+
## Documentation
|
|
104
|
+
|
|
105
|
+
| Document | Content |
|
|
106
|
+
|---|---|
|
|
107
|
+
| [docs/usage.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/usage.md) | Install, configure, point clients at the gateway, tune detection, error codes, metrics |
|
|
108
|
+
| [docs/deployment.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/deployment.md) | Network placement, container, sizing, monitoring, deployment checklist |
|
|
109
|
+
| [docs/evaluation.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/evaluation.md) | Measuring detection on your own samples |
|
|
110
|
+
| [CHANGELOG.md](https://github.com/jcjmenez/aliasgate/blob/main/CHANGELOG.md) | Changes by version |
|
|
111
|
+
| [docs/state-of-the-art.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/state-of-the-art.md) | Existing tools, detection models, research evidence, legal context, feasibility |
|
|
112
|
+
| [docs/architecture.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/architecture.md) | Components, algorithms, data flow, decisions |
|
|
113
|
+
| [docs/threat-model.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/threat-model.md) | What is protected, from whom, and the residual risks |
|
|
114
|
+
| [docs/plan/README.md](https://github.com/jcjmenez/aliasgate/blob/main/docs/plan/README.md) | Implementation plan in vertical slices |
|
|
115
|
+
| [AGENTS.md](https://github.com/jcjmenez/aliasgate/blob/main/AGENTS.md) | Working rules for coding agents and human contributors |
|
|
116
|
+
|
|
117
|
+
## Roadmap
|
|
118
|
+
|
|
119
|
+
| Slice | Deliverable | Status |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| 00 | Repository scaffold, tooling, CI | done |
|
|
122
|
+
| 01 | Core types, placeholder codec, mapping | done |
|
|
123
|
+
| 02 | Rule-based detectors with checksums | done |
|
|
124
|
+
| 03 | Pseudonymisation engine and restoration | done |
|
|
125
|
+
| 04 | Configuration and company dictionary | done |
|
|
126
|
+
| 05 | OpenAI Chat Completions payload handling | done |
|
|
127
|
+
| 06 | HTTP proxy (non-streaming) | done |
|
|
128
|
+
| 07 | Streaming restoration | done |
|
|
129
|
+
| 08 | Anthropic Messages dialect | done |
|
|
130
|
+
| 09 | ML detector (GLiNER) | done |
|
|
131
|
+
| 10 | Leak audit, policy, metrics | done |
|
|
132
|
+
| 11 | Evaluation harness | done |
|
|
133
|
+
| 12 | CLI, Docker image, first release | done |
|
|
134
|
+
|
|
135
|
+
## Development
|
|
136
|
+
|
|
137
|
+
Requires [uv](https://docs.astral.sh/uv/).
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
uv sync
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
uv run ruff format --check . && uv run ruff check . && uv run mypy && uv run pytest --cov -m "not ml"
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
See [CONTRIBUTING.md](https://github.com/jcjmenez/aliasgate/blob/main/CONTRIBUTING.md).
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
[Apache-2.0](https://github.com/jcjmenez/aliasgate/blob/main/LICENSE).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
aliasgate/__init__.py,sha256=_ySV8k5GfwcUy7iocNAWHcLCKsRWOzfM22-iLYaH9Fk,117
|
|
2
|
+
aliasgate/__main__.py,sha256=5ams6bMQj0hsbCPD3uwvJpOLBdI2dtukhF8LyopWvmI,173
|
|
3
|
+
aliasgate/audit.py,sha256=9EQGDbcL480YqaqRjZ0m5otKKwl4aYO_Xs651znDENI,3418
|
|
4
|
+
aliasgate/cli.py,sha256=i73UKeZM0uI9IfPSUoRqDe-GfzgUt1_2-S-WjwjrjKw,12055
|
|
5
|
+
aliasgate/config.py,sha256=WQh1yuNQmqDNNHpgTLpyE7BzFctMxnc6WH6rqL3bEYg,14988
|
|
6
|
+
aliasgate/engine.py,sha256=NKZNjL9m2VQXl11P59xUfjzdJv__VHgZWJUKQ45dX50,9840
|
|
7
|
+
aliasgate/factory.py,sha256=vVBiFFv0OJQo0xgmthmsRCnund4uGkuPmCB90_cFktw,2288
|
|
8
|
+
aliasgate/mapping.py,sha256=cu9_dwNkJNtY4C8MFf45_EJGbkDXBDhA1D8oXykpYN8,3755
|
|
9
|
+
aliasgate/metrics.py,sha256=P4MAwUx61F4WreNj2aMTU1u5oQHgjjpe2K_gfvzF1mY,5307
|
|
10
|
+
aliasgate/placeholders.py,sha256=JSYYQjPzn3l3SinSQtv0oOmLk4rxl3S5s6TonkYuFRo,1865
|
|
11
|
+
aliasgate/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
aliasgate/resolve.py,sha256=DNQZSUiiS8NhUSyo9t3GiF8cjHpPc3DRN_6Ceimd1Og,1164
|
|
13
|
+
aliasgate/stream.py,sha256=rRp-0bpM7rC6dTOnkp3k7y4RL7cIuqKTTEPW5JazprE,2057
|
|
14
|
+
aliasgate/terms.py,sha256=ILFslZTWfO5GC2suSm_n0tAkOxOn7sXbM4S0thK5OLY,741
|
|
15
|
+
aliasgate/types.py,sha256=YMJTw-mJTHlyGbb90gTHKK6qvjly5gb91v_HftSnVxk,2200
|
|
16
|
+
aliasgate/detectors/__init__.py,sha256=f_HU761vvCCYC89UzQNTRZQHUGq1ApHCrWJn6hxd1t8,67
|
|
17
|
+
aliasgate/detectors/cache.py,sha256=pJO4Cxs_kijICCxIoyK2dInp4VgVYhi-3HWoPtj79x4,2343
|
|
18
|
+
aliasgate/detectors/checksums.py,sha256=PiO4PqQDeOWizU1ao_-vFvEi5SedqZvO4Qe1BP5bdWI,3391
|
|
19
|
+
aliasgate/detectors/chunking.py,sha256=NQsvFvNiZjVlH0kdUYDV7zDtt9N4unIaSuUudO6gnLs,1810
|
|
20
|
+
aliasgate/detectors/dictionary.py,sha256=hMprVQciSv7LV4tNc0UCGNGEoDaeWhv_Gz3wIs_nXLo,3677
|
|
21
|
+
aliasgate/detectors/gliner.py,sha256=h3RbYbDnJAYOJAkbwIiCiFaFtCEz9BSstdykzJfTbVk,7748
|
|
22
|
+
aliasgate/detectors/literal.py,sha256=RUINJFodcfECkS55ovNxk1QCVdSly6zizCRQAUZFZSQ,639
|
|
23
|
+
aliasgate/detectors/rules.py,sha256=Lo-jVjSYnLuqz32TepxFXqxwqWWbRpfeKhhZTucyyP0,11030
|
|
24
|
+
aliasgate/dialects/__init__.py,sha256=KKuYRBzDK5rvrxkVu0A2wLhR5sW6AUWtTLaKOT2XVfA,82
|
|
25
|
+
aliasgate/dialects/anthropic_messages.py,sha256=Zkk-bYNrZNNDhhkVb5EviL3jatOCrD_3l41_U_riwzA,13533
|
|
26
|
+
aliasgate/dialects/base.py,sha256=bBVCDrIY0ggH_QzeH1vs54Ei6wvUcM75iKdiUXd1524,7119
|
|
27
|
+
aliasgate/dialects/openai_chat.py,sha256=yn3ZnCC9gFX6r4jjPwdyuJfqKeBXYGspoLRcxBYBzlU,15678
|
|
28
|
+
aliasgate/dialects/sse.py,sha256=42U4BanCcvdcreEwAU_DTQQ8eES95HW7lfC87KR4byY,4129
|
|
29
|
+
aliasgate/eval/__init__.py,sha256=hgGaGPVdAzonj-d8N555lb6Jz2ZkdzmxG8m5ye3azOk,190
|
|
30
|
+
aliasgate/eval/__main__.py,sha256=KPma4B3hxIm-qJn5EuvKg8C9crEDCx7CBIGCKTHBChU,4833
|
|
31
|
+
aliasgate/eval/corpus.py,sha256=GonwzFfT95dSHkBFu05Vr4h3WuYaLIA17-wfdy-B8Vo,4070
|
|
32
|
+
aliasgate/eval/metrics.py,sha256=8JGpTGVS9BNFxHw7WKWYMEsvGQlX9ty38-tbFzF33OM,7476
|
|
33
|
+
aliasgate/eval/report.py,sha256=KAkHR2ZhfKmls5cqX1MojOHWCgKZHIoaVk15Owh24uw,3662
|
|
34
|
+
aliasgate/proxy/__init__.py,sha256=Uh-Ype9NmonBItfagYQ2lVQ4oY8c5Q9DlCEjBMg8QSA,81
|
|
35
|
+
aliasgate/proxy/app.py,sha256=SB2LKFTIDRsv9ybnsynLdY4b-yqTIo96RzXu2O6fQNE,25478
|
|
36
|
+
aliasgate/proxy/errors.py,sha256=EFuOpg87MSA8UUmYyQozWjWI4C4a1kvMxOVpZn4xmuI,3572
|
|
37
|
+
aliasgate/proxy/headers.py,sha256=IFsrR5ec0MzcVCdKNJ109DlwQsIQ-ifE5oOZRyQ46Ws,1817
|
|
38
|
+
aliasgate-0.1.0.dist-info/METADATA,sha256=nWuykBggdmX1UCOPJq4eF5TGrGYU6DfUgJ7ZT54U5SI,7789
|
|
39
|
+
aliasgate-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
40
|
+
aliasgate-0.1.0.dist-info/entry_points.txt,sha256=KKyUEQdnlMVB2NuYmEtcdge7tO1uhs4QgLovAp7_hH4,49
|
|
41
|
+
aliasgate-0.1.0.dist-info/licenses/LICENSE,sha256=yVuuHRzgI17MzTVgt3LsHvuX80innw--CmNPDCzO_iw,11358
|
|
42
|
+
aliasgate-0.1.0.dist-info/RECORD,,
|