Fatal error in case of invalid value submitted

Created on 10 March 2022, over 2 years ago
Updated 5 October 2023, about 1 year ago

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.

πŸ› Bug report
Status

RTBC

Version

2.0

Component

Code

Created by

πŸ‡·πŸ‡ΊRussia ilya.no

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States mikemccaffrey

    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.

  • πŸ‡ΊπŸ‡ΈUnited States kelly.m.jacobs

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

Production build 0.71.5 2024