Skip to content

Commit 96b7dbe

Browse files
committed
Added check for restricted XML characters.
RestrictedChar ::= [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F].
1 parent d6c6757 commit 96b7dbe

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

DocX/HelperFunctions.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ internal static class HelperFunctions
1717
public const string DOCUMENT_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
1818
public const string TEMPLATE_DOCUMENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";
1919

20-
public static bool IsNullOrWhiteSpace(this string value)
20+
/// <summary>
21+
/// List of restricted character in xml: [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]
22+
/// See: https://www.w3.org/TR/xml11/#sec-xml11
23+
/// </summary>
24+
public static readonly char[] RestrictedXmlChar = new char[] {
25+
'\x1','\x2','\x3','\x4','\x5','\x6','\x7','\x8','\xb','\xc','\xe','\xf',
26+
'\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1e','\x1f',
27+
'\x7f','\x80','\x81','\x82','\x83','\x84','\x86','\x87','\x88','\x89','\x8a','\x8b','\x8c','\x8d','\x8e','\x8f',
28+
'\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9a','\x9b','\x9c','\x9d','\x9e','\x9f'
29+
};
30+
31+
public static bool IsNullOrWhiteSpace(this string value)
2132
{
2233
if (value == null) return true;
2334
return string.IsNullOrEmpty(value.Trim());
@@ -589,7 +600,15 @@ internal static List<XElement> FormatInput(string text, XElement rPr)
589600
break;
590601

591602
default:
592-
sb.Append(c);
603+
// Check the character against restricted list:
604+
// RestrictedChar ::= [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]
605+
// See https://www.w3.org/TR/xml11/#sec-xml11
606+
if( RestrictedXmlChar.Contains( c ) )
607+
{
608+
// skip the character
609+
}
610+
else
611+
sb.Append(c);
593612
break;
594613
}
595614

DocX/bin/Release/DocX.XML

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DocX/bin/Release/DocX.dll

0 Bytes
Binary file not shown.

UnitTests/DocXUnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ public void TableWithSpecifiedWidths()
150150
}
151151
}
152152

153+
[Test]
154+
public void Test_InvalidCharacter()
155+
{
156+
using( var output = File.Open( Path.Combine( _directoryWithFiles, "InvalidCharacters.docx" ), FileMode.Create ) )
157+
{
158+
using( var doc = DocX.Create( output ) )
159+
{
160+
doc.InsertParagraph( "\b" );
161+
Exception ex = null;
162+
try
163+
{
164+
doc.Save();
165+
}
166+
catch( Exception e )
167+
{
168+
ex = e;
169+
}
170+
Assert.IsTrue( ex == null );
171+
}
172+
}
173+
}
174+
153175
/// <summary>
154176
/// TextRemove should not remove empty paragraphs in case the paragraph is alone in the cell.
155177
/// In the rest cases empty paragraph may be removed.

0 commit comments

Comments
 (0)