-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
36 lines (31 loc) · 803 Bytes
/
Copy pathexample_test.go
File metadata and controls
36 lines (31 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package pathsafe_test
import (
"fmt"
"github.com/azghr/forge/pathsafe"
)
func ExampleSafeJoin() {
p, err := pathsafe.SafeJoin("/home/user", "docs/report.pdf")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(p)
// Output: /home/user/docs/report.pdf
}
func ExampleSafeJoin_traversal() {
_, err := pathsafe.SafeJoin("/home/user", "../etc/passwd")
fmt.Println(err)
// Output: pathsafe: result path is outside base directory
}
func ExampleSafeJoin_symlinkOption() {
// AllowSymlinkFollow resolves symlinks before the containment check.
// This prevents symlink-based traversal. When the path does not exist,
// an error is returned.
_, err := pathsafe.SafeJoin(
"/home/user",
"docs/report.pdf",
pathsafe.AllowSymlinkFollow(),
)
fmt.Println(err != nil)
// Output: true
}