diff --git a/changes-entries/65172.txt b/changes-entries/65172.txt index c6c9ddb20da..68c6e61c86e 100644 --- a/changes-entries/65172.txt +++ b/changes-entries/65172.txt @@ -2,5 +2,6 @@ and AuthDBDUserRealmQuery columns, exported as AUTHENTICATE_ environment variables, on a login answered from the cache (AuthBasicProvider/AuthDigestProvider "socache dbd"). Previously only - the first, uncached login for a user had them. PR 65172. + the first, uncached login for a user had them. A column which is + NULL now sets its variable to an empty value. PR 65172. [Arturo Bernal] diff --git a/modules/aaa/mod_authn_dbd.c b/modules/aaa/mod_authn_dbd.c index a27b0634949..23911fd7da2 100644 --- a/modules/aaa/mod_authn_dbd.c +++ b/modules/aaa/mod_authn_dbd.c @@ -160,7 +160,7 @@ static authn_status authn_dbd_password(request_rec *r, const char *user, if (dbd_password == NULL) { /* add the rest of the columns to the environment */ int i = 1; - const char *name; + const char *name, *value; for (name = apr_dbd_get_name(dbd->driver, res, i); name != NULL; name = apr_dbd_get_name(dbd->driver, res, i)) { @@ -181,8 +181,9 @@ static authn_status authn_dbd_password(request_rec *r, const char *user, if (env == NULL) { env = apr_table_make(r->pool, 2); } - apr_table_set(env, str, - apr_dbd_get_entry(dbd->driver, row, i)); + /* a NULL column is set as an empty value */ + value = apr_dbd_get_entry(dbd->driver, row, i); + apr_table_set(env, str, value ? value : ""); i++; } @@ -262,7 +263,7 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user, if (dbd_hash == NULL) { /* add the rest of the columns to the environment */ int i = 1; - const char *name; + const char *name, *value; for (name = apr_dbd_get_name(dbd->driver, res, i); name != NULL; name = apr_dbd_get_name(dbd->driver, res, i)) { @@ -283,8 +284,9 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user, if (env == NULL) { env = apr_table_make(r->pool, 2); } - apr_table_set(env, str, - apr_dbd_get_entry(dbd->driver, row, i)); + /* a NULL column is set as an empty value */ + value = apr_dbd_get_entry(dbd->driver, row, i); + apr_table_set(env, str, value ? value : ""); i++; } diff --git a/test/modules/aaa/env.py b/test/modules/aaa/env.py index eee1021dc10..5c45ed2e369 100644 --- a/test/modules/aaa/env.py +++ b/test/modules/aaa/env.py @@ -42,6 +42,8 @@ class AAATestEnv(HttpdTestEnv): # A role too long to be cached with the password hash. DBD_LONG_USER = "dbdlonguser" DBD_LONG_ROLE = "r" * 300 + # A user whose role column is SQL NULL. + DBD_NULL_USER = "dbdnulluser" def __init__(self, pytestconfig=None): super().__init__(pytestconfig=pytestconfig) @@ -68,7 +70,8 @@ def _write_dbd_file(self): digest = hashlib.sha1(self.DBD_PASSWORD.encode()).digest() pw_hash = "{SHA}" + base64.b64encode(digest).decode() for user, role in [(self.DBD_USER, self.DBD_ROLE), - (self.DBD_LONG_USER, self.DBD_LONG_ROLE)]: + (self.DBD_LONG_USER, self.DBD_LONG_ROLE), + (self.DBD_NULL_USER, None)]: con.execute("INSERT INTO users (user, password, role) " "VALUES (?, ?, ?)", (user, pw_hash, role)) con.commit() diff --git a/test/modules/aaa/htdocs/dbd/cgi/whoami.py b/test/modules/aaa/htdocs/dbd/cgi/whoami.py index e337ccd5554..8e19d3cc2bd 100755 --- a/test/modules/aaa/htdocs/dbd/cgi/whoami.py +++ b/test/modules/aaa/htdocs/dbd/cgi/whoami.py @@ -4,3 +4,4 @@ print("Content-Type: text/plain\n") print(f"user={os.environ.get('REMOTE_USER', '')}") print(f"role={os.environ.get('AUTHENTICATE_ROLE', '')}") +print(f"role_set={'AUTHENTICATE_ROLE' in os.environ}") diff --git a/test/modules/aaa/test_018_authn_dbd_cache.py b/test/modules/aaa/test_018_authn_dbd_cache.py index 11c82628a4e..b582c74b733 100644 --- a/test/modules/aaa/test_018_authn_dbd_cache.py +++ b/test/modules/aaa/test_018_authn_dbd_cache.py @@ -97,3 +97,14 @@ def test_authn_dbd_018_long_role_not_cached(self, env): assert f"user={user}" in body assert f"role={AAATestEnv.DBD_LONG_ROLE}" in body, \ f"AUTHENTICATE_ROLE missing: {body!r}" + + def test_authn_dbd_018_null_role_cached(self, env): + # A NULL role column sets AUTHENTICATE_ROLE to an empty value, on + # the first login and on the cache hit that follows it, rather than + # crashing the child. + user = AAATestEnv.DBD_NULL_USER + for _ in range(2): + body = self.whoami(env, user=user) + assert f"user={user}" in body + assert "role=\nrole_set=True\n" in body, \ + f"AUTHENTICATE_ROLE not set and empty: {body!r}"