Skip to content

Commit 209647f

Browse files
authored
Handle integer overflow cases in shellquote (#1822)
Resolves CodeQL warnings
1 parent ea583a7 commit 209647f

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

pkg/util/shellutil/shellquote.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
package shellutil
55

6-
import "regexp"
6+
import (
7+
"log"
8+
"regexp"
9+
)
10+
11+
const (
12+
MaxQuoteSize = 10000000 // 10MB
13+
)
714

815
var (
916
safePattern = regexp.MustCompile(`^[a-zA-Z0-9_/.-]+$`)
@@ -23,6 +30,10 @@ func HardQuote(s string) string {
2330
return s
2431
}
2532

33+
if !checkQuoteSize(s) {
34+
return ""
35+
}
36+
2637
buf := make([]byte, 0, len(s)+5)
2738
buf = append(buf, '"')
2839

@@ -51,6 +62,10 @@ func HardQuoteFish(s string) string {
5162
return s
5263
}
5364

65+
if !checkQuoteSize(s) {
66+
return ""
67+
}
68+
5469
buf := make([]byte, 0, len(s)+5)
5570
buf = append(buf, '"')
5671

@@ -72,6 +87,10 @@ func HardQuotePowerShell(s string) string {
7287
return "\"\""
7388
}
7489

90+
if !checkQuoteSize(s) {
91+
return ""
92+
}
93+
7594
buf := make([]byte, 0, len(s)+5)
7695
buf = append(buf, '"')
7796

@@ -113,6 +132,10 @@ func SoftQuote(s string) string {
113132
return s
114133
}
115134

135+
if !checkQuoteSize(s) {
136+
return ""
137+
}
138+
116139
buf := make([]byte, 0, len(s)+5)
117140
buf = append(buf, '"')
118141

@@ -128,3 +151,11 @@ func SoftQuote(s string) string {
128151
buf = append(buf, '"')
129152
return string(buf)
130153
}
154+
155+
func checkQuoteSize(s string) bool {
156+
if len(s) > MaxQuoteSize {
157+
log.Printf("string too long to quote: %s", s)
158+
return false
159+
}
160+
return true
161+
}

0 commit comments

Comments
 (0)