|
| 1 | +// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package spec |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/go-openapi/testify/v2/assert" |
| 13 | + "github.com/go-openapi/testify/v2/require" |
| 14 | +) |
| 15 | + |
| 16 | +// errNoExternalLoads is returned by the deny-all PathLoader used to prove that refusing |
| 17 | +// external loads is not, on its own, a mitigation for the amplification attack. |
| 18 | +var errNoExternalLoads = errors.New("no external loads allowed") |
| 19 | + |
| 20 | +// buildAmplificationSpec builds a self-contained spec where each of n definitions references |
| 21 | +// the next one twice via allOf. Without an expansion budget, expanding d0 inlines a tree with |
| 22 | +// 2^(n-1) leaves from O(n) bytes of input (a $ref amplification / "billion laughs" attack). |
| 23 | +func buildAmplificationSpec(t testing.TB, n int) []byte { |
| 24 | + t.Helper() |
| 25 | + |
| 26 | + defs := make(map[string]any, n) |
| 27 | + for i := range n { |
| 28 | + var sch any |
| 29 | + if i == n-1 { |
| 30 | + sch = map[string]any{"type": "string"} |
| 31 | + } else { |
| 32 | + next := fmt.Sprintf("#/definitions/d%d", i+1) |
| 33 | + sch = map[string]any{"allOf": []any{ |
| 34 | + map[string]any{"$ref": next}, |
| 35 | + map[string]any{"$ref": next}, |
| 36 | + }} |
| 37 | + } |
| 38 | + defs[fmt.Sprintf("d%d", i)] = sch |
| 39 | + } |
| 40 | + |
| 41 | + doc := map[string]any{ |
| 42 | + "swagger": "2.0", |
| 43 | + "info": map[string]any{"title": "x", "version": "1"}, |
| 44 | + "paths": map[string]any{}, |
| 45 | + "definitions": defs, |
| 46 | + } |
| 47 | + raw, err := json.Marshal(doc) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + return raw |
| 51 | +} |
| 52 | + |
| 53 | +func TestMaxExpansionNodesTriState(t *testing.T) { |
| 54 | + // 0 (zero value): default budget, so every caller is protected out of the box. |
| 55 | + assert.EqualT(t, DefaultMaxExpansionNodes, (&ExpandOptions{}).maxExpansionNodes()) |
| 56 | + |
| 57 | + // negative: unbounded. |
| 58 | + assert.EqualT(t, 0, (&ExpandOptions{MaxExpansionNodes: -1}).maxExpansionNodes()) |
| 59 | + |
| 60 | + // positive: explicit budget. |
| 61 | + assert.EqualT(t, 1234, (&ExpandOptions{MaxExpansionNodes: 1234}).maxExpansionNodes()) |
| 62 | +} |
| 63 | + |
| 64 | +func TestExpand_AmplificationBudget(t *testing.T) { |
| 65 | + // A deep amplification spec. Even a modest depth would explode without a budget. |
| 66 | + const depth = 40 |
| 67 | + raw := buildAmplificationSpec(t, depth) |
| 68 | + |
| 69 | + t.Run("explicit budget trips the guard", func(t *testing.T) { |
| 70 | + var sw Swagger |
| 71 | + require.NoError(t, json.Unmarshal(raw, &sw)) |
| 72 | + |
| 73 | + err := ExpandSpec(&sw, &ExpandOptions{MaxExpansionNodes: 2000}) |
| 74 | + require.ErrorIs(t, err, ErrExpandTooManyNodes) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("deny-all PathLoader is not a mitigation on its own", func(t *testing.T) { |
| 78 | + // All refs are fragment-only and resolve against the in-memory root, so refusing |
| 79 | + // external loads does not prevent the blow-up: the budget is what stops it. |
| 80 | + var sw Swagger |
| 81 | + require.NoError(t, json.Unmarshal(raw, &sw)) |
| 82 | + |
| 83 | + loaderCalled := false |
| 84 | + err := ExpandSpec(&sw, &ExpandOptions{ |
| 85 | + MaxExpansionNodes: 2000, |
| 86 | + PathLoader: func(p string) (json.RawMessage, error) { |
| 87 | + loaderCalled = true |
| 88 | + return nil, fmt.Errorf("%w: %s", errNoExternalLoads, p) |
| 89 | + }, |
| 90 | + }) |
| 91 | + require.ErrorIs(t, err, ErrExpandTooManyNodes) |
| 92 | + assert.FalseT(t, loaderCalled, "expected no external load attempts") |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("ContinueOnError does not suppress a budget breach", func(t *testing.T) { |
| 96 | + // The budget is a hard resource-exhaustion safeguard: unlike an unresolvable $ref, |
| 97 | + // it must surface even when the caller tolerates errors. |
| 98 | + var sw Swagger |
| 99 | + require.NoError(t, json.Unmarshal(raw, &sw)) |
| 100 | + |
| 101 | + err := ExpandSpec(&sw, &ExpandOptions{MaxExpansionNodes: 2000, ContinueOnError: true}) |
| 102 | + require.ErrorIs(t, err, ErrExpandTooManyNodes) |
| 103 | + }) |
| 104 | + |
| 105 | + t.Run("negative budget disables the guard", func(t *testing.T) { |
| 106 | + // A shallow spec that stays well under any real budget must expand fully when unbounded. |
| 107 | + shallow := buildAmplificationSpec(t, 8) |
| 108 | + var sw Swagger |
| 109 | + require.NoError(t, json.Unmarshal(shallow, &sw)) |
| 110 | + |
| 111 | + require.NoError(t, ExpandSpec(&sw, &ExpandOptions{MaxExpansionNodes: -1})) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func TestExpand_BudgetAllowsLegitSpec(t *testing.T) { |
| 116 | + // A shallow amplification spec (small node count) must expand cleanly under the default budget. |
| 117 | + raw := buildAmplificationSpec(t, 8) |
| 118 | + var sw Swagger |
| 119 | + require.NoError(t, json.Unmarshal(raw, &sw)) |
| 120 | + |
| 121 | + require.NoError(t, ExpandSpec(&sw, nil)) // nil options => default budget |
| 122 | + // d0 fully expanded: no $ref remains in the leaf chain. |
| 123 | + out, err := json.Marshal(sw.Definitions["d0"]) |
| 124 | + require.NoError(t, err) |
| 125 | + assert.StringNotContainsT(t, string(out), `"$ref"`) |
| 126 | +} |
| 127 | + |
| 128 | +func TestExpand_BudgetErrorIsSentinel(t *testing.T) { |
| 129 | + raw := buildAmplificationSpec(t, 40) |
| 130 | + var sw Swagger |
| 131 | + require.NoError(t, json.Unmarshal(raw, &sw)) |
| 132 | + |
| 133 | + err := ExpandSpec(&sw, &ExpandOptions{MaxExpansionNodes: 100}) |
| 134 | + require.Error(t, err) |
| 135 | + assert.TrueT(t, errors.Is(err, ErrExpandTooManyNodes)) |
| 136 | +} |
0 commit comments