Skip to content

Commit 3d89d3b

Browse files
committed
use apr_pool_cleanup_register to clean up oidc_cfg_provider_t
rather than calling oidc_cfg_provider_destroy everywhere explicitly Signed-off-by: Hans Zandbelt <[email protected]>
1 parent 2d79d1c commit 3d89d3b

File tree

9 files changed

+13
-50
lines changed

9 files changed

+13
-50
lines changed

ChangeLog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
04/15/2025
22
- fix memory leaks when using provider specific client keys in a multi-provider setup
3-
- bump to 2.4.17rc0
3+
- bump to 2.4.17rc1
44

55
04/08/2025
66
- proto: pass the scope parameter as returned from the token endpoint in the OIDC_scope

src/cfg/provider.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,16 @@ void oidc_cfg_provider_merge(apr_pool_t *pool, oidc_provider_t *dst, const oidc_
862862
dst->profile = add->profile != OIDC_CONFIG_POS_INT_UNSET ? add->profile : base->profile;
863863
}
864864

865+
static apr_status_t oidc_provider_config_cleanup(void *data) {
866+
oidc_provider_t *provider = (oidc_provider_t *)data;
867+
oidc_cfg_provider_destroy(provider);
868+
return APR_SUCCESS;
869+
}
870+
865871
oidc_provider_t *oidc_cfg_provider_create(apr_pool_t *pool) {
866872
oidc_provider_t *provider = apr_pcalloc(pool, sizeof(oidc_provider_t));
867873
oidc_cfg_provider_init(provider);
874+
apr_pool_cleanup_register(pool, provider, oidc_provider_config_cleanup, oidc_provider_config_cleanup);
868875
return provider;
869876
}
870877

src/handle/discovery.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ int oidc_discovery_response(request_rec *r, oidc_cfg_t *c) {
367367
if (oidc_cfg_metadata_dir_get(c) == NULL) {
368368
if ((oidc_provider_static_config(r, c, &provider) == TRUE) && (issuer != NULL)) {
369369
if (_oidc_strcmp(oidc_cfg_provider_issuer_get(provider), issuer) != 0) {
370-
oidc_cfg_provider_destroy(provider);
371370
return oidc_util_html_send_error(
372371
r, "Invalid Request",
373372
apr_psprintf(
@@ -377,7 +376,6 @@ int oidc_discovery_response(request_rec *r, oidc_cfg_t *c) {
377376
HTTP_INTERNAL_SERVER_ERROR);
378377
}
379378
}
380-
oidc_cfg_provider_destroy(provider);
381379
return oidc_request_authenticate_user(r, c, NULL, target_link_uri, login_hint, NULL, NULL,
382380
auth_request_params, path_scopes);
383381
}
@@ -448,19 +446,14 @@ int oidc_discovery_response(request_rec *r, oidc_cfg_t *c) {
448446
oidc_cfg_provider_ssl_validate_server_get(provider), &j_jwks,
449447
&force_refresh);
450448
json_decref(j_jwks);
451-
oidc_cfg_provider_destroy(provider);
452449
return OK;
453450
} else {
454451
/* now we've got a selected OP, send the user there to authenticate */
455-
int rv = oidc_request_authenticate_user(r, c, provider, target_link_uri, login_hint, NULL, NULL,
456-
auth_request_params, path_scopes);
457-
oidc_cfg_provider_destroy(provider);
458-
return rv;
452+
return oidc_request_authenticate_user(r, c, provider, target_link_uri, login_hint, NULL, NULL,
453+
auth_request_params, path_scopes);
459454
}
460455
}
461456

462-
oidc_cfg_provider_destroy(provider);
463-
464457
/* something went wrong */
465458
return oidc_util_html_send_error(r, "Invalid Request",
466459
"Could not find valid provider metadata for the selected OpenID Connect "

src/handle/info.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,15 @@ int oidc_info_request(request_rec *r, oidc_cfg_t *c, oidc_session_t *session, ap
106106

107107
/* get the current provider info */
108108
oidc_provider_t *provider = NULL;
109-
if (oidc_get_provider_from_session(r, c, session, &provider) == FALSE) {
110-
oidc_cfg_provider_destroy(provider);
109+
if (oidc_get_provider_from_session(r, c, session, &provider) == FALSE)
111110
return HTTP_INTERNAL_SERVER_ERROR;
112-
}
113111

114112
/* execute the actual refresh grant */
115113
if (oidc_refresh_token_grant(r, c, session, provider, NULL, NULL, NULL) == FALSE) {
116114
oidc_warn(r, "access_token could not be refreshed");
117-
oidc_cfg_provider_destroy(provider);
118115
return HTTP_INTERNAL_SERVER_ERROR;
119116
}
120117
needs_save = TRUE;
121-
oidc_cfg_provider_destroy(provider);
122118
}
123119
}
124120
}

