cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathaniel Alfred" <>
Subject RE: [VOLUNTEER] Re: DO NOT REPLY [Bug 13541] New: - SAVE_UPLOAD_FILES_TO_DISK should be configurable
Date Mon, 14 Oct 2002 14:58:04 GMT
Hi folks,

after posting to Bugzilla I have a few more thoughts about this issue.

We are looking here at the darker parts of the HTTP protocol
specification.  File uploading was introduced experimentally
in 1995 (RFC1867) and formalized with HTML 4.0 in 1998 (RFC2388).

As the feature was urgently needed at the time when the web has
already taken off, the RFC was apparently prepared in such a rush,
that it is not really well thought.

In order to use <input type="file"> the HTML form must use
<form method="POST" enctype="multipart/form-data">.  The browser
then encodes each <input> value as a part of the multipart request

The problem now is that the values of <input type="text"> and
other form input elements are interspersed with the possibly 
every large file content.  

For example, if you have a form

  <input type="file">
  <input type="radio">
  <input type="submit">

and you want to know the state of the radio button, you first
need to process the file data.

In a framework which wants to provide random and idem-potent
parameter access as in request.getParameter("foo") one needs to
read and buffer the complete request body.

Providing buffer space for megabytes of upload data is problematic.
The Java Servlet Specifications allows the container (Tomcat) to 
duck the problem and leave it to the servlet (Cocoon) to decode 
the request body.

That is exactly what is done in CocoonServlet.service:

        HttpServletRequest request =

        this.cocoon = getCocoon(request.getPathInfo(),

The RequestFactory is called upon to wrap the HttpServletRequest from
the container into a new HttpServletRequest after decoding the request
content by reading from req.getInputStream().

Currently there are three implementations of RequestFactory to choose 
from in org/apache/cocoon/components/request:

SimpleRequestFactoryImpl is the dummy implementation which just returns
the container request.

MaybeUploadRequestFactoryImpl is legacy from which apparently stems the
non-Javaesque argument list of getServletRequest().  However, it is the
fallback used if web.xml does not contain a request-factory

MultipartRequestFactoryImpl is the preferred implementation configured
in the example web.xml file.  It provides the choice between storing the
upload file content in memory (FilePartArray) or in a *permanent* file
(FilePartFile).   The upload directory is a global configuration
defaulting to WEB-INF/work/upload-dir.

However, CocoonServlet hardwires

    private static final boolean SAVE_UPLOADED_FILES_TO_DISK = true;

to always store the request in a file which will remain after the
end of the request.  This covers the use case in the Cocoon samples 
of providing a public inbox where one can drop files which shall be
handled lateron by another application.

However, for the other use case of processing the uploaded data directly
in the request, the permanent file approach causes serious trouble.
First there is race condition, if by chance or design two request use
the same filename at the same time.

Second, there is as far as I can see a *BIG* security hole here.  
The filename supplied in the request data is used verbatim in
the filepath on the server.  By crafting a request with enough ../ in 
the filename an attacker can overwrite any file writable by the

At the very least anybody can fill up my disk by sending fake file
request.  Note that it not necessary to have a file upload page.  All
happens at the very beginning of request handling before any Cocoon
access control mechanisms could stop it!!!

What needs to be done?

1.) Fix the security hole *IMMEDIATELY* and advise the user community.  
The last thing Cocoon needs is bad press about such things.

