Skip to content

Commit aed0082

Browse files
committed
Protect the members of a collection with a depth 0 WebDAV lock
A depth 0 write lock on a collection did not prevent other clients from adding or removing its internal members. Without the lock token, PUT and MKCOL could create new members, and DELETE, MOVE and COPY could remove, rename or add members of the locked collection. RFC 4918, section 7.4 states that a collection write lock, whether depth 0 or depth infinity, protects any request that would create a new resource in the collection, remove an internal member URL or change the segment name of an internal member. The lock of the direct parent collection is now checked for these requests whatever its depth.
1 parent a8fcbe5 commit aed0082

2 files changed

Lines changed: 114 additions & 17 deletions

File tree

‎java/org/apache/catalina/servlets/WebdavServlet.java‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ protected void doProppatch(HttpServletRequest req, HttpServletResponse resp) thr
11091109
return;
11101110
}
11111111

1112-
if (isLocked(path, req)) {
1112+
if (isLocked(path, req, false)) {
11131113
resp.sendError(WebdavStatus.SC_LOCKED);
11141114
return;
11151115
}
@@ -1291,7 +1291,7 @@ protected void doMkcol(HttpServletRequest req, HttpServletResponse resp) throws
12911291
return;
12921292
}
12931293

1294-
if (isLocked(path, req)) {
1294+
if (isLocked(path, req, true)) {
12951295
resp.sendError(WebdavStatus.SC_LOCKED);
12961296
return;
12971297
}
@@ -1340,7 +1340,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
13401340
return;
13411341
}
13421342

