Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Fields Validation regex field is too small #5719

Closed
ggiesen opened this issue Jan 29, 2021 · 3 comments
Closed

Custom Fields Validation regex field is too small #5719

ggiesen opened this issue Jan 29, 2021 · 3 comments

Comments

@ggiesen
Copy link

ggiesen commented Jan 29, 2021

Environment

  • Python version: 3.6.8
  • NetBox version: 2.10.2

Proposed Functionality

Increase the length of the Validation regex field in Custom Fields beyond the current 500 character limit (perhaps to something like 4096)

Use Case

In attempting to apply a validator to a custom field that defines static routes assigned to an IP on an interface, I use the following format:

<prefix>/<prefixlen> via gateway[,<prefix>/<prefixlen> via gateway]

Example:

192.0.2.0/24 via 203.0.113.1,198.51.100.0/24 via 203.0.113.1

To validate this accurately, use the following regular expression:

^\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b(,\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b)*$

Which is 838 characters long.

While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity, it serves to demonstrate how a regular expression to validate something even relatively simple can grow very long, and the current field length is inadequate

Database Changes

I expect minor changes to column sizes would be require

External Dependencies

None

@jeremystretch
Copy link
Member

This is pretty clearly excessive use of regular expressions. You're trying to leverage a regex to enforce the validity of several data types here, which is just generally a bad idea. You might consider splitting the relevant data into multiple fields or, better yet, writing a plugin to model it independently.

@DanSheps
Copy link
Member

DanSheps commented Feb 1, 2021

While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity

I think this sentence sums this up pretty well. You could validate this using a much shorter regex (repeating patterns and recursion) to accomplish the same validation and I don't see any normal circumstance where you need to exceed 500 characters for validation.

@DanSheps DanSheps added the pending closure Requires immediate attention to avoid being closed for inactivity label Feb 1, 2021
@ggiesen
Copy link
Author

ggiesen commented Feb 1, 2021

This is pretty clearly excessive use of regular expressions. You're trying to leverage a regex to enforce the validity of several data types here, which is just generally a bad idea. You might consider splitting the relevant data into multiple fields or, better yet, writing a plugin to model it independently.

I'm not sure how you'd split this into multiple fields and still make it usable. I grant writing a plugin may work but that's a much bigger step from writing a regex.

While the above expression could perhaps be shortened without sacrificing accuracy or increasing complexity

I think this sentence sums this up pretty well. You could validate this using a much shorter regex (repeating patterns and recursion) to accomplish the same validation and I don't see any normal circumstance where you need to exceed 500 characters for validation.

I've got the regex down to 482 chars:

^\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b(,\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b\/\b([0-9]|[1-2][0-9]|3[1-2])\b via \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\b)*$

It sacrifices a bit of readability, not that the original was terribly readable in the first place. Given that regex processing of things like IP addresses are very complicated, and the relatively simple example above still occupies 482 chars, I thought this was a no-brainer, but I guess I'm wrong. In the meantime I've managed to squeeze the regex down below the limit, and I'll wait for #5451 as the much better solution

@stale stale bot removed the pending closure Requires immediate attention to avoid being closed for inactivity label Feb 1, 2021
@ggiesen ggiesen closed this as completed Feb 1, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants