Mac Labs AI

hidden-char-scan

A Python detector for hidden format characters, bidirectional controls, and lookalike letters already present in a file. It reports them. It does not rewrite files, and it is not a general security auditor.

  • MIT License
  • Python 3.11+
  • Standard library
  • 16 detectors
  • Detector only

Built for openSUSE Hack Week 26.

Purpose

What it does

AI agents and review tools often read markdown, RPM spec files, systemd units, JSON (including MCP tool descriptors), and source code as ordinary text. Some Unicode characters do not show up clearly in a terminal or a diff.

hidden-char-scan walks the files you point it at and reports those characters when they are already there. Each finding includes a detector code, a severity, a line and column, and a code point printed as U+202E or a raw invalid byte as 0xFF. Reports never emit the raw character. A JSON report (--json) is available so another program, or an agent that can run a shell command, can read the same findings.

Detectors

What it flags

Sixteen detectors cover hidden format characters, bidirectional controls, lookalike letters, unusual spaces, encoding damage, related controls, and escape sequences for hidden characters in JSON, TOML, and YAML. Severity is chosen so the default --fail-on medium stays useful. A paragraph of Russian is not flagged just because it is not Latin. A zero-width joiner in an emoji sequence or after an Indic virama is information-only by default, and an emoji keycap is not reported.

BIDI
Bidirectional controls that can change the visual order of a line.
INVISIBLE
Format characters that are usually invisible, such as a zero-width space.
UNUSUAL_SPACE
Spaces other than the ordinary ASCII space, such as a no-break space.
BOM
A byte-order mark. Leading is info; later in the file is treated as high.
CONFUSABLE
Lookalike letters from another script inside a Latin word, plus a short list of punctuation and digit lookalikes.
MIXED_SCRIPT
A word or identifier that mixes Latin letters with another letter script.
COMBINING
A combining mark on Latin text, a digit, punctuation, or with no base character.
TAG
Unexpected characters in the Unicode tag block.
VARIATION_SELECTOR
A variation selector that is not attached to a symbol or emoji.
DEPRECATED
Deprecated format controls (U+206A through U+206F).
CONTROL
Unexpected C0 or C1 controls, line or paragraph separators, and lone surrogates.
BLANK_LOOKALIKE
Characters that look like a blank in many fonts but are not a space.
ENCODING
A byte that is not valid UTF-8, reported as 0xFF. The rest of the file is still scanned.
NONCHARACTER
Unicode noncharacters.
REPLACEMENT
U+FFFD, usually a sign that text was damaged by an earlier decoding step.
ESCAPED
In JSON, TOML, and YAML: an escape such as \u202e that stands for a hidden or control character. Each escape is reported on its own as U+XXXX; escapes are never joined into text.

Limits

What it does not do

  • It does not generate, encode, or recommend hidden characters.
  • It does not rewrite or “fix” files.
  • It is not a general security auditor, and it does not judge the meaning of a file.
  • It does not ship a full Unicode Security Mechanisms confusable database. The lookalike list is a curated subset, built with the standard library.
  • It does not scan binary files or follow symbolic links found while walking a directory. A text file that contains NUL bytes is still scanned, and it is reported as an error.
  • It never reports private-use or unassigned code points, and it does not flag a word made entirely of lookalike letters on a line with no Latin letters.
  • Directory scans read known source, document, and config types and count the files they did not read; --all-text reads every non-binary file.

Command line

Install and run

Python 3.11 or newer is required. There are no runtime dependencies beyond the standard library. The package is on PyPI:

pip install hidden-char-scan==0.1.1
hidden-char-scan path/to/file.md
hidden-char-scan --json src/
hidden-char-scan --fail-on medium .
hidden-char-scan --strict --fail-on info .

To work from source instead, clone the GitHub repository and run pip install -e . in it.

By default the tool recurses directories, skips VCS and cache folders, and fails the run when a finding at medium severity or higher is present. Information-only findings still appear in the report. Invalid UTF-8 is scanned, not skipped. Config can live in hidden-char-scan.toml, found by walking up toward the repository root.

Integrations

Beyond a one-off scan

