1.6.3 • Published 5 years ago

sanitize-filename v1.6.3

Weekly downloads
1,014,121
License
WTFPL OR ISC
Repository
github
Last release
5 years ago

sanitize-filename build status

Sanitize a string to be safe for use as a filename by removing directory paths and invalid characters.

Install

npm: sanitize-filename

npm install sanitize-filename

Example

var sanitize = require("sanitize-filename");

// 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 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:

var sanitize = require("sanitize-filename");
sanitize("..")
// -> ""

Non-unique Filenames

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

var sanitize = require("sanitize-filename");
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.
eog_seog_xx@openbaseio/package-build-statsbrs@furjs/cli@garjan/utils@fsp-fs/app-builder-lib@c8y/cli@mdn/yaritesting-cli-ajaywamptypescript-node-express-starter-simplifiedwegdigital-cli@theprojectsomething/pouchdb-server-monorepoanimekaizokupinterest-exportercsgodemo-cli@heh/nreadytest-testcafegenerator-code-vuegolem-sdk@vexcited/create-nodepress-appphantomshotoutput-serviceghost-azure-store@johnf/electron-builder@annoai/js-file-name-from-urlvisitjstdp-datamodelercypress-visual-regression-ressemble-jsmarvolo-botmarvolo-up@dedeardiansya/dyp@infinitebrahmanuniverse/nolb-sani@xqy/keystone@paiondata/nextwikijam3-react-sdkagogosml-test-generator-nodedocu-notion-dashwaveesm-payloadinfinni-ui-e2etestcafereplit-shellgenerator-mboultoureau-test-codeunleash-server@everything-registry/sub-chunk-2707logosj-webappmonitorg-app-builder-libmonitorg-dmg-buildermonitorg-electron-buildermonitorg-electron-builder-squirrel-windowsmoonshotmoorr-keystonenjtoolng-file-manager-expressngx-ssh-deploynanakonightwatch-generatornimbus-note-exporternode-api-cache-proxynoddity-service-servernode-pdnode-rdpnode-red-contrib-chatbotnode-red-contrib-chatbot-bildinode-red-contrib-chatbot-customnode-red-contrib-chatbot-discordnginitnocusaurusmocha-tape-deckmodified-payload-s3-uploadmarcos-venom-botmarkdown-it-obsidianmarkdown-it-obsidian-imagesmarkdown-it-wikitextmarkdown-it-wikilinksmarkscript-projectmnaosmobile-generatormocha-cassettesmm-musicmsdownmtcrawleemolidmongoboostermotokomp3-splitmovie-downloadermovie-samplermitekosurumqtt-content-servicemuppeteermuzovanomuseria-msmusic-automatonmuseriamy_ccdownmy-venom-bot-extendednativefier-fork-davejnativefier-next
1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.3

8 years ago

1.5.2

8 years ago

1.4.5

8 years ago

1.4.4

9 years ago

1.4.3

9 years ago

1.4.2

9 years ago

1.4.1

9 years ago

1.4.0

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.1

11 years ago

1.0.0

11 years ago