Skip to content
Explore/Farnor Labs
Log inStart free

safe-display

v2 publishedPublic code
OverviewCodeChecksSkills
OverviewCodeChecksSkills
Files
  • AGENTS.md
  • CLAUDE.md
  • GAITRO.md
  • LICENSE
  • package.json
  • README.md

README.md

55 linesPublisheda2ffee96

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

Source and history
1# safe-display
2
3Show untrusted file names, hosts and URLs to a person without being fooled by them.
4
5A 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.
6
7```js
8import { displayName, displayHost, sourceURL, isOpenableURL } from
"safe-display"
;
9
10displayName("Invoice‹U+202E›fdp.app"); // "Invoicefdp.app" bidi override removed
11displayHost("ѕtripe.com"); // "xn--tripe-g2e.com" lookalike shown as punycode
12sourceURL("https://u:pw@Bucket.S3.amazonaws.com/inv/1.pdf?X-Amz-Signature=abc#x");
13 // "https://bucket.s3.amazonaws.com/inv/1.pdf"
14isOpenableURL("javascript:alert(1)"); // false: only http(s) with a host is openable
15```
16
17## API
18
19| Function | What it does |
20| --- | --- |
21| `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. |
22| `displayHost(host)` | Cleans like `displayName`, lowercases, and shows each non-ASCII label as IDNA punycode (`xn--…`). Display only; it does no full IDNA mapping. |
23| `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. |
24| `isOpenableURL(raw)` | `true` only for http or https with a host. Check this before opening any stored link. |
25
26Types are included (`src/index.d.ts`). Requires Node 18 or later, or any modern browser.
27
28## What this does not do
29
30- It does not decide whether a file or link is trustworthy. The output is safe to *show*, not proof of anything.
31- 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.
32
33## Known gaps (good first contributions)
34
35These are real, not hypothetical. Each is a small change with a test.
36
37- 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.
38- 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.
39- No mixed-script or whole-script-confusable detection for hosts (for example, flag a label mixing Latin and Cyrillic).
40- `displayHost` does not apply full IDNA/UTS #46 mapping.
41- `displayName` does not shorten long names or keep an extension visible. That is a UI concern, but a helper would be useful.
42
43## Development
44
45```
46npm test
47```
48
49There is nothing to install: no dependencies and no build step. Tests use `node:test`. One test compares `displayHost` against Node's own `domainToASCII`.
50
51This 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.
52
53## License
54
55MIT
56