988
$file = file_get_contents($filename);
if ($file && isOwnerOf($username,$filename)){
echo $file;
return true;
}
else{
echo 'You are not authorized to view this file';
}
return false;
}
This code first reads a specified file into memory, then prints the file if the user is authorized to see
its contents. The read of the file into memory may be resource intensive and is unnecessary if the
user is not allowed to see the file anyway.
Example 3:
The DTD and the very brief XML below illustrate what is meant by an XML bomb. The ZERO entity
contains one character, the letter A. The choice of entity name ZERO is being used to indicate
length equivalent to that exponent on two, that is, the length of ZERO is 2^0. Similarly, ONE refers
to ZERO twice, therefore the XML parser will expand ONE to a length of 2, or 2^1. Ultimately,
we reach entity THIRTYTWO, which will expand to 2^32 characters in length, or 4 GB, probably
consuming far more data than expected.
Example Language: XML (Attack)
<?xml version="1.0"?>
<!DOCTYPE MaliciousDTD [
<!ENTITY ZERO "A">
<!ENTITY ONE "&ZERO;&ZERO;">
<!ENTITY TWO "&ONE;&ONE;">
...
<!ENTITY THIRTYTWO "&THIRTYONE;&THIRTYONE;">
]>
<data>&THIRTYTWO;</data>
Example 4:
This example attempts to check if an input string is a "sentence" [REF-1164].
Example Language: JavaScript (Bad)
var test_string = "Bad characters: $@#";
var bad_pattern = /^(\w+\s?)*$/i;
var result = test_string.search(bad_pattern);
The regular expression has a vulnerable backtracking clause inside (\w+\s?)*$ which can be
triggered to cause a Denial of Service by processing particular phrases.
To fix the backtracking problem, backtracking is removed with the ?= portion of the expression
which changes it to a lookahead and the \2 which prevents the backtracking. The modified example
is:
Example Language: JavaScript (Good)
var test_string = "Bad characters: $@#";
var good_pattern = /^((?=(\w+))\2\s?)*$/i;
var result = test_string.search(good_pattern);
Note that [REF-1164] has a more thorough (and lengthy) explanation of everything going on within
the RegEx.
Example 5:
CWE Version 4.14
CWE-405: Asymmetric Resource Consumption (Amplification)
CWE-405: Asymmetric Resource Consumption (Amplification)