Skip to content

Commit 2b9cd82

Browse files
enable gosec (#1299)
* enable gosec * gosec G404 - weak number generator * gosec G112 - slow loris * G306 - poor file permissions * gosec G101 - potential secret * gosec G115 potential overflow * gosec G107 - potentially tainted url * gosec G204
1 parent 4e45b41 commit 2b9cd82

File tree

14 files changed

+42
-21
lines changed

14 files changed

+42
-21
lines changed

.golangci.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ linters:
4545
# - gomoddirectives
4646
- gomodguard
4747
- goprintffuncname
48-
# - gosec
48+
- gosec
4949
# - gosmopolitan
5050
# - govet
5151
- grouper
@@ -116,6 +116,10 @@ linters:
116116
- third_party$
117117
- builtin$
118118
- examples$
119+
settings:
120+
gosec:
121+
config:
122+
G306: "0o644"
119123
formatters:
120124
enable:
121125
- gofmt

src/cmd/cli/command/commands.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io"
8-
"math/rand"
98
"os"
109
"os/exec"
1110
"path/filepath"
@@ -140,11 +139,11 @@ func Execute(ctx context.Context) error {
140139
fmt.Println("For help with warnings, check our FAQ at https://s.defang.io/warnings")
141140
}
142141

143-
if hasTty && !hideUpdate && rand.Intn(10) == 0 {
142+
if hasTty && !hideUpdate && pkg.RandomIndex(10) == 0 {
144143
if latest, err := GetLatestVersion(ctx); err == nil && isNewer(GetCurrentVersion(), latest) {
145144
term.Debug("Latest Version:", latest, "Current Version:", GetCurrentVersion())
146145
fmt.Println("A newer version of the CLI is available at https://github.com/DefangLabs/defang/releases/latest")
147-
if rand.Intn(10) == 0 && !pkg.GetenvBool("DEFANG_HIDE_HINTS") {
146+
if pkg.RandomIndex(10) == 0 && !pkg.GetenvBool("DEFANG_HIDE_HINTS") {
148147
fmt.Println("To silence these notices, do: export DEFANG_HIDE_UPDATE=1")
149148
}
150149
}

src/cmd/cli/command/hint.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package command
22

33
import (
44
"fmt"
5-
"math/rand"
65
"os"
76
"path/filepath"
87
"strings"
@@ -62,7 +61,7 @@ func printDefangHint(hint string, cmds ...string) {
6261
for _, arg := range cmds {
6362
fmt.Printf(" %s %s\n\n", executable, arg)
6463
}
65-
if rand.Intn(10) == 0 {
64+
if pkg.RandomIndex(10) == 0 {
6665
fmt.Println("To silence these hints, do: export DEFANG_HIDE_HINTS=1")
6766
}
6867
}

src/pkg/auth/auth.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ func ServeAuthCodeFlowServer(ctx context.Context, authPort int, tenant types.Ten
119119
})
120120

121121
// set the port and the handler to the server
122-
server := &http.Server{Addr: "0.0.0.0:" + strconv.Itoa(authPort), Handler: handler}
122+
server := &http.Server{
123+
Addr: "0.0.0.0:" + strconv.Itoa(authPort),
124+
Handler: handler,
125+
ReadHeaderTimeout: 5 * time.Second,
126+
}
123127

124128
// Start the server
125129
err = server.ListenAndServe()

src/pkg/cli/cert.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"math/rand"
109
"net"
1110
"net/http"
1211
"strings"
@@ -60,7 +59,7 @@ var (
6059
}
6160

6261
dialer := &net.Dialer{}
63-
rootAddr := net.JoinHostPort(ips[rand.Intn(len(ips))].String(), port)
62+
rootAddr := net.JoinHostPort(ips[pkg.RandomIndex(len(ips))].String(), port)
6463
return dialer.DialContext(ctx, network, rootAddr)
6564
},
6665
ForceAttemptHTTP2: true,

src/pkg/cli/client/byoc/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func GetPulumiBackend(stateUrl string) (string, string, error) {
4444
}
4545

4646
func runLocalCommand(ctx context.Context, dir string, env []string, cmd ...string) error {
47+
// TODO - use enums to define commands instead of passing strings down from the caller
48+
// #nosec G204
4749
command := exec.CommandContext(ctx, cmd[0], cmd[1:]...)
4850
command.Dir = dir
4951
command.Env = env

src/pkg/clouds/aws/ecs/cfn/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func createTemplate(stack string, containers []types.Container, overrides Templa
124124
},
125125
}
126126

127+
// #nosec G101 - not a secret
127128
const _privateRepoSecret = "PrivateRepoSecret"
128129
// 5. ECR pull-through cache rules
129130
// TODO: Creating pull through cache rules isn't supported in the following Regions:

src/pkg/clouds/aws/route53.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package aws
33
import (
44
"context"
55
"errors"
6-
"math/rand"
76
"time"
87

8+
"github.com/DefangLabs/defang/src/pkg"
99
"github.com/DefangLabs/defang/src/pkg/dns"
1010
"github.com/aws/aws-sdk-go-v2/service/route53"
1111
"github.com/aws/aws-sdk-go-v2/service/route53/types"
@@ -61,7 +61,7 @@ func GetDelegationSet(ctx context.Context, r53 Route53API) (*types.DelegationSet
6161
}
6262
// Return a random delegation set, to work around the 100 zones-per-delegation-set limit,
6363
// because we can't easily tell how many zones are using each delegation set.
64-
return &resp.DelegationSets[rand.Intn(len(resp.DelegationSets))], nil
64+
return &resp.DelegationSets[pkg.RandomIndex(len(resp.DelegationSets))], nil
6565
}
6666

6767
func GetHostedZoneByName(ctx context.Context, domain string, r53 Route53API) (*types.HostedZone, error) {

src/pkg/dns/resolver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"math/rand"
87
"net"
98
"slices"
109
"sort"
1110

11+
"github.com/DefangLabs/defang/src/pkg"
1212
"github.com/miekg/dns"
1313
)
1414

@@ -66,14 +66,15 @@ func (r RootResolver) getResolver(ctx context.Context, domain string) Resolver {
6666
if err != nil {
6767
return DirectResolver{}
6868
}
69-
return DirectResolver{NSServer: ns[rand.Intn(len(ns))].Host}
69+
return DirectResolver{NSServer: ns[pkg.RandomIndex(len(ns))].Host}
7070
}
7171

7272
func FindNSServers(ctx context.Context, domain string) ([]*net.NS, error) {
7373
nsServers := rootServers
7474
retries := 3
7575
for {
76-
nsServer := nsServers[rand.Intn(len(nsServers))].Host
76+
index := pkg.RandomIndex(len(nsServers))
77+
nsServer := nsServers[index].Host
7778
ns, err := ResolverAt(nsServer).LookupNS(ctx, domain)
7879
sort.Slice(ns, func(i, j int) bool { return ns[i].Host < ns[j].Host })
7980
if err != nil {

src/pkg/docker/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (d Docker) Run(ctx context.Context, env map[string]string, cmd ...string) (
1818
AutoRemove: true, // --rm; FIXME: this causes "No such container" if the container exits early
1919
PublishAllPorts: true, // -P
2020
Resources: container.Resources{
21-
Memory: int64(d.memory),
21+
Memory: int64(d.memory), // #nosec G115 - memory is expected to be a small number
2222
},
2323
}, nil, parsePlatform(d.platform), "")
2424
if err != nil {

0 commit comments

Comments
 (0)