1343-
if (isLocked(path, req)) {
1343+
if (isLocked(path, req, !resource.exists())) {
13441344
resp.sendError(WebdavStatus.SC_LOCKED);
13451345
return;
13461346
}
@@ -1393,7 +1393,7 @@ protected void doMove(HttpServletRequest req, HttpServletResponse resp) throws I
13931393

13941394
String path = getRelativePath(req);
13951395

1396-
if (isLocked(path, req)) {
1396+
if (isLocked(path, req, true)) {
13971397
resp.sendError(WebdavStatus.SC_LOCKED);
13981398
return;
13991399
}
@@ -2020,13 +2020,15 @@ private String getPathFromHref(String href, HttpServletRequest req) {
20202020
* Check to see if a resource is currently write locked. The method will look at the "If" header to make sure the
20212021
* client has demonstrated knowledge of the appropriate lock tokens.
20222022
*
2023-
* @param path The relative path
2024-
* @param req Servlet request
2023+
* @param path The relative path
2024+
* @param req Servlet request
2025+
* @param memberChange {@code true} if the request adds or removes the resource as an internal member of its parent
2026+
* collection
20252027
*
20262028
* @return <code>true</code> if the resource is locked (and no appropriate lock token has been found for at least
20272029
* one of the non-shared locks which are present on the resource).
20282030
*/
2029-
private boolean isLocked(String path, HttpServletRequest req) {
2031+
private boolean isLocked(String path, HttpServletRequest req, boolean memberChange) {
20302032

20312033
if (path == null) {
20322034
path = getRelativePath(req);
@@ -2037,33 +2039,37 @@ private boolean isLocked(String path, HttpServletRequest req) {
20372039
ifHeader = "";
20382040
}
20392041

2040-
return isLocked(path, req.getRemoteUser(), ifHeader);
2042+
return isLocked(path, req.getRemoteUser(), ifHeader, memberChange);
20412043
}
20422044

20432045

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

20562060
boolean unmatchedSharedLock = false;
20572061
// Check if the resource or a parent is already locked
20582062
String parentPath = path;
2063+
// RFC 4918, section 7.4: a write lock on a collection, including a depth 0 lock, protects its internal members
2064+
boolean directParent = false;
20592065
do {
20602066
LockInfo parentLock = resourceLocks.get(parentPath);
20612067
if (parentLock != null) {
20622068
if (parentLock.hasExpired()) {
20632069
resourceLocks.remove(parentPath);
20642070
} else {
20652071
// parentPath == path is a check for the first loop
2066-
if (parentPath == path || parentLock.depth > 0) {
2072+
if (parentPath == path || parentLock.depth > 0 || directParent) {
20672073
if (parentLock.isExclusive()) {
20682074
return !ifHeader.contains(":" + parentLock.token + ">") ||
20692075
(parentLock.principal != null && !parentLock.principal.equals(principal));
@@ -2072,7 +2078,7 @@ private boolean isLocked(String path, String principal, String ifHeader) {
20722078
LockInfo lock = sharedLocks.get(token);
20732079
if (lock != null) {
20742080
// The shared lock might not have the same depth
2075-
if (parentPath == path || lock.depth > 0) {
2081+
if (parentPath == path || lock.depth > 0 || directParent) {
20762082
if (ifHeader.contains(":" + token + ">") &&
20772083
(lock.principal == null || lock.principal.equals(principal))) {
20782084
return false;
@@ -2091,6 +2097,7 @@ private boolean isLocked(String path, String principal, String ifHeader) {
20912097
if (slash < 0) {
20922098
break;
20932099
}
2100+
directParent = memberChange && parentPath == path;
20942101
parentPath = parentPath.substring(0, slash);
20952102
} while (true);
20962103

@@ -2213,7 +2220,7 @@ private boolean copyResource(String path, HttpServletRequest req, HttpServletRes
22132220
}
22142221

22152222
// Check if destination is locked
2216-
if (isLocked(destinationPath, req)) {
2223+
if (isLocked(destinationPath, req, true)) {
22172224
resp.sendError(WebdavStatus.SC_LOCKED);
22182225
return false;
22192226
}
@@ -2381,7 +2388,7 @@ private boolean copyResource(Map<String,Integer> errorList, String source, Strin
23812388
private boolean deleteResource(String path, HttpServletRequest req, HttpServletResponse resp, boolean setStatus)
23822389
throws IOException {
23832390

2384-
if (isLocked(path, req)) {
2391+
if (isLocked(path, req, true)) {
23852392
resp.sendError(WebdavStatus.SC_LOCKED);
23862393
return false;
23872394
}
@@ -2469,7 +2476,7 @@ private void deleteCollection(HttpServletRequest req, String path, Map<String,In
24692476
}
24702477
childName += entry;
24712478

2472-
if (isLocked(childName, req.getRemoteUser(), ifHeader)) {
2479+
if (isLocked(childName, req.getRemoteUser(), ifHeader, false)) {
24732480

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

‎test/org/apache/catalina/servlets/TestWebdavServlet.java‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,96 @@ public void testIfHeader() throws Exception {
15431543
* list). With strict If processing (the default) such a header must fail the precondition check. Without strict
15441544
* If processing the header is ignored, as for any other invalid If header syntax.
15451545
*/
1546+
@Test
1547+
public void testDepthZeroCollectionLock() throws Exception {
1548+
Tomcat tomcat = getTomcatInstance();
1549+
1550+
// Create a temp webapp that can be safely written to
1551+
File tempWebapp = new File(getTemporaryDirectory(), "webdav-depth0");
1552+
tempWebapp.deleteOnExit();
1553+
File folder = new File(tempWebapp, "/locked");
1554+
Assert.assertTrue(folder.mkdirs());
1555+
try (FileOutputStream fos = new FileOutputStream(new File(folder, "member.txt"))) {
1556+
fos.write(CONTENT.getBytes());
1557+
}
1558+
try (FileOutputStream fos = new FileOutputStream(new File(tempWebapp, "outside.txt"))) {
1559+
fos.write(CONTENT.getBytes());
1560+
}
1561+
Context ctxt = tomcat.addContext("", tempWebapp.getAbsolutePath());
1562+
Wrapper webdavServlet = Tomcat.addServlet(ctxt, "webdav", new WebdavServlet());
1563+
webdavServlet.addInitParameter("readonly", "false");
1564+
ctxt.addServletMapping("/*", "webdav");
1565+
tomcat.start();
1566+
1567+
Client client = new Client();
1568+
client.setPort(getPort());
1569+
1570+
String lockToken = null;
1571+
// @formatter:off
1572+
client.setRequest(new String[] {
1573+
"LOCK /locked HTTP/1.1" + CRLF +
1574+
"Host: localhost:" + getPort() + CRLF +
1575+
"Depth: 0" + CRLF +
1576+
"Content-Length: " + LOCK_BODY.length() + CRLF +
1577+
"Connection: Close" + CRLF +
1578+
CRLF +
1579+
LOCK_BODY
1580+
});
1581+
// @formatter:on
1582+
client.connect();
1583+
client.processRequest(true);
1584+
Assert.assertEquals(HttpServletResponse.SC_OK, client.getStatusCode());
1585+
for (String header : client.getResponseHeaders()) {
1586+
if (header.startsWith("Lock-Token: ")) {
1587+
lockToken = header.substring("Lock-Token: ".length());
1588+
}
1589+
}
1590+
Assert.assertNotNull(lockToken);
1591+
1592+
// RFC 4918, section 7.4: the lock protects the internal member URLs of the collection
1593+
String destination = "Destination: http://localhost:" + getPort();
1594+
Assert.assertEquals(WebdavStatus.SC_LOCKED, doRequest(client, "PUT /locked/new.txt", null, CONTENT));
1595+
Assert.assertEquals(WebdavStatus.SC_LOCKED, doRequest(client, "MKCOL /locked/new", null, null));
1596+
Assert.assertEquals(WebdavStatus.SC_LOCKED, doRequest(client, "DELETE /locked/member.txt", null, null));
1597+
Assert.assertEquals(WebdavStatus.SC_LOCKED,
1598+
doRequest(client, "MOVE /locked/member.txt", destination + "/moved.txt", null));
1599+
Assert.assertEquals(WebdavStatus.SC_LOCKED,
1600+
doRequest(client, "MOVE /locked/member.txt", destination + "/locked/renamed.txt", null));
1601+
Assert.assertEquals(WebdavStatus.SC_LOCKED,
1602+
doRequest(client, "MOVE /outside.txt", destination + "/locked/moved.txt", null));
1603+
Assert.assertEquals(WebdavStatus.SC_LOCKED,
1604+
doRequest(client, "COPY /outside.txt", destination + "/locked/copy.txt", null));
1605+
Assert.assertTrue(new File(folder, "member.txt").isFile());
1606+
Assert.assertEquals(1, folder.list().length);
1607+
1608+
// A depth 0 lock does not protect the content of existing members
1609+
Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT,
1610+
doRequest(client, "PUT /locked/member.txt", null, CONTENT));
1611+
1612+
// The lock token of the collection allows new members
1613+
Assert.assertEquals(HttpServletResponse.SC_CREATED, doRequest(client, "PUT /locked/new.txt",
1614+
"If: <http://localhost:" + getPort() + "/locked> (" + lockToken + ")", CONTENT));
1615+
}
1616+
1617+
1618+
private int doRequest(Client client, String requestLine, String header, String body) throws Exception {
1619+
// @formatter:off
1620+
client.setRequest(new String[] {
1621+
requestLine + " HTTP/1.1" + CRLF +
1622+
"Host: localhost:" + getPort() + CRLF +
1623+
(header == null ? "" : header + CRLF) +
1624+
"Content-Length: " + (body == null ? 0 : body.length()) + CRLF +
1625+
"Connection: Close" + CRLF +
1626+
CRLF +
1627+
(body == null ? "" : body)
1628+
});
1629+
// @formatter:on
1630+
client.connect();
1631+
client.processRequest(true);
1632+
return client.getStatusCode();
1633+
}
1634+
1635+
15461636
@Test
15471637
public void testIfHeaderEmptyListStrict() throws Exception {
15481638
Client client = doTestIfHeaderEmptyList("webdav-emptyif-strict", true);

0 commit comments

Comments
 (0)