What this project is and how to use it.
safe-display
Show untrusted file names, hosts and URLs to a person without being fooled by them.
A file called Invoice + U+202E + fdp.app is displayed as Invoiceppa.pdf, but it is an app. A host written with a Cyrillic ѕ looks like stripe.com. A stored link can carry a live access token in its query string. Anything that lists downloads, attachments, uploads or links needs to deal with these. This package does that and nothing else: four pure functions, no dependencies, no I/O.
import { displayName, displayHost, sourceURL, isOpenableURL } from "safe-display";
displayName("Invoice‹U+202E›fdp.app"); // "Invoicefdp.app" bidi override removed
displayHost("ѕtripe.com"); // "xn--tripe-g2e.com" lookalike shown as punycode
sourceURL("https://u:pw@Bucket.S3.amazonaws.com/inv/1.pdf?X-Amz-Signature=abc#x");
// "https://bucket.s3.amazonaws.com/inv/1.pdf"
isOpenableURL("javascript:alert(1)"); // false: only http(s) with a host is openable
API
| Function |
What it does |
displayName(name) |
Removes bidi controls (U+061C, U+200E/F, U+202A–202E, U+2066–2069), zero-width characters (U+200B/C, U+FEFF), word joiner and invisible operators (U+2060–2064), soft hyphen (U+00AD), Mongolian vowel separator (U+180E), line and paragraph separators, and C0/C1 control characters. A zero-width joiner (U+200D) is kept only when it joins two emoji, so family and profession emoji survive. Keeps all other text, including emoji and any script. |
displayHost(host) |
Cleans like displayName, lowercases, and shows each non-ASCII label as IDNA punycode (xn--…). Display only; it does no full IDNA mapping. |
sourceURL(raw) |
Drops username, password, query and fragment; lowercases the host. Returns null for non-URLs and for http(s) URLs with no host written in them (https:///x, https:/x.com), rather than quietly repairing them. |
isOpenableURL(raw) |
true only for http or https with a host. Check this before opening any stored link. |
Types are included (src/index.d.ts). Requires Node 18 or later, or any modern browser.
What this does not do
- It does not decide whether a file or link is trustworthy. The output is safe to show, not proof of anything.
- It does not detect mixed-script lookalikes inside a host that is already ASCII, or look-alike letters inside a file name. See the open gaps below.
Known gaps (good first contributions)
These are real, not hypothetical. Each is a small change with a test.
- Persian and Indic text that needs a zero-width non-joiner (U+200C) or joiner (U+200D) loses it. Stripping is the safe default for look-alike names, but it changes how those words render. A script-aware rule would fix this.
- Unicode tag characters (U+E0000–E007F) and variation selectors are deliberately kept, because flag and emoji sequences use them. Outside those sequences they are invisible too, so a stricter mode would strip them.
- No mixed-script or whole-script-confusable detection for hosts (for example, flag a label mixing Latin and Cyrillic).
displayHost does not apply full IDNA/UTS #46 mapping.
displayName does not shorten long names or keep an extension visible. That is a UI concern, but a helper would be useful.
Development
npm test
There is nothing to install: no dependencies and no build step. Tests use node:test. One test compares displayHost against Node's own domainToASCII.
This package began as the sanitizer in SavedMe, a macOS app that lists recently saved files. The test cases come from the threats that app's review found.
License
MIT