Blame SOURCES/0012-optionally-delete-the-oldest-state-cookie-s-see-399.patch

50cc55
From 95baad3342aa7ef7172ad4922eb9f0d605dc469b Mon Sep 17 00:00:00 2001
50cc55
From: Hans Zandbelt <hans.zandbelt@zmartzone.eu>
50cc55
Date: Thu, 6 Dec 2018 14:55:44 +0100
50cc55
Subject: [PATCH 12/13] optionally delete the oldest state cookie(s); see #399
50cc55
50cc55
bump to 2.3.10rc3
50cc55
50cc55
Signed-off-by: Hans Zandbelt <hans.zandbelt@zmartzone.eu>
50cc55
(cherry picked from commit 46758a75eef8e3c91f2917fc7c6302136eb18809)
50cc55
---
50cc55
 auth_openidc.conf      |  8 +++--
50cc55
 src/config.c           | 27 +++++++++++++----
50cc55
 src/mod_auth_openidc.c | 66 +++++++++++++++++++++++++++++++++++++++---
50cc55
 src/mod_auth_openidc.h |  2 ++
50cc55
 src/parse.c            |  9 ++++--
50cc55
 src/parse.h            |  2 +-
50cc55
 6 files changed, 100 insertions(+), 14 deletions(-)
50cc55
50cc55
diff --git a/auth_openidc.conf b/auth_openidc.conf
50cc55
index 48dd027..33cea64 100644
50cc55
--- a/auth_openidc.conf
50cc55
+++ b/auth_openidc.conf
50cc55
@@ -450,8 +450,12 @@
50cc55
 # authentication requests. See: https://github.com/zmartzone/mod_auth_openidc/issues/331
50cc55
 # Setting this to 0 means unlimited, until the browser or server gives up which is the
50cc55
 # behavior of mod_auth_openidc < 2.3.8, which did not have this configuration option. 
50cc55
-# When not defined, the default is 7.
50cc55
-#OIDCStateMaxNumberOfCookies <number>
50cc55
+#
50cc55
+# The optional second boolean parameter if the oldest state cookie(s) will be deleted, 
50cc55
+# even if still valid; see #399.
50cc55
+#
50cc55
+# When not defined, the default is 7 and "false", thus the oldest cookie(s) will not be deleted.
50cc55
+#OIDCStateMaxNumberOfCookies <number> [false|true]
50cc55
 
50cc55
 ########################################################################################
50cc55
 #
50cc55
diff --git a/src/config.c b/src/config.c
50cc55
index 6fa6227..8e56716 100644
50cc55
--- a/src/config.c
50cc55
+++ b/src/config.c
50cc55
@@ -106,6 +106,8 @@
50cc55
 #define OIDC_DEFAULT_STATE_TIMEOUT 300
50cc55
 /* maximum number of parallel state cookies; 0 means unlimited, until the browser or server gives up */
50cc55
 #define OIDC_DEFAULT_MAX_NUMBER_OF_STATE_COOKIES 7
50cc55
+/* default setting for deleting the oldest state cookies */
50cc55
+#define OIDC_DEFAULT_DELETE_OLDEST_STATE_COOKIES 0
50cc55
 /* default session inactivity timeout */
50cc55
 #define OIDC_DEFAULT_SESSION_INACTIVITY_TIMEOUT 300
50cc55
 /* default session max duration */
