diff --git a/java/org/apache/catalina/servlets/WebdavServlet.java b/java/org/apache/catalina/servlets/WebdavServlet.java index d98ffd59e5b3..7701afa0b500 100644 --- a/java/org/apache/catalina/servlets/WebdavServlet.java +++ b/java/org/apache/catalina/servlets/WebdavServlet.java @@ -1372,7 +1372,7 @@ protected void doCopy(HttpServletRequest req, HttpServletResponse resp) throws I String path = getRelativePath(req); - copyResource(path, req, resp); + copyResource(path, req, resp, false); } @@ -1398,7 +1398,7 @@ protected void doMove(HttpServletRequest req, HttpServletResponse resp) throws I return; } - if (copyResource(path, req, resp)) { + if (copyResource(path, req, resp, true)) { deleteResource(path, req, resp, false); } } @@ -2104,12 +2104,14 @@ private boolean isLocked(String path, String principal, String ifHeader) { * @param path Path of the resource to copy * @param req Servlet request * @param resp Servlet response + * @param move {@code true} if the copy is the first step of a MOVE * * @return boolean true if the copy is successful * * @throws IOException If an IO error occurs */ - private boolean copyResource(String path, HttpServletRequest req, HttpServletResponse resp) throws IOException { + private boolean copyResource(String path, HttpServletRequest req, HttpServletResponse resp, boolean move) + throws IOException { // Check the source exists WebResource source = resources.getResource(path); @@ -2218,6 +2220,25 @@ private boolean copyResource(String path, HttpServletRequest req, HttpServletRes return false; } + // Validate Depth before the destination may be deleted + boolean infiniteCopy = true; + String depthHeader = req.getHeader("Depth"); + if (depthHeader != null) { + if (depthHeader.equals("infinity")) { + // NO-OP - this is the default + } else if (depthHeader.equals("0")) { + infiniteCopy = false; + } else { + resp.sendError(WebdavStatus.SC_BAD_REQUEST); + return false; + } + } + // RFC 4918 9.9.2: MOVE on a collection acts as Depth: infinity. Reject other values, as httpd mod_dav does. + if (move && !infiniteCopy && source.isDirectory()) { + resp.sendError(WebdavStatus.SC_BAD_REQUEST); + return false; + } + boolean overwrite = true; String overwriteHeader = req.getHeader("Overwrite"); if (overwriteHeader != null) { @@ -2247,19 +2268,6 @@ private boolean copyResource(String path, HttpServletRequest req, HttpServletRes Map errorList = new LinkedHashMap<>(); - boolean infiniteCopy = true; - String depthHeader = req.getHeader("Depth"); - if (depthHeader != null) { - if (depthHeader.equals("infinity")) { - // NO-OP - this is the default - } else if (depthHeader.equals("0")) { - infiniteCopy = false; - } else { - resp.sendError(WebdavStatus.SC_BAD_REQUEST); - return false; - } - } - boolean result = copyResource(errorList, path, destinationPath, infiniteCopy); if ((!result) || (!errorList.isEmpty())) { diff --git a/test/org/apache/catalina/servlets/TestWebdavServlet.java b/test/org/apache/catalina/servlets/TestWebdavServlet.java index e54e655c7770..0dd48fa84eb4 100644 --- a/test/org/apache/catalina/servlets/TestWebdavServlet.java +++ b/test/org/apache/catalina/servlets/TestWebdavServlet.java @@ -1905,6 +1905,85 @@ public void setPropertyValue(String propertyValue) { } + @Test + public void testMoveCollectionDepthZero() throws Exception { + File tempWebapp = new File(getTemporaryDirectory(), "webdav-move-depth"); + File sub = new File(tempWebapp, "src/sub"); + Assert.assertTrue(sub.mkdirs()); + writeFile(new File(tempWebapp, "src/a.txt")); + writeFile(new File(sub, "b.txt")); + + Client client = startWritableWebdav(tempWebapp); + + // RFC 4918 9.9.2: Depth: 0 is not valid for MOVE on a collection + // @formatter:off + client.setRequest(new String[] { + "MOVE /src HTTP/1.1" + CRLF + + "Host: localhost:" + getPort() + CRLF + + "Depth: 0" + CRLF + + "Destination: /dst" + CRLF + + "Connection: Close" + CRLF + + CRLF + }); + // @formatter:on + client.connect(); + client.processRequest(true); + Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, client.getStatusCode()); + Assert.assertTrue(new File(tempWebapp, "src/a.txt").isFile()); + Assert.assertTrue(new File(sub, "b.txt").isFile()); + Assert.assertFalse(new File(tempWebapp, "dst").exists()); + } + + + @Test + public void testCopyInvalidDepthKeepsDestination() throws Exception { + File tempWebapp = new File(getTemporaryDirectory(), "webdav-copy-depth"); + Assert.assertTrue(tempWebapp.mkdirs()); + writeFile(new File(tempWebapp, "file1.txt")); + File destination = new File(tempWebapp, "file2.txt"); + writeFile(destination); + + Client client = startWritableWebdav(tempWebapp); + + // @formatter:off + client.setRequest(new String[] { + "COPY /file1.txt HTTP/1.1" + CRLF + + "Host: localhost:" + getPort() + CRLF + + "Depth: 1" + CRLF + + "Overwrite: T" + CRLF + + "Destination: /file2.txt" + CRLF + + "Connection: Close" + CRLF + + CRLF + }); + // @formatter:on + client.connect(); + client.processRequest(true); + Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, client.getStatusCode()); + Assert.assertTrue(destination.isFile()); + } + + + private Client startWritableWebdav(File docBase) throws Exception { + Tomcat tomcat = getTomcatInstance(); + Context ctxt = tomcat.addContext("", docBase.getAbsolutePath()); + Wrapper webdavServlet = Tomcat.addServlet(ctxt, "webdav", new WebdavServlet()); + webdavServlet.addInitParameter("readonly", "false"); + ctxt.addServletMapping("/*", "webdav"); + tomcat.start(); + + Client client = new Client(); + client.setPort(getPort()); + return client; + } + + + private static void writeFile(File file) throws IOException { + try (FileWriter fw = new FileWriter(file)) { + fw.write(CONTENT); + } + } + + private void validateXml(String xmlContent) throws Exception { SAXParserFactory.newInstance().newSAXParser().getXMLReader().parse(new InputSource(new StringReader(xmlContent))); }