You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Jürgen Hoffmann <jh...@byteaction.de> on 2005/02/04 13:08:21 UTC

Bug in DefaultParameterParser

Hi,

I think we have found a Bug inside the DefaultParameterParser.



Problem concerning the decoding of pathinfo variables in 
org.apache.turbine.util.parser.DefaultParameterParser.setRequest(HttpServletRequest 
request)

In this method, we find the following code:

StringTokenizer st =
        new StringTokenizer(request.getPathInfo(), "/");
[...]
        tmp = URLDecoder.decode(st.nextToken());
[...]
        pathPart = URLDecoder.decode(st.nextToken());


The problem is that HttpServletRequest.getPathInfo() returns a decoded 
String (see JavaDocs of HttpServletRequest), but the following code 
requires the String to be encoded. Only if the pathinfo was still in 
encoded, the tokenization and the decoding of every individual token 
would work. As a result of this bug, the pathinfo variables may not 
contain any problematic characters like % and /. This is particularly 
problematic when arbitrary Strings, which a user might have entered on 
a form, have to be passed around as pathinfo variables.

To patch this bug, DefaultParamterParser might implement something 
like the following method to replace the call to request.getPathInfo:

private String getEncodedPathInfo(HttpServletRequest request)
{
    String encodedPathInfo = request.getRequestURI();
    String contextInfo = request.getContextPath();
    String servletPath = request.getServletPath();

    if (encodedPathInfo.startsWith(contextInfo))
    {
        encodedPathInfo =
encodedPathInfo.substring(contextInfo.length());

        if (encodedPathInfo.startsWith(servletPath))
        {
            encodedPathInfo = 
encodedPathInfo.substring(servletPath.length());
        }
        else
        {
            // The ServletPath is decoded. Therefore it might not 
match the start of the encoded RequestURI.

            log.warn("The servlet path " + servletPath + " probably 
requires encoding."
                    + " This is not supported by the 
DefaultParameterParser, yet."
                    + " The parsing of pathinfo variables may be 
unreliable."
                    + " In the best case, you might see an additional 
parameter servlet=something."
                    + " In the worst case, names and values might be 
confused."
                    + " Please install your application with a more 
conventional servlet path.");
        }
    }
    else
    {
        log.error("RequstURI " + encodedPathInfo + " does not start 
with ContextInfo " + contextInfo);
    }

    return encodedPathInfo;
} 

Kind regards

Juergen Hoffmann