forked from netwrix/pingcastle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafeXmlWriter.cs
173 lines (146 loc) · 3.67 KB
/
SafeXmlWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// Copyright (c) Ping Castle. All rights reserved.
// https://www.pingcastle.com
//
// Licensed under the Non-Profit OSL. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml;
namespace PingCastle.Data
{
// the goal of this class is to not crash the program when unsafe char (0x1F) is found
// indeed the xml specification doesn't support such char
// a workaround would be to ignore this error (char are encoded), but then loading will have to be modified too.
// that means that newer reports won't be loaded into older version of PingCastle.
// this is a problem, typically for Enterprise version not updated
// as a consequence, unsafe char are detected and replaced by character which does not trigger a problem when loading reports
internal class SafeXmlWriter : XmlWriter
{
XmlWriter x;
public SafeXmlWriter(XmlWriter xmlWriter)
{
x = xmlWriter;
}
public override void WriteString(string text)
{
bool unsafestring = false;
StringBuilder sb = new StringBuilder();
foreach (var c in text.ToCharArray())
{
if (char.IsControl(c))
{
unsafestring = true;
sb.Append('_');
}
else
{
sb.Append(c);
}
}
if (unsafestring)
{
Trace.WriteLine("unsafe string found: " + text);
}
x.WriteString(sb.ToString());
}
public override void Close()
{
x.Close();
}
public override void Flush()
{
x.Flush();
}
public override string LookupPrefix(string ns)
{
return x.LookupPrefix(ns);
}
public override void WriteBase64(byte[] buffer, int index, int count)
{
x.WriteBase64(buffer, index, count);
}
public override void WriteCData(string text)
{
x.WriteCData(text);
}
public override void WriteCharEntity(char ch)
{
x.WriteCharEntity(ch);
}
public override void WriteChars(char[] buffer, int index, int count)
{
x.WriteChars(buffer, index, count);
}
public override void WriteComment(string text)
{
x.WriteComment(text);
}
public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
x.WriteDocType(name, pubid, sysid, subset);
}
public override void WriteEndAttribute()
{
x.WriteEndAttribute();
}
public override void WriteEndDocument()
{
x.WriteEndDocument();
}
public override void WriteEndElement()
{
x.WriteEndElement();
}
public override void WriteEntityRef(string name)
{
x.WriteEntityRef(name);
}
public override void WriteFullEndElement()
{
x.WriteFullEndElement();
}
public override void WriteProcessingInstruction(string name, string text)
{
x.WriteProcessingInstruction(name, text);
}
public override void WriteRaw(string data)
{
x.WriteRaw(data);
}
public override void WriteRaw(char[] buffer, int index, int count)
{
x.WriteRaw(buffer, index, count);
}
public override void WriteStartAttribute(string prefix, string localName, string ns)
{
x.WriteStartAttribute(prefix, localName, ns);
}
public override void WriteStartDocument(bool standalone)
{
x.WriteStartDocument(standalone);
}
public override void WriteStartDocument()
{
x.WriteStartDocument();
}
public override void WriteStartElement(string prefix, string localName, string ns)
{
x.WriteStartElement(prefix, localName, ns);
}
public override WriteState WriteState
{
get { return x.WriteState; }
}
public override void WriteSurrogateCharEntity(char lowChar, char highChar)
{
x.WriteSurrogateCharEntity(lowChar, highChar);
}
public override void WriteWhitespace(string ws)
{
x.WriteWhitespace(ws);
}
}
}