Skip to content

Commit

Permalink
Improved "sanitization" to "safety" (#1574)
Browse files Browse the repository at this point in the history
Added a few additional points about how to handle user-specified names.
  • Loading branch information
adamshostack authored Jan 3, 2025
1 parent 171c639 commit 78eedcf
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cheatsheets/File_Upload_Cheat_Sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,17 @@ In conjunction with [content-type validation](#content-type-validation), validat

> This should not be used on its own, as bypassing it is pretty common and easy.
### Filename Sanitization
### Filename Safety

Filenames can endanger the system in multiple ways, either by using non acceptable characters, or by using special and restricted filenames. For Windows, refer to the following [MSDN guide](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#naming-conventions). For a wider overview on different filesystems and how they treat files, refer to [Wikipedia's Filename page](https://en.wikipedia.org/wiki/Filename).

In order to avoid the above mentioned threat, creating a **random string** as a file-name, such as generating a UUID/GUID, is essential. If the filename is required by the business needs, proper input validation should be done for client-side (_e.g._ active content that results in XSS and CSRF attacks) and back-end side (_e.g._ special files overwrite or creation) attack vectors. Filename length limits should be taken into consideration based on the system storing the files, as each system has its own filename length limit. If user filenames are required, consider implementing the following:

- Implement a maximum length
- Restrict characters to an allowed subset specifically, such as alphanumeric characters, hyphen, spaces, and periods
- Consider telling the user what an acceptable filename is.
- Restrict use of leading periods (hidden files) and sequential periods (directory traversal).
- Restrict the use of a leading hyphen or spaces to make it safer to use shell scripts to process files.
- If this is not possible, block-list dangerous characters that could endanger the framework and system that is storing and using the files.

### File Content Validation
Expand Down

0 comments on commit 78eedcf

Please sign in to comment.