Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable GET requests to JSP page?

Tags:

jsp

I am fixing some old defects and as part of one defect, I need to make sure that some requests are being only POST to the JSP page instead of a GET request. The application have a form which submits data to another JSP page (I know its wrong and against MVC but too late to fix it), since it is a JSP page, so we can POST the request or else we can GET the request. In case of a malicious user, can read the form and send the request as a GET from the browser like http://host:80/somejsp.jsp?param=value&param=value etc. In that case, it becomes a violation. I need to make sure that such GET requests are not processed. One way to do is to perform the below steps in the jsp page -

if (request.getMethod().equals("GET")) {
   // reroute the user as it is not a valid req
}

Is there any other way to do it?

like image 605
Shamik Avatar asked Jun 14 '10 16:06

Shamik


People also ask

How do you prevent users from directly accessing JSP files?

The simplest one is to move all those pages to WEB-INF folder where user can't access from the URL. It also means you only allow user to access servlet action and completely forbid user to access JSP pages. Go here for example. Save this answer.

Why is the first request to the JSP page the slowest?

This parsing incurs overhead that is avoided with JavaServer Pages, since JSP files are parsed only the first time they are requested. JSP will be slower than other approaches for this first request, because of the compilation step, but will be faster than the other approaches for all subsequent requests.

Who is responsible for accepting requested JSP?

Full Stack Java developer - Java + JSP + Restful WS + Spring The web server needs a JSP engine, i.e, a container to process JSP pages. The JSP container is responsible for intercepting requests for JSP pages.


1 Answers

Two solutions:

  1. Add a <security-constraint> with an empty <auth-constraint> on an <url-pattern> of *.jsp and <http-method> of GET which will block GET requests on JSP files to everyone (as suggested by McDowell):

    <security-constraint>
        <display-name>Restrict GET requests on JSP files</display-name>
        <web-resource-collection>
            <web-resource-name>JSP files</web-resource-name>
            <url-pattern>*.jsp</url-pattern>
            <http-method>GET</http-method>
        </web-resource-collection>
        <auth-constraint />
    </security-constraint> 
    
  2. Create a Filter which listens on an <url-pattern> of *.jsp and does basically the following in the doFilter() method.

    if (((HttpServletRequest) request).getMethod().equals("GET")) {
        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } else {
        chain.doFilter(request, response);
    }
    

No need to copypaste the same over all JSP pages which would only be prone to IllegalStateException: response already committed errors.

like image 66
BalusC Avatar answered Oct 24 '22 11:10

BalusC