Skip to content

Commit 15beddf

Browse files
notrojarturobernalgmanfred-kaiser
authored
[2.4.x] Merge changes to mod_dav (#1090)
* modules/dav/fs/repos.c (dav_fs_get_resource): Return a 404 rather than a 400 where r->path_info is not empty for a file; a valid but unsatisfiable request to a path which cannot exist, e.g. /dav/foo.txt/blah where /dav/foo.txt is not a directory. Github: closes #465 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1919580 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit a288cd3) mod_dav: Fix error handling for dav_fs_dir_file_name(): dav_fs_dir_file_name() will not set *fname_p to NULL on failure, and all callers of dav_fs_dir_file_name() does not check the return value of dav_fs_dir_file_name(), which could lead to an undefined behavior against fname_p. Fix this by adding return value check of dav_fs_dir_file_name() Submitted by: Zhou Qingyang <zhou1615 umn.edu> Github: closes #309 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1923813 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit 1e298dd) * modules/dav/fs/repos.c (dav_fs_copymove_file): Log the pathname when opening the source or destination file fails. Suggested by: Bingyu Shen <ahshenbingyu gmail.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> GitHub: closes #143, closes #760 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1938296 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit 1c838de) Merge r1939146 from trunk (test changes already on 2.4.x): mod_dav: Do not advertise disabled TRACE dav_method_options() builds its own Allow field and unconditionally advertised TRACE for existing DAV resources, even when TraceEnable was off. Respect TraceEnable when constructing the DAV Allow field. PR: 70264 GitHub: closes #827 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1939146 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit 261058f) mod_dav: Enforce the lock-owner check on the untagged If: fallback too (#889) dav_find_submitted_locktoken() scans the whole If: header for a usable locktoken without checking whether the lock belongs to the authenticated user. dav_validate_resource_state() repeats that owner check itself on the tagged-list path, but not here, so a non-matching If: tag (one whose URI does not apply to the resource) forces the untagged fallback and lets an authenticated writer use another user's locktoken. Factor the existing owner check out into dav_lock_owned_by_other_user() and call it from dav_find_submitted_locktoken() as well: a lock owned by another user is skipped during the scan instead of being treated as a usable match. The scan continues past the skip, so a header that legitimately carries the current user's own token - under any tag, or none - still succeeds. * modules/dav/main/util.c (dav_lock_owned_by_other_user): New. (dav_find_submitted_locktoken): Take request_rec and skip a lock owned by another user. Both call sites updated. (dav_validate_resource_state): Use the new helper; no behaviour change. (cherry picked from commit 9a9457f) mod_dav: Ignore negated lock tokens when checking for a submitted token: * modules/dav/main/util.c (dav_find_submitted_locktoken): Skip states with a "Not" condition. * test/modules/dav/test_006_if_not_locktoken.py: New test. * changes-entries/dav-if-not-locktoken.txt: New file. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> (cherry picked from commit d7ed293) mod_dav: XML-quote core property values taken from the subrequest: * modules/dav/main/props.c (dav_insert_coreprop): Quote the getcontenttype and getcontentlanguage values. * test/modules/dav/test_007_propfind_quoting.py: New test. * changes-entries/dav-propfind-quoting.txt: New file. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> (cherry picked from commit c58a6f2) * test/modules/dav/test_002_options_trace.py (TestOptionsTraceDisabled.test_dav_002_001): Mark as backported in 2.4.72. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * test/modules/dav/test_007_propfind_quoting.py (TestDavPropfindQuoting._class_scope): Set DavLockDB, which has no default in 2.4.x. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: Arturo Bernal <abernal@apache.org> Co-authored-by: Manfred Kaiser <manfred.kaiser@logfile.at>
1 parent b8e005d commit 15beddf

10 files changed

Lines changed: 213 additions & 16 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*) mod_dav: Do not accept a lock token given under "Not" in the If
2+
header as submitting that token for a locked resource. [Joe Orton]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*) mod_dav: Do not list TRACE in the Allow field of an OPTIONS response when
2+
TraceEnable is off. PR 70264
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*) mod_dav: Escape the getcontenttype and getcontentlanguage
2+
properties in PROPFIND responses. [Joe Orton]

