You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
57
+
// Vulnerability: Executing a command with user-supplied input can lead to command injection vulnerabilities.
58
+
// An attacker can inject malicious commands to gain unauthorized access or perform destructive actions.
59
+
// Best practice is to avoid using user input directly in commands and use safe alternatives like parameterized queries.
48
60
userInput:="ls -l; rm -rf /"
49
61
cmd:=exec.Command("sh", "-c", userInput)
50
62
cmd.Run()
51
63
52
64
// Gosec G104: Errors unhandled
65
+
// Vulnerability: Ignoring errors can lead to unexpected behavior and security vulnerabilities.
66
+
// Unhandled errors may result in resource leaks, inconsistent state, or exposure of sensitive information.
67
+
// Best practice is to properly handle and log errors to ensure the stability and security of the application.
53
68
f, _:=os.Open("file.txt")
54
69
deferf.Close()
55
70
56
71
// Gosec G201: SQL query construction using format string
57
72
// CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
73
+
// Vulnerability: Constructing SQL queries by directly concatenating user input can lead to SQL injection vulnerabilities.
74
+
// An attacker can manipulate the input to modify the SQL query and gain unauthorized access to the database.
75
+
// Best practice is to use parameterized queries or prepared statements to separate user input from the SQL query structure.
58
76
username:="admin"
59
77
pass:="' OR 1=1--"
60
78
query:=fmt.Sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'", username, pass)
@@ -63,24 +81,36 @@ func main() {
63
81
64
82
// Gosec G401: Use of weak cryptographic primitive
65
83
// CWE-327: Use of a Broken or Risky Cryptographic Algorithm
84
+
// Vulnerability: Using weak cryptographic primitives, such as DES, can compromise the security of encrypted data.
85
+
// These algorithms have known vulnerabilities and are susceptible to attacks.
86
+
// Best practice is to use strong, modern cryptographic algorithms like AES with appropriate key sizes.
66
87
key:= []byte("weak-key")
67
88
block, _:=des.NewCipher(key)
68
89
fmt.Printf("%x", block)
69
90
70
91
// Gosec G402: TLS MinVersion too low
71
92
// CWE-326: Inadequate Encryption Strength
93
+
// Vulnerability: Using a low TLS version, such as SSL 3.0, can expose the communication to known vulnerabilities.
94
+
// Older TLS versions have weaknesses that can be exploited by attackers to compromise the security of the connection.
95
+
// Best practice is to use a minimum TLS version of 1.2 or higher and disable support for older, insecure versions.
72
96
config:=&tls.Config{
73
97
MinVersion: tls.VersionSSL30,
74
98
}
75
99
_, _=tls.Dial("tcp", "example.com:443", config)
76
100
77
101
// Gosec G404: Use of weak random number generator
78
102
// CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)
103
+
// Vulnerability: Using a weak random number generator, such as the default math/rand package, can lead to predictable and insecure random values.
104
+
// Attackers may be able to guess or reproduce the generated random numbers, compromising the security of the system.
105
+
// Best practice is to use a cryptographically secure random number generator, such as crypto/rand, for security-sensitive operations.
79
106
token:=rand.Int()
80
107
fmt.Println("Random token:", token)
81
108
82
109
// Gosec G501: Blacklisted import crypto/rc4
83
110
// CWE-327: Use of a Broken or Risky Cryptographic Algorithm
111
+
// Vulnerability: The RC4 stream cipher is considered weak and should not be used for encryption.
112
+
// It has biases and vulnerabilities that can be exploited to recover the plaintext from the ciphertext.
113
+
// Best practice is to use secure encryption algorithms like AES-GCM or ChaCha20-Poly1305.
84
114
cipher, _:=rc4.NewCipher([]byte("secret"))
85
115
fmt.Printf("%x", cipher)
86
116
@@ -92,18 +122,27 @@ func main() {
92
122
93
123
// Gosec G107: Potential HTTP request made with variable url
94
124
// CWE-88: Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')
125
+
// Vulnerability: Making an HTTP request with a user-supplied URL can lead to server-side request forgery (SSRF) vulnerabilities.
126
+
// An attacker can manipulate the URL to make requests to internal or external systems, potentially accessing sensitive data or performing unauthorized actions.
127
+
// Best practice is to validate and sanitize the URL input, restrict the allowed domains or schemes, and use a whitelist approach if possible.
95
128
url:=resp.Request.URL.Query().Get("url")
96
129
http.Get(url)
97
130
98
131
// Gosec G109: Potential Integer overflow made by strconv.Atoi result conversion to int16/32
99
132
// CWE-190: Integer Overflow or Wraparound
133
+
// Vulnerability: Converting a string to an integer without proper bounds checking can lead to integer overflow vulnerabilities.
134
+
// If the input string represents a number that is too large for the target integer type, it can cause unexpected behavior or security issues.
135
+
// Best practice is to use appropriate integer types with sufficient range and perform proper error handling and input validation.
100
136
val:=resp.Request.URL.Query().Get("val")
101
137
num, _:=strconv.Atoi(val)
102
138
varintValint16=int16(num)
103
139
fmt.Println(intVal)
104
140
105
141
// Gosec G110: Potential DoS vulnerability via decompression bomb
106
142
// CWE-409: Improper Handling of Highly Compressed Data (Data Amplification)
143
+
// Vulnerability: Decompressing user-supplied compressed data without proper limits can lead to denial-of-service (DoS) attacks.
144
+
// An attacker can craft a small compressed payload that expands to a extremely large size upon decompression, consuming excessive memory and CPU resources.
145
+
// Best practice is to set appropriate size limits on the decompressed data and handle decompression errors gracefully.
0 commit comments