Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sanitize-filename-ts

Package Overview
Dependencies
Maintainers
7
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanitize-filename-ts

Sanitize a string for use as a filename

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
7
Created
Source

sanitize-filename-ts

Sanitize a string to be safe for use as a filename by removing directory paths and invalid characters. This package is a fork of sanitize-filename, updated and rewritten in Typescript.

Install

npm: sanitize-filename-ts

npm install sanitize-filename-ts

Example

import { sanitize } from "sanitize-filename-ts";

// Some string that may be unsafe or invalid as a filename
var UNSAFE_USER_INPUT = "~/.\u0000ssh/authorized_keys";

// Sanitize the string to be safe for use as a filename.
var filename = sanitize(UNSAFE_USER_INPUT);
// -> "~.sshauthorized_keys"

Details

sanitize-filename-ts removes the following:

  • Control characters (0x000x1f and 0x800x9f)
  • Reserved characters (/, ?, <, >, \, :, *, |, and ")
  • Unix reserved filenames (. and ..)
  • Trailing periods and spaces (for Windows)
  • Windows reserved filenames (CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9)

The resulting string is truncated to 255 bytes in length. The string will not contain any directory paths and will be safe to use as a filename.

Empty String "" Result

An empty string "" can be returned. For example:

import { sanitize } from "sanitize-filename-ts";
sanitize("..");
// -> ""

Non-unique Filenames

Two different inputs can return the same value. For example:

import { sanitize } from "sanitize-filename-ts";
sanitize("file?");
// -> "file"
sanitize("*file*");
// -> "file"

File Systems

Sanitized filenames will be safe for use on modern Windows, OS X, and Unix file systems (NTFS, ext, etc.).

FAT 8.3 filenames are not supported.

Test Your File System

The test program will use various strings (including the Big List of Naughty Strings) to create files in the working directory. Run npm test to run tests against your file system.

API

sanitize(inputString, [options])

Sanitize inputString by removing or replacing invalid characters.

Options:

  • options.replacement: optional, string/function, default: "". If passed as a string, it's used as the replacement for invalid characters. If passed as a function, the function will be called with the invalid characters and it's return value will be used as the replacement. See String.prototype.replace for more info.

Keywords

FAQs

Package last updated on 08 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc