Skip to content
Merged
2 changes: 2 additions & 0 deletions changes-entries/dav-if-not-locktoken.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*) mod_dav: Do not accept a lock token given under "Not" in the If
header as submitting that token for a locked resource. [Joe Orton]
2 changes: 2 additions & 0 deletions changes-entries/dav-options-trace.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*) mod_dav: Do not list TRACE in the Allow field of an OPTIONS response when
TraceEnable is off. PR 70264
2 changes: 2 additions & 0 deletions changes-entries/dav-propfind-quoting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*) mod_dav: Escape the getcontenttype and getcontentlanguage
properties in PROPFIND responses. [Joe Orton]
22 changes: 17 additions & 5 deletions modules/dav/fs/repos.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ static dav_error * dav_fs_copymove_file(

if ((status = apr_file_open(&inf, src, APR_READ | APR_BINARY,
APR_OS_DEFAULT, p)) != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_ERR, status, p, APLOGNO(10629)
"Could not open file %s for reading.", src);
/* ### use something besides 500? */
return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0, status,
"Could not open file for reading");
Expand All @@ -375,6 +377,8 @@ static dav_error * dav_fs_copymove_file(
if (status != APR_SUCCESS) {
apr_file_close(inf);

ap_log_perror(APLOG_MARK, APLOG_ERR, status, p, APLOGNO(10630)
"Could not open file %s for writing.", dst);
return dav_new_error(p, MAP_IO2HTTP(status), 0, status,
"Could not open file for writing");
}
Expand Down Expand Up @@ -569,8 +573,13 @@ static dav_error *dav_fs_copymoveset(int is_move, apr_pool_t *p,

/* Get directory and filename for resources */
/* ### should test these result values... */
(void) dav_fs_dir_file_name(src, &src_dir, &src_file);
(void) dav_fs_dir_file_name(dst, &dst_dir, &dst_file);
err = dav_fs_dir_file_name(src, &src_dir, &src_file);
if (err != NULL)
return err;

err = dav_fs_dir_file_name(dst, &dst_dir, &dst_file);
if (err != NULL)
return err;

/* Get the corresponding state files for each resource */
dav_dbm_get_statefiles(p, src_file, &src_state1, &src_state2);
Expand Down Expand Up @@ -618,11 +627,14 @@ static dav_error *dav_fs_deleteset(apr_pool_t *p, const dav_resource *resource)
const char *state1;
const char *state2;
const char *pathname;
dav_error *err;
apr_status_t status;

/* Get directory, filename, and state-file names for the resource */
/* ### should test this result value... */
(void) dav_fs_dir_file_name(resource, &dirpath, &fname);
err = dav_fs_dir_file_name(resource, &dirpath, &fname);
if (err != NULL)
return err;
dav_dbm_get_statefiles(p, fname, &state1, &state2);

/* build the propset pathname for the file */
Expand Down Expand Up @@ -797,10 +809,10 @@ static dav_error * dav_fs_get_resource(
{
/*
** The base of the path refers to a file -- nothing should
** be in path_info. The resource is simply an error: it
** be in path_info. The resource cannot exist: it
** can't be a null or a locknull resource.
*/
return dav_new_error(r->pool, HTTP_BAD_REQUEST, 0, 0,
return dav_new_error(r->pool, HTTP_NOT_FOUND, 0, 0,
"The URL contains extraneous path "
"components. The resource could not "
"be identified.");
Expand Down
6 changes: 5 additions & 1 deletion modules/dav/main/mod_dav.c
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,8 @@ static int dav_method_search(request_rec *r)
/* handle the OPTIONS method */
static int dav_method_options(request_rec *r)
{
core_server_config *conf =
ap_get_core_module_config(r->server->module_config);
const dav_hooks_locks *locks_hooks = DAV_GET_HOOKS_LOCKS(r);
const dav_hooks_vsn *vsn_hooks = DAV_GET_HOOKS_VSN(r);
const dav_hooks_binding *binding_hooks = DAV_GET_HOOKS_BINDING(r);
Expand Down Expand Up @@ -1829,7 +1831,9 @@ static int dav_method_options(request_rec *r)
apr_table_addn(methods, "HEAD", "");
apr_table_addn(methods, "POST", "");
apr_table_addn(methods, "DELETE", "");
apr_table_addn(methods, "TRACE", "");
if (conf->trace_enable != AP_TRACE_DISABLE) {
apr_table_addn(methods, "TRACE", "");
}
apr_table_addn(methods, "PROPFIND", "");
apr_table_addn(methods, "PROPPATCH", "");
apr_table_addn(methods, "COPY", "");
Expand Down
5 changes: 3 additions & 2 deletions modules/dav/main/props.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ static dav_error * dav_insert_coreprop(dav_propdb *propdb,
dav_do_prop_subreq(propdb);
}
if (propdb->subreq->content_type != NULL) {
value = propdb->subreq->content_type;
value = apr_xml_quote_string(propdb->p,
propdb->subreq->content_type, 0);
}
break;

Expand All @@ -412,7 +413,7 @@ static dav_error * dav_insert_coreprop(dav_propdb *propdb,
}
if ((lang = apr_table_get(propdb->subreq->headers_out,
"Content-Language")) != NULL) {
value = lang;
value = apr_xml_quote_string(propdb->p, lang, 0);
}
break;
}
Expand Down
29 changes: 22 additions & 7 deletions modules/dav/main/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,18 @@ static dav_error * dav_process_if_header(request_rec *r, dav_if_header **p_ih)
return NULL;
}

static int dav_find_submitted_locktoken(const dav_if_header *if_header,
/*
** If an authenticated user created a lock, only that user may submit its
** locktoken to manipulate a resource.
*/
static int dav_lock_owned_by_other_user(const request_rec *r,
const dav_lock *lock)
{
return lock->auth_user && (!r->user || strcmp(lock->auth_user, r->user));
}

static int dav_find_submitted_locktoken(const request_rec *r,
const dav_if_header *if_header,
const dav_lock *lock_list,
const dav_hooks_locks *locks_hooks)
{
Expand All @@ -904,7 +915,9 @@ static int dav_find_submitted_locktoken(const dav_if_header *if_header,
state_list != NULL;
state_list = state_list->next) {

if (state_list->type == dav_if_opaquelock) {
/* A locktoken given under "Not" has not been submitted. */
if (state_list->type == dav_if_opaquelock
&& state_list->condition == DAV_IF_COND_NORMAL) {
const dav_lock *lock;

/* given state_list->locktoken, match it */
Expand All @@ -924,6 +937,10 @@ static int dav_find_submitted_locktoken(const dav_if_header *if_header,
*/
for (lock = lock_list; lock != NULL; lock = lock->next) {

if (dav_lock_owned_by_other_user(r, lock)) {
continue;
}

if (!(*locks_hooks->compare_locktoken)(state_list->locktoken, lock->locktoken)) {
return 1;
}
Expand Down Expand Up @@ -1313,9 +1330,7 @@ static dav_error * dav_validate_resource_state(apr_pool_t *p,
** the lock, only the same user may submit that locktoken
** to manipulate a resource.
*/
if (lock->auth_user &&
(!r->user ||
strcmp(lock->auth_user, r->user))) {
if (dav_lock_owned_by_other_user(r, lock)) {
const char *errmsg;

errmsg = apr_pstrcat(p, "User \"",
Expand Down Expand Up @@ -1433,7 +1448,7 @@ static dav_error * dav_validate_resource_state(apr_pool_t *p,
** Note that seen_locktoken == 0 implies lock_list != NULL
** which implies locks_hooks != NULL.
*/
if (dav_find_submitted_locktoken(if_header, lock_list,
if (dav_find_submitted_locktoken(r, if_header, lock_list,
locks_hooks)) {
/*
** We found a match! We're set... none of the If: header
Expand Down Expand Up @@ -1493,7 +1508,7 @@ static dav_error * dav_validate_resource_state(apr_pool_t *p,
** Note that seen_locktoken == 0 implies lock_list != NULL which implies
** locks_hooks != NULL.
*/
if (dav_find_submitted_locktoken(if_header, lock_list, locks_hooks)) {
if (dav_find_submitted_locktoken(r, if_header, lock_list, locks_hooks)) {
/*
** We found a match! We're set... we have a matching state list,
** and the If: header specified the locktoken somewhere. We're done.
Expand Down
2 changes: 1 addition & 1 deletion test/modules/dav/test_002_options_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _class_scope(self, env):

# TRACE is not offered while the server refuses it
@pytest.mark.parametrize("path", ["/dav/file.txt", "/dav/col/"])
@not_backported("r1939146", "DAV OPTIONS Allow honouring TraceEnable")
@not_backported("r1939146", "DAV OPTIONS Allow honouring TraceEnable", backported_in="2.4.72")
def test_dav_002_001(self, env, path):
status, allow = _request(env, "OPTIONS", path)
assert status == 200
Expand Down
98 changes: 98 additions & 0 deletions test/modules/dav/test_006_if_not_locktoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import glob
import os

import pytest

from pyhttpd.conf import HttpdConf


LOCK_BODY = ('<?xml version="1.0" encoding="utf-8"?>'
'<D:lockinfo xmlns:D="DAV:">'
'<D:lockscope><D:exclusive/></D:lockscope>'
'<D:locktype><D:write/></D:locktype>'
'<D:owner><D:href>test-owner</D:href></D:owner>'
'</D:lockinfo>')

BOGUS_TOKEN = '<opaquelocktoken:00000000-0000-0000-0000-000000000000>'


class TestDavIfNotLocktoken:
"""A locktoken given under "Not" in the If: header is an assertion
that the token is not held, and must not count as submitting the
token for a locked resource."""

@pytest.fixture(autouse=True, scope='class')
@classmethod
def _class_scope(cls, env):
dav_dir = os.path.join(env.gen_dir, 'dav-ifnot')
os.makedirs(dav_dir, exist_ok=True)
for f in glob.glob(os.path.join(env.gen_dir, 'davlock-ifnot*')):
os.remove(f)
dav_dir_conf = dav_dir.replace('\\', '/')
lock_db_conf = os.path.join(env.gen_dir,
'davlock-ifnot').replace('\\', '/')
conf = HttpdConf(env, extras={
'base': f"""
DavLockDB "{lock_db_conf}"
""",
f"test1.{env.http_tld}": f"""
Alias /dav-ifnot "{dav_dir_conf}"
<Directory "{dav_dir_conf}">
Dav On
Require all granted
</Directory>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0

def _url(self, env, name):
return env.mkurl("http", "test1", f"/dav-ifnot/{name}")

def _locked(self, env, name):
"""Create and lock a resource, returning its URL and the
locktoken in Coded-URL form."""
with open(os.path.join(env.gen_dir, 'dav-ifnot', name), 'w') as fd:
fd.write('locked')
url = self._url(env, name)
r = env.curl_raw(url, options=[
'-X', 'LOCK',
'-H', 'Content-Type: text/xml',
'--data-binary', LOCK_BODY])
assert r.response["status"] == 200
return url, r.response["header"]["lock-token"]

def _put(self, env, url, if_header):
return env.curl_raw(url, options=[
'-X', 'PUT',
'-H', f'If: {if_header}',
'--data-binary', 'changed'])

# "Not <token>" in a tagged list for another resource
def test_dav_006_01_not_token_other_resource(self, env):
url, token = self._locked(env, 'one.txt')
other = self._url(env, 'other.txt')
r = self._put(env, url, f'<{other}> (Not {token})')
assert r.response["status"] == 423
env.httpd_error_log.ignore_recent(matches=[r'.*dav:error.*'])

# As above, where a tagged list for the resource itself also matches
def test_dav_006_02_not_token_matching_list(self, env):
url, token = self._locked(env, 'two.txt')
other = self._url(env, 'other.txt')
r = self._put(env, url,
f'<{url}> (Not {BOGUS_TOKEN}) <{other}> (Not {token})')
assert r.response["status"] == 423
env.httpd_error_log.ignore_recent(matches=[r'.*dav:error.*'])

# The token is still accepted when submitted without "Not",
# whichever resource the list is tagged for.
@pytest.mark.parametrize("tag", ['three.txt', 'other.txt'])
def test_dav_006_03_token_submitted(self, env, tag):
url, token = self._locked(env, 'three.txt')
r = self._put(env, url, f'<{self._url(env, tag)}> ({token})')
assert r.response["status"] == 204
r = env.curl_raw(url, options=['-X', 'UNLOCK',
'-H', f'Lock-Token: {token}'])
assert r.response["status"] == 204
61 changes: 61 additions & 0 deletions test/modules/dav/test_007_propfind_quoting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import xml.etree.ElementTree as ET

import pytest

from pyhttpd.conf import HttpdConf


CONTENT_TYPE = 'text/x-test; note="a&b<c>"'

PROPFIND_BODY = ('<?xml version="1.0" encoding="utf-8"?>'
'<D:propfind xmlns:D="DAV:">'
'<D:prop><D:getcontenttype/></D:prop>'
'</D:propfind>')


class TestDavPropfindQuoting:
"""DAV:getcontenttype must be XML-quoted in a PROPFIND response."""

@pytest.fixture(autouse=True, scope='class')
@classmethod
def _class_scope(cls, env):
dav_dir = os.path.join(env.gen_dir, 'dav-quote')
os.makedirs(dav_dir, exist_ok=True)
with open(os.path.join(dav_dir, 'hello.txt'), 'w') as fd:
fd.write('hello')
dav_dir_conf = dav_dir.replace('\\', '/')
lock_db_conf = os.path.join(env.gen_dir,
'davlock-quote').replace('\\', '/')
conf = HttpdConf(env, extras={
'base': f"""
DavLockDB "{lock_db_conf}"
""",
f"test1.{env.http_tld}": f"""
Alias /dav-quote "{dav_dir_conf}"
<Directory "{dav_dir_conf}">
Dav On
Require all granted
ForceType '{CONTENT_TYPE}'
</Directory>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0

@pytest.mark.parametrize("body", [PROPFIND_BODY, None])
def test_dav_007_01_getcontenttype(self, env, body):
url = env.mkurl("http", "test1", "/dav-quote/hello.txt")
options = ['-X', 'PROPFIND', '-H', 'Depth: 0']
if body:
options += ['-H', 'Content-Type: text/xml',
'--data-binary', body]
r = env.curl_raw(url, options=options)
assert r.response["status"] == 207
# fails to parse if the property value is not quoted
root = ET.fromstring(r.response["body"])
prop = root.find('.//{DAV:}getcontenttype')
assert prop is not None
assert prop.text == CONTENT_TYPE
assert len(prop) == 0
Loading