crowdsec/pkg/cwhub/cwhub.go

55 lines
1.3 KiB
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"
"github.com/crowdsecurity/go-cs-lib/version"
)
2020-07-27 11:47:32 +00:00
// hubTransport wraps a Transport to set a custom User-Agent.
type hubTransport struct {
http.RoundTripper
}
func (t *hubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", "crowdsec/"+version.String())
return t.RoundTripper.RoundTrip(req)
}
// hubClient is the HTTP client used to communicate with the CrowdSec Hub.
var hubClient = &http.Client{
Timeout: 120 * time.Second,
Transport: &hubTransport{http.DefaultTransport},
}
// 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)
})
}