diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs index 09c2e67b3d..0d20e5be36 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs @@ -308,7 +308,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters // We check if the parsed media type has value at this stage when we have iterated // over all the parameters and we know if the parsing was sucessful. - if (!parser.ParsingFailed) + if (!parser.ParsingFailed && parser.CurrentOffset >= start) { return new MediaTypeSegmentWithQuality( new StringSegment(mediaType, start, parser.CurrentOffset - start), diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/AcceptHeaderParserTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/AcceptHeaderParserTest.cs index 36a167cc55..b8a470cff6 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/AcceptHeaderParserTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/AcceptHeaderParserTest.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using Microsoft.Extensions.Primitives; using Xunit; @@ -48,6 +49,21 @@ namespace Microsoft.AspNetCore.Mvc.Formatters.Internal } } + [Fact] + public void ParseAcceptHeader_ParsesSimpleHeaderWithMultipleValues_InvalidFormat() + { + // Arrange + var header = "application/json, application/xml,;q=0.8"; + var expectedException = "\"Invalid values ';q=0.8'.\""; + + // Act + var ex = Assert.Throws( + () => { AcceptHeaderParser.ParseAcceptHeader(new List { header }); }); + + // Assert + Assert.Equal(expectedException, ex.Message); + } + [Fact] public void ParseAcceptHeader_ParsesMultipleHeaderValues() {