50cc55
@@ -1001,11 +1003,12 @@ static const char *oidc_set_client_auth_bearer_token(cmd_parms *cmd,
50cc55
  * set the maximun number of parallel state cookies
50cc55
  */
50cc55
 static const char *oidc_set_max_number_of_state_cookies(cmd_parms *cmd,
50cc55
-		void *struct_ptr, const char *arg) {
50cc55
+		void *struct_ptr, const char *arg1, const char *arg2) {
50cc55
 	oidc_cfg *cfg = (oidc_cfg *) ap_get_module_config(
50cc55
 			cmd->server->module_config, &auth_openidc_module);
50cc55
-	const char *rv = oidc_parse_max_number_of_state_cookies(cmd->pool, arg,
50cc55
-			&cfg->max_number_of_state_cookies);
50cc55
+	const char *rv = oidc_parse_max_number_of_state_cookies(cmd->pool, arg1,
50cc55
+			arg2, &cfg->max_number_of_state_cookies,
50cc55
+			&cfg->delete_oldest_state_cookies);
50cc55
 	return OIDC_CONFIG_DIR_RV(cmd, rv);
50cc55
 }
50cc55
 
50cc55
@@ -1018,6 +1021,15 @@ int oidc_cfg_max_number_of_state_cookies(oidc_cfg *cfg) {
50cc55
 	return cfg->max_number_of_state_cookies;
50cc55
 }
50cc55
 
50cc55
+/*
50cc55
+ * return the number of oldest state cookies that need to be deleted
50cc55
+ */
50cc55
+int oidc_cfg_delete_oldest_state_cookies(oidc_cfg *cfg) {
50cc55
+	if (cfg->delete_oldest_state_cookies == OIDC_CONFIG_POS_INT_UNSET)
50cc55
+		return OIDC_DEFAULT_DELETE_OLDEST_STATE_COOKIES;
50cc55
+	return cfg->delete_oldest_state_cookies;
50cc55
+}
50cc55
+
50cc55
 /*
50cc55
  * create a new server config record with defaults
50cc55
  */
50cc55
@@ -1127,6 +1139,7 @@ void *oidc_create_server_config(apr_pool_t *pool, server_rec *svr) {
50cc55
 	c->http_timeout_short = OIDC_DEFAULT_HTTP_TIMEOUT_SHORT;
50cc55
 	c->state_timeout = OIDC_DEFAULT_STATE_TIMEOUT;
50cc55
 	c->max_number_of_state_cookies = OIDC_CONFIG_POS_INT_UNSET;
50cc55
+	c->delete_oldest_state_cookies = OIDC_CONFIG_POS_INT_UNSET;
50cc55
 	c->session_inactivity_timeout = OIDC_DEFAULT_SESSION_INACTIVITY_TIMEOUT;
50cc55
 
50cc55
 	c->cookie_domain = NULL;
50cc55
@@ -1445,6 +1458,10 @@ void *oidc_merge_server_config(apr_pool_t *pool, void *BASE, void *ADD) {
50cc55
 			add->max_number_of_state_cookies != OIDC_CONFIG_POS_INT_UNSET ?
50cc55
 					add->max_number_of_state_cookies :
50cc55
 					base->max_number_of_state_cookies;
50cc55
+	c->delete_oldest_state_cookies =
50cc55
+			add->delete_oldest_state_cookies != OIDC_CONFIG_POS_INT_UNSET ?
50cc55
+					add->delete_oldest_state_cookies :
50cc55
+					base->delete_oldest_state_cookies;
50cc55
 	c->session_inactivity_timeout =
50cc55
 			add->session_inactivity_timeout
50cc55
 			!= OIDC_DEFAULT_SESSION_INACTIVITY_TIMEOUT ?
50cc55
@@ -2656,11 +2673,11 @@ const command_rec oidc_config_cmds[] = {
50cc55
 				(void*)APR_OFFSETOF(oidc_cfg, state_timeout),
50cc55
 				RSRC_CONF,
50cc55
 				"Time to live in seconds for state parameter (cq. interval in which the authorization request and the corresponding response need to be completed)."),
50cc55
-		AP_INIT_TAKE1(OIDCStateMaxNumberOfCookies,
50cc55
+		AP_INIT_TAKE12(OIDCStateMaxNumberOfCookies,
50cc55
 				oidc_set_max_number_of_state_cookies,
50cc55
 				(void*)APR_OFFSETOF(oidc_cfg, max_number_of_state_cookies),
50cc55
 				RSRC_CONF,
50cc55
-				"Maximun number of parallel state cookies i.e. outstanding authorization requests."),
50cc55
+				"Maximun number of parallel state cookies i.e. outstanding authorization requests and whether to delete the oldest cookie(s)."),
50cc55
 		AP_INIT_TAKE1(OIDCSessionInactivityTimeout,
50cc55
 				oidc_set_session_inactivity_timeout,
50cc55
 				(void*)APR_OFFSETOF(oidc_cfg, session_inactivity_timeout),
50cc55
diff --git a/src/mod_auth_openidc.c b/src/mod_auth_openidc.c
50cc55
index 897a449..8740e02 100644
50cc55
--- a/src/mod_auth_openidc.c
50cc55
+++ b/src/mod_auth_openidc.c
50cc55
@@ -686,15 +686,53 @@ static apr_byte_t oidc_unsolicited_proto_state(request_rec *r, oidc_cfg *c,
50cc55
 	return TRUE;
50cc55
 }
50cc55
 
50cc55
+typedef struct oidc_state_cookies_t {
50cc55
+	char *name;
50cc55
+	apr_time_t timestamp;
50cc55
+	struct oidc_state_cookies_t *next;
50cc55
+} oidc_state_cookies_t;
50cc55
+
50cc55
+static int oidc_delete_oldest_state_cookies(request_rec *r,
50cc55
+		int number_of_valid_state_cookies, int max_number_of_state_cookies,
50cc55
+		oidc_state_cookies_t *first) {
50cc55
+	oidc_state_cookies_t *cur = NULL, *prev = NULL, *prev_oldest = NULL,
50cc55
+			*oldest = NULL;
50cc55
+	while (number_of_valid_state_cookies >= max_number_of_state_cookies) {
50cc55
+		oldest = first;
50cc55
+		prev_oldest = NULL;
50cc55
+		prev = first;
50cc55
+		cur = first->next;
50cc55
+		while (cur) {
50cc55
+			if ((cur->timestamp < oldest->timestamp)) {
50cc55
+				oldest = cur;
50cc55
+				prev_oldest = prev;
50cc55
+			}
50cc55
+			prev = cur;
50cc55
+			cur = cur->next;
50cc55
+		}
50cc55
+		oidc_warn(r,
50cc55
+				"deleting oldest state cookie: %s (time until expiry " APR_TIME_T_FMT " seconds)",
50cc55
+				oldest->name, apr_time_sec(oldest->timestamp - apr_time_now()));
50cc55
+		oidc_util_set_cookie(r, oldest->name, "", 0, NULL);
50cc55
+		if (prev_oldest)
50cc55
+			prev_oldest->next = oldest->next;
50cc55
+		else
50cc55
+			first = first->next;
50cc55
+		number_of_valid_state_cookies--;
50cc55
+	}
50cc55
+	return number_of_valid_state_cookies;
50cc55
+}
50cc55
+
50cc55
 /*
50cc55
  * clean state cookies that have expired i.e. for outstanding requests that will never return
50cc55
  * successfully and return the number of remaining valid cookies/outstanding-requests while
50cc55
  * doing so
50cc55
  */
50cc55
 static int oidc_clean_expired_state_cookies(request_rec *r, oidc_cfg *c,
50cc55
-		const char *currentCookieName) {
50cc55
+		const char *currentCookieName, int delete_oldest) {
50cc55
 	int number_of_valid_state_cookies = 0;
50cc55
-	char *cookie, *tokenizerCtx;
50cc55
+	oidc_state_cookies_t *first = NULL, *last = NULL;
50cc55
+	char *cookie, *tokenizerCtx = NULL;
50cc55
 	char *cookies = apr_pstrdup(r->pool, oidc_util_hdr_in_cookie_get(r));
50cc55
 	if (cookies != NULL) {
50cc55
 		cookie = apr_strtok(cookies, OIDC_STR_SEMI_COLON, &tokenizerCtx);
50cc55
@@ -722,6 +760,18 @@ static int oidc_clean_expired_state_cookies(request_rec *r, oidc_cfg *c,
50cc55
 								oidc_util_set_cookie(r, cookieName, "", 0,
50cc55
 										NULL);
50cc55
 							} else {
50cc55
+								if (first == NULL) {
50cc55
+									first = apr_pcalloc(r->pool,
50cc55
+											sizeof(oidc_state_cookies_t));
50cc55
+									last = first;
50cc55
+								} else {
50cc55
+									last->next = apr_pcalloc(r->pool,
50cc55
+											sizeof(oidc_state_cookies_t));
50cc55
+									last = last->next;
50cc55
+								}
50cc55
+								last->name = cookieName;
50cc55
+								last->timestamp = ts;
50cc55
+								last->next = NULL;
50cc55
 								number_of_valid_state_cookies++;
50cc55
 							}
50cc55
 							oidc_proto_state_destroy(proto_state);
50cc55
@@ -732,6 +782,12 @@ static int oidc_clean_expired_state_cookies(request_rec *r, oidc_cfg *c,
50cc55
 			cookie = apr_strtok(NULL, OIDC_STR_SEMI_COLON, &tokenizerCtx);
50cc55
 		}
50cc55
 	}
50cc55
+
50cc55
+	if (delete_oldest > 0)
50cc55
+		number_of_valid_state_cookies = oidc_delete_oldest_state_cookies(r,
50cc55
+				number_of_valid_state_cookies, c->max_number_of_state_cookies,
50cc55
+				first);
50cc55
+
50cc55
 	return number_of_valid_state_cookies;
50cc55
 }
50cc55
 
50cc55
@@ -746,7 +802,7 @@ static apr_byte_t oidc_restore_proto_state(request_rec *r, oidc_cfg *c,
50cc55
 	const char *cookieName = oidc_get_state_cookie_name(r, state);
50cc55
 
50cc55
 	/* clean expired state cookies to avoid pollution */
50cc55
-	oidc_clean_expired_state_cookies(r, c, cookieName);
50cc55
+	oidc_clean_expired_state_cookies(r, c, cookieName, FALSE);
50cc55
 
50cc55
 	/* get the state cookie value first */
50cc55
 	char *cookieValue = oidc_util_get_cookie(r, cookieName);
50cc55
@@ -820,10 +876,12 @@ static int oidc_authorization_request_set_cookie(request_rec *r, oidc_cfg *c,
50cc55
 	 * clean expired state cookies to avoid pollution and optionally
50cc55
 	 * try to avoid the number of state cookies exceeding a max
50cc55
 	 */
50cc55
-	int number_of_cookies = oidc_clean_expired_state_cookies(r, c, NULL);
50cc55
+	int number_of_cookies = oidc_clean_expired_state_cookies(r, c, NULL,
50cc55
+			oidc_cfg_delete_oldest_state_cookies(c));
50cc55
 	int max_number_of_cookies = oidc_cfg_max_number_of_state_cookies(c);
50cc55
 	if ((max_number_of_cookies > 0)
50cc55
 			&& (number_of_cookies >= max_number_of_cookies)) {
50cc55
+
50cc55
 		oidc_warn(r,
50cc55
 				"the number of existing, valid state cookies (%d) has exceeded the limit (%d), no additional authorization request + state cookie can be generated, aborting the request",
50cc55
 				number_of_cookies, max_number_of_cookies);
50cc55
diff --git a/src/mod_auth_openidc.h b/src/mod_auth_openidc.h
50cc55
index f89f392..c3a0a23 100644
50cc55
--- a/src/mod_auth_openidc.h
50cc55
+++ b/src/mod_auth_openidc.h
50cc55
@@ -380,6 +380,7 @@ typedef struct oidc_cfg {
50cc55
 	int http_timeout_short;
50cc55
 	int state_timeout;
50cc55
 	int max_number_of_state_cookies;
50cc55
+	int delete_oldest_state_cookies;
50cc55
 	int session_inactivity_timeout;
50cc55
 	int session_cache_fallback_to_cookie;
50cc55
 
50cc55
@@ -691,6 +692,7 @@ int oidc_cfg_session_cache_fallback_to_cookie(request_rec *r);
50cc55
 const char *oidc_parse_pkce_type(apr_pool_t *pool, const char *arg, oidc_proto_pkce_t **type);
50cc55
 const char *oidc_cfg_claim_prefix(request_rec *r);
50cc55
 int oidc_cfg_max_number_of_state_cookies(oidc_cfg *cfg);
50cc55
+int oidc_cfg_delete_oldest_state_cookies(oidc_cfg *cfg);
50cc55
 
50cc55
 // oidc_util.c
50cc55
 int oidc_strnenvcmp(const char *a, const char *b, int len);
50cc55
diff --git a/src/parse.c b/src/parse.c
50cc55
index 0f986fd..2d09584 100644
50cc55
--- a/src/parse.c
50cc55
+++ b/src/parse.c
50cc55
@@ -1245,7 +1245,12 @@ const char *oidc_parse_auth_request_method(apr_pool_t *pool, const char *arg,
50cc55
  * parse the maximum number of parallel state cookies
50cc55
  */
50cc55
 const char *oidc_parse_max_number_of_state_cookies(apr_pool_t *pool,
50cc55
-		const char *arg, int *int_value) {
50cc55
-	return oidc_parse_int_valid(pool, arg, int_value,
50cc55
+		const char *arg1, const char *arg2, int *int_value, int *bool_value) {
50cc55
+	const char *rv = NULL;
50cc55
+
50cc55
+	rv = oidc_parse_int_valid(pool, arg1, int_value,
50cc55
 			oidc_valid_max_number_of_state_cookies);
50cc55
+	if ((rv == NULL) && (arg2 != NULL))
50cc55
+		rv = oidc_parse_boolean(pool, arg2, bool_value);
50cc55
+	return rv;
50cc55
 }
50cc55
diff --git a/src/parse.h b/src/parse.h
50cc55
index 6355db4..bdf5651 100644
50cc55
--- a/src/parse.h
50cc55
+++ b/src/parse.h
50cc55
@@ -117,7 +117,7 @@ const char *oidc_parse_info_hook_data(apr_pool_t *pool, const char *arg, apr_has
50cc55
 const char *oidc_parse_token_binding_policy(apr_pool_t *pool, const char *arg, int *int_value);
50cc55
 const char *oidc_token_binding_policy2str(apr_pool_t *pool, int v);
50cc55
 const char *oidc_parse_auth_request_method(apr_pool_t *pool, const char *arg, int *method);
50cc55
-const char *oidc_parse_max_number_of_state_cookies(apr_pool_t *pool, const char *arg, int *int_value);
50cc55
+const char *oidc_parse_max_number_of_state_cookies(apr_pool_t *pool, const char *arg1, const char *arg2, int *int_value, int *bool_value);
50cc55
 
50cc55
 typedef const char *(*oidc_valid_int_function_t)(apr_pool_t *, int);
50cc55
 typedef const char *(*oidc_valid_function_t)(apr_pool_t *, const char *);
50cc55
-- 
50cc55
2.26.2
50cc55