https://github.com/depp/safepath
Go library which checks that file paths are safe to use
https://github.com/depp/safepath
Last synced: 12 months ago
JSON representation
Go library which checks that file paths are safe to use
- Host: GitHub
- URL: https://github.com/depp/safepath
- Owner: depp
- License: mit
- Created: 2021-04-15T19:59:59.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-16T02:56:22.000Z (about 5 years ago)
- Last Synced: 2025-03-22T12:18:44.955Z (about 1 year ago)
- Language: Go
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SafePath: Path Sanitizing
SafePath checks that pathnames are safe for use.
## Installation
SafePath can be installed with the `go get` command.
```shell
go get github.com/depp/safepath
```
## Usage
```go
import "github.com/depp/safepath"
```
To check if a filename, choose a combination of `Rules` flags and call the `CheckPathSegment()` function. For example, the `URLUnescaped` rule rejects any path which would require percent-encoding when used in a URL.
```go
rules := safepath.URLUnescaped
filename := "my_file.txt"
if err := rules.CheckPathSegment(filename); err != nil {
return err
}
```
Relative paths can be checked with the `CheckPath()` function. Note that this function only recognizes the path separator /, it does not recognize \\.
```go
rules := safepath.URLUnescaped
filepath := "directory/my_file.txt"
if err := rules.CheckPathSegment(filepath); err != nil {
return err
}
```
Error messages from this library are descriptive. Some examples:
```
Error: invalid path "/": path is absolute
Error: invalid path segment "NUL.TXT": uses reserved Windows filename "nul"
```
## Unicode
The notion of “safe” in Unicode is a bit difficult to define. This library does not have a comprehensive notion of safety for non-ASCII characters. Currently, there are only two rules that affect non-ASCII characters: `ValidUTF8` and `ASCIIOnly`.
## License
SafePath is provided under the MIT license. See LICENSE.txt for details.