-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketdir.go
More file actions
43 lines (39 loc) · 939 Bytes
/
socketdir.go
File metadata and controls
43 lines (39 loc) · 939 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
37
38
39
40
41
42
43
package ts
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// ensureSecureSocketDir rejects symlinked socket directories and non-directory
// path components before a Unix socket is created underneath them.
func ensureSecureSocketDir(dir string) error {
clean := filepath.Clean(dir)
switch clean {
case ".", string(filepath.Separator):
return nil
}
parent := filepath.Dir(clean)
if parent != clean {
if err := ensureSecureSocketDir(parent); err != nil {
return err
}
}
info, err := os.Lstat(clean)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return fmt.Errorf("lstat %s: %w", clean, err)
}
if info.Mode()&os.ModeSymlink != 0 {
if filepath.Dir(clean) == string(filepath.Separator) {
return nil
}
return fmt.Errorf("socket directory %s is a symlink", clean)
}
if !info.IsDir() {
return fmt.Errorf("socket directory %s exists and is not a directory", clean)
}
return nil
}