Fix int convertion cve (#25)

This commit is contained in:
Yang
2022-11-04 15:31:34 +08:00
committed by GitHub
parent a525fcb1b5
commit 31b78bf40c
4 changed files with 66 additions and 11 deletions

View File

@@ -95,7 +95,7 @@ func (f fallback) Parse(annotations Annotations, config *Ingress, globalContext
codesStr := splitBySeparator(codes, ",") codesStr := splitBySeparator(codes, ",")
var codesUint32 []uint32 var codesUint32 []uint32
for _, rawCode := range codesStr { for _, rawCode := range codesStr {
code, err := strconv.Atoi(rawCode) code, err := strconv.ParseUint(rawCode, 10, 32)
if err != nil { if err != nil {
IngressLog.Errorf("Custom HTTP code %s within ingress %s/%s is invalid", rawCode, config.Namespace, config.Name) IngressLog.Errorf("Custom HTTP code %s within ingress %s/%s is invalid", rawCode, config.Namespace, config.Name)
continue continue

View File

@@ -61,21 +61,21 @@ func (l localRateLimit) Parse(annotations Annotations, config *Ingress, _ *Globa
config.localRateLimit = local config.localRateLimit = local
}() }()
multiplier := defaultBurstMultiplier var multiplier uint32 = defaultBurstMultiplier
if m, err := annotations.ParseIntForMSE(limitBurstMultiplier); err == nil { if m, err := annotations.ParseUint32ForMSE(limitBurstMultiplier); err == nil {
multiplier = m multiplier = m
} }
if rpm, err := annotations.ParseIntForMSE(limitRPM); err == nil { if rpm, err := annotations.ParseUint32ForMSE(limitRPM); err == nil {
local = &localRateLimitConfig{ local = &localRateLimitConfig{
MaxTokens: uint32(rpm * multiplier), MaxTokens: rpm * multiplier,
TokensPerFill: uint32(rpm), TokensPerFill: rpm,
FillInterval: minute, FillInterval: minute,
} }
} else if rps, err := annotations.ParseIntForMSE(limitRPS); err == nil { } else if rps, err := annotations.ParseUint32ForMSE(limitRPS); err == nil {
local = &localRateLimitConfig{ local = &localRateLimitConfig{
MaxTokens: uint32(rps * multiplier), MaxTokens: rps * multiplier,
TokensPerFill: uint32(rps), TokensPerFill: rps,
FillInterval: second, FillInterval: second,
} }
} }

View File

@@ -166,6 +166,54 @@ func (a Annotations) ParseIntForMSE(key string) (int, error) {
return 0, ErrMissingAnnotations return 0, ErrMissingAnnotations
} }
func (a Annotations) ParseInt32(key string) (int32, error) {
if len(a) == 0 {
return 0, ErrMissingAnnotations
}
val, ok := a[buildNginxAnnotationKey(key)]
if ok {
i, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return 0, ErrInvalidAnnotationValue
}
return int32(i), nil
}
return 0, ErrMissingAnnotations
}
func (a Annotations) ParseInt32ForMSE(key string) (int32, error) {
if len(a) == 0 {
return 0, ErrMissingAnnotations
}
val, ok := a[buildMSEAnnotationKey(key)]
if ok {
i, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return 0, ErrInvalidAnnotationValue
}
return int32(i), nil
}
return 0, ErrMissingAnnotations
}
func (a Annotations) ParseUint32ForMSE(key string) (uint32, error) {
if len(a) == 0 {
return 0, ErrMissingAnnotations
}
val, ok := a[buildMSEAnnotationKey(key)]
if ok {
i, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return 0, ErrInvalidAnnotationValue
}
return uint32(i), nil
}
return 0, ErrMissingAnnotations
}
func (a Annotations) ParseIntASAP(key string) (int, error) { func (a Annotations) ParseIntASAP(key string) (int, error) {
if result, err := a.ParseInt(key); err == nil { if result, err := a.ParseInt(key); err == nil {
return result, nil return result, nil
@@ -173,6 +221,13 @@ func (a Annotations) ParseIntASAP(key string) (int, error) {
return a.ParseIntForMSE(key) return a.ParseIntForMSE(key)
} }
func (a Annotations) ParseInt32ASAP(key string) (int32, error) {
if result, err := a.ParseInt32(key); err == nil {
return result, nil
}
return a.ParseInt32ForMSE(key)
}
func (a Annotations) Has(key string) bool { func (a Annotations) Has(key string) bool {
if len(a) == 0 { if len(a) == 0 {
return false return false

View File

@@ -60,8 +60,8 @@ func (r retry) Parse(annotations Annotations, config *Ingress, _ *GlobalContext)
config.Retry = retryConfig config.Retry = retryConfig
}() }()
if count, err := annotations.ParseIntASAP(retryCount); err == nil { if count, err := annotations.ParseInt32ASAP(retryCount); err == nil {
retryConfig.retryCount = int32(count) retryConfig.retryCount = count
} }
if timeout, err := annotations.ParseIntASAP(perRetryTimeout); err == nil { if timeout, err := annotations.ParseIntASAP(perRetryTimeout); err == nil {