‎modules/dav/fs/repos.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ static dav_error * dav_fs_copymove_file(
364364

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

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

570574
/* Get directory and filename for resources */
571575
/* ### should test these result values... */
572-
(void) dav_fs_dir_file_name(src, &src_dir, &src_file);
573-
(void) dav_fs_dir_file_name(dst, &dst_dir, &dst_file);
576+
err = dav_fs_dir_file_name(src, &src_dir, &src_file);
577+
if (err != NULL)
578+
return err;
579+
580+
err = dav_fs_dir_file_name(dst, &dst_dir, &dst_file);
581+
if (err != NULL)
582+
return err;
574583

575584
/* Get the corresponding state files for each resource */
576585
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)
618627
const char *state1;
619628
const char *state2;
620629
const char *pathname;
630+
dav_error *err;
621631
apr_status_t status;
622632

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

628640
/* build the propset pathname for the file */
@@ -797,10 +809,10 @@ static dav_error * dav_fs_get_resource(
797809
{
798810
/*
799811
** The base of the path refers to a file -- nothing should
800-
** be in path_info. The resource is simply an error: it
812+
** be in path_info. The resource cannot exist: it
801813
** can't be a null or a locknull resource.
802814
*/
803-
return dav_new_error(r->pool, HTTP_BAD_REQUEST, 0, 0,
815+
return dav_new_error(r->pool, HTTP_NOT_FOUND, 0, 0,
804816
"The URL contains extraneous path "
805817
"components. The resource could not "
806818
"be identified.");

‎modules/dav/main/mod_dav.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,8 @@ static int dav_method_search(request_rec *r)
16981698
/* handle the OPTIONS method */
16991699
static int dav_method_options(request_rec *r)
17001700
{
1701+
core_server_config *conf =
1702+
ap_get_core_module_config(r->server->module_config);
17011703
const dav_hooks_locks *locks_hooks = DAV_GET_HOOKS_LOCKS(r);
17021704
const dav_hooks_vsn *vsn_hooks = DAV_GET_HOOKS_VSN(r);
17031705
const dav_hooks_binding *binding_hooks = DAV_GET_HOOKS_BINDING(r);
@@ -1829,7 +1831,9 @@ static int dav_method_options(request_rec *r)
18291831
apr_table_addn(methods, "HEAD", "");
18301832
apr_table_addn(methods, "POST", "");
18311833
apr_table_addn(methods, "DELETE", "");
1832-
apr_table_addn(methods, "TRACE", "");
1834+
if (conf->trace_enable != AP_TRACE_DISABLE) {
1835+
apr_table_addn(methods, "TRACE", "");
1836+
}
18331837
apr_table_addn(methods, "PROPFIND", "");
18341838
apr_table_addn(methods, "PROPPATCH", "");
18351839
apr_table_addn(methods, "COPY", "");

‎modules/dav/main/props.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ static dav_error * dav_insert_coreprop(dav_propdb *propdb,
399399
dav_do_prop_subreq(propdb);
400400
}
401401
if (propdb->subreq->content_type != NULL) {
402-
value = propdb->subreq->content_type;
402+
value = apr_xml_quote_string(propdb->p,
403+
propdb->subreq->content_type, 0);
403404
}
404405
break;
405406

@@ -412,7 +413,7 @@ static dav_error * dav_insert_coreprop(dav_propdb *propdb,
412413
}
413414
if ((lang = apr_table_get(propdb->subreq->headers_out,
414415
"Content-Language")) != NULL) {
415-
value = lang;
416+
value = apr_xml_quote_string(propdb->p, lang, 0);
416417
}
417418
break;
418419
}

‎modules/dav/main/util.c‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,18 @@ static dav_error * dav_process_if_header(request_rec *r, dav_if_header **p_ih)
893893
return NULL;
894894
}
895895

896-
static int dav_find_submitted_locktoken(const dav_if_header *if_header,
896+
/*
897+
** If an authenticated user created a lock, only that user may submit its
898+
** locktoken to manipulate a resource.
899+
*/
900+
static int dav_lock_owned_by_other_user(const request_rec *r,
901+
const dav_lock *lock)
902+
{
903+
return lock->auth_user && (!r->user || strcmp(lock->auth_user, r->user));
904+
}
905+
906+
static int dav_find_submitted_locktoken(const request_rec *r,
907+
const dav_if_header *if_header,
897908
const dav_lock *lock_list,
898909
const dav_hooks_locks *locks_hooks)
899910
{
@@ -904,7 +915,9 @@ static int dav_find_submitted_locktoken(const dav_if_header *if_header,
904915
state_list != NULL;
905916
state_list = state_list->next) {
906917

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

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

940+
if (dav_lock_owned_by_other_user(r, lock)) {
941+
continue;
942+
}
943+
927944
if (!(*locks_hooks->compare_locktoken)(state_list->locktoken, lock->locktoken)) {
928945
return 1;
929946
}
@@ -1313,9 +1330,7 @@ static dav_error * dav_validate_resource_state(apr_pool_t *p,
13131330
** the lock, only the same user may submit that locktoken
13141331
** to manipulate a resource.
13151332
*/
1316-
if (lock->auth_user &&
1317-
(!r->user ||
1318-
strcmp(lock->auth_user, r->user))) {
1333+
if (dav_lock_owned_by_other_user(r, lock)) {
13191334
const char *errmsg;
13201335

13211336
errmsg = apr_pstrcat(p, "User \"",
@@ -1433,7 +1448,7 @@ static dav_error * dav_validate_resource_state(apr_pool_t *p,
14331448
** Note that seen_locktoken == 0 implies lock_list != NULL
14341449
** which implies locks_hooks != NULL.
14351450
*/
1436-
if (dav_find_submitted_locktoken(if_header, lock_list,
1451+
if (dav_find_submitted_locktoken(r, if_header, lock_list,
14371452
locks_hooks)) {
14381453
/*
14391454
** 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,
14931508
** Note that seen_locktoken == 0 implies lock_list != NULL which implies
14941509
** locks_hooks != NULL.
14951510
*/
1496-
if (dav_find_submitted_locktoken(if_header, lock_list, locks_hooks)) {
1511+
if (dav_find_submitted_locktoken(r, if_header, lock_list, locks_hooks)) {
14971512
/*
14981513
** We found a match! We're set... we have a matching state list,
14991514
** and the If: header specified the locktoken somewhere. We're done.

‎test/modules/dav/test_002_options_trace.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _class_scope(self, env):
4848

4949
# TRACE is not offered while the server refuses it
5050
@pytest.mark.parametrize("path", ["/dav/file.txt", "/dav/col/"])
51-
@not_backported("r1939146", "DAV OPTIONS Allow honouring TraceEnable")
51+
@not_backported("r1939146", "DAV OPTIONS Allow honouring TraceEnable", backported_in="2.4.72")
5252
def test_dav_002_001(self, env, path):
5353
status, allow = _request(env, "OPTIONS", path)
5454
assert status == 200
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import glob
2+
import os
3+
4+
import pytest
5+
6+
from pyhttpd.conf import HttpdConf
7+
8+
9+
LOCK_BODY = ('<?xml version="1.0" encoding="utf-8"?>'
10+
'<D:lockinfo xmlns:D="DAV:">'
11+
'<D:lockscope><D:exclusive/></D:lockscope>'
12+
'<D:locktype><D:write/></D:locktype>'
13+
'<D:owner><D:href>test-owner</D:href></D:owner>'
14+
'</D:lockinfo>')
15+
16+
BOGUS_TOKEN = '<opaquelocktoken:00000000-0000-0000-0000-000000000000>'
17+
18+
19+
class TestDavIfNotLocktoken:
20+
"""A locktoken given under "Not" in the If: header is an assertion
21+
that the token is not held, and must not count as submitting the
22+
token for a locked resource."""
23+
24+
@pytest.fixture(autouse=True, scope='class')
25+
@classmethod
26+
def _class_scope(cls, env):
27+
dav_dir = os.path.join(env.gen_dir, 'dav-ifnot')
28+
os.makedirs(dav_dir, exist_ok=True)
29+
for f in glob.glob(os.path.join(env.gen_dir, 'davlock-ifnot*')):
30+
os.remove(f)
31+
dav_dir_conf = dav_dir.replace('\\', '/')
32+
lock_db_conf = os.path.join(env.gen_dir,
33+
'davlock-ifnot').replace('\\', '/')
34+
conf = HttpdConf(env, extras={
35+
'base': f"""
36+
DavLockDB "{lock_db_conf}"
37+
""",
38+
f"test1.{env.http_tld}": f"""
39+
Alias /dav-ifnot "{dav_dir_conf}"
40+
<Directory "{dav_dir_conf}">
41+
Dav On
42+
Require all granted
43+
</Directory>
44+
""",
45+
})
46+
conf.add_vhost_test1()
47+
conf.install()
48+
assert env.apache_restart() == 0
49+
50+
def _url(self, env, name):
51+
return env.mkurl("http", "test1", f"/dav-ifnot/{name}")
52+
53+
def _locked(self, env, name):
54+
"""Create and lock a resource, returning its URL and the
55+
locktoken in Coded-URL form."""
56+
with open(os.path.join(env.gen_dir, 'dav-ifnot', name), 'w') as fd:
57+
fd.write('locked')
58+
url = self._url(env, name)
59+
r = env.curl_raw(url, options=[
60+
'-X', 'LOCK',
61+
'-H', 'Content-Type: text/xml',
62+
'--data-binary', LOCK_BODY])
63+
assert r.response["status"] == 200
64+
return url, r.response["header"]["lock-token"]
65+
66+
def _put(self, env, url, if_header):
67+
return env.curl_raw(url, options=[
68+
'-X', 'PUT',
69+
'-H', f'If: {if_header}',
70+
'--data-binary', 'changed'])
71+
72+
# "Not <token>" in a tagged list for another resource
73+
def test_dav_006_01_not_token_other_resource(self, env):
74+
url, token = self._locked(env, 'one.txt')
75+
other = self._url(env, 'other.txt')
76+
r = self._put(env, url, f'<{other}> (Not {token})')
77+
assert r.response["status"] == 423
78+
env.httpd_error_log.ignore_recent(matches=[r'.*dav:error.*'])
79+
80+
# As above, where a tagged list for the resource itself also matches
81+
def test_dav_006_02_not_token_matching_list(self, env):
82+
url, token = self._locked(env, 'two.txt')
83+
other = self._url(env, 'other.txt')
84+
r = self._put(env, url,
85+
f'<{url}> (Not {BOGUS_TOKEN}) <{other}> (Not {token})')
86+
assert r.response["status"] == 423
87+
env.httpd_error_log.ignore_recent(matches=[r'.*dav:error.*'])
88+
89+
# The token is still accepted when submitted without "Not",
90+
# whichever resource the list is tagged for.
91+
@pytest.mark.parametrize("tag", ['three.txt', 'other.txt'])
92+
def test_dav_006_03_token_submitted(self, env, tag):
93+
url, token = self._locked(env, 'three.txt')
94+
r = self._put(env, url, f'<{self._url(env, tag)}> ({token})')
95+
assert r.response["status"] == 204
96+
r = env.curl_raw(url, options=['-X', 'UNLOCK',
97+
'-H', f'Lock-Token: {token}'])
98+
assert r.response["status"] == 204
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
import xml.etree.ElementTree as ET
3+
4+
import pytest
5+
6+
from pyhttpd.conf import HttpdConf
7+
8+
9+
CONTENT_TYPE = 'text/x-test; note="a&b<c>"'
10+
11+
PROPFIND_BODY = ('<?xml version="1.0" encoding="utf-8"?>'
12+
'<D:propfind xmlns:D="DAV:">'
13+
'<D:prop><D:getcontenttype/></D:prop>'
14+
'</D:propfind>')
15+
16+
17+
class TestDavPropfindQuoting:
18+
"""DAV:getcontenttype must be XML-quoted in a PROPFIND response."""
19+
20+
@pytest.fixture(autouse=True, scope='class')
21+
@classmethod
22+
def _class_scope(cls, env):
23+
dav_dir = os.path.join(env.gen_dir, 'dav-quote')
24+
os.makedirs(dav_dir, exist_ok=True)
25+
with open(os.path.join(dav_dir, 'hello.txt'), 'w') as fd:
26+
fd.write('hello')
27+
dav_dir_conf = dav_dir.replace('\\', '/')
28+
lock_db_conf = os.path.join(env.gen_dir,
29+
'davlock-quote').replace('\\', '/')
30+
conf = HttpdConf(env, extras={
31+
'base': f"""
32+
DavLockDB "{lock_db_conf}"
33+
""",
34+
f"test1.{env.http_tld}": f"""
35+
Alias /dav-quote "{dav_dir_conf}"
36+
<Directory "{dav_dir_conf}">
37+
Dav On
38+
Require all granted
39+
ForceType '{CONTENT_TYPE}'
40+
</Directory>
41+
""",
42+
})
43+
conf.add_vhost_test1()
44+
conf.install()
45+
assert env.apache_restart() == 0
46+
47+
@pytest.mark.parametrize("body", [PROPFIND_BODY, None])
48+
def test_dav_007_01_getcontenttype(self, env, body):
49+
url = env.mkurl("http", "test1", "/dav-quote/hello.txt")
50+
options = ['-X', 'PROPFIND', '-H', 'Depth: 0']
51+
if body:
52+
options += ['-H', 'Content-Type: text/xml',
53+
'--data-binary', body]
54+
r = env.curl_raw(url, options=options)
55+
assert r.response["status"] == 207
56+
# fails to parse if the property value is not quoted
57+
root = ET.fromstring(r.response["body"])
58+
prop = root.find('.//{DAV:}getcontenttype')
59+
assert prop is not None
60+
assert prop.text == CONTENT_TYPE
61+
assert len(prop) == 0

0 commit comments

Comments
 (0)