Skip to content

Commit b683f4d

Browse files
authored
Merge pull request #75 from atilsensalduz/sec/CWE-78
Fix security vulnerabilities flagged by gosec scanner
2 parents 06a0361 + e183326 commit b683f4d

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

internal/config/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package config
22

33
import (
44
"fmt"
5-
"github.com/spf13/viper"
65
"os"
76
"path/filepath"
87
"strings"
98
"time"
9+
10+
"github.com/spf13/viper"
1011
)
1112

1213
type Config struct {
@@ -46,7 +47,9 @@ func LoadConfig() (*Config, error) {
4647
setConfig()
4748

4849
viper.SetEnvKeyReplacer(strings.NewReplacer(`.`, `_`))
49-
viper.BindEnv("github.token", "GITHUB_TOKEN")
50+
if err := viper.BindEnv("github.token", "GITHUB_TOKEN"); err != nil {
51+
return nil, fmt.Errorf("failed to bind environment variable: %w", err)
52+
}
5053
viper.AutomaticEnv()
5154

5255
// Read the config file first

pkg/browser/browser.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package browser
22

33
import (
4+
"errors"
5+
"net/url"
46
"os/exec"
57
"runtime"
68
)
79

8-
func OpenInBrowser(url string) error {
10+
func OpenInBrowser(rawURL string) error {
11+
// Validate the URL to prevent command injection
12+
parsedURL, err := url.Parse(rawURL)
13+
if err != nil || parsedURL.Scheme == "" || parsedURL.Host == "" {
14+
return errors.New("invalid URL")
15+
}
16+
917
var cmd string
1018
var args []string
1119

1220
switch runtime.GOOS {
1321
case "windows":
1422
cmd = "cmd"
15-
args = []string{"/c", "start"}
23+
args = []string{"/c", "start", parsedURL.String()}
1624
case "darwin":
1725
cmd = "open"
26+
args = []string{parsedURL.String()}
1827
default: // "linux", "freebsd", "openbsd", "netbsd"
1928
cmd = "xdg-open"
29+
args = []string{parsedURL.String()}
2030
}
21-
args = append(args, url)
22-
31+
32+
// #nosec G204 - URL is validated above and is safe to use
2333
return exec.Command(cmd, args...).Start()
2434
}

0 commit comments

Comments
 (0)