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.
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
| 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 |
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.
These are real, not hypothetical. Each is a small change with a test.
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.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.
MIT
| 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). |
| 3 | 3 | ||
| 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 |
| 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 mark | 8 | // U+061C Arabic letter mark |
| 9 | // U+180E Mongolian vowel separator |
| 7 | // U+200B-200F zero-width space/joiners, LRM, RLM | 10 | // U+200B-200F zero-width space/joiners, LRM, RLM |
| 8 | // U+2028-202E line/paragraph separators, bidi embeddings and overrides | 11 | // U+2028-202E line/paragraph separators, bidi embeddings and overrides |
| 12 | // U+2060-2064 word joiner and invisible operators |
| 9 | // U+2066-2069 bidi isolates | 13 | // U+2066-2069 bidi isolates |
| 10 | // U+FEFF byte order mark / zero-width no-break space | 14 | // 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; |
| 12 | 18 |
| 13 | /** | 19 | /** |
| 14 | * A file name or other label, safe to render: bidi overrides, zero-width | 20 | * A file name or other label, safe to render: bidi overrides, zero-width |
| 1 | 1 | // safe-display: pure functions for showing untrusted strings to a person. | |
| 2 | 2 | // No dependencies, no I/O. Ported from the Sanitize module of SavedMe (a macOS app). | |
| 3 | 3 | ||
| 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. | |
| 5 | 6 | // U+0000-001F, U+007F-009F C0, DEL, C1 (includes \n \r \t) | |
| 7 | + | // U+00AD soft hyphen | |
| 6 | 8 | // U+061C Arabic letter mark | |
| 9 | + | // U+180E Mongolian vowel separator | |
| 7 | 10 | // U+200B-200F zero-width space/joiners, LRM, RLM | |
| 8 | 11 | // U+2028-202E line/paragraph separators, bidi embeddings and overrides | |
| 12 | + | // U+2060-2064 word joiner and invisible operators | |
| 9 | 13 | // U+2066-2069 bidi isolates | |
| 10 | 14 | // 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; | |
| 12 | 18 | ||
| 13 | 19 | /** | |
| 14 | 20 | * A file name or other label, safe to render: bidi overrides, zero-width |
| }); |
| 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) + "c" + cp(0x2063) + "e" + cp(0x2064) + ".pdf"), "invoice.pdf"); |
| 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 | }); |
| 65 | 65 | assert.equal(displayHost(h), domainToASCII(h), h); | |
| 66 | 66 | } | |
| 67 | 67 | }); | |
| 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 | + | }); |