diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index 127e905ff3d3a2..e4b26591038502 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -358,7 +358,14 @@ private List ReadTargets(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - targets.Add(ReadTarget(ref reader, reader.GetString()!)); + string? targetName = reader.GetString(); + + if (string.IsNullOrEmpty(targetName)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(targetName))); + } + + targets.Add(ReadTarget(ref reader, targetName)); } reader.CheckEndObject(); @@ -374,7 +381,14 @@ private Target ReadTarget(ref Utf8JsonReader reader, string targetName) while (reader.Read() && reader.IsTokenTypeProperty()) { - libraries.Add(ReadTargetLibrary(ref reader, reader.GetString()!)); + string? targetLibraryName = reader.GetString(); + + if (string.IsNullOrEmpty(targetLibraryName)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(targetLibraryName))); + } + + libraries.Add(ReadTargetLibrary(ref reader, targetLibraryName)); } reader.CheckEndObject(); @@ -448,7 +462,16 @@ private IEnumerable ReadTargetLibraryDependencies(ref Utf8JsonReader while (reader.TryReadStringProperty(out string? name, out string? version)) { - dependencies.Add(new Dependency(Pool(name)!, Pool(version)!)); + if (string.IsNullOrEmpty(name)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(name))); + } + if (string.IsNullOrEmpty(version)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(version))); + } + + dependencies.Add(new Dependency(Pool(name), Pool(version))); } reader.CheckEndObject(); @@ -464,7 +487,12 @@ private static List ReadPropertyNames(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string libraryName = reader.GetString()!; + string? libraryName = reader.GetString(); + + if (string.IsNullOrEmpty(libraryName)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(libraryName))); + } reader.Skip(); runtimes.Add(libraryName); @@ -486,7 +514,12 @@ private static List ReadRuntimeFiles(ref Utf8JsonReader reader) string? assemblyVersion = null; string? fileVersion = null; - string path = reader.GetString()!; + string? path = reader.GetString(); + + if (string.IsNullOrEmpty(path)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(path))); + } reader.ReadStartObject(); @@ -521,9 +554,16 @@ private List ReadTargetLibraryRuntimeTargets(ref Utf8Jso while (reader.Read() && reader.IsTokenTypeProperty()) { + string? runtimePath = reader.GetString(); + + if (string.IsNullOrEmpty(runtimePath)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(runtimePath))); + } + var runtimeTarget = new RuntimeTargetEntryStub { - Path = reader.GetString()! + Path = runtimePath }; reader.ReadStartObject(); @@ -565,7 +605,13 @@ private List ReadTargetLibraryResources(ref Utf8JsonReader rea while (reader.Read() && reader.IsTokenTypeProperty()) { - string path = reader.GetString()!; + string? path = reader.GetString(); + + if (string.IsNullOrEmpty(path)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(path))); + } + string? locale = null; reader.ReadStartObject(); @@ -599,7 +645,12 @@ private Dictionary ReadLibraries(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string libraryName = reader.GetString()!; + string? libraryName = reader.GetString(); + + if (string.IsNullOrEmpty(libraryName)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(libraryName))); + } libraries.Add(Pool(libraryName), ReadOneLibrary(ref reader)); } @@ -650,10 +701,15 @@ private LibraryStub ReadOneLibrary(ref Utf8JsonReader reader) reader.CheckEndObject(); + if (string.IsNullOrEmpty(type)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(type))); + } + return new LibraryStub() { Hash = hash, - Type = Pool(type)!, + Type = Pool(type), Serviceable = serviceable, Path = path, HashPath = hashPath, @@ -669,9 +725,14 @@ private static List ReadRuntimes(ref Utf8JsonReader reader) while (reader.Read() && reader.IsTokenTypeProperty()) { - string runtime = reader.GetString()!; + string? runtime = reader.GetString(); string?[] fallbacks = reader.ReadStringArray(); + if (string.IsNullOrEmpty(runtime)) + { + throw new FormatException(SR.Format(SR.RequiredFieldNotSpecified, nameof(runtime))); + } + runtimeFallbacks.Add(new RuntimeFallbacks(runtime, fallbacks)); } diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resources/Strings.resx b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resources/Strings.resx index a761f3deb34d0e..0cd624c03e09da 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/Resources/Strings.resx +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/Resources/Strings.resx @@ -1,17 +1,17 @@  @@ -138,4 +138,7 @@ An item with the same key has already been added. Key: {0} - + + The required field '{0}' was not specified. + + \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextJsonReaderTest.cs b/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextJsonReaderTest.cs index fcdca9eb8d40eb..e16ec056079a97 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextJsonReaderTest.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextJsonReaderTest.cs @@ -792,5 +792,48 @@ public void IgnoresUnknownPropertiesInCompilationOptions() context.CompilationOptions.PublicSign.Should().Be(true); context.CompilationOptions.WarningsAsErrors.Should().Be(true); } + + [Fact] + public void FailsToReadNullLibraryType() + { + Assert.Throws(() => Read( +@"{ + ""libraries"":{ + ""System.Banana/1.0.0"": { + ""type"": null, + ""serviceable"": false, + ""sha512"": ""HASH-System.Banana"" + } + } +} +")); + } + + [Fact] + public void FailsToReadEmptyTargetName() + { + Assert.Throws(() => Read( +@"{ + ""targets"": { + """": {} + } + }")); + } + + [Fact] + public void FailsToReadEmptyLibraryType() + { + Assert.Throws(() => Read( +@"{ + ""libraries"":{ + ""System.Banana/1.0.0"": { + ""type"": """", + ""serviceable"": false, + ""sha512"": ""HASH-System.Banana"" + } + } +} +")); + } } }