src/handle/logout.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ static void oidc_logout_revoke_tokens(request_rec *r, oidc_cfg_t *c, oidc_sessio
119119

120120
out:
121121

122-
oidc_cfg_provider_destroy(provider);
123122
oidc_debug(r, "leave");
124123
}
125124

@@ -234,7 +233,6 @@ int oidc_logout_request(request_rec *r, oidc_cfg_t *c, oidc_session_t *session,
234233
}
235234
if (provider) {
236235
oidc_logout_cleanup_by_sid(r, sid, c, provider, revoke_tokens);
237-
oidc_cfg_provider_destroy(provider);
238236
} else {
239237
oidc_info(r, "No provider for front channel logout found");
240238
}
@@ -434,10 +432,6 @@ static int oidc_logout_backchannel(request_rec *r, oidc_cfg_t *cfg) {
434432
oidc_jwt_destroy(jwt);
435433
jwt = NULL;
436434
}
437-
if (provider != NULL) {
438-
oidc_cfg_provider_destroy(provider);
439-
provider = NULL;
440-
}
441435

442436
oidc_http_hdr_err_out_add(r, OIDC_HTTP_HDR_CACHE_CONTROL, "no-cache, no-store");
443437
oidc_http_hdr_err_out_add(r, OIDC_HTTP_HDR_PRAGMA, "no-cache");
@@ -524,7 +518,5 @@ int oidc_logout(request_rec *r, oidc_cfg_t *c, oidc_session_t *session) {
524518
url = s_logout_request;
525519
}
526520

527-
oidc_cfg_provider_destroy(provider);
528-
529521
return oidc_logout_request(r, c, session, url, TRUE);
530522
}

src/handle/refresh.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,6 @@ int oidc_refresh_token_request(request_rec *r, oidc_cfg_t *c, oidc_session_t *se
389389
/* add the redirect location header */
390390
oidc_http_hdr_out_location_set(r, return_to);
391391

392-
oidc_cfg_provider_destroy(provider);
393-
394392
return HTTP_MOVED_TEMPORARILY;
395393
}
396394

@@ -427,21 +425,16 @@ apr_byte_t oidc_refresh_access_token_before_expiry(request_rec *r, oidc_cfg_t *c
427425
if (t_expires > apr_time_now())
428426
return TRUE;
429427

430-
if (oidc_get_provider_from_session(r, cfg, session, &provider) == FALSE) {
431-
oidc_cfg_provider_destroy(provider);
428+
if (oidc_get_provider_from_session(r, cfg, session, &provider) == FALSE)
432429
return FALSE;
433-
}
434430

435431
if (oidc_refresh_token_grant(r, cfg, session, provider, NULL, NULL, NULL) == FALSE) {
436432
oidc_warn(r, "access_token could not be refreshed");
437-
oidc_cfg_provider_destroy(provider);
438433
*needs_save = FALSE;
439434
return FALSE;
440435
}
441436

442437
*needs_save = TRUE;
443438

444-
oidc_cfg_provider_destroy(provider);
445-
446439
return TRUE;
447440
}