The quickest fix is (what I hacked into my

    private static final boolean SAVE_UPLOADED_FILES_TO_DISK = false;

if you want to handle multipart/form-data (but it breaks the upload

If you only need default form encoding, you can also disable it in


2.) Clean the filename in
from ".." and other dangerous stuff.  Then make
configurable.  (I think MaybeUploadRequestFactoryImpl can be removed

3.) Refactor MultipartRequestFactoryImpl and friends.

How should MultipartRequestFactoryImpl be refactored?

The FilePartArray stuff works fine.  The only problem is that for
of a request it has provide buffer space for the whole request body.
a few large uploads running in parallel it can easily exhaust all JVM

Rather than choosing in the configuration either/or array/file a hybrid
approach should be used.  Small upload request (configurable limit
are kept in memory.

Larger uploads are written to a *temporary* file created with a random
in tempdir and automatically removed by CocoonServlet at the end of the 

The samples/xsp/upload.xsp is then made working again by reading out the
upload content from the request and storing it as permanent file.

FilePart filePart = (FilePart)request.get("uploaded_file");
InputStream contentStream = filePart.getInputStream();
File uploadFile = new File(filePart.getSafeFilePath(uploadDir));
// copy contentStream to uploadFile

This could actually be turned into an FileUploadAction with

<map:act type="upload" src="path/to/inbox">
  <map:parameter name="overwrite" value="deny|allow|rename"/>
  <map:parameter name="maxsize" value="10000000"/>
  ... Thank you for your file
... Sorry, your file could not be stored

SimpleRequestFactoryImpl should become the fallback request factory in
CocoonServlet, unless web.xml (or cocoon.xconf) says otherwise.
The request factory in the example configuration should be
MultipartRequestFactoryImpl with a reasonable limits for array/file

The RequestFactory should be extended to allow registering different
request wrappers depending on the method ("POST") and content type
("multipart/form-data").  That can come handy if more special request
decoding is required (WEBDAV?, SOAP?).



    RequestFactory.register("POST", "application/x-www-form-urlencoded",

    RequestFactory.register("POST", "multipart/form-data",

    RequestFactory.register("...", "...",


    RequestFactory factory =
    HttpServletRequest request = factory.getServletRequest(req);

Further room for enhancements are:

1.) <input type=file> allows also for multiple file selection.  That is
not yet handled at all.

2.) Even if file uploading is disabled, read out the request body to
advance to the
next HTTP/1.1 request in the socket.  (When using an upstream load
balancer, the
following request may even be from a different user.)

3.) Sort out the character encoding between the file content sent by the
and what the servlet expects.

So there is plenty of volunteer work left to be done...

Cheers, Alfred.

>-----Original Message-----
>From: Geoff Howard []
>Sent: Freitag, 11. Oktober 2002 23:36
>Subject: [VOLUNTEER] Re: DO NOT REPLY [Bug 13541] New: -
>SAVE_UPLOAD_FILES_TO_DISK should be configurable
>I'm willing to volunteer to work on issues surrounding
>this "bug" if someone can get me up to speed on where
>things stand in the code.  Specifically, what would
>help for starters is: 
>1) Why is SAVE_UPLOADED_FILES_TO_DISK hardcoded now? 
>I assume that its because of some unresolved problem
>with the condition in which
>handles the false condition?  Is something wrong with
>FilePartArray as it stands now?  Is this not the
>correct solution?
>2) What is the story with
>MaybeUploadRequestFactoryImpl?  From the comments in
>the code it seems unfinished.
>3) Same with SimpleRequestFactoryImpl?  It seems like
>specifying one of these in web.xml is overlapped in
>functionality with SAVE_UPLOADED_FILES_TO_DISK.  If
>so, which direction should it go?  To them or away
>from them?
>4) There is a message in the archives demonstrating
>the use of an action to deal with uploads (though I
>think it depends on the default functionality).  Is
>this the right direction to encourage use?  I assume
>that InputModules could handle things the same way if
>they replace Actions in the future.
>It seems to me that this is what is needed for
>- files should not be uploaded and saved by default
>from any page.  that has security hole written all
>over it.
>- when upload of a file is desired, there should be a
>configurable default directory (as there is now) _and_
>the ability to designate alternative locations either
>in the sitemap, and _maybe_ via runtime/request
>- ideally, some pipelines should be able to allow them
>and others not - this way you can require
>authentication easily.  any other suggestions on how
>to allow uploads only to select users?
>The first could be handled simply by changing
>CocoonServlet to use a parameter from web.xml instead
>of it's final boolean.  (I'd also argue that if the
>other changes don't happen, this parameter should be
>set to false in the default config for security
>The second would involve modifying the behavior of
>MultipartParser and friends in
>Am I wrong that the default configuration means that I
>can upload files all day long to live cocoon instances
>everywhere right now (unless they have specified
>SimpleRequestFactoryImpl in web.xml)?!?
>Geoff Howard
>--- wrote:
>> AT
>> AND 
>> SAVE_UPLOAD_FILES_TO_DISK should be configurable
>>            Summary: SAVE_UPLOAD_FILES_TO_DISK should
>> be configurable
>>            Product: Cocoon 2
>>            Version: Current CVS
>>           Platform: All
>>         OS/Version: All
>>             Status: NEW
>>           Severity: Enhancement
>>           Priority: Other
>>          Component: core
>>         AssignedTo:
>>         ReportedBy:
>> CocoonServlet passes a final boolean
>> RequestFactory.
>> This should become a ServetConfig init parameter to
>> allow using the 
>> MultipartRequestFactoryImpl producing FilePartArray
>> objects for upload requests.
>> Provided one is not afraid of memory exhaustion,
>> FilePartArray avoids the 
>> problems of FilePartFile (race condition, privacy
>> issue, housekeeping).
>> To unsubscribe, e-mail:
>> For additional commands, email:
>Do you Yahoo!?
>Faith Hill - Exclusive Performances, Videos & More
>To unsubscribe, e-mail:
>For additional commands, email:

This message is for the named person's use only. It may contain
confidential, proprietary or legally privileged information. No
confidentiality or privilege is waived or lost by any mistransmission.
If you receive this message in error, please notify the sender urgently
and then immediately delete the message and any copies of it from your
system. Please also immediately destroy any hardcopies of the message.
You must not, directly or indirectly, use, disclose, distribute, print,
or copy any part of this message if you are not the intended recipient.
The sender's company reserves the right to monitor all e-mail
communications through their networks. Any views expressed in this
message are those of the individual sender, except where the message
states otherwise and the sender is authorised to state them to be the
views of the sender's company. 

To unsubscribe, e-mail:
For additional commands, email:

View raw message