The same detector, with the same exit status, can be run by an agent, a hook, or CI. Exit 0 means nothing reached the --fail-on threshold, 1 means a finding did, and 2 means the run was not fully checked: usage, git, unreadable input, a bad baseline, or, with --require-scan, no file scanned at all. An error is not a clean result.

Any AI that can run a shell command can call the CLI directly. Portable instructions are in skills/hidden-char-scan/SKILL.md. The command line is the primary interface; MCP is optional.

python -m hidden_char_scan --json PATH...
hidden-char-scan --git-staged
hidden-char-scan --git-diff=origin/main...HEAD
hidden-char-scan --git-diff=origin/main...HEAD --added-lines-only
hidden-char-scan --require-scan .
printf '%s' "$TEXT" | hidden-char-scan --json --stdin
hidden-char-scan --sarif -o results.sarif .
hidden-char-scan --write-baseline hcs-baseline.json .
hidden-char-scan --baseline hcs-baseline.json .
python -m hidden_char_scan.mcp

--git-staged and --git-diff=REF scan only the files git reports as changed. Write the equals form so a path is not mistaken for a ref. If git is missing, the directory is not a repository, or the ref is bad, the tool exits 2; it never silently scans everything. --git-staged reads the index, the content that would actually be committed, not later working-tree edits. Standard input is accepted as - or --stdin, and is shown as <stdin>.

--added-lines-only, with --git-staged or --git-diff, reports only findings on lines the diff adds or changes, from git diff --unified=0. New, renamed, and binary files, and files git cannot diff line by line, are reported whole and listed in the summary, so nothing is dropped silently. A baseline still works with it.

Each finding includes a sanitized snippet of the surrounding line. Hidden characters appear as U+XXXX, invalid bytes as 0xNN, and the same marks in the line as <U+XXXX>. --no-context leaves the snippet out.

--write-baseline PATH records current findings; --baseline PATH then fails only on new ones. A baseline is for known, accepted findings, not a way to hide them. --require-scan (or require_scan = true) turns a run that scanned no files into an error, which is the setting to use for a full-tree CI scan. The fingerprint is path, line, column, detector, and code (U+XXXX, or 0xNN for ENCODING).

--sarif writes a SARIF 2.1.0 report. A GitHub Actions example in examples/github-actions/hidden-char-scan.yml runs --fail-on medium --sarif with --require-scan, uploads that report with if: always(), and includes an optional job that scans only the files a pull request changed, using --git-diff. The example has not been run on GitHub from this tree. A pre-commit example is in examples/pre-commit-config.yaml (install the package first so the command is on PATH). .pre-commit-hooks.yaml lets you use the public repository as a repo: hook; there are no release tags yet, so pin rev: to a commit SHA.

For agents that speak the Model Context Protocol, python -m hidden_char_scan.mcp or the hidden-char-scan-mcp console script starts a standard-library stdio server with two read-only tools, scan_paths and scan_text. Example client configs in examples/mcp/ use the placeholder /ABSOLUTE/PATH/TO/VENV/bin/python. The server has not been submitted to an MCP registry. It has been tried from this tree against the official MCP Python SDK over stdio, and against the MCP Inspector command-line client. Both listed only the two read-only tools, and scan_text did not return the input text.

Report shape

What a finding looks like

Illustration only This page does not run the scanner. The specimen above is a labeled artifact. The rows below are a static picture of what the real tool reports, with code points written as U+XXXX. To run the real scanner on sample files or your own text, use the live demo on Hugging Face Spaces.

Fail hidden_zwsp.md · 1 high · fail-on medium
SeverityDetectorLineColCode pointNameWhy
high INVISIBLE 3 25 U+200B ZERO WIDTH SPACE Invisible or format character. It does not display in most editors and can change tokenization.

Live demo

Try it on Hugging Face

Live on Hugging Face Spaces

A free CPU Gradio Space runs this same scanner on prepared sample files, pasted text, or a small upload. Findings are printed as U+XXXX or 0xFF, never as the raw character. The Space only reports what is already in the input; it has no control that inserts or rewrites characters.

mackenzietechdocs/hidden-char-scan · Gradio · cpu-basic (no GPU)

Open the live demo

The same command works locally for an agent, a hook, or CI.

This project was built for openSUSE Hack Week 26.