Problem/Motivation

When we try to submit value like 168.0.0 we get fatal error. This happens, because widget calls IpAddress class without try catch.
Responsible code is in massageFormValues() function.

Steps to reproduce

1) Add field of type 'IP Address' to some Content Type.
2) Try to create node with invalid IP.

Proposed resolution

Add try catch in massageFormValues() function.

Remaining tasks

Review patch.

Comments

ilya.no created an issue. See original summary.

ilya.no’s picture

Attaching initial patch.

wjackson’s picture

Status: Needs review » Reviewed & tested by the community

Works for me! Marking RTBC! Thank you, ilya.no!

mikemccaffrey’s picture

I don't think the solution is to pass the value as is if it is wrong, since we do actually need to deal with the fact that the IP address is not a properly formatted range (or if someone had the gall to try to enter a range using asterisks).

The validateIpAddressElement function in IpAddressWidgetBase.php actually depends on there being an exception to validate the form field:

   // Instantiate our IP, will throw \Exception if invalid.
    try {
      $ip_address = new IpAddress($value);
    }
    catch (\Exception $e) {
      // Make error messages a bit more relevant.
      if ($settings['allow_range']) {
        $form_state->setError($element, t('Invalid IP or range.'));
      }
      else {
        $form_state->setError($element, t('Invalid IP.'));
      }
      return;
    }

The question is why the IpAddress function isn't being constructed within that try catch in the validation function on form submission.

kelly.m.jacobs’s picture

As an alternative solution, this patch will change it so we only run the massageFormValues logic if the form has passed validation.

hexblot’s picture

fixed by commit a29b74c under #3247108 — note that @ilya.no proposed the same fix in patch #2 here two years earlier; sickness29's later MR also carried the try/catch and was what ultimately landed.

hexblot’s picture

Status: Reviewed & tested by the community » Closed (outdated)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.