Skip to content
Explore/Farnor Labs
Log inStart free

safe-display

v2 publishedPublic code
OverviewCodeChecksSkills
OverviewCodeChecksSkills
Timeline/v2

Strip more invisible characters from displayName, without breaking…

Published

displayName now strips more invisible characters (word joiner and invisible operators, soft hyphen, Mongolian vowel separator) and no longer breaks emoji that use a zero-width joiner, such as family and profession emoji. Adds 3 tests and updates the README's API notes and known gaps.

This version

Asked for by
Nate Warner
Built by
Claude Code
Published by
Nate Warner
When
Oct 31
Risk
Medium risk
Checks
Check 6 passed
Review history (draft #1)
Browse the code at v2

Files changed since v1

3 files

Logic

  • src/index.js+8 −2Changes STRIPPED.

Docs

  • README.md+3 −2Changes the “api” section, the “known gaps good first contributions” section.

Tests

  • test/safe-display.test.js+24 −0Changes cp.
README.mdReference doc · Changes the “api” section, the “known gaps good first contributions” section.Show

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–200D/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 realother 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.

  • OtherPersian invisibleand charactersIndic aretext notthat strippedneeds yet:a wordzero-width non-joiner and invisible operators (U+2060–2064200C), softor hyphenjoiner (U+00AD200D) 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

src/index.jsChanges STRIPPED.Show
lines 1–20
1// safe-display: pure functions for showing untrusted strings to a person.1// safe-display: pure functions for showing untrusted strings to a person.
2// No dependencies, no I/O. Ported from the Sanitize module of SavedMe (a macOS app).2// No dependencies, no I/O. Ported from the Sanitize module of SavedMe (a macOS app).
33
4// Bidi controls, zero-width characters, C0/C1 controls and line breaks.4// Characters that are invisible or that change how text is ordered, so two different
test/safe-display.test.jsChanges cp.Show
lines 65–91
65 assert.equal(displayHost(h), domainToASCII(h), h);65 assert.equal(displayHost(h), domainToASCII(h), h);
66 }66 }
67});67
5
// names can look the same or a name can read backwards.
5// U+0000-001F, U+007F-009F C0, DEL, C1 (includes \n \r \t)6// U+0000-001F, U+007F-009F C0, DEL, C1 (includes \n \r \t)
7// U+00AD soft hyphen
6// U+061C Arabic letter mark8// U+061C Arabic letter mark
9// U+180E Mongolian vowel separator
7// U+200B-200F zero-width space/joiners, LRM, RLM10// U+200B-200F zero-width space/joiners, LRM, RLM
8// U+2028-202E line/paragraph separators, bidi embeddings and overrides11// U+2028-202E line/paragraph separators, bidi embeddings and overrides
12// U+2060-2064 word joiner and invisible operators
9// U+2066-2069 bidi isolates13// U+2066-2069 bidi isolates
10// U+FEFF byte order mark / zero-width no-break space14// U+FEFF byte order mark / zero-width no-break space
11const STRIPPED = /[\u0000-\u001F\u007F-\u009F\u061C\u200B-\u200F\u2028-\u202E\u2066-\u2069\uFEFF]/gu;15// U+200D (zero-width joiner) is kept when it joins two emoji, as in a family or a
16// profession emoji, and stripped everywhere else.
17const STRIPPED = /[\u0000-\u001F\u007F-\u009F\u00AD\u061C\u180E\u200B-\u200C\u200E-\u200F\u2028-\u202E\u2060-\u2064\u2066-\u2069\uFEFF]|(?<!\p{Extended_Pictographic}[\uFE0F\p{Emoji_Modifier}]*)\u200D|\u200D(?!\p{Extended_Pictographic})/gu;
1218
13/**19/**
14 * A file name or other label, safe to render: bidi overrides, zero-width20 * A file name or other label, safe to render: bidi overrides, zero-width
11// safe-display: pure functions for showing untrusted strings to a person.
22// No dependencies, no I/O. Ported from the Sanitize module of SavedMe (a macOS app).
33
4−// Bidi controls, zero-width characters, C0/C1 controls and line breaks.
4+// Characters that are invisible or that change how text is ordered, so two different
5+// names can look the same or a name can read backwards.
56// U+0000-001F, U+007F-009F C0, DEL, C1 (includes \n \r \t)
7+// U+00AD soft hyphen
68// U+061C Arabic letter mark
9+// U+180E Mongolian vowel separator
710// U+200B-200F zero-width space/joiners, LRM, RLM
811// U+2028-202E line/paragraph separators, bidi embeddings and overrides
12+// U+2060-2064 word joiner and invisible operators
913// U+2066-2069 bidi isolates
1014// U+FEFF byte order mark / zero-width no-break space
11−const STRIPPED = /[\u0000-\u001F\u007F-\u009F\u061C\u200B-\u200F\u2028-\u202E\u2066-\u2069\uFEFF]/gu;
15+// U+200D (zero-width joiner) is kept when it joins two emoji, as in a family or a
16+// profession emoji, and stripped everywhere else.
17+const STRIPPED = /[\u0000-\u001F\u007F-\u009F\u00AD\u061C\u180E\u200B-\u200C\u200E-\u200F\u2028-\u202E\u2060-\u2064\u2066-\u2069\uFEFF]|(?<!\p{Extended_Pictographic}[\uFE0F\p{Emoji_Modifier}]*)\u200D|\u200D(?!\p{Extended_Pictographic})/gu;
1218
1319/**
1420 * A file name or other label, safe to render: bidi overrides, zero-width
});
68
69const cp = (...codes) => String.fromCodePoint(...codes);
70
71test("displayName strips word joiner, invisible operators, soft hyphen and Mongolian vowel separator", () => {
72 assert.equal(displayName("in" + cp(0x2060) + "vo" + cp(0x2061) + "i" + cp(0x2062) + "c" + cp(0x2063) + "e" + cp(0x2064) + ".pdf"), "invoice.pdf");
73 assert.equal(displayName("inv" + cp(0xad) + "oice" + cp(0x180e) + ".pdf"), "invoice.pdf");
74});
75
76test("displayName keeps ZWJ emoji sequences, variation selectors and flag tag sequences intact", () => {
77 const family = cp(0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467);
78 assert.equal(displayName(family), family);
79 const heart = cp(0x2764, 0xfe0f);
80 assert.equal(displayName(heart), heart);
81 const technologist = cp(0x1f468, 0x1f3fd, 0x200d, 0x1f4bb);
82 assert.equal(displayName(technologist), technologist);
83 const england = cp(0x1f3f4, 0xe0067, 0xe0062, 0xe0065, 0xe006e, 0xe0067, 0xe007f);
84 assert.equal(displayName(england), england);
85});
86
87test("displayName still strips a ZWJ that is not between two emoji", () => {
88 assert.equal(displayName("a" + cp(0x200d) + "b"), "ab");
89 assert.equal(displayName(cp(0x1f468, 0x200d) + "x"), cp(0x1f468) + "x");
90 assert.equal(displayName("x" + cp(0x200d, 0x1f469)), "x" + cp(0x1f469));
91});
6565 assert.equal(displayHost(h), domainToASCII(h), h);
6666 }
6767});
68+
69+const cp = (...codes) => String.fromCodePoint(...codes);
70+
71+test("displayName strips word joiner, invisible operators, soft hyphen and Mongolian vowel separator", () => {
72+ assert.equal(displayName("in" + cp(0x2060) + "vo" + cp(0x2061) + "i" + cp(0x2062) +
73+ assert.equal(displayName("inv" + cp(0xad) + "oice" + cp(0x180e) + ".pdf"), "invoice.pdf");
74+});
75+
76+test("displayName keeps ZWJ emoji sequences, variation selectors and flag tag sequences intact", () => {
77+ const family = cp(0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467);
78+ assert.equal(displayName(family), family);
79+ const heart = cp(0x2764, 0xfe0f);
80+ assert.equal(displayName(heart), heart);
81+ const technologist = cp(0x1f468, 0x1f3fd, 0x200d, 0x1f4bb);
82+ assert.equal(displayName(technologist), technologist);
83+ const england = cp(0x1f3f4, 0xe0067, 0xe0062, 0xe0065, 0xe006e, 0xe0067, 0xe007f);
84+ assert.equal(displayName(england), england);
85+});
86+
87+test("displayName still strips a ZWJ that is not between two emoji", () => {
88+ assert.equal(displayName("a" + cp(0x200d) + "b"), "ab");
89+ assert.equal(displayName(cp(0x1f468, 0x200d) + "x"), cp(0x1f468) + "x");
90+ assert.equal(displayName("x" + cp(0x200d, 0x1f469)), "x" + cp(0x1f469));
91+});
"c"
+
cp
(
0x2063
)
+
"e"
+
cp
(
0x2064
)
+
".pdf"
),
"invoice.pdf"
);