|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using FluentAssertions; |
| 6 | +using Microsoft.OpenApi.Any; |
| 7 | +using Microsoft.OpenApi.Readers; |
| 8 | +using Microsoft.OpenApi.Readers.Exceptions; |
| 9 | +using Microsoft.OpenApi.Readers.ParseNodes; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.OpenApi.Tests |
| 13 | +{ |
| 14 | + [Collection("DefaultSettings")] |
| 15 | + public class YamlAliasExpansionTests |
| 16 | + { |
| 17 | + // A "billion laughs" YAML bomb: each level references the previous one multiple times, |
| 18 | + // so materializing the shared node graph into an independent object tree expands |
| 19 | + // exponentially. The conversion must fail fast instead of exhausting memory. |
| 20 | + private const string YamlBomb = |
| 21 | + """ |
| 22 | + a: &a ["x","x","x","x","x","x","x","x","x"] |
| 23 | + b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a] |
| 24 | + c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b] |
| 25 | + d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c] |
| 26 | + e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d] |
| 27 | + f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e] |
| 28 | + g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f] |
| 29 | + h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g] |
| 30 | + i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h] |
| 31 | + """; |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void ExponentialAliasExpansionIsRejected() |
| 35 | + { |
| 36 | + var node = ParseNode.Create(new(new()), YamlHelper.ParseYamlString(YamlBomb)); |
| 37 | + |
| 38 | + Assert.Throws<OpenApiReaderException>(() => node.CreateAny()); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void ExcessiveNestingDepthIsRejected() |
| 43 | + { |
| 44 | + // Deeper than the conversion depth limit, which protects the recursive |
| 45 | + // converter from stack exhaustion. |
| 46 | + const int depth = 70; |
| 47 | + var deeplyNested = new string('[', depth) + new string(']', depth); |
| 48 | + |
| 49 | + var node = ParseNode.Create(new(new()), YamlHelper.ParseYamlString(deeplyNested)); |
| 50 | + |
| 51 | + Assert.Throws<OpenApiReaderException>(() => node.CreateAny()); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void ReadReturnsDiagnosticErrorForExponentialAliasExpansion() |
| 56 | + { |
| 57 | + // A "billion laughs" YAML bomb must surface as a diagnostic error |
| 58 | + // rather than throwing or exhausting memory. |
| 59 | + var input = |
| 60 | + $$""" |
| 61 | + openapi: 3.0.0 |
| 62 | + info: |
| 63 | + title: bomb |
| 64 | + version: 1.0.0 |
| 65 | + paths: {} |
| 66 | + x-bomb: |
| 67 | + {{YamlBombIndented()}} |
| 68 | + """; |
| 69 | + |
| 70 | + var reader = new OpenApiStringReader(); |
| 71 | + reader.Read(input, out var diagnostic); |
| 72 | + |
| 73 | + diagnostic.Errors.Should().NotBeEmpty(); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void LegitimateAliasesStillConvert() |
| 78 | + { |
| 79 | + var input = |
| 80 | + """ |
| 81 | + a: &val hello |
| 82 | + b: *val |
| 83 | + """; |
| 84 | + |
| 85 | + var node = ParseNode.Create(new(new()), YamlHelper.ParseYamlString(input)); |
| 86 | + |
| 87 | + var anyObject = Assert.IsType<OpenApiObject>(node.CreateAny()); |
| 88 | + Assert.Equal("hello", ((OpenApiString)anyObject["a"]).Value); |
| 89 | + Assert.Equal("hello", ((OpenApiString)anyObject["b"]).Value); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void ConversionLimitsDefaultToDocumentedValues() |
| 94 | + { |
| 95 | + Assert.Equal(64u, OpenApiReaderLimits.DefaultMaxDepth); |
| 96 | + Assert.Equal(5_000_000u, OpenApiReaderLimits.DefaultMaxNodeCount); |
| 97 | + Assert.Equal(OpenApiReaderLimits.DefaultMaxDepth, OpenApiReaderLimits.MaxDepth); |
| 98 | + Assert.Equal(OpenApiReaderLimits.DefaultMaxNodeCount, OpenApiReaderLimits.MaxNodeCount); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void SettingMaxDepthToZeroThrows() |
| 103 | + { |
| 104 | + Assert.Throws<ArgumentOutOfRangeException>(() => OpenApiReaderLimits.MaxDepth = 0); |
| 105 | + // The invalid assignment must not have changed the effective limit. |
| 106 | + Assert.Equal(OpenApiReaderLimits.DefaultMaxDepth, OpenApiReaderLimits.MaxDepth); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void SettingMaxNodeCountToZeroThrows() |
| 111 | + { |
| 112 | + Assert.Throws<ArgumentOutOfRangeException>(() => OpenApiReaderLimits.MaxNodeCount = 0); |
| 113 | + // The invalid assignment must not have changed the effective limit. |
| 114 | + Assert.Equal(OpenApiReaderLimits.DefaultMaxNodeCount, OpenApiReaderLimits.MaxNodeCount); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void RaisingMaxDepthAllowsDocumentsDeeperThanTheDefault() |
| 119 | + { |
| 120 | + // A document nested deeper than the default depth limit (64) is rejected by default |
| 121 | + // but can be permitted by a consumer that opts into a higher limit. |
| 122 | + const int depth = 70; |
| 123 | + var deeplyNested = new string('[', depth) + new string(']', depth); |
| 124 | + |
| 125 | + try |
| 126 | + { |
| 127 | + OpenApiReaderLimits.MaxDepth = (uint)(depth + 10); |
| 128 | + var node = ParseNode.Create(new(new()), YamlHelper.ParseYamlString(deeplyNested)); |
| 129 | + Assert.IsType<OpenApiArray>(node.CreateAny()); |
| 130 | + } |
| 131 | + finally |
| 132 | + { |
| 133 | + OpenApiReaderLimits.MaxDepth = OpenApiReaderLimits.DefaultMaxDepth; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static string YamlBombIndented() |
| 138 | + { |
| 139 | + return " " + YamlBomb.Replace("\r\n", "\n").Replace("\n", "\n "); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments