Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions java/org/apache/catalina/servlets/WebdavServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@
return;
}

if (isLocked(path, req)) {
if (isLocked(path, req, false)) {
resp.sendError(WebdavStatus.SC_LOCKED);
return;
}
Expand Down Expand Up @@ -1291,7 +1291,7 @@
return;
}

if (isLocked(path, req)) {
if (isLocked(path, req, true)) {
resp.sendError(WebdavStatus.SC_LOCKED);
return;
}
Expand Down Expand Up @@ -1340,7 +1340,7 @@
return;
}

if (isLocked(path, req)) {
if (isLocked(path, req, !resource.exists())) {
resp.sendError(WebdavStatus.SC_LOCKED);
return;
}
Expand Down Expand Up @@ -1393,7 +1393,7 @@

String path = getRelativePath(req);

if (isLocked(path, req)) {
if (isLocked(path, req, true)) {
resp.sendError(WebdavStatus.SC_LOCKED);
return;
}
Expand Down Expand Up @@ -2020,13 +2020,15 @@
* Check to see if a resource is currently write locked. The method will look at the "If" header to make sure the
* client has demonstrated knowledge of the appropriate lock tokens.
*
* @param path The relative path
* @param req Servlet request
* @param path The relative path
* @param req Servlet request
* @param memberChange {@code true} if the request adds or removes the resource as an internal member of its parent
* collection
*
* @return <code>true</code> if the resource is locked (and no appropriate lock token has been found for at least
* one of the non-shared locks which are present on the resource).
*/
private boolean isLocked(String path, HttpServletRequest req) {
private boolean isLocked(String path, HttpServletRequest req, boolean memberChange) {

if (path == null) {
path = getRelativePath(req);
Expand All @@ -2037,33 +2039,37 @@
ifHeader = "";
}

return isLocked(path, req.getRemoteUser(), ifHeader);
return isLocked(path, req.getRemoteUser(), ifHeader, memberChange);
}


/**
* Check to see if a resource is currently write locked.
*
* @param path Path of the resource
* @param principal The authenticated principal name
* @param ifHeader "If" HTTP header which was included in the request
* @param path Path of the resource
* @param principal The authenticated principal name
* @param ifHeader "If" HTTP header which was included in the request
* @param memberChange {@code true} if the request adds or removes the resource as an internal member of its parent
* collection
*
* @return <code>true</code> if the resource is locked (and no appropriate lock token has been found for at least
* one of the non-shared locks which are present on the resource).
*/
private boolean isLocked(String path, String principal, String ifHeader) {
private boolean isLocked(String path, String principal, String ifHeader, boolean memberChange) {

boolean unmatchedSharedLock = false;
// Check if the resource or a parent is already locked
String parentPath = path;
// RFC 4918, section 7.4: a write lock on a collection, including a depth 0 lock, protects its internal members
boolean directParent = false;
do {
LockInfo parentLock = resourceLocks.get(parentPath);
if (parentLock != null) {
if (parentLock.hasExpired()) {
resourceLocks.remove(parentPath);
} else {
// parentPath == path is a check for the first loop
if (parentPath == path || parentLock.depth > 0) {
if (parentPath == path || parentLock.depth > 0 || directParent) {
if (parentLock.isExclusive()) {
return !ifHeader.contains(":" + parentLock.token + ">") ||
(parentLock.principal != null && !parentLock.principal.equals(principal));
Expand All @@ -2072,7 +2078,7 @@
LockInfo lock = sharedLocks.get(token);
if (lock != null) {
// The shared lock might not have the same depth
if (parentPath == path || lock.depth > 0) {
if (parentPath == path || lock.depth > 0 || directParent) {
if (ifHeader.contains(":" + token + ">") &&
(lock.principal == null || lock.principal.equals(principal))) {
return false;
Expand All @@ -2091,6 +2097,7 @@
if (slash < 0) {
break;
}
directParent = memberChange && parentPath == path;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional: parentPath is the same instance as path only on the first iteration, same idiom as the existing checks.

parentPath = parentPath.substring(0, slash);
} while (true);

Expand Down Expand Up @@ -2213,7 +2220,7 @@
}

// Check if destination is locked
if (isLocked(destinationPath, req)) {
if (isLocked(destinationPath, req, true)) {
resp.sendError(WebdavStatus.SC_LOCKED);
return false;
}
Expand Down Expand Up @@ -2381,7 +2388,7 @@
private boolean deleteResource(String path, HttpServletRequest req, HttpServletResponse resp, boolean setStatus)
throws IOException {

if (isLocked(path, req)) {
if (isLocked(path, req, true)) {
resp.sendError(WebdavStatus.SC_LOCKED);
return false;
}
Expand Down Expand Up @@ -2469,7 +2476,7 @@
}
childName += entry;

if (isLocked(childName, req.getRemoteUser(), ifHeader)) {
if (isLocked(childName, req.getRemoteUser(), ifHeader, false)) {

errorList.put(childName, Integer.valueOf(WebdavStatus.SC_LOCKED));

Expand Down
90 changes: 90 additions & 0 deletions test/org/apache/catalina/servlets/TestWebdavServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,96 @@ public void testIfHeader() throws Exception {
* list). With strict If processing (the default) such a header must fail the precondition check. Without strict
* If processing the header is ignored, as for any other invalid If header syntax.
*/
@Test
public void testDepthZeroCollectionLock() throws Exception {
Tomcat tomcat = getTomcatInstance();

// Create a temp webapp that can be safely written to
File tempWebapp = new File(getTemporaryDirectory(), "webdav-depth0");
tempWebapp.deleteOnExit();
File folder = new File(tempWebapp, "/locked");
Assert.assertTrue(folder.mkdirs());
try (FileOutputStream fos = new FileOutputStream(new File(folder, "member.txt"))) {
fos.write(CONTENT.getBytes());
}
try (FileOutputStream fos = new FileOutputStream(new File(tempWebapp, "outside.txt"))) {
fos.write(CONTENT.getBytes());
}
Context ctxt = tomcat.addContext("", tempWebapp.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());

String lockToken = null;
// @formatter:off
client.setRequest(new String[] {
"LOCK /locked HTTP/1.1" + CRLF +
"Host: localhost:" + getPort() + CRLF +
"Depth: 0" + CRLF +
"Content-Length: " + LOCK_BODY.length() + CRLF +
"Connection: Close" + CRLF +
CRLF +
LOCK_BODY
});
// @formatter:on
client.connect();
client.processRequest(true);
Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
for (String header : client.getResponseHeaders()) {
if (header.startsWith("Lock-Token: ")) {
lockToken = header.substring("Lock-Token: ".length());
}
}
Assert.assertNotNull(lockToken);

// RFC 4918, section 7.4: the lock protects the internal member URLs of the collection
String destination = "Destination: http://localhost:" + getPort();
Assert.assertEquals(WebdavStatus.SC_LOCKED, doRequest(client, "PUT /locked/new.txt", null, CONTENT));
Assert.assertEquals(WebdavStatus.SC_LOCKED, doRequest(client, "MKCOL /locked/new", null, null));
Assert.assertEquals(WebdavStatus.SC_LOCKED, doRequest(client, "DELETE /locked/member.txt", null, null));
Assert.assertEquals(WebdavStatus.SC_LOCKED,
doRequest(client, "MOVE /locked/member.txt", destination + "/moved.txt", null));
Assert.assertEquals(WebdavStatus.SC_LOCKED,
doRequest(client, "MOVE /locked/member.txt", destination + "/locked/renamed.txt", null));
Assert.assertEquals(WebdavStatus.SC_LOCKED,
doRequest(client, "MOVE /outside.txt", destination + "/locked/moved.txt", null));
Assert.assertEquals(WebdavStatus.SC_LOCKED,
doRequest(client, "COPY /outside.txt", destination + "/locked/copy.txt", null));
Assert.assertTrue(new File(folder, "member.txt").isFile());
Assert.assertEquals(1, folder.list().length);

// A depth 0 lock does not protect the content of existing members
Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT,
doRequest(client, "PUT /locked/member.txt", null, CONTENT));

// The lock token of the collection allows new members
Assert.assertEquals(HttpServletResponse.SC_CREATED, doRequest(client, "PUT /locked/new.txt",
"If: <http://localhost:" + getPort() + "/locked> (" + lockToken + ")", CONTENT));
}


private int doRequest(Client client, String requestLine, String header, String body) throws Exception {
// @formatter:off
client.setRequest(new String[] {
requestLine + " HTTP/1.1" + CRLF +
"Host: localhost:" + getPort() + CRLF +
(header == null ? "" : header + CRLF) +
"Content-Length: " + (body == null ? 0 : body.length()) + CRLF +
"Connection: Close" + CRLF +
CRLF +
(body == null ? "" : body)
});
// @formatter:on
client.connect();
client.processRequest(true);
return client.getStatusCode();
}


@Test
public void testIfHeaderEmptyListStrict() throws Exception {
Client client = doTestIfHeaderEmptyList("webdav-emptyif-strict", true);
Expand Down
Loading