Giter Club home page Giter Club logo

node-sanitize-filename's Introduction

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 (0x00โ€“0x1f and 0x80โ€“0x9f)
  • 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.

node-sanitize-filename's People

Contributors

actionshrimp avatar carltonf avatar dependabot[bot] avatar greenkeeperio-bot avatar joelmukuthu avatar mikeralphson avatar parshap avatar patontheback avatar paulirish avatar tjenkinson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-sanitize-filename's Issues

This library is insecure in practice

After seeing the name "sanitize-filename", I thought that the library was going to output a name that is considered to be safe (secure) for use a filename. After reviewing the code, I concluded otherwise.

For example, .. is returned as .., which is not suitable for most I/O-related operations (imagine: creating a new "temporary" directory/file, then removing the temporary directory with rimraf. Oops, now you've lost much more than the temporary directory/file). Besides this obvious case, there are several other constraints outlined at https://msdn.microsoft.com/en-us/library/aa365247.aspx.

This could be fixed in several ways, including but not limited to:

  • Return an empty string if the filename is insecure (then callers can use sanitize(filename) || 'defaultname' if they want to).
  • Add a prefix / suffix if the filename is insecure.

ES6 import/export syntax to use module

As the title suggests: It would be great if this was usable with the import/export syntax.

At least my IDE is complaining:

import * as sanitize from 'sanitize-filename';

This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

Should \u202f get sanitized?

When streaming a filename with a \u202f character using express.js, it throws an error when setting the Content-Disposition header:

// throws ERR_INVALID_CHAR
res.setHeader(
    'Content-Disposition',
    'attachment; filename="example\u202f.txt"',
);

After sanitizing a string with the \u202f character, it is still present in the string. Should it get sanitized by sanitize-filename or is that expected?

[unsure] Adjust how sanitize is made available for Typescript?

AFAICT, the way which sanitize is exported isn't ideal:

https://github.com/parshap/node-sanitize-filename/blob/master/index.d.ts#L8

export = sanitize;

When I try to reference it using a standard Typescript import statement,

image

I get these two errors:

'sanitize' can only be imported by using 'import sanitize = require(\"sanitize-filename\")' or by turning on the 'esModuleInterop' flag and using a default import.
`This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

As far as I can tell, this might be resolved changing export=sanitize to export sanitize.

ref: https://stackoverflow.com/questions/68785926/this-module-can-only-be-referenced-with-ecmascript-imports-exports-by-turning-on

Browser Usage?

Is this package safe to use in a browser environment? By the name I was a bit worried (node-sanitize-filename) but it doesn't seem any of the deps are node dependent and, after double checking my webpack bundles, it doesn't seem like library size is an issue (minimized it added only about 2kb to my output). I also read through the code and don't see it using any node built-ins like path or fs.

Just wanted to check before using in a SPA so I don't run into any surprises. Also, if it's not node dependent, and you don't foresee it being so in the future, maybe it's worth renaming the package to just "sanitize-filename" so other folks don't get tripped up by that? I could see this package being useful in a variety of applications both front-end and back-end. There's a lot of questions on stack overflow pertaining to this (which is fact how I came here) and it would be nice to depend on a small package like this that's actively maintained instead of just copying and pasting regexes.

Not deleting dots

var sanitizeFilename = require("sanitize-filename")
sanitizeFilename('../test')

returns..
"..test"

In the README it says
sanitize-filename removes the following: Unix reserved filenames (. and ..)

Is the correct behavior?

Electron Buffer issue

Hello, I am using this library to sanitize my file names. While developing the application in dev environment I didn't have any problems. After I build the .exe and installed on the one of the prod pc and tried to use it the application crashed and in my dev tools tab got this error - "Uncaught ReferenceError: Buffer is not defined". The problem causes only in electron application,

Stack- React/Typescript/Mobx/Webpack/Electron

semicolons

probably should sanitize them out too

Allow targetting specific file systems

Currently given strings are sanitized against all known file systems' requirements. It would be nice to allow the user to specify the file system they need to protect against.

See discussion in #9.

Different operating systems and different filesystems have different rules about what is a valid filename. We could let the user pass in a "target" system (e.g., "windows", or "fat32") and only sanitize against that system's rule.

That said, I think the default behavior should still be to sanitize against all systems. It seems like a safe default for users that don't bother to look at the options. It also handles the common use case of naming files that will be shared across systems (e.g., files served via http).

If last path name is number, sanitize make error.

If last path name is number, sanitize make error. if your filename compose with only number like '//user/fish/888', sanitize function prameter 'input' is number. so input.replace happen exception

Analysis: 100% of dependency updates in this repository can be merged.

Hey there ๐Ÿ‘‹

Our bot, Adaptly, found that 10 out of 10 currently open dependency update PRs can be merged.
That's 100% right there:

View Safe to Merge PRs1. Bump express from 4.17.1 to 4.17.3
2. Bump qs from 6.5.2 to 6.5.3
3. Bump decode-uri-component from 0.2.0 to 0.2.2
4. Bump follow-redirects from 1.7.0 to 1.14.8
5. Bump ajv from 6.10.2 to 6.12.6
6. Bump cached-path-relative from 1.0.2 to 1.1.0
7. Bump path-parse from 1.0.6 to 1.0.7
8. Bump lodash from 4.17.15 to 4.17.21
9. Bump elliptic from 6.3.2 to 6.5.4
10. Bump ini from 1.3.5 to 1.3.7

feels

๐Ÿ”Ž ย  How does Adaptly know this?

It analyses changelogs of dependencies updated in a PR.
If no breaking changes are found in the changelogs, PR is marked as good to merge.

โœจ Try Adaptly yourself

Feel free to try Adaptly on your repositories and finally
merge dependency update PRs. Let us know if you have any questions.

Best of luck with your projects,
Lauris
[email protected]

Replace characters with similar

Instead of removing all illegal characters. Some can be replaced by alternatives, for example:

  • * --> โˆ—
  • ' --> ๏ผ‡
  • " --> ๏ผ‚

Is this a good idea?

please put comma character also for sanitization.

put comma in illegalRe to sanitize file name. It will be help full while setting the contentDisposition header with filename.

setHeader('Content-disposition', 'attachment;filename=' + sanitizedFileName);

Configure additional invalid characters

Hi, I'm using the module for sanitizing file names for downloaded video files, and process those files later on using FFmpeg. The problem is, your module deems a ' as safe, but it will cause an error if used with FFmpeg.
There might be workarounds for that, or I could manually remove '-characters, but I'd prefer if you could offer us the option to specify our own reserved characters, either in addition to the existing ones or as a replacement to them.

For example, have and option to call sanitize.(<input>, {invalid: ["'"], replacement: "_"} and have it replace everything it already replaces, but also '.

This shouldn't be too much work but I think it would be very useful!

Strict mode to throw on error?

Hello,

This module is super useful :-)
I was wondering if you considered throwing an error if the path appeared to be invalid? Or to expose a function to validate if the path is safe?

Thank you for your answer :-)

Empty filename default

  • If a file contains only illegal characters, it should be defaulted to something, perhaps "file"
  • If a file contains an illegal name before the last period (i.e. extension), it should be defaulted to something, perhaps "file.extension"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.