Skip to content

Commit 11f91aa

Browse files
Fix unsafe casts in YAML processing
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
1 parent d9bc12d commit 11f91aa

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

DevProxy.Abstractions/Extensions/YamlConfigurationExtensions.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ private void FlattenMappingNode(YamlMappingNode mappingNode, string prefix)
8282

8383
foreach (var entry in mappingNode.Children)
8484
{
85-
var key = ((YamlScalarNode)entry.Key).Value ?? string.Empty;
85+
var key = GetScalarValue(entry.Key);
86+
if (key is null)
87+
{
88+
continue;
89+
}
8690

8791
// Handle YAML merge key (<<)
8892
if (key == "<<")
@@ -116,7 +120,11 @@ private void FlattenMappingNode(YamlMappingNode mappingNode, string prefix)
116120
// Then process regular keys (they override merged values)
117121
foreach (var entry in mappingNode.Children)
118122
{
119-
var key = ((YamlScalarNode)entry.Key).Value ?? string.Empty;
123+
var key = GetScalarValue(entry.Key);
124+
if (key is null)
125+
{
126+
continue;
127+
}
120128

121129
// Skip merge key
122130
if (key == "<<")
@@ -132,11 +140,20 @@ private void FlattenMappingNode(YamlMappingNode mappingNode, string prefix)
132140
}
133141
}
134142

143+
private static string? GetScalarValue(YamlNode node)
144+
{
145+
return node is YamlScalarNode scalarNode ? scalarNode.Value : null;
146+
}
147+
135148
private void CollectMergedValues(YamlMappingNode mappingNode, string prefix, Dictionary<string, string?> values)
136149
{
137150
foreach (var entry in mappingNode.Children)
138151
{
139-
var key = ((YamlScalarNode)entry.Key).Value ?? string.Empty;
152+
var key = GetScalarValue(entry.Key);
153+
if (key is null)
154+
{
155+
continue;
156+
}
140157

141158
// Skip nested merge keys in merged content
142159
if (key == "<<")

DevProxy.Abstractions/Utils/ProxyYaml.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Globalization;
56
using System.Text.Json;
67
using YamlDotNet.Core;
78
using YamlDotNet.RepresentationModel;
@@ -104,7 +105,11 @@ public static bool TryConvertYamlToJson(string yamlContent, out string? jsonCont
104105
var mergedValues = new Dictionary<string, object?>();
105106
foreach (var entry in mappingNode.Children)
106107
{
107-
var key = ((YamlScalarNode)entry.Key).Value ?? string.Empty;
108+
var key = GetScalarValue(entry.Key);
109+
if (key is null)
110+
{
111+
continue;
112+
}
108113

109114
if (key == "<<")
110115
{
@@ -144,7 +149,11 @@ public static bool TryConvertYamlToJson(string yamlContent, out string? jsonCont
144149
// Then, process explicit keys (they override merged values)
145150
foreach (var entry in mappingNode.Children)
146151
{
147-
var key = ((YamlScalarNode)entry.Key).Value ?? string.Empty;
152+
var key = GetScalarValue(entry.Key);
153+
if (key is null)
154+
{
155+
continue;
156+
}
148157

149158
// Skip merge keys
150159
if (key == "<<")
@@ -159,6 +168,11 @@ public static bool TryConvertYamlToJson(string yamlContent, out string? jsonCont
159168
return result;
160169
}
161170

171+
private static string? GetScalarValue(YamlNode node)
172+
{
173+
return node is YamlScalarNode scalarNode ? scalarNode.Value : null;
174+
}
175+
162176
private static List<object?> ConvertSequenceNode(YamlSequenceNode sequenceNode)
163177
{
164178
var result = new List<object?>();
@@ -209,7 +223,7 @@ public static bool TryConvertYamlToJson(string yamlContent, out string? jsonCont
209223
}
210224

211225
// Check for floating point values
212-
if (double.TryParse(value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var doubleValue))
226+
if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var doubleValue))
213227
{
214228
return doubleValue;
215229
}

0 commit comments

Comments
 (0)