src/handle/response.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ static int oidc_response_process(request_rec *r, oidc_cfg_t *c, oidc_session_t *
565565
oidc_http_hdr_out_location_set(r,
566566
oidc_util_absolute_url(r, c, oidc_cfg_default_sso_url_get(c)));
567567
OIDC_METRICS_COUNTER_INC(r, c, OM_AUTHN_RESPONSE_ERROR_STATE_MISMATCH);
568-
oidc_cfg_provider_destroy(provider);
569568
return HTTP_MOVED_TEMPORARILY;
570569
}
571570
oidc_error(r,
@@ -578,7 +577,7 @@ static int oidc_response_process(request_rec *r, oidc_cfg_t *c, oidc_session_t *
578577
}
579578

580579
OIDC_METRICS_COUNTER_INC(r, c, OM_AUTHN_RESPONSE_ERROR_STATE_MISMATCH);
581-
oidc_cfg_provider_destroy(provider);
580+
582581
return oidc_util_html_send_error(r, "Invalid Authorization Response",
583582
"Could not match the authorization response to an earlier request via "
584583
"the state parameter and corresponding state cookie",
@@ -588,21 +587,18 @@ static int oidc_response_process(request_rec *r, oidc_cfg_t *c, oidc_session_t *
588587
/* see if the response is an error response */
589588
if (apr_table_get(params, OIDC_PROTO_ERROR) != NULL) {
590589
OIDC_METRICS_COUNTER_INC(r, c, OM_AUTHN_RESPONSE_ERROR_PROVIDER);
591-
oidc_cfg_provider_destroy(provider);
592590
return oidc_response_authorization_error(r, c, proto_state, apr_table_get(params, OIDC_PROTO_ERROR),
593591
apr_table_get(params, OIDC_PROTO_ERROR_DESCRIPTION));
594592
}
595593

596594
/* handle the code, implicit or hybrid flow */
597595
if (oidc_response_flows(r, c, proto_state, provider, params, response_mode, &jwt) == FALSE) {
598596
OIDC_METRICS_COUNTER_INC(r, c, OM_AUTHN_RESPONSE_ERROR_PROTOCOL);
599-
oidc_cfg_provider_destroy(provider);
600597
return oidc_response_authorization_error(r, c, proto_state, "Error in handling response type.", NULL);
601598
}
602599

603600
if (jwt == NULL) {
604601
oidc_error(r, "no id_token was provided");
605-
oidc_cfg_provider_destroy(provider);
606602
return oidc_response_authorization_error(r, c, proto_state, "No id_token was provided.", NULL);
607603
}
608604

@@ -638,7 +634,6 @@ static int oidc_response_process(request_rec *r, oidc_cfg_t *c, oidc_session_t *
638634
if (_oidc_strcmp(session->remote_user, r->user) != 0) {
639635
oidc_warn(r, "user set from new id_token is different from current one");
640636
oidc_jwt_destroy(jwt);
641-
oidc_cfg_provider_destroy(provider);
642637
return oidc_response_authorization_error(r, c, proto_state, "User changed!", NULL);
643638
}
644639
}
@@ -652,7 +647,6 @@ static int oidc_response_process(request_rec *r, oidc_cfg_t *c, oidc_session_t *
652647
apr_table_get(params, OIDC_PROTO_STATE), original_url, userinfo_jwt) == FALSE) {
653648
oidc_proto_state_destroy(proto_state);
654649
oidc_jwt_destroy(jwt);
655-
oidc_cfg_provider_destroy(provider);
656650
return HTTP_INTERNAL_SERVER_ERROR;
657651
}
658652

@@ -662,15 +656,13 @@ static int oidc_response_process(request_rec *r, oidc_cfg_t *c, oidc_session_t *
662656
oidc_error(r, "remote user could not be set");
663657
oidc_jwt_destroy(jwt);
664658
OIDC_METRICS_COUNTER_INC(r, c, OM_AUTHN_RESPONSE_ERROR_REMOTE_USER);
665-
oidc_cfg_provider_destroy(provider);
666659
return oidc_response_authorization_error(
667660
r, c, proto_state, "Remote user could not be set: contact the website administrator", NULL);
668661
}
669662

670663
/* cleanup */
671664
oidc_proto_state_destroy(proto_state);
672665
oidc_jwt_destroy(jwt);
673-
oidc_cfg_provider_destroy(provider);
674666

675667
/* check that we've actually authenticated a user; functions as error handling for oidc_get_remote_user */
676668
if (r->user == NULL) {

src/handle/session_management.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,23 @@ int oidc_session_management(request_rec *r, oidc_cfg_t *c, oidc_session_t *sessi
173173
/* see if this is a request for the OP iframe */
174174
if (_oidc_strcmp("iframe_op", cmd) == 0) {
175175
if (oidc_cfg_provider_check_session_iframe_get(provider) != NULL) {
176-
oidc_cfg_provider_destroy(provider);
177176
return oidc_session_management_iframe_op(r, c, session,
178177
oidc_cfg_provider_check_session_iframe_get(provider));
179178
}
180-
oidc_cfg_provider_destroy(provider);
181179
return HTTP_NOT_FOUND;
182180
}
183181

184182
/* see if this is a request for the RP iframe */
185183
if (_oidc_strcmp("iframe_rp", cmd) == 0) {
186184
if ((oidc_cfg_provider_client_id_get(provider) != NULL) &&
187185
(oidc_cfg_provider_check_session_iframe_get(provider) != NULL)) {
188-
oidc_cfg_provider_destroy(provider);
189186
return oidc_session_management_iframe_rp(r, c, session,
190187
oidc_cfg_provider_client_id_get(provider),
191188
oidc_cfg_provider_check_session_iframe_get(provider));
192189
}
193190
oidc_debug(r, "iframe_rp command issued but no client (%s) and/or no check_session_iframe (%s) set",
194191
oidc_cfg_provider_client_id_get(provider),
195192
oidc_cfg_provider_check_session_iframe_get(provider));
196-
oidc_cfg_provider_destroy(provider);
197193
return HTTP_NOT_FOUND;
198194
}
199195

@@ -206,7 +202,6 @@ int oidc_session_management(request_rec *r, oidc_cfg_t *c, oidc_session_t *sessi
206202
* those for the redirect_uri itself; do we need to store those as part of the
207203
* session now?
208204
*/
209-
oidc_cfg_provider_destroy(provider);
210205
return oidc_request_authenticate_user(
211206
r, c, provider, apr_psprintf(r->pool, "%s?session=iframe_rp", oidc_util_redirect_uri(r, c)), NULL,
212207
id_token_hint, "none", oidc_cfg_dir_path_auth_request_params_get(r),
@@ -216,7 +211,5 @@ int oidc_session_management(request_rec *r, oidc_cfg_t *c, oidc_session_t *sessi
216211
/* handle failure in fallthrough */
217212
oidc_error(r, "unknown command: %s", cmd);
218213

219-
oidc_cfg_provider_destroy(provider);
220-
221214
return HTTP_INTERNAL_SERVER_ERROR;
222215
}

src/handle/userinfo.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ apr_byte_t oidc_userinfo_refresh_claims(request_rec *r, oidc_cfg_t *cfg, oidc_se
197197
/* get the current provider info */
198198
if (oidc_get_provider_from_session(r, cfg, session, &provider) == FALSE) {
199199
*needs_save = TRUE;
200-
oidc_cfg_provider_destroy(provider);
201200
return FALSE;
202201
}
203202

@@ -239,8 +238,6 @@ apr_byte_t oidc_userinfo_refresh_claims(request_rec *r, oidc_cfg_t *cfg, oidc_se
239238

240239
oidc_debug(r, "return: %d", rc);
241240

242-
oidc_cfg_provider_destroy(provider);
243-
244241
return rc;
245242
}
246243

0 commit comments

Comments
 (0)