@@ -23,6 +23,7 @@ ngx_http_auth_digest_create_loc_conf(ngx_conf_t *cf)
2323
2424 conf -> timeout = NGX_CONF_UNSET_UINT ;
2525 conf -> expires = NGX_CONF_UNSET_UINT ;
26+ conf -> drop_time = NGX_CONF_UNSET_UINT ;
2627 conf -> replays = NGX_CONF_UNSET_UINT ;
2728 return conf ;
2829}
@@ -36,6 +37,7 @@ ngx_http_auth_digest_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
3637
3738 ngx_conf_merge_sec_value (conf -> timeout , prev -> timeout , 60 );
3839 ngx_conf_merge_sec_value (conf -> expires , prev -> expires , 10 );
40+ ngx_conf_merge_sec_value (conf -> drop_time , prev -> drop_time , 300 );
3941 ngx_conf_merge_value (conf -> replays , prev -> replays , 20 );
4042 ngx_conf_merge_str_value (conf -> realm , prev -> realm , "" )
4143 if (conf -> user_file .value .len == 0 ) {
@@ -686,12 +688,20 @@ ngx_http_auth_digest_verify_hash(ngx_http_request_t *r, ngx_http_auth_digest_cre
686688 // make sure nonce and nc are both valid
687689 ngx_shmtx_lock (& shpool -> mutex );
688690 found = (ngx_http_auth_digest_node_t * )ngx_http_auth_digest_rbtree_find (key , ngx_http_auth_digest_rbtree -> root , ngx_http_auth_digest_rbtree -> sentinel );
689- if (found != NULL && ngx_bitvector_test (found -> nc , nc )){
691+ if (found != NULL ){
692+ if (found -> expires <= ngx_time ()) {
693+ fields -> stale = 1 ;
694+ goto invalid ;
695+ }
696+ if (!ngx_bitvector_test (found -> nc , nc )){
697+ goto invalid ;
698+ }
690699 if (ngx_bitvector_test (found -> nc , 0 )){
691700 // if this is the first use of this nonce, switch the expiration time from the timeout
692701 // param to now+expires. using the 0th element of the nc vector to flag this...
693702 ngx_bitvector_set (found -> nc , 0 );
694703 found -> expires = ngx_time () + alcf -> expires ;
704+ found -> drop_time = ngx_time () + alcf -> drop_time ;
695705 }
696706
697707 // mark this nc as ‘used’ to prevent replays
@@ -743,6 +753,7 @@ ngx_http_auth_digest_verify_hash(ngx_http_request_t *r, ngx_http_auth_digest_cre
743753 info_header -> hash = 1 ;
744754 return NGX_OK ;
745755 }else {
756+ invalid :
746757 // nonce is invalid/expired or client reused an nc value. suspicious...
747758 ngx_shmtx_unlock (& shpool -> mutex );
748759 return NGX_DECLINED ;
@@ -1046,7 +1057,7 @@ static void ngx_http_auth_digest_rbtree_prune_walk(ngx_rbtree_node_t *node, ngx_
10461057 }
10471058
10481059 ngx_http_auth_digest_node_t * dnode = (ngx_http_auth_digest_node_t * ) node ;
1049- if (dnode -> expires <= ngx_time ()){
1060+ if (dnode -> drop_time <= ngx_time ()){
10501061 ngx_rbtree_node_t * * dropnode = ngx_array_push (ngx_http_auth_digest_cleanup_list );
10511062 dropnode [0 ] = node ;
10521063 }
@@ -1088,6 +1099,7 @@ static ngx_http_auth_digest_nonce_t ngx_http_auth_digest_next_nonce(ngx_http_req
10881099 return nonce ;
10891100 }
10901101 node -> expires = nonce .t + alcf -> timeout ;
1102+ node -> drop_time = nonce .t + alcf -> timeout ;
10911103 ngx_memset (node -> nc , 0xff , ngx_bitvector_size (1 + alcf -> replays ));
10921104 ((ngx_rbtree_node_t * )node )-> key = key ;
10931105 ngx_rbtree_insert (ngx_http_auth_digest_rbtree , & node -> node );
0 commit comments