|
| 1 | +// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package validate |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/go-openapi/loads" |
| 13 | + "github.com/go-openapi/spec" |
| 14 | + "github.com/go-openapi/strfmt" |
| 15 | + "github.com/go-openapi/testify/v2/assert" |
| 16 | + "github.com/go-openapi/testify/v2/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCompositionBudget(t *testing.T) { |
| 20 | + // each of these schemas loops through allOf, anyOf, oneOf or not and their $ref: |
| 21 | + // without a budget, building the validator overflows the goroutine stack |
| 22 | + // (fatal, not recoverable) or never ends |
| 23 | + for _, tc := range []struct { |
| 24 | + name string |
| 25 | + schema string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "allOf cycle", |
| 29 | + schema: `{"definitions":{"CycleAllOf":{"allOf":[{"$ref":"#/definitions/CycleAllOf"}]}},"properties":{"x":{"$ref":"#/definitions/CycleAllOf"}}}`, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "anyOf cycle", |
| 33 | + schema: `{"definitions":{"CycleAnyOf":{"anyOf":[{"type":"string"},{"$ref":"#/definitions/CycleAnyOf"}]}},"properties":{"x":{"$ref":"#/definitions/CycleAnyOf"}}}`, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "oneOf cycle", |
| 37 | + schema: `{"definitions":{"CycleOneOf":{"oneOf":[{"$ref":"#/definitions/CycleOneOf"}]}},"properties":{"x":{"$ref":"#/definitions/CycleOneOf"}}}`, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "not cycle", |
| 41 | + schema: `{"definitions":{"CycleNot":{"not":{"$ref":"#/definitions/CycleNot"}}},"properties":{"x":{"$ref":"#/definitions/CycleNot"}}}`, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "mutual cycle", |
| 45 | + schema: `{ |
| 46 | + "definitions":{ |
| 47 | + "CycleMutualA":{"allOf":[{"$ref":"#/definitions/CycleMutualB"}]}, |
| 48 | + "CycleMutualB":{"anyOf":[{"$ref":"#/definitions/CycleMutualA"}]} |
| 49 | + }, |
| 50 | + "properties":{"x":{"$ref":"#/definitions/CycleMutualA"}} |
| 51 | + }`, |
| 52 | + }, |
| 53 | + { |
| 54 | + // 2 branches per level: 2^depth validators without a budget |
| 55 | + name: "fan-out cycle", |
| 56 | + schema: `{ |
| 57 | + "definitions":{"CycleFanOut":{"allOf":[{"$ref":"#/definitions/CycleFanOut"},{"$ref":"#/definitions/CycleFanOut"}]}}, |
| 58 | + "properties":{"x":{"$ref":"#/definitions/CycleFanOut"}} |
| 59 | + }`, |
| 60 | + }, |
| 61 | + } { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + var schema spec.Schema |
| 64 | + require.NoError(t, json.Unmarshal([]byte(tc.schema), &schema)) |
| 65 | + |
| 66 | + defer func() { |
| 67 | + r := recover() |
| 68 | + require.NotNil(t, r) |
| 69 | + msg, ok := r.(string) |
| 70 | + require.TrueT(t, ok) |
| 71 | + assert.True(t, strings.HasPrefix(msg, "Invalid schema provided to SchemaValidator: ")) |
| 72 | + assert.True(t, strings.Contains(msg, fmt.Sprintf("deeper than %d levels or into more than %d validators", maxCompositionDepth, maxCompositionValidators))) |
| 73 | + }() |
| 74 | + |
| 75 | + _ = AgainstSchema(&schema, map[string]any{"x": 1}, strfmt.Default) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestCompositionBudgetRecursiveSchema(t *testing.T) { |
| 81 | + // a $ref back to an ancestor through properties or items is built as the data is walked: |
| 82 | + // the depth of the data bounds it, and it does not draw from the budget |
| 83 | + const tree = `{ |
| 84 | + "definitions":{ |
| 85 | + "BudgetNode":{ |
| 86 | + "allOf":[ |
| 87 | + {"type":"object","required":["name"],"properties":{"name":{"type":"string"}}}, |
| 88 | + {"properties":{"children":{"type":"array","items":{"$ref":"#/definitions/BudgetNode"}}}} |
| 89 | + ] |
| 90 | + } |
| 91 | + }, |
| 92 | + "properties":{"root":{"$ref":"#/definitions/BudgetNode"}} |
| 93 | + }` |
| 94 | + |
| 95 | + var schema spec.Schema |
| 96 | + require.NoError(t, json.Unmarshal([]byte(tree), &schema)) |
| 97 | + |
| 98 | + // nest the data deeper than both caps: each level only builds the validators of one Node |
| 99 | + const depth = 2 * maxCompositionDepth |
| 100 | + node := map[string]any{nameProp: "leaf"} |
| 101 | + for range depth { |
| 102 | + node = map[string]any{nameProp: "node", "children": []any{node}} |
| 103 | + } |
| 104 | + |
| 105 | + require.NoError(t, AgainstSchema(&schema, map[string]any{"root": node}, strfmt.Default)) |
| 106 | + |
| 107 | + node["children"] = []any{map[string]any{"children": []any{}}} // missing name |
| 108 | + require.Error(t, AgainstSchema(&schema, map[string]any{"root": node}, strfmt.Default)) |
| 109 | +} |
| 110 | + |
| 111 | +func TestCompositionBudgetWideSchema(t *testing.T) { |
| 112 | + // many siblings within the budget still build |
| 113 | + const width = maxCompositionValidators |
| 114 | + branches := make([]spec.Schema, width) |
| 115 | + for i := range branches { |
| 116 | + branches[i] = *spec.StringProperty() |
| 117 | + } |
| 118 | + schema := &spec.Schema{SchemaProps: spec.SchemaProps{AllOf: branches}} |
| 119 | + |
| 120 | + require.NoError(t, AgainstSchema(schema, "a", strfmt.Default)) |
| 121 | + require.Error(t, AgainstSchema(schema, 1, strfmt.Default)) |
| 122 | + |
| 123 | + // one more panics |
| 124 | + schema.AllOf = append(schema.AllOf, *spec.StringProperty()) |
| 125 | + require.Panics(t, func() { _ = AgainstSchema(schema, "a", strfmt.Default) }) |
| 126 | +} |
| 127 | + |
| 128 | +func TestCompositionBudgetAmplification(t *testing.T) { |
| 129 | + // no cycle, and within maxCompositionDepth: A0 refers twice to A1, which refers twice to A2, |
| 130 | + // and so on. Expanding it stays within the budget of go-openapi/spec, but it would build |
| 131 | + // 2^levels validators. |
| 132 | + const levels = 13 |
| 133 | + definitions := make(map[string]any, levels+1) |
| 134 | + for i := range levels { |
| 135 | + ref := map[string]any{jsonRef: fmt.Sprintf("#/definitions/BudgetA%d", i+1)} |
| 136 | + definitions[fmt.Sprintf("BudgetA%d", i)] = map[string]any{"allOf": []any{ref, ref}} |
| 137 | + } |
| 138 | + definitions[fmt.Sprintf("BudgetA%d", levels)] = map[string]any{jsonType: stringType} |
| 139 | + |
| 140 | + doc, err := json.Marshal(map[string]any{ |
| 141 | + swaggerDefinitions: definitions, |
| 142 | + "properties": map[string]any{"x": map[string]any{jsonRef: "#/definitions/BudgetA0"}}, |
| 143 | + }) |
| 144 | + require.NoError(t, err) |
| 145 | + |
| 146 | + var schema spec.Schema |
| 147 | + require.NoError(t, json.Unmarshal(doc, &schema)) |
| 148 | + |
| 149 | + require.PanicsWithValue(t, |
| 150 | + invalidSchemaProvidedMsg(tooManyCompositionValidatorsMsg("x", maxCompositionDepth, maxCompositionValidators)).Error(), |
| 151 | + func() { _ = AgainstSchema(&schema, map[string]any{"x": "a"}, strfmt.Default) }, |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +func TestCompositionBudgetDeepSchema(t *testing.T) { |
| 156 | + // nesting allOf within the depth cap still builds |
| 157 | + schema := spec.StringProperty() |
| 158 | + for range maxCompositionDepth { |
| 159 | + schema = &spec.Schema{SchemaProps: spec.SchemaProps{AllOf: []spec.Schema{*schema}}} |
| 160 | + } |
| 161 | + |
| 162 | + require.NoError(t, AgainstSchema(schema, "a", strfmt.Default)) |
| 163 | + require.Error(t, AgainstSchema(schema, 1, strfmt.Default)) |
| 164 | + |
| 165 | + // one level more panics |
| 166 | + schema = &spec.Schema{SchemaProps: spec.SchemaProps{AllOf: []spec.Schema{*schema}}} |
| 167 | + require.Panics(t, func() { _ = AgainstSchema(schema, "a", strfmt.Default) }) |
| 168 | +} |
| 169 | + |
| 170 | +func TestCompositionBudgetSpec(t *testing.T) { |
| 171 | + // Spec stops on the circular ancestry of CycleSpec before it checks defaults and examples. |
| 172 | + // ContinueOnErrors goes on: building a validator for the default or the example panics, |
| 173 | + // and the spec validator returns it as an error (default) or a warning (example). |
| 174 | + const doc = `{ |
| 175 | + "swagger":"2.0", |
| 176 | + "info":{"title":"t","version":"1"}, |
| 177 | + "paths":{}, |
| 178 | + "definitions":{ |
| 179 | + "CycleSpec":{"default":1,"example":1,"allOf":[{"$ref":"#/definitions/CycleSpec"}]} |
| 180 | + } |
| 181 | + }` |
| 182 | + |
| 183 | + analyzed, err := loads.Analyzed(json.RawMessage(doc), "") |
| 184 | + require.NoError(t, err) |
| 185 | + |
| 186 | + validator := NewSpecValidator(analyzed.Schema(), strfmt.Default) |
| 187 | + validator.SetContinueOnErrors(true) |
| 188 | + |
| 189 | + var errs, warnings *Result |
| 190 | + require.NotPanics(t, func() { errs, warnings = validator.Validate(analyzed) }) |
| 191 | + |
| 192 | + const tooDeep = "nests allOf, anyOf, oneOf and not deeper than" |
| 193 | + assert.True(t, hasMessageContaining(errs.Errors, "has circular ancestry")) |
| 194 | + assert.True(t, hasMessageContaining(errs.Errors, tooDeep)) |
| 195 | + assert.True(t, hasMessageContaining(warnings.Errors, tooDeep)) |
| 196 | +} |
| 197 | + |
| 198 | +func hasMessageContaining(errs []error, substr string) bool { |
| 199 | + for _, err := range errs { |
| 200 | + if strings.Contains(err.Error(), substr) { |
| 201 | + return true |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return false |
| 206 | +} |
0 commit comments