@@ -3,37 +3,83 @@ package utils
33import (
44 "fmt"
55 "os/exec"
6+ "path/filepath"
67 "strings"
78)
89
10+ // validatePath performs basic validation on file paths to prevent command injection
11+ func validatePath (path string ) error {
12+ // Use filepath.Clean to sanitize the path
13+ cleanPath := filepath .Clean (path )
14+
15+ // Ensure the path doesn't contain shell metacharacters
16+ if strings .ContainsAny (cleanPath , ";|&$`(){}[]<>" ) {
17+ return fmt .Errorf ("path contains invalid characters: %s" , path )
18+ }
19+
20+ // Ensure it's not an absolute path to prevent access to system directories
21+ if filepath .IsAbs (cleanPath ) && ! strings .HasPrefix (cleanPath , "/tmp/" ) {
22+ return fmt .Errorf ("path must be relative or in /tmp: %s" , path )
23+ }
24+
25+ return nil
26+ }
27+
928func GenerateCertificates (path string , caPath string ) error {
1029 var err error
1130 fmt .Println ("====Generating TLS Certificates" )
12- geeTlsKeyCmd := strings .Replace ("openssl genpkey -algorithm RSA -out path/tls.key" , "path" , path , - 1 )
13- genCsrCmd := strings .Replace ("openssl req -new -key path/tls.key -config path/server.cnf -out path/tls.csr" , "path" , path , - 1 )
14- genCrtCmd := strings .Replace (strings .Replace ("openssl x509 -req -CA caPath/cacert.pem -CAkey caPath/ca-private-key.pem -CAcreateserial -CAserial path/cacert.srl -in path/tls.csr -out path/tls.crt -days 365" , "path" , path , - 1 ), "caPath" , caPath , - 1 )
15- rvariable := []string {geeTlsKeyCmd , genCsrCmd , genCrtCmd }
16- for _ , j := range rvariable {
17- cmd := exec .Command ("bash" , "-c" , j )
31+
32+ // Validate paths to prevent command injection
33+ if err := validatePath (path ); err != nil {
34+ return fmt .Errorf ("invalid path: %w" , err )
35+ }
36+ if err := validatePath (caPath ); err != nil {
37+ return fmt .Errorf ("invalid caPath: %w" , err )
38+ }
39+
40+ // Use safer approach with individual commands instead of shell execution
41+ commands := [][]string {
42+ {"openssl" , "genpkey" , "-algorithm" , "RSA" , "-out" , filepath .Join (path , "tls.key" )},
43+ {"openssl" , "req" , "-new" , "-key" , filepath .Join (path , "tls.key" ), "-config" , filepath .Join (path , "server.cnf" ), "-out" , filepath .Join (path , "tls.csr" )},
44+ {"openssl" , "x509" , "-req" , "-CA" , filepath .Join (caPath , "cacert.pem" ), "-CAkey" , filepath .Join (caPath , "ca-private-key.pem" ), "-CAcreateserial" , "-CAserial" , filepath .Join (path , "cacert.srl" ), "-in" , filepath .Join (path , "tls.csr" ), "-out" , filepath .Join (path , "tls.crt" ), "-days" , "365" },
45+ }
46+
47+ for _ , cmdArgs := range commands {
48+ // #nosec G204 - Command arguments are constructed from validated paths
49+ cmd := exec .Command (cmdArgs [0 ], cmdArgs [1 :]... )
1850 err = cmd .Run ()
51+ if err != nil {
52+ return fmt .Errorf ("failed to execute command %v: %w" , cmdArgs , err )
53+ }
1954 }
2055 return err
2156}
2257
2358func GenerateCACertificate (caPath string ) error {
2459 var err error
2560 fmt .Println ("====Generating CA Certificates" )
26- genKeyCmd := strings .Replace ("openssl genrsa -out caPath/ca-private-key.pem 2048" , "caPath" , caPath , - 1 )
27- genCACertCmd := strings .Replace ("openssl req -new -x509 -days 3650 -key caPath/ca-private-key.pem -out caPath/cacert.pem -subj '/CN=TlsTest/C=US/ST=California/L=RedwoodCity/O=Progress/OU=MarkLogic'" , "caPath" , caPath , - 1 )
28- pwdCMD := "pwd"
29- rvariable := []string {pwdCMD , genKeyCmd , genCACertCmd }
30- for _ , j := range rvariable {
31- fmt .Println (j )
32- cmd := exec .Command ("bash" , "-c" , j )
61+
62+ // Validate path to prevent command injection
63+ if err := validatePath (caPath ); err != nil {
64+ return fmt .Errorf ("invalid caPath: %w" , err )
65+ }
66+
67+ // Use safer approach with individual commands instead of shell execution
68+ commands := [][]string {
69+ {"pwd" },
70+ {"openssl" , "genrsa" , "-out" , filepath .Join (caPath , "ca-private-key.pem" ), "2048" },
71+ {"openssl" , "req" , "-new" , "-x509" , "-days" , "3650" , "-key" , filepath .Join (caPath , "ca-private-key.pem" ), "-out" , filepath .Join (caPath , "cacert.pem" ), "-subj" , "/CN=TlsTest/C=US/ST=California/L=RedwoodCity/O=Progress/OU=MarkLogic" },
72+ }
73+
74+ for _ , cmdArgs := range commands {
75+ fmt .Println (strings .Join (cmdArgs , " " ))
76+ // #nosec G204 - Command arguments are constructed from validated paths
77+ cmd := exec .Command (cmdArgs [0 ], cmdArgs [1 :]... )
3378 err = cmd .Run ()
3479 if err != nil {
3580 fmt .Println (err )
81+ return fmt .Errorf ("failed to execute command %v: %w" , cmdArgs , err )
3682 }
3783 }
38- return err
84+ return nil
3985}
0 commit comments