Skip to content

Commit

Permalink
Fix default configurations (#597)
Browse files Browse the repository at this point in the history
* fix default perms on SQLite file

* seed the prng securely

* fix defaults to enforce certificates verification

* ensure file is within path

* ensure the directory doesn't exist beforehand

* verify certificate by default

* disable http ip forward headers
  • Loading branch information
buixor committed Feb 2, 2021
1 parent bb25a26 commit e74f221
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 23 deletions.
13 changes: 9 additions & 4 deletions cmd/crowdsec-cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
Expand Down Expand Up @@ -35,11 +38,13 @@ func backupConfigToDirectory(dirPath string) error {
return fmt.Errorf("directory path can't be empty")
}
log.Infof("Starting configuration backup")
_, err = os.Stat(dirPath)
if err == nil {
return fmt.Errorf("%s already exists", dirPath)
/*if parent directory doesn't exist, bail out. create final dir with Mkdir*/
parentDir := filepath.Dir(dirPath)
if _, err := os.Stat(parentDir); err != nil {
return errors.Wrapf(err, "while checking parent directory %s existence", parentDir)
}
if err = os.MkdirAll(dirPath, os.ModePerm); err != nil {

if err = os.Mkdir(dirPath, 0600); err != nil {
return fmt.Errorf("error while creating %s : %s", dirPath, err)
}

Expand Down
21 changes: 10 additions & 11 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
saferand "crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"math/big"
"os"
"strings"
"time"
Expand Down Expand Up @@ -42,20 +43,18 @@ const (
)

func generatePassword(length int) string {
rand.Seed(time.Now().UnixNano())

charset := upper + lower + digits
charsetLength := len(charset)

buf := make([]byte, length)
buf[0] = digits[rand.Intn(len(digits))]
buf[1] = upper[rand.Intn(len(upper))]
buf[2] = lower[rand.Intn(len(lower))]

for i := 3; i < length; i++ {
buf[i] = charset[rand.Intn(len(charset))]
for i := 0; i < length; i++ {
rInt, err := saferand.Int(saferand.Reader, big.NewInt(int64(charsetLength)))
if err != nil {
log.Fatalf("failed getting data from prng for password generation : %s", err)
}
buf[i] = charset[rInt.Int64()]
}
rand.Shuffle(len(buf), func(i, j int) {
buf[i], buf[j] = buf[j], buf[i]
})

return string(buf)
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ db_config:
max_age: 7d
api:
client:
insecure_skip_verify: true
insecure_skip_verify: false
credentials_path: /etc/crowdsec/local_api_credentials.yaml
server:
log_level: info
Expand Down
2 changes: 1 addition & 1 deletion config/user.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ db_config:
port: 3306
api:
client:
insecure_skip_verify: true # default true
insecure_skip_verify: false # default true
credentials_path: /etc/crowdsec/local_api_credentials.yaml
server:
#log_level: info
Expand Down
2 changes: 1 addition & 1 deletion docker/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ db_config:
max_age: 7d
api:
client:
insecure_skip_verify: true
insecure_skip_verify: false
credentials_path: /etc/crowdsec/local_api_credentials.yaml
server:
log_level: info
Expand Down
2 changes: 1 addition & 1 deletion docs/v1.X/docs/references/crowdsec-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ db_config:
max_age: 7d
api:
client:
insecure_skip_verify: true
insecure_skip_verify: false
credentials_path: /etc/crowdsec/local_api_credentials.yaml
server:
log_level: info
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
InsecureSkipVerify = true
InsecureSkipVerify = false
)

type ApiClient struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func NewServer(config *csconfig.LocalApiServerCfg) (*APIServer, error) {
}
log.Debugf("starting router, logging to %s", logFile)
router := gin.New()
/*related to https://github.com/gin-gonic/gin/pull/2474
Gin team doesn't seem to be willing to have a opt-in/opt-out on the trusted proxies.
For now, let's not trust that. */
router.ForwardedByClientIP = false

/*The logger that will be used by handlers*/
clog := log.New()
Expand Down
2 changes: 1 addition & 1 deletion pkg/csconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *GlobalConfig) LoadConfiguration() error {
}
}
if c.API.Client.InsecureSkipVerify == nil {
apiclient.InsecureSkipVerify = true
apiclient.InsecureSkipVerify = false
} else {
apiclient.InsecureSkipVerify = *c.API.Client.InsecureSkipVerify
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/cwhub/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cwhub
import (
"bytes"
"crypto/sha256"
"path/filepath"

//"errors"
"github.com/pkg/errors"
Expand Down Expand Up @@ -127,7 +128,6 @@ func DownloadItem(cscli *csconfig.CscliCfg, target Item, overwrite bool) (Item,
return target, nil
}
}

req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, HubBranch, target.RemotePath), nil)
if err != nil {
return target, errors.Wrap(err, fmt.Sprintf("while downloading %s", req.URL.String()))
Expand Down Expand Up @@ -159,6 +159,14 @@ func DownloadItem(cscli *csconfig.CscliCfg, target Item, overwrite bool) (Item,
tmpdirs := strings.Split(tdir+"/"+target.RemotePath, "/")
parent_dir := strings.Join(tmpdirs[:len(tmpdirs)-1], "/")

/*ensure that target file is within target dir*/
finalPath, err := filepath.Abs(tdir + "/" + target.RemotePath)
if err != nil {
return target, errors.Wrapf(err, "Abs error on %s", tdir+"/"+target.RemotePath)
}
if !strings.HasPrefix(finalPath, tdir) {
return target, fmt.Errorf("path %s escapes %s, abort", target.RemotePath, tdir)
}
/*check dir*/
if _, err = os.Stat(parent_dir); os.IsNotExist(err) {
log.Debugf("%s doesn't exist, create", parent_dir)
Expand All @@ -167,7 +175,7 @@ func DownloadItem(cscli *csconfig.CscliCfg, target Item, overwrite bool) (Item,
}
}
/*check actual file*/
if _, err = os.Stat(tdir + "/" + target.RemotePath); !os.IsNotExist(err) {
if _, err = os.Stat(finalPath); !os.IsNotExist(err) {
log.Warningf("%s : overwrite", target.Name)
log.Debugf("target: %s/%s", tdir, target.RemotePath)
} else {
Expand Down
16 changes: 16 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"os"
"time"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
Expand Down Expand Up @@ -30,6 +31,21 @@ func NewClient(config *csconfig.DatabaseCfg) (*Client, error) {
}
switch config.Type {
case "sqlite":

/*if it's the first startup, we want to touch and chmod file*/
if _, err := os.Stat(config.DbPath); os.IsNotExist(err) {
f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath)
}
if err := f.Close(); err != nil {
return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath)
}
} else { /*ensure file perms*/
if err := os.Chmod(config.DbPath, 0600); err != nil {
return &Client{}, fmt.Errorf("unable to set perms on %s: %v", config.DbPath, err)
}
}
client, err = ent.Open("sqlite3", fmt.Sprintf("file:%s?_busy_timeout=100000&_fk=1", config.DbPath))
if err != nil {
return &Client{}, fmt.Errorf("failed opening connection to sqlite: %v", err)
Expand Down

0 comments on commit e74f221

Please sign in to comment.