diff --git a/changes-entries/dav-if-not-locktoken.txt b/changes-entries/dav-if-not-locktoken.txt new file mode 100644 index 00000000000..3ef286ca3d2 --- /dev/null +++ b/changes-entries/dav-if-not-locktoken.txt @@ -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] diff --git a/changes-entries/dav-options-trace.txt b/changes-entries/dav-options-trace.txt new file mode 100644 index 00000000000..a7d10a354a0 --- /dev/null +++ b/changes-entries/dav-options-trace.txt @@ -0,0 +1,2 @@ + *) mod_dav: Do not list TRACE in the Allow field of an OPTIONS response when + TraceEnable is off. PR 70264 diff --git a/changes-entries/dav-propfind-quoting.txt b/changes-entries/dav-propfind-quoting.txt new file mode 100644 index 00000000000..d2ad83f19ae --- /dev/null +++ b/changes-entries/dav-propfind-quoting.txt @@ -0,0 +1,2 @@ + *) mod_dav: Escape the getcontenttype and getcontentlanguage + properties in PROPFIND responses. [Joe Orton] diff --git a/modules/dav/fs/repos.c b/modules/dav/fs/repos.c index 832dca9e34e..6fd6834e7c0 100644 --- a/modules/dav/fs/repos.c +++ b/modules/dav/fs/repos.c @@ -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"); @@ -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"); } @@ -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); @@ -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 */ @@ -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."); diff --git a/modules/dav/main/mod_dav.c b/modules/dav/main/mod_dav.c index 6aa0cbe8861..b74f9da042c 100644 --- a/modules/dav/main/mod_dav.c +++ b/modules/dav/main/mod_dav.c @@ -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); @@ -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", ""); diff --git a/modules/dav/main/props.c b/modules/dav/main/props.c index b5827505415..34ac544df37 100644 --- a/modules/dav/main/props.c +++ b/modules/dav/main/props.c @@ -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; @@ -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; } diff --git a/modules/dav/main/util.c b/modules/dav/main/util.c index c82e88e74bc..a60a48d58e4 100644 --- a/modules/dav/main/util.c +++ b/modules/dav/main/util.c @@ -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) { @@ -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 */ @@ -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; } @@ -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 \"", @@ -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 @@ -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. diff --git a/test/modules/dav/test_002_options_trace.py b/test/modules/dav/test_002_options_trace.py index 1d22133b08b..616fa49d6c9 100644 --- a/test/modules/dav/test_002_options_trace.py +++ b/test/modules/dav/test_002_options_trace.py @@ -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 diff --git a/test/modules/dav/test_006_if_not_locktoken.py b/test/modules/dav/test_006_if_not_locktoken.py new file mode 100644 index 00000000000..d63d2c76a1a --- /dev/null +++ b/test/modules/dav/test_006_if_not_locktoken.py @@ -0,0 +1,98 @@ +import glob +import os + +import pytest + +from pyhttpd.conf import HttpdConf + + +LOCK_BODY = ('' + '' + '' + '' + 'test-owner' + '') + +BOGUS_TOKEN = '' + + +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}" + + Dav On + Require all granted + + """, + }) + 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 " 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 diff --git a/test/modules/dav/test_007_propfind_quoting.py b/test/modules/dav/test_007_propfind_quoting.py new file mode 100644 index 00000000000..aa05e7f9f6c --- /dev/null +++ b/test/modules/dav/test_007_propfind_quoting.py @@ -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"' + +PROPFIND_BODY = ('' + '' + '' + '') + + +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}" + + Dav On + Require all granted + ForceType '{CONTENT_TYPE}' + + """, + }) + 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