== Recommended Secure Coding Practices

Avoid using paths provided by users or other untrusted sources if possible. If this is required, check that the path does not reference an unauthorized directory or file. See https://www.owasp.org/index.php/Testing_Directory_traversal/file_include_(OTG-AUTHZ-001)[OWASP recommendations] as to how to test for directory traversal. Note that the paths length should be validated too.


No File and directory names should be exposed. They can contain sensitive information. This means that a user should not be able to list the content of unauthorized directories.


Make sure that no attackers can test for the existence or absence of sensitive files. Knowing that a specific file exists can reveal a vulnerability or at least expose file and directory names.


Files and directories should be created with restricted permissions and ownership. Only authorized users and applications should be able to access the files, and they should have as little permissions as needed. Modifying a file's permissions is not good enough. The permissions should be restricted from the very beginning.


Writing user input into files should be done with caution. It could fill the storage space if the amount of data written is not controlled. It could also write dangerous data which will later be used by an application or returned to another user. This is why the user input should be validated before being written.


Reading a file can lead to other vulnerabilities. Any file could have been modified by an attacker. Thus the same validation as for any user input should be performed on file content.


Once a file is read, its content should only be exposed to authorized users.


Add limits to the number of files your application access simultaneously or create because of a user action. It is possible to perform a Denial of Service attack by opening too many files, and thus exhausting available file descriptors, or by filling the file system with new files. Release file descriptors by closing files as soon as possible.

We also recommended to have tools monitoring your system and alerting you whenever resources are nearly exhausted.


Do not allow untrusted code to access the filesystem. For some programming languages, child-processes may have access to file descriptors opened by the parent process before the creation of the child process. This creates a vulnerability when a child process doesn't have the permission to access a file but is still able to modify it via the inherited file descriptor. Check your language documentation for "file descriptor leak" or the use of the flags ``++O_CLOEXEC++``, ``++FD_CLOEXEC++``, or ``++bInheritHandles++``. File descriptors can be inherited in the following languages: C, {cpp}, C#, Objective-C, Swift, Go (but disabled by default), some JVM versions, Javascript and TypeScript in Nodejs, Some PHP versions,  Python, Ruby, Rust, VB6 and VB.NET.
