File names and URLs: similar strings with different rules

Topic illustration for the article: File names and URLs: similar strings with different rules
Practical illustration for this guide

Two files can both appear as report.pdf while one contains U+200B before the dot. Yet https://…/report%E2%80%8B.pdf is a URL and must be handled by a URL parser, not by file-system string rules.

Load a filename containing ZWSP

Obtain the exact name

A file browser may hide a trailing space, dot or zero-width character. Read the name through an API, list code points and compare length. A shortened visual label is not reliable evidence.

Operating systems differ

Windows reserves characters and names, while trailing-space behaviour depends on the API. Other file systems have different rules and case sensitivity. Test a portable archive in every target environment.

A URL has its own grammar

URLs contain scheme, host, path, query and fragment. Percent-encoding represents bytes within particular components. Blind replacement of %E2%80%8B can change meaning or a signed request. Use new URL and edit only the intended component.

Never decode twice by accident

A second decodeURIComponent can turn an encoded percent sign into a new delimiter or control. Decode exactly where the protocol requires it, handle URIError and retain the original string for audit.

Renaming changes references

Build an old-to-new table and detect collisions before a bulk operation. Update links, imports, attachments and backups. If two names normalize to one value, an automatic job can overwrite data.

Keep a security boundary

Generate a server-side storage name for uploads and retain the user name as metadata. Resolve path segments inside the intended directory. Unicode cleaning does not prevent path traversal.

Inspecting a name and a URL

Detect U+200B in the file name first. Then construct and parse a URL with platform tools. Do not feed a complete URL to a file-name cleaner.

const filename = "report\u200B.pdf";
console.log([...filename].map(c => c.codePointAt(0).toString(16)));
const url = new URL("https://example.com/report%E2%80%8B.pdf");
console.log(url.pathname);

Before a bulk rename

  1. Export exact names and code points.
  2. Create a mapping table.
  3. Detect collisions and reserved names.
  4. Update references on a test copy.
  5. Verify backup restoration.

Open the cleaner

Sources and specifications

Related guides

Author: Mykola. File systems, paths and URLs are deliberately treated as different layers with different rules.