@@ -56,6 +56,17 @@ static inline ATTR_WARN_UNUSED mpack_error_t _imsg_destroy(
5656 dd_imsg * nonnull imsg );
5757static void _imsg_cleanup (dd_imsg * nullable * imsg );
5858
59+ static void _set_redirect_code_and_location (
60+ struct block_params * nonnull block_params ,
61+ // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
62+ int code , zend_string * nullable location ,
63+ zend_string * nullable security_response_id );
64+
65+ static void _set_block_code_and_type (struct block_params * nonnull block_params ,
66+ // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
67+ int code , dd_response_type type ,
68+ zend_string * nullable security_response_id );
69+
5970static dd_result _dd_command_exec (dd_conn * nonnull conn ,
6071 const dd_command_spec * nonnull spec , void * unspecnull ctx )
6172{
@@ -299,7 +310,8 @@ static void _imsg_cleanup(dd_imsg *nullable *imsg)
299310static void _add_appsec_span_data_frag (mpack_node_t node );
300311static void _set_appsec_span_data (mpack_node_t node );
301312
302- static void _command_process_block_parameters (mpack_node_t root )
313+ static void _command_process_block_parameters (
314+ struct block_params * nonnull block_params , mpack_node_t root )
303315{
304316 int status_code = DEFAULT_BLOCKING_RESPONSE_CODE ;
305317 dd_response_type type = DEFAULT_RESPONSE_TYPE ;
@@ -365,10 +377,12 @@ static void _command_process_block_parameters(mpack_node_t root)
365377 "Blocking parameters: status_code=%d, type=%d, security_response_id=%s" ,
366378 status_code , type ,
367379 security_response_id ? ZSTR_VAL (security_response_id ) : "NULL" );
368- dd_set_block_code_and_type (status_code , type , security_response_id );
380+ _set_block_code_and_type (
381+ block_params , status_code , type , security_response_id );
369382}
370383
371- static void _command_process_redirect_parameters (mpack_node_t root )
384+ static void _command_process_redirect_parameters (
385+ struct block_params * nonnull block_params , mpack_node_t root )
372386{
373387 int status_code = 0 ;
374388 zend_string * location = NULL ;
@@ -423,9 +437,52 @@ static void _command_process_redirect_parameters(mpack_node_t root)
423437 "security_response_id=%s" ,
424438 status_code , location ? ZSTR_VAL (location ) : "NULL" ,
425439 security_response_id ? ZSTR_VAL (security_response_id ) : "NULL" );
426- dd_set_redirect_code_and_location (
427- status_code , location , security_response_id );
440+
441+ _set_redirect_code_and_location (
442+ block_params , status_code , location , security_response_id );
428443}
444+
445+ static void _set_block_code_and_type (struct block_params * nonnull block_params ,
446+ // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
447+ int code , dd_response_type type , zend_string * nullable security_response_id )
448+ {
449+ dd_response_type _type = type ;
450+ // Account for lack of enum type safety
451+ switch (type ) {
452+ case response_type_auto :
453+ case response_type_html :
454+ case response_type_json :
455+ _type = type ;
456+ break ;
457+ default :
458+ _type = response_type_auto ;
459+ break ;
460+ }
461+
462+ block_params -> security_response_id = security_response_id ;
463+ block_params -> response_type = _type ;
464+ block_params -> response_code = code ;
465+ block_params -> redirection_location = NULL ;
466+ }
467+
468+ static void _set_redirect_code_and_location (
469+ struct block_params * nonnull block_params ,
470+ // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
471+ int code , zend_string * nullable location ,
472+ zend_string * nullable security_response_id )
473+ {
474+ int response_code = DEFAULT_REDIRECTION_RESPONSE_CODE ;
475+
476+ // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
477+ if (code >= 300 && code < 400 ) {
478+ response_code = code ;
479+ }
480+ block_params -> security_response_id = security_response_id ;
481+ block_params -> response_type = response_type_auto ;
482+ block_params -> response_code = response_code ;
483+ block_params -> redirection_location = location ;
484+ }
485+
429486static void _command_process_stack_trace_parameters (mpack_node_t root )
430487{
431488 size_t count = mpack_node_map_count (root );
@@ -540,13 +597,14 @@ static dd_result _command_process_actions(
540597 if (dd_mpack_node_lstr_eq (verdict , "block" ) && res != dd_should_block &&
541598 res != dd_should_redirect ) { // Redirect take over block
542599 res = dd_should_block ;
543- _command_process_block_parameters (mpack_node_array_at (action , 1 ));
600+ _command_process_block_parameters (
601+ & ctx -> block_params , mpack_node_array_at (action , 1 ));
544602 dd_tags_add_blocked ();
545603 } else if (dd_mpack_node_lstr_eq (verdict , "redirect" ) &&
546604 res != dd_should_redirect ) {
547605 res = dd_should_redirect ;
548606 _command_process_redirect_parameters (
549- mpack_node_array_at (action , 1 ));
607+ & ctx -> block_params , mpack_node_array_at (action , 1 ));
550608 dd_tags_add_blocked ();
551609 } else if (dd_mpack_node_lstr_eq (verdict , "record" ) &&
552610 res == dd_success ) {
0 commit comments