force-downloaded report file flooded with HTML by mistake



[PHP]
class ReportGenerator {

function ReportGenerator() {}

/**

* Generate the HTTP headers necessary for this file type. Can be
called statically

*

* @access public

* @param mixed $filename
* @see file_get_contents
* @see actual_path

*/

function &generateHTTPHeaders($filename) { // STATIC VOID METHOD



if (!preg_match('/.+\.[a-zA-Z0-9\-_]+$/i', $filename)) die("Filename:
\"$filename\" must have an extension");


$ext = substr($filename, strrpos($filename, '.') + 1,
strlen($filename));



switch (strtolower(trim($ext))) {

case 'pdf':

$ctype = 'application/pdf';

break;

case 'exe':

$ctype = 'application/octet-stream';

break;

case 'zip':

$ctype = 'appliation/zip';

break;

case 'doc':

$ctype = 'application/msword';

break;

case 'xls':

$ctype = 'application/vnd.ms-excel';

break;

case 'csv':

$ctype = 'application/vnd.ms-excel';

break;

case 'ppt':

$ctype = 'application/vnd.ms-powerpoint';

break;

case 'gif':

$ctype = 'image/gif';

break;

case 'png':

$ctype = 'image/png';

break;

case 'jpg':

$ctype = 'image/jpg';

break;

case 'jpeg':

$ctype = 'image/jpg';

break;

default:

$ctype = 'application/force-download';

break;

}



header('Pragma: public');

header('Expires: 0');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header("Content-Type: $ctype");

$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);

if ((is_integer(strpos($user_agent, 'msie'))) &&
(is_integer(strpos($user_agent, 'win')))) {

header('Content-Disposition: filename=' .
basename(actual_path($filename)) . ';');

} else {

header('Content-Disposition: inline; filename=' .
basename(actual_path($filename)) . ';');

}

header('Content-Transfer-Encoding: binary');

header('Content-Length: ' . @filesize(actual_path($filename)));

if (function_exists('file_get_contents')) {

echo @file_get_contents(actual_path($filename));

} else {

@readfile(actual_path($filename));

}

}


}
[/PHP]

The class method generateHTTPHeaders() can be called statically to
force-download a file (Excel, CSV, PDF, etc.) per customer requirement.
However, upon attempting to do so:

[PHP]
// REAL IMPORTANT!!! CHMOD OR THE WORLD CAN SEE YOUR REPORTS!!!!!!!
if ($this->isSuccessful) @chmod(0770,
actual_path($newReportFileName)); // CHANGE PERMISSIONS (IF IN UNIX)
TO PREVENT WORLD FROM ACCESSING FILE
if ($this->isSuccessful)
ReportGenerator::generateHTTPHeaders($newReportFileName); // THIS
CAUSES THE FORCED DOWNLOAD
@unlink(actual_path($newReportFileName)); // FILE HAS BEEN
FORCE-DOWNLOADED AND IS NO LONGER NEEDED ON SERVER
@unlink(actual_path($reportFileName)); // REMOVE THE
TEMPORARY FILE AS WELL, PROVIDED IT EXISTS AND/OR YOU CAN
if ($this->isSuccessful) exit();
[/PHP]

The force-downloaded file you get is completely flooded with HTML,
particularly the HTML of the page itself! I do not understand how this
is possible, particularly since I ran careful traces up until the
headers are spawned, and the resulting file contents are legitimate
report data (no HTML) up until you generate the headers at the end of
the method, then suddenly HTML pours in like a breached levee!

Any ideas? This is as much code as I am able to display that would be
relevant to the problem, I'm afraid.

Thanx
Phil

.



Relevant Pages

  • [NEWS] AeroMail Multiple Vulnerabilities
    ... AeroMail versions prior to 1.45 ... uploaded files as it should. ... You can add additional headers to outgoing e-mail messages by sending some ... JavaScript and HTML code is active, ...
    (Securiteam)
  • Re: Report Generator Suggestions
    ... I explained how HTML doesn't support stuff like headers and footers and, although I don't understand exactly what you are recomending, you are basically telling me to use HTML. ... you can generate extra revenue by creating custom reports for customers who request it. ...
    (microsoft.public.vc.mfc)
  • Re: How could this possibly happen (HttpURLConnection + simple JSP page)
    ... In PHP that would throw some nasty HTTP-related warnings and potentially break the page as you are changing the headers after HTML display? ... You are writing code in a special format that is compiled in to a servlet, which is then executed by the server when the page is requested. ... The other thing is that writing things "after HTML display" is probably very difficult as the display is done on the users computer when they have recieved the response. ... When you create a response for the user it does not necessarily get sent back as soon as you start, and more importantly the headers do not necessarily finish as soon as you start writing to the body. ...
    (comp.lang.java.help)
  • Re: OE6 and Hotmail
    ... Robert Aldwinckle wrote: ... From his screen shot and as far as I can tell, 'cos I just tried the same he has indeed done a file>save as HTML for offline viewing, that's the default setting for saving. ... look at the headers for an E-mail which you sent in your Sent Items folder ... headers in it with the equivalent data from the saved message in the HTML. ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)