crowdsec/pkg/cwhub/cwhub.go

41 lines
871 B
Go
Raw Normal View History

2020-07-27 11:47:32 +00:00
package cwhub
import (
"fmt"
"net/http"
2022-05-24 13:46:48 +00:00
"path/filepath"
"sort"
2022-05-24 13:46:48 +00:00
"strings"
"time"
)
2020-07-27 11:47:32 +00:00
var hubClient = &http.Client{
Timeout: 120 * time.Second,
}
// safePath returns a joined path and ensures that it does not escape the base directory.
func safePath(dir, filePath string) (string, error) {
absBaseDir, err := filepath.Abs(filepath.Clean(dir))
if err != nil {
return "", err
}
absFilePath, err := filepath.Abs(filepath.Join(dir, filePath))
if err != nil {
return "", err
}
2023-06-29 09:34:59 +00:00
if !strings.HasPrefix(absFilePath, absBaseDir) {
return "", fmt.Errorf("path %s escapes base directory %s", filePath, dir)
2020-07-27 11:47:32 +00:00
}
return absFilePath, nil
2020-07-27 11:47:32 +00:00
}
// SortItemSlice sorts a slice of items by name, case insensitive.
func SortItemSlice(items []*Item) {
sort.Slice(items, func(i, j int) bool {
return strings.ToLower(items[i].Name) < strings.ToLower(items[j].Name)
})
}