Skip to content

Commit

Permalink
gosec: handle warnings
Browse files Browse the repository at this point in the history
All medium and high gosec warnings are handled.

This was mostly 0600 file permissions,
filepath.Clean(), or annotating other conditions.
  • Loading branch information
billgraziano committed Aug 7, 2022
1 parent 1be883a commit a7410c6
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/readfast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func run(server, session string, maxRows int, parse, format bool) {
log.Info(fmt.Sprintf("session: %s (%s)", xeSession.Name, xeSession.WildCard))
query := fmt.Sprintf("SELECT object_name, event_data, file_name, file_offset FROM sys.fn_xe_file_target_read_file('%s', NULL, NULL, NULL);", xeSession.WildCard)
start := time.Now()
rows, err := info.DB.Query(query)
rows, err := info.DB.Query(query) // #nosec G201 -- string doeesn't come from user
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tcpserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {

var connCount int

l, err := net.Listen("tcp", listen)
l, err := net.Listen("tcp", listen) //#nosec G102 -- dev server only
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/xelogstash/main_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
_ "expvar"
"fmt"
"net/http"
_ "net/http/pprof"
_ "net/http/pprof" //#nosec G108 -- pprof only exposed on localhost if http_metrics is true
"runtime"
"time"

Expand Down
5 changes: 3 additions & 2 deletions cmd/xeparse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func main() {

for _, f := range files {
fi := filepath.Join(dirname, f.Name())
fi = filepath.Clean(fi)
if filepath.Ext(fi) != ".xml" {
continue
}
log.Info("file:", fi)
b, err := ioutil.ReadFile(fi)
b, err := ioutil.ReadFile(fi) //#nosec G304 -- file doesn't come from user input
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -87,7 +88,7 @@ func main() {
basefile := strings.TrimSuffix(fi, filepath.Ext(fi))
newfile := basefile + ".json"
//outfile := filepath.Join(dirname, newfile)
err = ioutil.WriteFile(newfile, out.Bytes(), 0666)
err = ioutil.WriteFile(newfile, out.Bytes(), 0600)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/process_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (p *Program) processSession(
if err != nil {
log.Error(errors.Wrap(err, "xe.parse"))
if source.LogBadXML {
err = ioutil.WriteFile("bad_xml.log", []byte(eventData), 0666)
err = ioutil.WriteFile("bad_xml.log", []byte(eventData), 0600)
if err != nil {
log.Error(errors.Wrap(err, "write bad xml: ioutil.writefile"))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"math/rand"
"net/http"
_ "net/http/pprof" // pprof
_ "net/http/pprof" //#nosec G108 -- pprof only exposed on localhost if http_metrics is true
"runtime"
"time"

Expand Down
2 changes: 1 addition & 1 deletion pkg/sink/sink_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (fs *FileSink) open(id string) error {
}

fqfile := filepath.Join(eventDir, fileName)
lf, err := os.OpenFile(fqfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
lf, err := os.OpenFile(filepath.Clean(fqfile), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return errors.Wrap(err, "os.openfile")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (f *File) GetOffset() (fileName string, offset int64, xestatus string, err
var fp *os.File
_, err = os.Stat(f.Name)
if os.IsNotExist(err) {
fp, err = os.OpenFile(f.Name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
fp, err = os.OpenFile(f.Name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return "", 0, StateReset, errors.Wrap(err, "create")
}
Expand All @@ -192,7 +192,7 @@ func (f *File) GetOffset() (fileName string, offset int64, xestatus string, err
return "", 0, StateReset, errors.Wrap(err, "stat")
}

readonly, err := os.OpenFile(f.Name, os.O_RDONLY, 0666)
readonly, err := os.OpenFile(f.Name, os.O_RDONLY, 0600)
if err != nil {
return "", 0, StateReset, errors.Wrap(err, "openreadonly")
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (f *File) GetOffset() (fileName string, offset int64, xestatus string, err
}

// TODO close & reopen the file
fp, err = os.OpenFile(f.Name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
fp, err = os.OpenFile(f.Name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return "", 0, StateReset, errors.Wrap(err, "openappend")
}
Expand Down Expand Up @@ -340,7 +340,7 @@ func (f *File) Done(xeFileName string, offset int64, xestatus string) error {
}

// Write the new file
newStatusFile, err := os.OpenFile(f.Name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
newStatusFile, err := os.OpenFile(f.Name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return errors.Wrap(err, "create")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func PrintSamples() error {
return errors.Wrap(err, "os.executable")
}
exeDir := filepath.Dir(executable)
fileName := filepath.Join(exeDir, "samples.xe.json")
fileName := filepath.Clean(filepath.Join(exeDir, "samples.xe.json"))

file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errors.Wrap(err, "os.openfile")
}
defer file.Close()
defer file.Close() //#nosec G307

for _, v := range results {
//json, err := json.Unmarshal(v.Sample)
Expand Down

0 comments on commit a7410c6

Please sign in to comment.