1 25 26 package net.killingar.actions; 27 28 import java.util.Properties ; 29 30 public class PathActionSupport extends ActionSupport 31 { 32 34 String path; 36 String realPath; 37 38 String [] permittedPaths; 39 String permittedPathsConcat; 40 41 public String getPath() { return path; } 43 public String getRealPath() { return realPath; } 44 45 47 public String execute() throws Exception 49 { 50 try 51 { 52 javax.servlet.http.HttpServletRequest req = webwork.action.ServletActionContext.getRequest(); 53 54 boolean isInclude = false; 55 String uri; 56 57 uri = (String ) req.getAttribute("javax.servlet.include.request_uri"); 58 if (uri != null) 59 isInclude = true; 60 else 61 uri = req.getRequestURI(); 62 63 StringBuffer cb = new StringBuffer (); 64 String servletPath; 65 66 if (isInclude) 67 servletPath = (String ) req.getAttribute("javax.servlet.include.servlet_path"); 68 else 69 servletPath = req.getServletPath(); 70 71 if (servletPath != null) 72 cb.append(servletPath); 73 74 String pathInfo; 75 if (isInclude) 76 pathInfo = (String ) req.getAttribute("javax.servlet.include.path_info"); 77 else 78 pathInfo = req.getPathInfo(); 79 80 if (pathInfo != null) 81 cb.append(pathInfo); 82 83 String relPath = cb.toString(); 84 85 String path = req.getRealPath(relPath); 86 87 if (!new java.io.File (path).isDirectory()) 88 { 89 relPath = relPath.substring(0, relPath.lastIndexOf('/')); 90 path = req.getRealPath(relPath); 91 } 92 93 this.path = relPath; 94 this.realPath = path; 95 96 if (permittedPaths == null) 98 { 99 Properties p = net.killingar.Utils.getProperties(); 100 permittedPathsConcat = p.getProperty(getClass().getName()+".permittedPaths", ""); 101 permittedPaths = permittedPathsConcat.split(","); 102 } 103 104 boolean accessGranted = false; 105 relPath += "/"; 106 for (int i = 0; i < permittedPaths.length; i++) 107 { 108 if (relPath.matches(permittedPaths[i])) 109 { 110 accessGranted = true; 111 break; 112 } 113 } 114 115 if (!accessGranted) 116 throw new Exception ("access denied to path "+this.path+" ("+getClass().toString()+")"); } 118 catch (Exception e) 119 { 120 addErrorMessage("getting paths failed, exception thrown ("+e.toString()+")"); 121 e.printStackTrace(); 122 123 return ERROR; 124 } 125 126 return super.execute(); 127 } 128 } 129 | Popular Tags |