Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:(soap-endpoint): skip duplicated xml parameters #1048

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/SoapCore.Tests/InvalidXMLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,63 @@ public async Task MissingNamespace()
Assert.IsTrue(context.Response.Body.Length > 0);
}

[TestMethod]
public async Task DuplicatedElement()
{
// Arrange
var logger = NullLoggerFactory.Instance.CreateLogger<SoapEndpointMiddleware<CustomMessage>>();

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<DuplicatedElementService>();
serviceCollection.AddSoapCore();

var options = new SoapOptions()
{
Path = "/Service.asmx",
EncoderOptions = new[]
{
new SoapEncoderOptions
{
MessageVersion = MessageVersion.Soap11,
WriteEncoding = Encoding.UTF8,
ReaderQuotas = XmlDictionaryReaderQuotas.Max
}
},
ServiceType = typeof(DuplicatedElementService),
SoapModelBounder = new MockModelBounder(),
SoapSerializer = SoapSerializer.XmlSerializer
};

var soapCore = new SoapEndpointMiddleware<CustomMessage>(logger, (innerContext) => Task.CompletedTask, options);

var context = new DefaultHttpContext();
context.Request.Path = new PathString("/Service.asmx");
context.Request.Method = "POST";
context.Response.Body = new MemoryStream();

// Act
var request = @"
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:rlx=""https://dos.brianfeucht.com/"">
<soapenv:Body>
<rlx:Test>
<eventRef>a</eventRef>
<eventRef>b</eventRef>
<other>c</other>
</rlx:Test>
</soapenv:Body>
</soapenv:Envelope>";
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(request), false);
context.Request.ContentType = "text/xml; charset=utf-8";

await soapCore.Invoke(context, serviceCollection.BuildServiceProvider());

// Assert
context.Response.Body.Seek(0, SeekOrigin.Begin);
using var response = new StreamReader(context.Response.Body, Encoding.UTF8);
var body = await response.ReadToEndAsync();
Assert.IsTrue(body.Contains("<TestResult>a c</TestResult>"));
}

[ServiceContract(Namespace = "https://dos.brianfeucht.com/")]
public class DenialOfServiceProofOfConcept
{
Expand All @@ -78,5 +135,15 @@ public Task<string> SpinTheThread(string a, string b)
return Task.FromResult("Hello World");
}
}

[ServiceContract(Namespace = "https://dos.brianfeucht.com/")]
public class DuplicatedElementService
{
[OperationContract]
public Task<string> Test(string eventRef, string other)
{
return Task.FromResult(eventRef + " " + other);
}
}
}
}
4 changes: 3 additions & 1 deletion src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ private void SetMessageHeadersToProperty(Message requestMessage, object serviceI
private object[] GetRequestArguments(Message requestMessage, XmlDictionaryReader xmlReader, OperationDescription operation, HttpContext httpContext)
{
var arguments = new object[operation.AllParameters.Length];
var alreadyProcessedParameters = new bool[operation.AllParameters.Length];

IEnumerable<Type> serviceKnownTypes = operation
.GetServiceKnownTypesHierarchy()
Expand All @@ -691,7 +692,7 @@ private object[] GetRequestArguments(Message requestMessage, XmlDictionaryReader
while (!xmlReader.EOF)
{
var parameterInfo = operation.InParameters.FirstOrDefault(p => p.Name == xmlReader.LocalName);
if (parameterInfo == null)
if (parameterInfo == null || alreadyProcessedParameters[parameterInfo.Index])
{
xmlReader.Skip();
continue;
Expand All @@ -704,6 +705,7 @@ private object[] GetRequestArguments(Message requestMessage, XmlDictionaryReader
}

lastParameterIndex = parameterInfo.Index;
alreadyProcessedParameters[lastParameterIndex] = true;

var argumentValue = _serializerHelper.DeserializeInputParameter(
xmlReader,
Expand Down
Loading