|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package numericbounds |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "go/ast" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + "golang.org/x/tools/go/analysis" |
| 25 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 26 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 28 | + markershelper "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 29 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 30 | + "sigs.k8s.io/kube-api-linter/pkg/markers" |
| 31 | +) |
| 32 | + |
| 33 | +const name = "numericbounds" |
| 34 | + |
| 35 | +// JavaScript safe integer bounds (2^53 - 1 and -(2^53 - 1)) |
| 36 | +const ( |
| 37 | + maxSafeInt = 9007199254740991 // 2^53 - 1 |
| 38 | + minSafeInt = -9007199254740991 // -(2^53 - 1) |
| 39 | +) |
| 40 | + |
| 41 | +var errMarkerMissingValue = errors.New("marker value not found") |
| 42 | + |
| 43 | +// Analyzer is the analyzer for the numericbounds package. |
| 44 | +// It checks that numeric fields have appropriate bounds validation markers. |
| 45 | +var Analyzer = &analysis.Analyzer{ |
| 46 | + Name: name, |
| 47 | + Doc: "Checks that numeric fields (int32, int64) have appropriate minimum and maximum bounds validation markers", |
| 48 | + Run: run, |
| 49 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 50 | +} |
| 51 | + |
| 52 | +func run(pass *analysis.Pass) (any, error) { |
| 53 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 54 | + if !ok { |
| 55 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 56 | + } |
| 57 | + |
| 58 | + inspect.InspectFields(func(field *ast.Field, _ extractjsontags.FieldTagInfo, markersAccess markershelper.Markers) { |
| 59 | + checkField(pass, field, markersAccess) |
| 60 | + }) |
| 61 | + |
| 62 | + return nil, nil //nolint:nilnil |
| 63 | +} |
| 64 | + |
| 65 | +func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelper.Markers) { |
| 66 | + if field == nil || len(field.Names) == 0 { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Unwrap pointers and slices to get the underlying type |
| 71 | + fieldType, isSlice := unwrapType(field.Type) |
| 72 | + |
| 73 | + // Get the underlying numeric type identifier (int32 or int64) |
| 74 | + ident := getNumericTypeIdent(pass, fieldType) |
| 75 | + if ident == nil { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // Only check int32 and int64 types |
| 80 | + if ident.Name != "int32" && ident.Name != "int64" { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + fieldName := utils.FieldName(field) |
| 85 | + fieldMarkers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) |
| 86 | + |
| 87 | + // Determine which markers to look for based on whether the field is a slice |
| 88 | + minMarker, maxMarker := getMarkerNames(isSlice) |
| 89 | + |
| 90 | + // Get minimum and maximum marker values |
| 91 | + minimum, minErr := getMarkerNumericValue(fieldMarkers, minMarker) |
| 92 | + maximum, maxErr := getMarkerNumericValue(fieldMarkers, maxMarker) |
| 93 | + |
| 94 | + // Check if markers are missing |
| 95 | + minMissing := errors.Is(minErr, errMarkerMissingValue) |
| 96 | + maxMissing := errors.Is(maxErr, errMarkerMissingValue) |
| 97 | + |
| 98 | + // Report any invalid marker values (e.g., non-numeric values) |
| 99 | + if minErr != nil && !minMissing { |
| 100 | + pass.Reportf(field.Pos(), "field %s has an invalid minimum marker: %v", fieldName, minErr) |
| 101 | + return |
| 102 | + } |
| 103 | + if maxErr != nil && !maxMissing { |
| 104 | + pass.Reportf(field.Pos(), "field %s has an invalid maximum marker: %v", fieldName, maxErr) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + // Report if both markers are missing |
| 109 | + if minMissing && maxMissing { |
| 110 | + pass.Reportf(field.Pos(), "field %s of type %s should have minimum and maximum bounds validation markers", fieldName, ident.Name) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + // Report if only one marker is present |
| 115 | + if minMissing { |
| 116 | + pass.Reportf(field.Pos(), "field %s of type %s has maximum but is missing minimum bounds validation marker", fieldName, ident.Name) |
| 117 | + return |
| 118 | + } |
| 119 | + if maxMissing { |
| 120 | + pass.Reportf(field.Pos(), "field %s of type %s has minimum but is missing maximum bounds validation marker", fieldName, ident.Name) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + // For int64 fields, check if bounds are within JavaScript safe integer range |
| 125 | + checkJavaScriptSafeBounds(pass, field, fieldName, ident.Name, minimum, maximum) |
| 126 | +} |
| 127 | + |
| 128 | +// unwrapType unwraps pointers and slices to get the underlying type. |
| 129 | +// Returns the unwrapped type and a boolean indicating if it's a slice. |
| 130 | +func unwrapType(expr ast.Expr) (ast.Expr, bool) { |
| 131 | + isSlice := false |
| 132 | + |
| 133 | + // Unwrap pointer if present (e.g., *int32) |
| 134 | + if starExpr, ok := expr.(*ast.StarExpr); ok { |
| 135 | + expr = starExpr.X |
| 136 | + } |
| 137 | + |
| 138 | + // Check if it's a slice and unwrap (e.g., []int32) |
| 139 | + if arrayType, ok := expr.(*ast.ArrayType); ok { |
| 140 | + isSlice = true |
| 141 | + expr = arrayType.Elt |
| 142 | + |
| 143 | + // Handle pointer inside slice (e.g., []*int32) |
| 144 | + if starExpr, ok := expr.(*ast.StarExpr); ok { |
| 145 | + expr = starExpr.X |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return expr, isSlice |
| 150 | +} |
| 151 | + |
| 152 | +// getMarkerNames returns the appropriate minimum and maximum marker names |
| 153 | +// based on whether the field is a slice. |
| 154 | +func getMarkerNames(isSlice bool) (minMarker, maxMarker string) { |
| 155 | + if isSlice { |
| 156 | + return markers.KubebuilderItemsMinimumMarker, markers.KubebuilderItemsMaximumMarker |
| 157 | + } |
| 158 | + return markers.KubebuilderMinimumMarker, markers.KubebuilderMaximumMarker |
| 159 | +} |
| 160 | + |
| 161 | +// checkJavaScriptSafeBounds checks if int64 bounds are within JavaScript safe integer range. |
| 162 | +func checkJavaScriptSafeBounds(pass *analysis.Pass, field *ast.Field, fieldName, typeName string, minimum, maximum float64) { |
| 163 | + if typeName != "int64" { |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + if minimum < minSafeInt || maximum > maxSafeInt { |
| 168 | + pass.Reportf(field.Pos(), |
| 169 | + "field %s of type int64 has bounds [%d, %d] that exceed safe integer range [%d, %d]. Consider using a string type to avoid precision loss in JavaScript clients", |
| 170 | + fieldName, int64(minimum), int64(maximum), minSafeInt, maxSafeInt) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// getMarkerNumericValue extracts the numeric value from the first instance of the marker with the given name. |
| 175 | +func getMarkerNumericValue(markerSet markershelper.MarkerSet, markerName string) (float64, error) { |
| 176 | + markerList := markerSet.Get(markerName) |
| 177 | + if len(markerList) == 0 { |
| 178 | + return 0, errMarkerMissingValue |
| 179 | + } |
| 180 | + |
| 181 | + marker := markerList[0] |
| 182 | + rawValue, ok := marker.Expressions[""] |
| 183 | + if !ok { |
| 184 | + return 0, errMarkerMissingValue |
| 185 | + } |
| 186 | + |
| 187 | + // Parse as float64 using strconv for better error handling |
| 188 | + value, err := strconv.ParseFloat(rawValue, 64) |
| 189 | + if err != nil { |
| 190 | + return 0, fmt.Errorf("error converting value to number: %w", err) |
| 191 | + } |
| 192 | + |
| 193 | + return value, nil |
| 194 | +} |
| 195 | + |
| 196 | +// getNumericTypeIdent returns the identifier for int32 or int64 types. |
| 197 | +// It handles type aliases by looking up the underlying type. |
| 198 | +// Note: This function expects pointers and slices to already be unwrapped. |
| 199 | +func getNumericTypeIdent(pass *analysis.Pass, expr ast.Expr) *ast.Ident { |
| 200 | + ident, ok := expr.(*ast.Ident) |
| 201 | + if !ok { |
| 202 | + return nil |
| 203 | + } |
| 204 | + |
| 205 | + // Check if it's a basic int32 or int64 type |
| 206 | + if ident.Name == "int32" || ident.Name == "int64" { |
| 207 | + return ident |
| 208 | + } |
| 209 | + |
| 210 | + // Check if it's a type alias to int32 or int64 |
| 211 | + if !utils.IsBasicType(pass, ident) { |
| 212 | + typeSpec, ok := utils.LookupTypeSpec(pass, ident) |
| 213 | + if ok { |
| 214 | + return getNumericTypeIdent(pass, typeSpec.Type) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments