pasteval 0.0.1__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.
- pasteval-0.0.1/LICENSE +21 -0
- pasteval-0.0.1/PKG-INFO +131 -0
- pasteval-0.0.1/README.md +111 -0
- pasteval-0.0.1/pyproject.toml +41 -0
- pasteval-0.0.1/setup.cfg +4 -0
- pasteval-0.0.1/src/pasteval/__init__.py +3 -0
- pasteval-0.0.1/src/pasteval/__main__.py +8 -0
- pasteval-0.0.1/src/pasteval/cli.py +97 -0
- pasteval-0.0.1/src/pasteval/detector.py +54 -0
- pasteval-0.0.1/src/pasteval/redactor.py +47 -0
- pasteval-0.0.1/src/pasteval/rules.py +52 -0
- pasteval-0.0.1/src/pasteval.egg-info/PKG-INFO +131 -0
- pasteval-0.0.1/src/pasteval.egg-info/SOURCES.txt +18 -0
- pasteval-0.0.1/src/pasteval.egg-info/dependency_links.txt +1 -0
- pasteval-0.0.1/src/pasteval.egg-info/entry_points.txt +2 -0
- pasteval-0.0.1/src/pasteval.egg-info/requires.txt +3 -0
- pasteval-0.0.1/src/pasteval.egg-info/top_level.txt +1 -0
- pasteval-0.0.1/tests/test_cli.py +115 -0
- pasteval-0.0.1/tests/test_detector.py +96 -0
- pasteval-0.0.1/tests/test_redactor.py +68 -0
pasteval-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Harshil Shah
|
|
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.
|
pasteval-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pasteval
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Redact sensitive information from text before you paste it into AI tools.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://pasteval.com
|
|
7
|
+
Keywords: redaction,privacy,secrets,cli,sanitize
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Environment :: Console
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Security
|
|
13
|
+
Classifier: Topic :: Text Processing :: Filters
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# Pasteval
|
|
22
|
+
|
|
23
|
+
Pasteval is a small command-line tool that redacts sensitive information from
|
|
24
|
+
text before you paste it into an AI tool or any other external service.
|
|
25
|
+
|
|
26
|
+
It runs entirely on your computer. No account, no API key, no network access,
|
|
27
|
+
and no dependencies beyond Python itself.
|
|
28
|
+
|
|
29
|
+
## Why use it
|
|
30
|
+
|
|
31
|
+
When you paste a log, an email, or a config snippet into a chatbot, it is easy
|
|
32
|
+
to include something you did not mean to share. Pasteval replaces what it
|
|
33
|
+
recognizes with a placeholder, so the text stays useful and the value stays
|
|
34
|
+
private.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
Pasteval needs Python 3.11 or newer. Install it from source:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
git clone <this repository>
|
|
42
|
+
cd pasteval
|
|
43
|
+
pip install -e .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
Paste text interactively, then press Ctrl+D on a new line to finish
|
|
49
|
+
(Ctrl+Z then Enter on Windows):
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pasteval
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Pipe text in:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
cat notes.txt | pasteval
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Read a file:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pasteval --file notes.txt
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Other options:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pasteval --help
|
|
71
|
+
pasteval --version
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The redacted text is written to standard output. A short summary (types and
|
|
75
|
+
counts only, never the values) is written to standard error, so you can pipe
|
|
76
|
+
the output safely, for example `pasteval --file notes.txt | pbcopy` on macOS.
|
|
77
|
+
|
|
78
|
+
## Example
|
|
79
|
+
|
|
80
|
+
Input:
|
|
81
|
+
|
|
82
|
+
```text
|
|
83
|
+
Hi, I'm Alex.
|
|
84
|
+
My email is alex@example.com.
|
|
85
|
+
Please copy sam@example.com, and reply to alex@example.com.
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Output:
|
|
89
|
+
|
|
90
|
+
```text
|
|
91
|
+
Hi, I'm Alex.
|
|
92
|
+
My email is [EMAIL_1].
|
|
93
|
+
Please copy [EMAIL_2], and reply to [EMAIL_1].
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The same value always gets the same placeholder, so the text still makes sense.
|
|
97
|
+
|
|
98
|
+
## What is redacted
|
|
99
|
+
|
|
100
|
+
| Type | Placeholder |
|
|
101
|
+
|---|---|
|
|
102
|
+
| Email addresses | `[EMAIL_1]`, `[EMAIL_2]`, ... |
|
|
103
|
+
|
|
104
|
+
## Security limitations
|
|
105
|
+
|
|
106
|
+
Pasteval reduces the risk of accidental sharing. It does not guarantee it.
|
|
107
|
+
|
|
108
|
+
- It can miss things (false negatives). Anything not listed above is left as
|
|
109
|
+
it is, including names, postal addresses, passwords, and API keys.
|
|
110
|
+
- Email addresses containing non-English characters are not detected.
|
|
111
|
+
- It can occasionally redact something harmless (false positives).
|
|
112
|
+
- Text you paste into a terminal may remain in the terminal's scrollback.
|
|
113
|
+
- Always read the output before you share it.
|
|
114
|
+
|
|
115
|
+
Pasteval never sends your text anywhere, never saves it, and never prints the
|
|
116
|
+
original values in its messages. If something goes wrong it prints an error
|
|
117
|
+
and no text at all.
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pip install -e ".[dev]"
|
|
123
|
+
pytest
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Detection rules live in `src/pasteval/rules.py`. To add one, add a `Rule` to
|
|
127
|
+
the `RULES` list and write tests for it.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT. See [LICENSE](LICENSE).
|
pasteval-0.0.1/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Pasteval
|
|
2
|
+
|
|
3
|
+
Pasteval is a small command-line tool that redacts sensitive information from
|
|
4
|
+
text before you paste it into an AI tool or any other external service.
|
|
5
|
+
|
|
6
|
+
It runs entirely on your computer. No account, no API key, no network access,
|
|
7
|
+
and no dependencies beyond Python itself.
|
|
8
|
+
|
|
9
|
+
## Why use it
|
|
10
|
+
|
|
11
|
+
When you paste a log, an email, or a config snippet into a chatbot, it is easy
|
|
12
|
+
to include something you did not mean to share. Pasteval replaces what it
|
|
13
|
+
recognizes with a placeholder, so the text stays useful and the value stays
|
|
14
|
+
private.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
Pasteval needs Python 3.11 or newer. Install it from source:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
git clone <this repository>
|
|
22
|
+
cd pasteval
|
|
23
|
+
pip install -e .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Paste text interactively, then press Ctrl+D on a new line to finish
|
|
29
|
+
(Ctrl+Z then Enter on Windows):
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pasteval
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Pipe text in:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
cat notes.txt | pasteval
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Read a file:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pasteval --file notes.txt
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Other options:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pasteval --help
|
|
51
|
+
pasteval --version
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The redacted text is written to standard output. A short summary (types and
|
|
55
|
+
counts only, never the values) is written to standard error, so you can pipe
|
|
56
|
+
the output safely, for example `pasteval --file notes.txt | pbcopy` on macOS.
|
|
57
|
+
|
|
58
|
+
## Example
|
|
59
|
+
|
|
60
|
+
Input:
|
|
61
|
+
|
|
62
|
+
```text
|
|
63
|
+
Hi, I'm Alex.
|
|
64
|
+
My email is alex@example.com.
|
|
65
|
+
Please copy sam@example.com, and reply to alex@example.com.
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Output:
|
|
69
|
+
|
|
70
|
+
```text
|
|
71
|
+
Hi, I'm Alex.
|
|
72
|
+
My email is [EMAIL_1].
|
|
73
|
+
Please copy [EMAIL_2], and reply to [EMAIL_1].
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The same value always gets the same placeholder, so the text still makes sense.
|
|
77
|
+
|
|
78
|
+
## What is redacted
|
|
79
|
+
|
|
80
|
+
| Type | Placeholder |
|
|
81
|
+
|---|---|
|
|
82
|
+
| Email addresses | `[EMAIL_1]`, `[EMAIL_2]`, ... |
|
|
83
|
+
|
|
84
|
+
## Security limitations
|
|
85
|
+
|
|
86
|
+
Pasteval reduces the risk of accidental sharing. It does not guarantee it.
|
|
87
|
+
|
|
88
|
+
- It can miss things (false negatives). Anything not listed above is left as
|
|
89
|
+
it is, including names, postal addresses, passwords, and API keys.
|
|
90
|
+
- Email addresses containing non-English characters are not detected.
|
|
91
|
+
- It can occasionally redact something harmless (false positives).
|
|
92
|
+
- Text you paste into a terminal may remain in the terminal's scrollback.
|
|
93
|
+
- Always read the output before you share it.
|
|
94
|
+
|
|
95
|
+
Pasteval never sends your text anywhere, never saves it, and never prints the
|
|
96
|
+
original values in its messages. If something goes wrong it prints an error
|
|
97
|
+
and no text at all.
|
|
98
|
+
|
|
99
|
+
## Development
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pip install -e ".[dev]"
|
|
103
|
+
pytest
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Detection rules live in `src/pasteval/rules.py`. To add one, add a `Rule` to
|
|
107
|
+
the `RULES` list and write tests for it.
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pasteval"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Redact sensitive information from text before you paste it into AI tools."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
dependencies = []
|
|
14
|
+
keywords = ["redaction", "privacy", "secrets", "cli", "sanitize"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Topic :: Security",
|
|
21
|
+
"Topic :: Text Processing :: Filters",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
dev = ["pytest>=8"]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://pasteval.com"
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
pasteval = "pasteval.cli:main"
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.dynamic]
|
|
34
|
+
version = { attr = "pasteval.__version__" }
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
38
|
+
|
|
39
|
+
[tool.pytest.ini_options]
|
|
40
|
+
testpaths = ["tests"]
|
|
41
|
+
pythonpath = ["src"]
|
pasteval-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Command-line interface: reading input, writing output, exit codes.
|
|
2
|
+
|
|
3
|
+
No detection logic lives here. Sanitized text goes to stdout; everything else
|
|
4
|
+
(hints, summaries, errors) goes to stderr so stdout can be piped safely.
|
|
5
|
+
|
|
6
|
+
Exit codes: 0 success, 1 runtime error, 2 usage error (set by argparse).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import sys
|
|
11
|
+
from collections.abc import Sequence
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from pasteval import __version__
|
|
15
|
+
from pasteval.redactor import RedactionResult, redact
|
|
16
|
+
|
|
17
|
+
EXIT_OK = 0
|
|
18
|
+
EXIT_ERROR = 1
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
22
|
+
parser = argparse.ArgumentParser(
|
|
23
|
+
prog="pasteval",
|
|
24
|
+
description=(
|
|
25
|
+
"Redact sensitive information from text before sharing it. "
|
|
26
|
+
"Reads standard input (paste or pipe) unless --file is given, "
|
|
27
|
+
"and prints the redacted text to standard output."
|
|
28
|
+
),
|
|
29
|
+
)
|
|
30
|
+
parser.add_argument(
|
|
31
|
+
"--file",
|
|
32
|
+
type=Path,
|
|
33
|
+
metavar="PATH",
|
|
34
|
+
help="read a UTF-8 text file instead of standard input",
|
|
35
|
+
)
|
|
36
|
+
parser.add_argument(
|
|
37
|
+
"--version", action="version", version=f"pasteval {__version__}"
|
|
38
|
+
)
|
|
39
|
+
return parser
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _read_input(file: Path | None) -> str:
|
|
43
|
+
"""Read all input as UTF-8.
|
|
44
|
+
|
|
45
|
+
We read bytes and decode them ourselves so line endings (\\n or \\r\\n)
|
|
46
|
+
arrive exactly as they were written.
|
|
47
|
+
"""
|
|
48
|
+
if file is not None:
|
|
49
|
+
return file.read_bytes().decode("utf-8")
|
|
50
|
+
if sys.stdin.isatty():
|
|
51
|
+
print(
|
|
52
|
+
"Paste your text, then press Ctrl+D on a new line to finish "
|
|
53
|
+
"(Ctrl+Z then Enter on Windows).",
|
|
54
|
+
file=sys.stderr,
|
|
55
|
+
)
|
|
56
|
+
return sys.stdin.buffer.read().decode("utf-8")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _summary(result: RedactionResult) -> str:
|
|
60
|
+
"""Describe what was redacted using type names and counts only."""
|
|
61
|
+
total = sum(result.counts.values())
|
|
62
|
+
if total == 0:
|
|
63
|
+
return "pasteval: nothing to redact."
|
|
64
|
+
details = ", ".join(f"{count} {name}" for name, count in result.counts.items())
|
|
65
|
+
noun = "item" if total == 1 else "items"
|
|
66
|
+
return f"pasteval: redacted {total} {noun}: {details}"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
70
|
+
args = build_parser().parse_args(argv)
|
|
71
|
+
|
|
72
|
+
# Error messages below are fixed strings. They never include the
|
|
73
|
+
# exception text or any input, so they cannot leak what the user pasted.
|
|
74
|
+
try:
|
|
75
|
+
text = _read_input(args.file)
|
|
76
|
+
except UnicodeDecodeError:
|
|
77
|
+
print("pasteval: error: input is not valid UTF-8 text.", file=sys.stderr)
|
|
78
|
+
return EXIT_ERROR
|
|
79
|
+
except OSError:
|
|
80
|
+
source = f"file '{args.file}'" if args.file else "standard input"
|
|
81
|
+
print(f"pasteval: error: could not read {source}.", file=sys.stderr)
|
|
82
|
+
return EXIT_ERROR
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
result = redact(text)
|
|
86
|
+
except Exception: # noqa: BLE001 - fail closed on any redaction bug
|
|
87
|
+
print(
|
|
88
|
+
"pasteval: error: redaction failed; no output was written.",
|
|
89
|
+
file=sys.stderr,
|
|
90
|
+
)
|
|
91
|
+
return EXIT_ERROR
|
|
92
|
+
|
|
93
|
+
# Only now, with redaction finished, do we write anything to stdout.
|
|
94
|
+
sys.stdout.buffer.write(result.text.encode("utf-8"))
|
|
95
|
+
sys.stdout.buffer.flush()
|
|
96
|
+
print(_summary(result), file=sys.stderr)
|
|
97
|
+
return EXIT_OK
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Find sensitive values in text and resolve overlapping matches."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
|
|
6
|
+
from pasteval.rules import RULES, Rule
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True)
|
|
10
|
+
class Match:
|
|
11
|
+
"""A span of text [start, end) that one rule recognized.
|
|
12
|
+
|
|
13
|
+
`key` holds the normalized sensitive value. It is excluded from repr() so
|
|
14
|
+
that printing or logging a Match can never leak the value by accident.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
start: int
|
|
18
|
+
end: int
|
|
19
|
+
rule_name: str
|
|
20
|
+
key: str = field(repr=False)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def find_matches(text: str, rules: Sequence[Rule] = RULES) -> list[Match]:
|
|
24
|
+
"""Return non-overlapping matches, ordered by position in the text.
|
|
25
|
+
|
|
26
|
+
Overlap rule: the match that starts earliest wins; on a tie the longest
|
|
27
|
+
wins; on a further tie the rule listed first wins.
|
|
28
|
+
"""
|
|
29
|
+
candidates: list[tuple[int, int, int, Match]] = []
|
|
30
|
+
for priority, rule in enumerate(rules):
|
|
31
|
+
for found in rule.pattern.finditer(text):
|
|
32
|
+
if found.start() == found.end():
|
|
33
|
+
continue # ignore empty matches from a badly written rule
|
|
34
|
+
match = Match(
|
|
35
|
+
start=found.start(),
|
|
36
|
+
end=found.end(),
|
|
37
|
+
rule_name=rule.name,
|
|
38
|
+
key=rule.normalize(found.group()),
|
|
39
|
+
)
|
|
40
|
+
# Sort key: earliest start, then longest (negative length), then
|
|
41
|
+
# rule order.
|
|
42
|
+
candidates.append(
|
|
43
|
+
(match.start, -(match.end - match.start), priority, match)
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
candidates.sort(key=lambda item: item[:3])
|
|
47
|
+
|
|
48
|
+
kept: list[Match] = []
|
|
49
|
+
covered_up_to = 0
|
|
50
|
+
for _, _, _, match in candidates:
|
|
51
|
+
if match.start >= covered_up_to:
|
|
52
|
+
kept.append(match)
|
|
53
|
+
covered_up_to = match.end
|
|
54
|
+
return kept
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Replace detected values with numbered placeholders."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
from pasteval.detector import find_matches
|
|
7
|
+
from pasteval.rules import RULES, Rule
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class RedactionResult:
|
|
12
|
+
"""The redacted text plus how many values of each type were replaced.
|
|
13
|
+
|
|
14
|
+
`counts` contains only type names and numbers, never the values, so it is
|
|
15
|
+
safe to show to the user.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
text: str
|
|
19
|
+
counts: dict[str, int]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def redact(text: str, rules: Sequence[Rule] = RULES) -> RedactionResult:
|
|
23
|
+
"""Return `text` with every detected value replaced by a placeholder.
|
|
24
|
+
|
|
25
|
+
Placeholders are numbered per type in order of first appearance. The same
|
|
26
|
+
value always gets the same placeholder; everything else is left untouched.
|
|
27
|
+
"""
|
|
28
|
+
placeholders: dict[tuple[str, str], str] = {} # (type, value) -> "[EMAIL_1]"
|
|
29
|
+
next_number: dict[str, int] = {}
|
|
30
|
+
counts: dict[str, int] = {}
|
|
31
|
+
|
|
32
|
+
pieces: list[str] = []
|
|
33
|
+
position = 0
|
|
34
|
+
for match in find_matches(text, rules):
|
|
35
|
+
identity = (match.rule_name, match.key)
|
|
36
|
+
if identity not in placeholders:
|
|
37
|
+
number = next_number.get(match.rule_name, 0) + 1
|
|
38
|
+
next_number[match.rule_name] = number
|
|
39
|
+
placeholders[identity] = f"[{match.rule_name}_{number}]"
|
|
40
|
+
|
|
41
|
+
pieces.append(text[position:match.start]) # untouched text before it
|
|
42
|
+
pieces.append(placeholders[identity])
|
|
43
|
+
position = match.end
|
|
44
|
+
counts[match.rule_name] = counts.get(match.rule_name, 0) + 1
|
|
45
|
+
|
|
46
|
+
pieces.append(text[position:]) # untouched text after the last match
|
|
47
|
+
return RedactionResult(text="".join(pieces), counts=counts)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Detection rules.
|
|
2
|
+
|
|
3
|
+
A rule is a name plus a compiled regular expression. To support a new kind of
|
|
4
|
+
sensitive value, add a Rule to RULES and write tests for it. Nothing else in
|
|
5
|
+
the application needs to change.
|
|
6
|
+
|
|
7
|
+
Order matters only as a tie-breaker: when two rules match exactly the same
|
|
8
|
+
span of text, the rule listed first wins.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _unchanged(value: str) -> str:
|
|
17
|
+
return value
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class Rule:
|
|
22
|
+
"""One kind of sensitive value.
|
|
23
|
+
|
|
24
|
+
name: used in the placeholder, e.g. "EMAIL" gives "[EMAIL_1]".
|
|
25
|
+
pattern: compiled regex that matches the value.
|
|
26
|
+
normalize: turns a matched value into the key used to decide whether two
|
|
27
|
+
matches are "the same value". Emails lower-case here so that
|
|
28
|
+
Alex@Example.com and alex@example.com share one placeholder.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
name: str
|
|
32
|
+
pattern: re.Pattern[str]
|
|
33
|
+
normalize: Callable[[str], str] = _unchanged
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Email addresses, deliberately conservative (ASCII only):
|
|
37
|
+
# - the local part starts with a letter or digit
|
|
38
|
+
# - the domain needs at least one dot and a letter-only ending of 2+ letters
|
|
39
|
+
# - the lookbehind/lookahead stop us matching the middle of a longer token
|
|
40
|
+
# Internationalized addresses (non-ASCII) are not detected.
|
|
41
|
+
_EMAIL_PATTERN = re.compile(
|
|
42
|
+
r"(?<![A-Za-z0-9._%+-])" # not mid-token
|
|
43
|
+
r"[A-Za-z0-9][A-Za-z0-9._%+-]*" # local part
|
|
44
|
+
r"@"
|
|
45
|
+
r"(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+" # domain labels
|
|
46
|
+
r"[A-Za-z]{2,}" # top-level domain
|
|
47
|
+
r"(?![A-Za-z0-9-])" # domain really ends
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
EMAIL = Rule(name="EMAIL", pattern=_EMAIL_PATTERN, normalize=str.lower)
|
|
51
|
+
|
|
52
|
+
RULES: tuple[Rule, ...] = (EMAIL,)
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pasteval
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Redact sensitive information from text before you paste it into AI tools.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://pasteval.com
|
|
7
|
+
Keywords: redaction,privacy,secrets,cli,sanitize
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Environment :: Console
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Security
|
|
13
|
+
Classifier: Topic :: Text Processing :: Filters
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# Pasteval
|
|
22
|
+
|
|
23
|
+
Pasteval is a small command-line tool that redacts sensitive information from
|
|
24
|
+
text before you paste it into an AI tool or any other external service.
|
|
25
|
+
|
|
26
|
+
It runs entirely on your computer. No account, no API key, no network access,
|
|
27
|
+
and no dependencies beyond Python itself.
|
|
28
|
+
|
|
29
|
+
## Why use it
|
|
30
|
+
|
|
31
|
+
When you paste a log, an email, or a config snippet into a chatbot, it is easy
|
|
32
|
+
to include something you did not mean to share. Pasteval replaces what it
|
|
33
|
+
recognizes with a placeholder, so the text stays useful and the value stays
|
|
34
|
+
private.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
Pasteval needs Python 3.11 or newer. Install it from source:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
git clone <this repository>
|
|
42
|
+
cd pasteval
|
|
43
|
+
pip install -e .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
Paste text interactively, then press Ctrl+D on a new line to finish
|
|
49
|
+
(Ctrl+Z then Enter on Windows):
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pasteval
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Pipe text in:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
cat notes.txt | pasteval
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Read a file:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pasteval --file notes.txt
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Other options:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pasteval --help
|
|
71
|
+
pasteval --version
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The redacted text is written to standard output. A short summary (types and
|
|
75
|
+
counts only, never the values) is written to standard error, so you can pipe
|
|
76
|
+
the output safely, for example `pasteval --file notes.txt | pbcopy` on macOS.
|
|
77
|
+
|
|
78
|
+
## Example
|
|
79
|
+
|
|
80
|
+
Input:
|
|
81
|
+
|
|
82
|
+
```text
|
|
83
|
+
Hi, I'm Alex.
|
|
84
|
+
My email is alex@example.com.
|
|
85
|
+
Please copy sam@example.com, and reply to alex@example.com.
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Output:
|
|
89
|
+
|
|
90
|
+
```text
|
|
91
|
+
Hi, I'm Alex.
|
|
92
|
+
My email is [EMAIL_1].
|
|
93
|
+
Please copy [EMAIL_2], and reply to [EMAIL_1].
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The same value always gets the same placeholder, so the text still makes sense.
|
|
97
|
+
|
|
98
|
+
## What is redacted
|
|
99
|
+
|
|
100
|
+
| Type | Placeholder |
|
|
101
|
+
|---|---|
|
|
102
|
+
| Email addresses | `[EMAIL_1]`, `[EMAIL_2]`, ... |
|
|
103
|
+
|
|
104
|
+
## Security limitations
|
|
105
|
+
|
|
106
|
+
Pasteval reduces the risk of accidental sharing. It does not guarantee it.
|
|
107
|
+
|
|
108
|
+
- It can miss things (false negatives). Anything not listed above is left as
|
|
109
|
+
it is, including names, postal addresses, passwords, and API keys.
|
|
110
|
+
- Email addresses containing non-English characters are not detected.
|
|
111
|
+
- It can occasionally redact something harmless (false positives).
|
|
112
|
+
- Text you paste into a terminal may remain in the terminal's scrollback.
|
|
113
|
+
- Always read the output before you share it.
|
|
114
|
+
|
|
115
|
+
Pasteval never sends your text anywhere, never saves it, and never prints the
|
|
116
|
+
original values in its messages. If something goes wrong it prints an error
|
|
117
|
+
and no text at all.
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pip install -e ".[dev]"
|
|
123
|
+
pytest
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Detection rules live in `src/pasteval/rules.py`. To add one, add a `Rule` to
|
|
127
|
+
the `RULES` list and write tests for it.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/pasteval/__init__.py
|
|
5
|
+
src/pasteval/__main__.py
|
|
6
|
+
src/pasteval/cli.py
|
|
7
|
+
src/pasteval/detector.py
|
|
8
|
+
src/pasteval/redactor.py
|
|
9
|
+
src/pasteval/rules.py
|
|
10
|
+
src/pasteval.egg-info/PKG-INFO
|
|
11
|
+
src/pasteval.egg-info/SOURCES.txt
|
|
12
|
+
src/pasteval.egg-info/dependency_links.txt
|
|
13
|
+
src/pasteval.egg-info/entry_points.txt
|
|
14
|
+
src/pasteval.egg-info/requires.txt
|
|
15
|
+
src/pasteval.egg-info/top_level.txt
|
|
16
|
+
tests/test_cli.py
|
|
17
|
+
tests/test_detector.py
|
|
18
|
+
tests/test_redactor.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pasteval
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""CLI tests run the real command in a subprocess, so they exercise the true
|
|
2
|
+
stdin/stdout/stderr streams and exit codes."""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pasteval
|
|
10
|
+
from pasteval import cli
|
|
11
|
+
|
|
12
|
+
SRC_DIR = str(Path(pasteval.__file__).resolve().parents[1])
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def run(*args: str, stdin: bytes = b"") -> subprocess.CompletedProcess[bytes]:
|
|
16
|
+
env = {**os.environ, "PYTHONPATH": SRC_DIR}
|
|
17
|
+
return subprocess.run(
|
|
18
|
+
[sys.executable, "-m", "pasteval", *args],
|
|
19
|
+
input=stdin,
|
|
20
|
+
capture_output=True,
|
|
21
|
+
env=env,
|
|
22
|
+
timeout=30,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_piped_input_is_redacted_on_stdout() -> None:
|
|
27
|
+
result = run(stdin=b"Mail alex@example.com please.\n")
|
|
28
|
+
assert result.returncode == 0
|
|
29
|
+
assert result.stdout == b"Mail [EMAIL_1] please.\n"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_summary_goes_to_stderr_without_the_value() -> None:
|
|
33
|
+
result = run(stdin=b"alex@example.com and sam@example.com")
|
|
34
|
+
assert b"redacted 2 items: 2 EMAIL" in result.stderr
|
|
35
|
+
assert b"alex" not in result.stderr
|
|
36
|
+
assert b"example.com" not in result.stderr
|
|
37
|
+
assert b"redacted" not in result.stdout
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_nothing_found_is_still_success_and_unchanged() -> None:
|
|
41
|
+
text = "plain text, nothing sensitive\n".encode()
|
|
42
|
+
result = run(stdin=text)
|
|
43
|
+
assert result.returncode == 0
|
|
44
|
+
assert result.stdout == text
|
|
45
|
+
assert b"nothing to redact" in result.stderr
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_line_endings_and_unicode_survive_byte_for_byte() -> None:
|
|
49
|
+
text = "héllo 👋\r\nmail: zoe@example.com\r\n".encode()
|
|
50
|
+
result = run(stdin=text)
|
|
51
|
+
assert result.stdout == "héllo 👋\r\nmail: [EMAIL_1]\r\n".encode()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_file_input(tmp_path: Path) -> None:
|
|
55
|
+
path = tmp_path / "note.txt"
|
|
56
|
+
path.write_bytes(b"to: alex@example.com\n")
|
|
57
|
+
result = run("--file", str(path))
|
|
58
|
+
assert result.returncode == 0
|
|
59
|
+
assert result.stdout == b"to: [EMAIL_1]\n"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_missing_file_exits_1_with_no_stdout(tmp_path: Path) -> None:
|
|
63
|
+
result = run("--file", str(tmp_path / "missing.txt"))
|
|
64
|
+
assert result.returncode == 1
|
|
65
|
+
assert result.stdout == b""
|
|
66
|
+
assert b"could not read" in result.stderr
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_invalid_utf8_exits_1_and_prints_no_input(tmp_path: Path) -> None:
|
|
70
|
+
path = tmp_path / "bad.txt"
|
|
71
|
+
path.write_bytes(b"alex@example.com \xff\xfe")
|
|
72
|
+
result = run("--file", str(path))
|
|
73
|
+
assert result.returncode == 1
|
|
74
|
+
assert result.stdout == b""
|
|
75
|
+
assert b"alex" not in result.stderr
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_unknown_option_is_a_usage_error() -> None:
|
|
79
|
+
result = run("--nope")
|
|
80
|
+
assert result.returncode == 2
|
|
81
|
+
assert result.stdout == b""
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_version() -> None:
|
|
85
|
+
result = run("--version")
|
|
86
|
+
assert result.returncode == 0
|
|
87
|
+
assert result.stdout.decode().strip() == f"pasteval {pasteval.__version__}"
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_help() -> None:
|
|
91
|
+
result = run("--help")
|
|
92
|
+
assert result.returncode == 0
|
|
93
|
+
assert b"--file" in result.stdout
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_redaction_failure_prints_no_original_text(monkeypatch, capsysbinary) -> None:
|
|
97
|
+
"""If redaction itself crashes, the original text must never be printed."""
|
|
98
|
+
|
|
99
|
+
def broken(_text: str):
|
|
100
|
+
raise RuntimeError("boom alex@example.com")
|
|
101
|
+
|
|
102
|
+
class FakeStdin:
|
|
103
|
+
buffer = __import__("io").BytesIO(b"secret alex@example.com")
|
|
104
|
+
|
|
105
|
+
@staticmethod
|
|
106
|
+
def isatty() -> bool:
|
|
107
|
+
return False
|
|
108
|
+
|
|
109
|
+
monkeypatch.setattr(cli, "redact", broken)
|
|
110
|
+
monkeypatch.setattr(sys, "stdin", FakeStdin)
|
|
111
|
+
assert cli.main([]) == 1
|
|
112
|
+
captured = capsysbinary.readouterr()
|
|
113
|
+
assert captured.out == b""
|
|
114
|
+
assert b"alex" not in captured.err
|
|
115
|
+
assert b"redaction failed" in captured.err
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from pasteval.detector import Match, find_matches
|
|
6
|
+
from pasteval.rules import Rule
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def spans(text: str) -> list[str]:
|
|
10
|
+
"""The pieces of `text` that were detected, for readable assertions."""
|
|
11
|
+
return [text[m.start:m.end] for m in find_matches(text)]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.mark.parametrize(
|
|
15
|
+
"text, expected",
|
|
16
|
+
[
|
|
17
|
+
("alex@example.com", ["alex@example.com"]),
|
|
18
|
+
("Mail alex@example.com.", ["alex@example.com"]), # sentence period
|
|
19
|
+
("<alex@example.com>", ["alex@example.com"]),
|
|
20
|
+
("mailto:alex@example.com", ["alex@example.com"]),
|
|
21
|
+
("first.last+tag@mail.example.co.uk", ["first.last+tag@mail.example.co.uk"]),
|
|
22
|
+
("a@example.com,b@example.org", ["a@example.com", "b@example.org"]),
|
|
23
|
+
("USER_1@EXAMPLE.COM", ["USER_1@EXAMPLE.COM"]),
|
|
24
|
+
],
|
|
25
|
+
)
|
|
26
|
+
def test_emails_are_detected(text: str, expected: list[str]) -> None:
|
|
27
|
+
assert spans(text) == expected
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.mark.parametrize(
|
|
31
|
+
"text",
|
|
32
|
+
[
|
|
33
|
+
"",
|
|
34
|
+
"no sensitive data here",
|
|
35
|
+
"@handle on social media",
|
|
36
|
+
"user@localhost", # no dot in the domain
|
|
37
|
+
"user@example", # no top-level domain
|
|
38
|
+
"user@example.c", # top-level domain too short
|
|
39
|
+
"user@.com",
|
|
40
|
+
"user@-example.com",
|
|
41
|
+
"price is 5@ each",
|
|
42
|
+
"a @ b.com",
|
|
43
|
+
"decorator @app.route",
|
|
44
|
+
"git@",
|
|
45
|
+
],
|
|
46
|
+
)
|
|
47
|
+
def test_incomplete_or_lookalike_text_is_not_detected(text: str) -> None:
|
|
48
|
+
assert spans(text) == []
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_matches_are_in_text_order_with_normalized_key() -> None:
|
|
52
|
+
matches = find_matches("B@Example.com then a@example.com")
|
|
53
|
+
assert [(m.start, m.rule_name, m.key) for m in matches] == [
|
|
54
|
+
(0, "EMAIL", "b@example.com"),
|
|
55
|
+
(19, "EMAIL", "a@example.com"),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_repr_never_contains_the_value() -> None:
|
|
60
|
+
(match,) = find_matches("alex@example.com")
|
|
61
|
+
assert "alex" not in repr(match)
|
|
62
|
+
assert "example.com" not in repr(match)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# The overlap rule is tested with made-up rules so it does not depend on
|
|
66
|
+
# which real rules exist.
|
|
67
|
+
def _rule(name: str, pattern: str) -> Rule:
|
|
68
|
+
return Rule(name=name, pattern=re.compile(pattern))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _names(text: str, rules: list[Rule]) -> list[tuple[str, str]]:
|
|
72
|
+
return [(m.rule_name, text[m.start:m.end]) for m in find_matches(text, rules)]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_overlap_earliest_start_wins() -> None:
|
|
76
|
+
rules = [_rule("LATE", "cdef"), _rule("EARLY", "abcd")]
|
|
77
|
+
assert _names("abcdef", rules) == [("EARLY", "abcd")]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_overlap_same_start_longest_wins() -> None:
|
|
81
|
+
rules = [_rule("SHORT", "abc"), _rule("LONG", "abcdef")]
|
|
82
|
+
assert _names("abcdef", rules) == [("LONG", "abcdef")]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_overlap_same_span_first_rule_wins() -> None:
|
|
86
|
+
rules = [_rule("FIRST", "abc"), _rule("SECOND", "abc")]
|
|
87
|
+
assert _names("abc", rules) == [("FIRST", "abc")]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_non_overlapping_matches_from_different_rules_are_all_kept() -> None:
|
|
91
|
+
rules = [_rule("A", "aaa"), _rule("B", "bbb")]
|
|
92
|
+
assert _names("bbb aaa", rules) == [("B", "bbb"), ("A", "aaa")]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_match_is_a_plain_value_object() -> None:
|
|
96
|
+
assert Match(0, 3, "X", "abc") == Match(0, 3, "X", "abc")
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from pasteval.redactor import redact
|
|
4
|
+
from pasteval.rules import Rule
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_single_email() -> None:
|
|
8
|
+
result = redact("My email is alex@example.com.")
|
|
9
|
+
assert result.text == "My email is [EMAIL_1]."
|
|
10
|
+
assert result.counts == {"EMAIL": 1}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_different_emails_get_different_placeholders() -> None:
|
|
14
|
+
result = redact("a@example.com and b@example.com")
|
|
15
|
+
assert result.text == "[EMAIL_1] and [EMAIL_2]"
|
|
16
|
+
assert result.counts == {"EMAIL": 2}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_repeated_email_reuses_its_placeholder() -> None:
|
|
20
|
+
result = redact("a@example.com, b@example.com, a@example.com")
|
|
21
|
+
assert result.text == "[EMAIL_1], [EMAIL_2], [EMAIL_1]"
|
|
22
|
+
assert result.counts == {"EMAIL": 3}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_emails_compare_case_insensitively() -> None:
|
|
26
|
+
result = redact("Alex@Example.com / alex@example.com")
|
|
27
|
+
assert result.text == "[EMAIL_1] / [EMAIL_1]"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_text_without_sensitive_data_is_unchanged() -> None:
|
|
31
|
+
text = "Nothing to see here.\n\tJust ordinary text @ 5pm.\n"
|
|
32
|
+
result = redact(text)
|
|
33
|
+
assert result.text == text
|
|
34
|
+
assert result.counts == {}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_empty_input() -> None:
|
|
38
|
+
assert redact("").text == ""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_multiline_formatting_is_preserved() -> None:
|
|
42
|
+
text = "Hi, I'm Alex.\n My email is alex@example.com.\r\n\r\nThanks!\n"
|
|
43
|
+
expected = "Hi, I'm Alex.\n My email is [EMAIL_1].\r\n\r\nThanks!\n"
|
|
44
|
+
assert redact(text).text == expected
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_unicode_text_is_preserved() -> None:
|
|
48
|
+
text = "Привет 👋 — écris à zoé: zoe@example.com。ありがとう"
|
|
49
|
+
expected = "Привет 👋 — écris à zoé: [EMAIL_1]。ありがとう"
|
|
50
|
+
assert redact(text).text == expected
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_output_is_deterministic() -> None:
|
|
54
|
+
text = "b@example.com a@example.com b@example.com"
|
|
55
|
+
assert redact(text) == redact(text)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_numbering_is_per_type_and_dropped_overlaps_use_no_number() -> None:
|
|
59
|
+
rules = [
|
|
60
|
+
Rule(name="WORD", pattern=re.compile(r"cat|dog")),
|
|
61
|
+
Rule(name="PAIR", pattern=re.compile(r"cat dog")), # overlaps WORD
|
|
62
|
+
Rule(name="NUM", pattern=re.compile(r"\d+")),
|
|
63
|
+
]
|
|
64
|
+
result = redact("cat dog 42 dog 7 42", rules)
|
|
65
|
+
# "cat dog" (longer, same start) beats "cat"; the swallowed "dog" inside
|
|
66
|
+
# it is dropped, so the later standalone "dog" is WORD_1, not WORD_2.
|
|
67
|
+
assert result.text == "[PAIR_1] [NUM_1] [WORD_1] [NUM_2] [NUM_1]"
|
|
68
|
+
assert result.counts == {"PAIR": 1, "NUM": 3, "WORD": 1}
|