-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-validator.php
215 lines (185 loc) · 6.33 KB
/
xml-validator.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**** Testing Input ****
<?xml version='1.0'?>
<!DOCTYPE change-log [
<!ENTITY systemEntity SYSTEM "robots.txt">
]>
<change-log>
<text>&systemEntity;</text>
</change-log>
*/
function HandleXmlError($errno, $errstr, $errfile, $errline){
if ($errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0)){
throw new DOMException($errstr);
}else{
return false;
}//end if
}// end function HandleXmlError
try {
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
$lEnableHTMLControls = FALSE;
//$lFormMethod = "GET";
$lEnableJavaScriptValidation = FALSE;
$lEnableXMLValidation = FALSE;
$lEnableXMLEncoding = FALSE;
$lProtectAgainstMethodTampering = FALSE;
libxml_disable_entity_loader(FALSE);
break;
case "1": // This code is insecure
$lEnableHTMLControls = TRUE;
//$lFormMethod = "GET";
$lEnableJavaScriptValidation = TRUE;
$lEnableXMLValidation = FALSE;
$lEnableXMLEncoding = FALSE;
$lProtectAgainstMethodTampering = FALSE;
libxml_disable_entity_loader(FALSE);
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
$lEnableHTMLControls = TRUE;
//$lFormMethod = "POST";
$lEnableJavaScriptValidation = TRUE;
$lEnableXMLValidation = TRUE;
$lEnableXMLEncoding = TRUE;
$lProtectAgainstMethodTampering = TRUE;
libxml_disable_entity_loader(TRUE);
break;
}//end switch
if ($lEnableHTMLControls) {
$lHTMLControlAttributes='required="required"';
}else{
$lHTMLControlAttributes="";
}// end if
$lFormSubmitted = FALSE;
if (isset($_POST["xml-validator-php-submit-button"]) || isset($_REQUEST["xml-validator-php-submit-button"])) {
$lFormSubmitted = TRUE;
}// end if
if ($lFormSubmitted){
if ($lProtectAgainstMethodTampering) {
$lXMLValidatorSubmitButton = $_POST["xml-validator-php-submit-button"];
$lXML = $_POST["xml"];
}else{
$lXMLValidatorSubmitButton = $_REQUEST["xml-validator-php-submit-button"];
$lXML = $_REQUEST["xml"];
}// end if $lProtectAgainstMethodTampering
try {
if ($lEnableXMLEncoding){
$lXMLToLog = $Encoder->encodeForXML($lXML);
}else{
$lXMLToLog = $lXML;
};
$LogHandler->writeToLog("Recieved request to validate XML for: " . $lXMLToLog);
} catch (Exception $e) {
//do nothing
}// end try
}// end if $lFormSubmitted
} catch (Exception $e) {
echo $CustomErrorHandler->FormatError($e, $lQueryString);
}// end try;
?>
<script type="text/javascript">
<?php
if($lEnableJavaScriptValidation){
echo "var lValidateInput = \"TRUE\"" . PHP_EOL;
}else{
echo "var lValidateInput = \"FALSE\"" . PHP_EOL;
}// end if
?>
function onSubmitOfForm(/*HTMLFormElement*/ theForm){
try{
var lUnsafePhrases = /ENTITY/i;
if(lValidateInput == "TRUE"){
if (theForm.xml.value.length === 0){
alert('Please enter a value.');
return false;
}// end if
if (theForm.xml.value.search(lUnsafePhrases) > -1){
alert('Dangerous phrases detected. We can\'t allow these. This all powerful blacklist will stop such attempts.\n\nMuch like padlocks, filtering cannot be defeated.\n\nBlacklisting is l33t like l33tspeak.');
return false;
}// end if
}// end if(lValidateInput)
return true;
}catch(e){
alert("Error: " + e.message);
}// end catch
}// end function onSubmitOfForm(/*HTMLFormElement*/ theForm)
</script>
<div class="page-title">XML Validator</div>
<?php include_once (__SITE_ROOT__.'/includes/back-button.inc');?>
<?php include_once (__SITE_ROOT__.'/includes/hints/hints-menu-wrapper.inc'); ?>
<form action="./index.php?page=xml-validator.php"
method="POST"
enctype="application/x-www-form-urlencoded"
onsubmit="return onSubmitOfForm(this);"
>
<input type="hidden" name="page" value="xml-validator.php" />
<table>
<tr id="id-bad-cred-tr" style="display: none;">
<td colspan="2" class="error-message">
Authentication Error: Bad XML Input
</td>
</tr>
<tr><td></td></tr>
<tr>
<td colspan="2" class="form-header">Please Enter XML to Validate</td>
</tr>
<tr>
<td colspan="2">
<span class="label">Example:</span>
<somexml><message>Hello World</message></somexml>
</td>
</tr>
<tr>
<td class="label">XML</td>
<td>
<textarea name="xml" rows="8" cols="50" id="idXMLTextArea" title="Please enter XML to validate" autofocus="autofocus" <?php echo $lHTMLControlAttributes ?>></textarea>
</td>
</tr>
<tr><td></td></tr>
<tr>
<td colspan="2" style="text-align:center;">
<input name="xml-validator-php-submit-button" class="button" type="submit" value="Validate XML" />
</td>
</tr>
</table>
</form>
<?php
if (isset($lXMLValidatorSubmitButton) && !empty($lXMLValidatorSubmitButton) && strlen($lXML) > 0){
try{
if(!($lEnableXMLValidation && (preg_match(XML_EXTERNAL_ENTITY_REGEX_PATTERNS, $lXML) || !preg_match(VALID_XML_CHARACTERS, $lXML)))){
echo "<fieldset>";
echo "<legend>XML Submitted</legend>";
echo "<div width='600px' class=\"important-code\">" . $Encoder->encodeForXML($lXML) . "</div>";
echo "</fieldset>";
echo "<div> </div>";
try {
set_error_handler('HandleXmlError');
$lDOMDocument = new DOMDocument();
$lDOMDocument->resolveExternals = true;
$lDOMDocument->substituteEntities = true;
$lDOMDocument->preserveWhiteSpace=true;
$lDOMDocument->loadXML($lXML);
echo "<fieldset>";
echo "<legend>Text Content Parsed From XML</legend>";
echo "<div width='600px'>" . $lDOMDocument->textContent . "</div>";
echo "</fieldset>";
echo "<div> </div>";
restore_error_handler();
} catch(Exception $e) {
echo $CustomErrorHandler->FormatError($e, "Could not parse XML because the input is mal-formed or could not be interpreted.");
}//end try
}else{
echo "<div> </div>";
echo "<div style=\"width:500px;margin-right:auto;margin-left:auto;\" class=\"warning-message\">
Possible XML external entity injection attack detected.<br/>
Support has been notified.
</div>";
}//end if
} catch (Exception $e) {
echo $CustomErrorHandler->FormatError($e, $lQueryString);
}// end try;
}// end if (isset($_POST))
?>