Skip to content

Commit

Permalink
Limit recursion level in XmlDecoder.ReadMatrix() and XmlEncoder.Write…
Browse files Browse the repository at this point in the history
…Matrix() (#1295)
  • Loading branch information
AlinMoldovean committed Feb 25, 2021
1 parent c382922 commit c541994
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Stack/Opc.Ua.Core/Types/Encoders/XmlDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,17 @@ public Array ReadEnumeratedArray(string fieldName, System.Type enumType)
/// </summary>
private Matrix ReadMatrix(string fieldName)
{
// check the nesting level for avoiding a stack overflow.
if (m_nestingLevel > m_context.MaxEncodingNestingLevels)
{
throw ServiceResultException.Create(
StatusCodes.BadEncodingLimitsExceeded,
"Maximum nesting level of {0} was exceeded",
m_context.MaxEncodingNestingLevels);
}

m_nestingLevel++;

Array elements = null;
Int32Collection dimensions = null;
TypeInfo typeInfo = null;
Expand All @@ -2647,6 +2658,8 @@ private Matrix ReadMatrix(string fieldName)
EndField(fieldName);
}

m_nestingLevel--;

if (elements == null)
{
throw new ServiceResultException(StatusCodes.BadDecodingError, "The Matrix contains invalid elements");
Expand Down
13 changes: 13 additions & 0 deletions Stack/Opc.Ua.Core/Types/Encoders/XmlEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,17 @@ public void WriteExtensionObjectBody(object body)
/// </summary>
private void WriteMatrix(string fieldName, Matrix value)
{
// check the nesting level for avoiding a stack overflow.
if (m_nestingLevel > m_context.MaxEncodingNestingLevels)
{
throw ServiceResultException.Create(
StatusCodes.BadEncodingLimitsExceeded,
"Maximum nesting level of {0} was exceeded",
m_context.MaxEncodingNestingLevels);
}

m_nestingLevel++;

if (BeginField(fieldName, value == null, true, true))
{
PushNamespace(Namespaces.OpcUaXsd);
Expand All @@ -1967,6 +1978,8 @@ private void WriteMatrix(string fieldName, Matrix value)

EndField(fieldName);
}

m_nestingLevel--;
}

/// <summary>
Expand Down

0 comments on commit c541994

Please sign in to comment.