Skip to content

Commit

Permalink
Merge pull request #1451 from antony-liu/poi/bug-60626
Browse files Browse the repository at this point in the history
poi: #60626 - ArrayIndexOutOfBoundsException in EvilUnclosedBRFixingInputStream
  • Loading branch information
tonyqus authored Dec 15, 2024
2 parents bcdb67f + d7c158e commit db5ec30
Show file tree
Hide file tree
Showing 7 changed files with 1,246 additions and 317 deletions.
37 changes: 32 additions & 5 deletions OpenXmlFormats/Vml/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using System.ComponentModel;
using NPOI.OpenXml4Net.Util;
using NPOI.Util;

namespace NPOI.OpenXmlFormats.Vml
{
Expand Down Expand Up @@ -634,6 +635,7 @@ public class CT_Shape {
private ST_TrueFalse strokedField;
private string wrapcoordsField;

private string _xml;
public static CT_Shape Parse(XmlNode node, XmlNamespaceManager namespaceManager)
{
if (node == null)
Expand Down Expand Up @@ -693,11 +695,28 @@ public static CT_Shape Parse(XmlNode node, XmlNamespaceManager namespaceManager)
else if (childNode.LocalName == "ClientData")
ctObj.ClientData.Add(CT_ClientData.Parse(childNode, namespaceManager));
}
ctObj._xml = node.OuterXml;
return ctObj;
}



public override string ToString()
{
if(string.IsNullOrEmpty(this._xml))
{
using(MemoryStream out1 = new MemoryStream())
using(StreamWriter sw = new StreamWriter(out1))
{
Write(sw, "shape");
return Encoding.UTF8.GetString(out1.ToArray());
}
}
else
{
return this._xml;
}

}
public void Write(StreamWriter sw, string nodeName)
{
sw.Write(string.Format("<v:{0}", nodeName));
Expand Down Expand Up @@ -3370,11 +3389,14 @@ public static CT_Textbox Parse(XmlNode node, XmlNamespaceManager namespaceManage
ctObj.id = XmlHelper.ReadString(node.Attributes["id"]);
ctObj.style = XmlHelper.ReadString(node.Attributes["style"]);
ctObj.inset = XmlHelper.ReadString(node.Attributes["inset"]);
ctObj.ItemXml = node.InnerXml;
ctObj.ItemXml = node.OuterXml;
return ctObj;
}


public override string ToString()
{
return this.ItemXml;
}

internal void Write(StreamWriter sw, string nodeName)
{
Expand Down Expand Up @@ -3736,7 +3758,7 @@ public class CT_Shapetype {
// CT_Shapetype obj = (CT_Shapetype)serializer.Deserialize(tr);
// return obj;
//}

private string _xml;
public static CT_Shapetype Parse(XmlNode node, XmlNamespaceManager namespaceManager)
{
if (node == null)
Expand Down Expand Up @@ -3807,10 +3829,15 @@ public static CT_Shapetype Parse(XmlNode node, XmlNamespaceManager namespaceMana
else if (childNode.LocalName == "textdata")
ctObj.textdata.Add(CT_Rel.Parse(childNode, namespaceManager));
}

ctObj._xml = node.OuterXml;
return ctObj;
}


public override string ToString()
{
return _xml;
}

public void Write(StreamWriter sw, string nodeName)
{
Expand Down
240 changes: 240 additions & 0 deletions main/Util/ReplacingInputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace NPOI.Util
{

/// <summary>
/// <para>
/// Simple FilterInputStream that can replace occurrences of bytes with something else.
/// </para>
/// <para>
/// This has been taken from inbot-utils. (MIT licensed)
/// </para>
/// </summary>
/// @see <see href="https://github.com/Inbot/inbot-utils">inbot-utils</see>
public class ReplacingInputStream : FilterInputStream
{

// while matching, this is where the bytes go.
int[] buf;
private int matchedIndex=0;
private int unbufferIndex=0;
private int replacedIndex=0;

private byte[] pattern;
private byte[] replacement;
private State state=State.NOT_MATCHED;

// simple state machine for keeping track of what we are doing
private enum State
{
NOT_MATCHED,
MATCHING,
REPLACING,
UNBUFFER
}

//private static Charset UTF8 = Charset.forName("UTF-8");

/// <summary>
/// Replace occurrences of pattern in the input. Note: input is assumed to be UTF-8 encoded. If not the case use byte[] based pattern and replacement.
/// </summary>
/// <param name="in">input</param>
/// <param name="pattern">pattern to replace.</param>
/// <param name="replacement">the replacement or null</param>
public ReplacingInputStream(InputStream in1, String pattern, String replacement)
: this(in1, Encoding.UTF8.GetBytes(pattern), replacement==null ? null : Encoding.UTF8.GetBytes(replacement))
{

}

/// <summary>
/// <para>
/// Replace occurrences of pattern in the input.
/// </para>
/// <para>
/// If you want to normalize line endings DOS/MAC (\n\r | \r) to UNIX (\n), you can call the following:<br/>
/// {@code new ReplacingInputStream(new ReplacingInputStream(is, "\n\r", "\n"), "\r", "\n")}
/// </para>
/// </summary>
/// <param name="in">input</param>
/// <param name="pattern">pattern to replace</param>
/// <param name="replacement">the replacement or null</param>
public ReplacingInputStream(InputStream in1, byte[] pattern, byte[] replacement)
: base(in1)
{
;
if(pattern == null || pattern.Length == 0)
{
throw new ArgumentException("pattern length should be > 0");
}
this.pattern = pattern;
this.replacement = replacement;
// we will never match more than the pattern length
buf = new int[pattern.Length];
}
public override int Read(byte[] b, int off, int len)
{

// copy of parent logic; we need to call our own read() instead of super.read(), which delegates instead of calling our read
if(b == null)
{
throw new NullReferenceException();
}
else if(off < 0 || len < 0 || len > b.Length - off)
{
throw new IndexOutOfRangeException();
}
else if(len == 0)
{
return 0;
}

int c = Read();
if(c == -1)
{
return -1;
}
b[off] = (byte) c;

int i = 1;
for(; i < len; i++)
{
c = Read();
if(c == -1)
{
break;
}
b[off + i] = (byte) c;
}
return i;

}
public override int Read(byte[] b)
{

// call our own read
return Read(b, 0, b.Length);
}
public override int Read()
{

// use a simple state machine to figure out what we are doing
int next;
switch(state)
{
default:
case State.NOT_MATCHED:
// we are not currently matching, replacing, or unbuffering
next=base.Read();
if(pattern[0] != next)
{
return next;
}

// clear whatever was there
Arrays.Fill(buf, 0);
// make sure we start at 0
matchedIndex=0;

buf[matchedIndex++]=next;
if(pattern.Length == 1)
{
// edge-case when the pattern length is 1 we go straight to replacing
state=State.REPLACING;
// reset replace counter
replacedIndex=0;
}
else
{
// pattern of length 1
state=State.MATCHING;
}
// recurse to continue matching
return Read();

case State.MATCHING:
// the previous bytes matched part of the pattern
next=base.Read();
if(pattern[matchedIndex]==next)
{
buf[matchedIndex++]=next;
if(matchedIndex==pattern.Length)
{
// we've found a full match!
if(replacement==null || replacement.Length==0)
{
// the replacement is empty, go straight to NOT_MATCHED
state=State.NOT_MATCHED;
matchedIndex=0;
}
else
{
// start replacing
state=State.REPLACING;
replacedIndex=0;
}
}
}
else
{
// mismatch -> unbuffer
buf[matchedIndex++]=next;
state=State.UNBUFFER;
unbufferIndex=0;
}
return Read();

case State.REPLACING:
// we've fully matched the pattern and are returning bytes from the replacement
next=replacement[replacedIndex++];
if(replacedIndex==replacement.Length)
{
state=State.NOT_MATCHED;
replacedIndex=0;
}
return next;

case State.UNBUFFER:
// we partially matched the pattern before encountering a non matching byte
// we need to serve up the buffered bytes before we go back to NOT_MATCHED
next=buf[unbufferIndex++];
if(unbufferIndex==matchedIndex)
{
state=State.NOT_MATCHED;
matchedIndex=0;
}
return next;
}
}
public override String ToString()
{
return state.ToString() + " " + matchedIndex + " " + replacedIndex + " " + unbufferIndex;
}

}
}

3 changes: 2 additions & 1 deletion ooxml/XSSF/UserModel/XSSFVMLDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ limitations under the License.
using NPOI.OpenXmlFormats.Vml.Spreadsheet;
using System.Text;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using NPOI.Util;

namespace NPOI.XSSF.UserModel
{
Expand Down Expand Up @@ -116,7 +117,7 @@ internal void Read(Stream is1)
//Stream vmlsm = new EvilUnclosedBRFixingInputStream(is1); --TODO:: add later

doc.LoadXml(
data.Replace("<br>","").Replace("</br>", "")
data.Replace("<br>", "<br/>").Replace("</br>", "<br/>")
);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
Expand Down
Loading

0 comments on commit db5ec30

Please sign in to comment.