package test.cwe400.cwe.examples; import java.io.IOException; import java.util.concurrent.TimeUnit; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ThreadResourceAbuse extends HttpServlet { static final int DEFAULT_RETRY_AFTER = 5*1000; static final int MAX_RETRY_AFTER = 10*1000; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // BAD: Get thread pause time from request parameter without validation String delayTimeStr = request.getParameter("DelayTime"); try { int delayTime = Integer.valueOf(delayTimeStr); new UncheckedSyncAction(delayTime).start(); } catch (NumberFormatException e) { } } protected void doGet2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // BAD: Get thread pause time from request parameter without validation try { int delayTime = request.getParameter("nodelay") != null ? 0 : Integer.valueOf(request.getParameter("DelayTime")); new UncheckedSyncAction(delayTime).start(); } catch (NumberFormatException e) { } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // BAD: Get thread pause time from context init parameter without validation String delayTimeStr = getServletContext().getInitParameter("DelayTime"); try { int delayTime = Integer.valueOf(delayTimeStr); new UncheckedSyncAction(delayTime).start(); } catch (NumberFormatException e) { } } protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // GOOD: Get thread pause time from request cookie with validation Cookie[] cookies = request.getCookies(); for ( int i=0; i 0 && waitTime < 5000) { Thread.sleep(waitTime); // Do other updates } } catch (InterruptedException e) { } } } class CheckedSyncAction2 extends Thread { int waitTime; public CheckedSyncAction2(int waitTime) { this.waitTime = waitTime; } @Override public void run() { // GOOD: enforce an upper limit on wait time try { if (waitTime >= 5000) { // No action } else { Thread.sleep(waitTime); } // Do other updates } catch (InterruptedException e) { } } } protected void doPost2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // GOOD: Get thread pause time from init container parameter with validation String delayTimeStr = getServletContext().getInitParameter("DelayTime"); try { int delayTime = Integer.valueOf(delayTimeStr); new CheckedSyncAction2(delayTime).start(); } catch (NumberFormatException e) { } } protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // BAD: Get thread pause time from request cookie without validation Cookie[] cookies = request.getCookies(); for ( int i=0; i