From 4798f26665f5e099cc72f5674f966dd86831ee56 Mon Sep 17 00:00:00 2001 From: Claudia Murialdo Date: Wed, 8 Nov 2023 20:51:51 -0300 Subject: [PATCH 1/2] Include defensive programming to handle unclosed tags when the 'WriteElement' method receives a null value --- .../GxClasses/Domain/GXXmlReadWrite.cs | 6 ++- .../DotNetUnitTest/Domain/XMLWriterTest.cs | 49 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 dotnet/test/DotNetUnitTest/Domain/XMLWriterTest.cs diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs index f34142d60..728addb48 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs @@ -2141,6 +2141,10 @@ string removeUnallowedChars( string s) public short WriteElement (string Name, string Value) { WriteStartElement(Name); + if (Value==null) + { + Value = string.Empty; + } valueBuffer = Value; return 0; } @@ -2154,7 +2158,7 @@ public short WriteElement (string Name, object Value) public short WriteElement (string Name) { - WriteElement (Name, ""); + WriteElement (Name, string.Empty); return 0; } diff --git a/dotnet/test/DotNetUnitTest/Domain/XMLWriterTest.cs b/dotnet/test/DotNetUnitTest/Domain/XMLWriterTest.cs new file mode 100644 index 000000000..b3e1ce1b0 --- /dev/null +++ b/dotnet/test/DotNetUnitTest/Domain/XMLWriterTest.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using GeneXus.Application; +using GeneXus.XML; +using Xunit; + +namespace UnitTesting +{ + public class XMLWriterTest : FileSystemTest + { + [Fact] + public void dfwpnumNullElementTest() + { + string content = dfwpnumTest(null, false); + Assert.Contains("", content, StringComparison.OrdinalIgnoreCase); + } + [Fact] + public void dfwpnumEmptyElementWithoutEndTest() + { + string content = dfwpnumTest("validValue", false); + Assert.Contains("validValue", content, StringComparison.OrdinalIgnoreCase); + } + internal string dfwpnumTest(string varcharValue, bool closeElements) + { + GxContext context = new GxContext(); + string fileName = Path.Combine(BaseDir, "dfwpnumTest.txt"); + GXXMLWriter GXSoapXMLWriter = new GXXMLWriter(context.GetPhysicalPath()); + GXSoapXMLWriter.Open(fileName); + GXSoapXMLWriter.WriteStartDocument("utf-8", 0); + GXSoapXMLWriter.WriteStartElement("SOAP-ENV:Envelope"); + GXSoapXMLWriter.WriteElement("varchar", varcharValue); + GXSoapXMLWriter.WriteAttribute("xmlns", "StorageExpiration"); + string sDateCnv = "0000-00-00"; + GXSoapXMLWriter.WriteElement("date", sDateCnv); + GXSoapXMLWriter.WriteAttribute("xmlns", "StorageExpiration"); + if (closeElements) + { + GXSoapXMLWriter.WriteEndElement(); + } + GXSoapXMLWriter.Close(); + return File.ReadAllText(fileName); + } + + } +} From 87709e00506b42a37c5aa5d9ce2ae36e4a0e3fde Mon Sep 17 00:00:00 2001 From: Claudia Murialdo Date: Thu, 16 Nov 2023 10:12:47 -0300 Subject: [PATCH 2/2] Add the same defensive programming to handle unclosed tags on .NET. --- dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs b/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs index aa04926c8..2c5fe2630 100644 --- a/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs +++ b/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs @@ -1132,7 +1132,7 @@ internal void AppendNode(GXSOAPContext ctx, Node node, int extent) char[] trimChars = { '\t', ' ' }; - if (node.NodeType != ElementType) return; + if (node==null || node.NodeType != ElementType) return; switch (node.NodeType) { case ElementType: @@ -2131,6 +2131,10 @@ string removeUnallowedChars( string s) public short WriteElement (string Name, string Value) { WriteStartElement(Name); + if (Value==null) + { + Value = string.Empty; + } valueBuffer = Value; return 0; }