SELECT FOR UPDATE Not working with Write/Read set ups
Hello guys,
It seems the FOR UPDATE queries are not being parsed correctly, please take a look of the error below:
The following query is not working:
WordPress database error The MySQL server is running with the --read-only option so it cannot execute this statement for query SELECT * FROMwp_wfls_2fa_secretsWHEREuser_id= 1 FOR UPDATE made by do_action(‘wp_ajax_nopriv_wordfence_ls_authenticate’), WP_Hook->do_action, WP_Hook->apply_filters, WordfenceLS\\Controller_AJAX->_ajax_handler, call_user_func, WordfenceLS\\Controller_AJAX->_ajax_authenticate_callback, wp_authenticate, apply_filters(‘authenticate’), WP_Hook->apply_filters, WordfenceLS\\Controller_WordfenceLS->_authenticate, WordfenceLS\\Controller_TOTP->validate_2fa, wpdb->get_row, wpdb->print_error
Our current set up to below:
$wpdb->add_database(array(
'host' => DB_WRITE_HOSTNAME, // Use the RDS cluster write endpoint here
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
'write' => 1, // master can write
'read' => !defined('DB_READ_HOSTNAME') ? 1 : 0, // master only reads if no replica is configured
));
if(defined('DB_READ_HOSTNAME')):
$wpdb->add_database(array(
'host' => DB_READ_HOSTNAME, // Read endpoint here
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
'write' => 0,
'read' => 1,
));
endif;
A possible solution it would be to include FOR in the Possible writes check
if ( preg_match( '/(?:ALTER|CREATE|ANALYZE|CHECK|OPTIMIZE|REPAIR|CALL|DELETE|DROP|INSERT|LOAD|REPLACE|UPDATE|SET|RENAME|FOR)\s/i', $q ) ) {
But UPDATE is already included. I want to review regex...
Yes I noticed it so the right way to go it would be to work out on the regex, however, the proposed the solution above (including the FOR) it does the job for me, at least as a temporary solution, I have this code on prod so needed to fix it asap. Thanks though!