Custom Fields & Unreliable Regex

  • Posts: 55
  • Thank you received: 2
  • Hikaserial Subscription
9 years 1 month ago #235651

-- url of the page with the problem -- : www.ultamation.com
-- HikaShop version -- : 2.6.1
-- Joomla version -- : 2.5.11
-- PHP version -- : 5.3.29

Hi,

We have a custom item field for capturing information specific to an order line at the point it's put into the basket. The information is an IP address and we're using the regex feature to validate the IP address before it's accepted, however, the regex doesn't appear to work properly all the time.

The config is as follows:

Label IPv4 Address
Table item
Column name lic_ipv4_addr
Field type text
Required Yes
Custom Error Message The licence requires a valid IPv4 address for the target device (dotted quad, no leading zeros). You must know this before purchasing and changes will require an additional purchase.
Default value
Regular expression check ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
Attributes
Placeholder
Input filtering Yes
Maximum length 15
Size
Read only No
Translatable No

The regex
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
works in general, but we've just found out that HikaShop **incorrectly** accepts "192168.1.1"

We've tried other regex strings, such as;
^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$
And this doesn't work at all - even for valid ip addresses.

For our licencing model, it is imperative that we correctly cover only VALID (base 10) addresses (e.g. 192.168.001.001 should be disallowed) so we can't go overly simple on the regex.

Any ideas?!
Thanks,
Oliver

Please Log in or Create an account to join the conversation.

  • Posts: 83858
  • Thank you received: 13576
  • MODERATOR
9 years 1 month ago #235658

Hi,

Well, you just need to write the regex so that it accepts only what you want.
I'm not sure what you want.
You said that you don't want 192.168.1.1 but that's a totally valid IP address.
So I understand that you don't want local IP addresses.
But then, it means that you should also not accept 10.*.*.* nor the range 172.16.0.0 - 172.31.255.255 which are also both local IP addresses ranges, no ?
And in that case, it makes the regex quite complex and long to write.

Frankly, such complex rules with differents ranges being excluded with four different numbers should probably not be made with a regex. In that case, I would personally write a custom plugin to add my one custom type of custom fields. That way, I could have my own PHP check function and do all the tests I want easily rather than trying to cram everything into one regex. That way, I could also prevent impossible IP addresses like 0.0.0.0 or 123.123.123.0 etc. Because I don't see a way to have a regex handling all these restrictions.

If you want to write your own plugin for a custom field type, you can look at our Fields API:
www.hikashop.com/support/documentation/6...entation.html#fields
And at the folder plugins/hikashop/datepickerfield/datepickerfield.php which adds the "advanced date picker" field type to HikaShop when the plugin is activated.

Last edit: 9 years 1 month ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 55
  • Thank you received: 2
  • Hikaserial Subscription
9 years 1 month ago #235846

Hi Nicolas,

I tried to be clear, but I obviously failed to explain properly.

The point I am trying to make is that *some* regular expression that are *correct*, are giving either false positives, or false negatives in HikaShop. Regardless of your opinion on complexity or whether this is a valid effort or not, this behaviour is incorrect.

Frankly, if you provide a regex function to validate input, it should work.

The plugin option isn't very attractive as regex would be quite satisfactory for me (if it worked) and I'd prefer to maintain as standard an installation as possible.

Regarding the IP addresses - you misunderstood.
192.168.1.1 would be valid
192168.1.1 is invalid as there is a period missing between the 192.168 element
192.168.001.001 is invalid as (in line with the RFC) an octet that begins with a 0 prefix (excluding the single '0') is to be treated as octal - which is invalid in my case.
The regex expressions I provided are valid, and do correctly match the forms I wish to accept - as I say, there appears to be something else going on that is giving erroneous results in HikaShop.

It could the version of PHP for all I know!

Please Log in or Create an account to join the conversation.

  • Posts: 83858
  • Thank you received: 13576
  • MODERATOR
9 years 1 month ago #235878

Hi,

That's because the regex is used directly in the javascript check without escaping slashes.
Use that regex and it will work:
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

The following user(s) said Thank You: ohall

Please Log in or Create an account to join the conversation.

  • Posts: 55
  • Thank you received: 2
  • Hikaserial Subscription
9 years 1 month ago #236144

It doesn't work

Apologies - it does work - I had included a trailing space at the end.

Last edit: 9 years 1 month ago by ohall. Reason: Epic fail on my part

Please Log in or Create an account to join the conversation.

Time to create page: 0.390 seconds
Powered by Kunena Forum