Error: Call to undefined function Drupal\flood_control\flood_control_get_whitelist_ips()

Created on 29 April 2024, 8 months ago
Updated 25 June 2024, 6 months ago

Problem/Motivation

Getting an issue when I try to turn on basic_auth core module on Drupal 10. This error takes down the entire site.

This function is undefined introduced from the recent patch:
https://git.drupalcode.org/project/flood_control/-/commit/5ea73c0f2261b2...

Error: Call to undefined function Drupal\flood_control\flood_control_get_whitelist_ips() in Drupal\flood_control\FloodWhiteList->isIpWhitelisted() (line 90 of/flood_control/src/FloodWhiteList.php)
#0 /flood_control/src/FloodWhiteList.php(44): Drupal\flood_control\FloodWhiteList->isIpWhitelisted()
#1 /basic_auth/src/Authentication/Provider/BasicAuth.php(92): Drupal\flood_control\FloodWhiteList->isAllowed('basic_auth.fail...', 50, 3600)

Steps to reproduce

  • Turn on flood_control
  • Turn on basic_auth
πŸ› Bug report
Status

Closed: works as designed

Version

2.3

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kvo_4x
  • πŸ‡³πŸ‡±Netherlands batigolix Utrecht

    @kvo_4x can you provide step by step instructions on how to replicate this error?

  • I am facing the same issue with the shield on. https://www.drupal.org/project/shield β†’

    Simply enable the shield alongside with flood_control

  • I can also confirm this issue happens with Flood Control and Drupal Shield module enabled, using Drupal 10.2.6. Doesn't seem to occur in Drupal 10.1.8.

    This seems to be due to the isIpWhitelisted function trying to call the flood_control_get_whitelist_ips function that is defined in the flood_control.module file:

    <?php
    ...
    protected function isIpWhitelisted(string $ipAddress = ''): bool {
        $request = $this->requestStack->getCurrentRequest();
    
        if ($request && !$ipAddress) {
          $ipAddress = $request->getClientIp() ?? '';
        }
    
        // Gets the values from the config.
        $ipsWhiteList = flood_control_get_whitelist_ips();
    .....
    ?>

    it seems to be failing to resolve that particular function when called from that location. Oddly enough, in Form/FloodControlSettingsForm.php, the code does a similar call:

    <?php
    ...
     public function validateForm(array &$form, FormStateInterface $form_state) {
        // Validating whitelisted ip addresses.
        $whitelistIps = flood_control_get_whitelist_ips($form_state->getValue('ip_white_list'));
        // Checking single ip addresses.
        if (!empty($whitelistIps['addresses'])) {
          foreach ($whitelistIps['addresses'] as $ipAddress) {
            if (!filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) {
              $form_state->setErrorByName('ip_white_list', $this->t('IP address %ip_address is not valid.', ['%ip_address' => $ipAddress]));
            }
          }
        }
    .....
    ?>

    But that one does NOT cause an error.

    The error seems to partly stem from the namespace causing PHP to look in the wrong spot, IE its looking for Drupal\flood_control\flood_control_get_whitelist_ips(), but since that function isn't in the Drupal/flood_control namespace, it fails. However, I did try doing something like:

    <?php
    ...
    protected function isIpWhitelisted(string $ipAddress = ''): bool {
    ....
    $ipsWhiteList = \flood_control_get_whitelist_ips();
    ....
    ?>

    But it results in essentially the same error (and I'm not sure if that is even that way to go about that anyways).

    I'm not sure whether this is an issue with the flood control module itself, or some issue introduced by core that is causing this issue with resolving the function.

  • πŸ‡ΊπŸ‡¦Ukraine vselivanov Kyiv, Ukraine

    Hi!
    Have the same error. It happens because shield middleware do it's job before modules are loaded. It calls authenticate and flood whitelist checks, but there is no flood_control_get_whitelist_ips() function yet.

    This patch to the shield module helped me:
    https://www.drupal.org/project/shield/issues/3277210#comment-15104934 πŸ› Shield middleware invokes hooks before modules are loaded, corrupting module_implements cache Needs work

  • Status changed to Closed: works as designed 6 months ago
  • πŸ‡¦πŸ‡ΊAustralia elc

    This very much sounds like a bug in Shield rather than Flood Control - there's no way functionality will exist if the modules haven't been loaded yet! It is outside the bounds of reason for a module to ensure that a file that is always loaded is loaded in every situation.

    Looking at what Shield does, it would be better to use Apache/Nginx for the HTTP auth so that the site underneath is not loaded at all until valid credentials are provided.

    Anyway, this is not a bug for this module.

Production build 0.71.5 2024