Error on non-regex search for invalid regex string #7523

Closed
opened 2025-12-29 20:24:43 +01:00 by adam · 8 comments
Owner

Originally created by @sleepinggenius2 on GitHub (Jan 16, 2023).

Originally assigned to: @kkthxbye-code, @jeremystretch on GitHub.

NetBox version

v3.4.2

Python version

3.8

Steps to Reproduce

  1. Create a Site with Name = "Foo (Bar)" and Slug = "foo-bar"
  2. Perform a default global search or explicit Partial match search for the string "(Bar"
  3. Receive error

Expected Behavior

The Site should have been found and the string appropriately highlighted in the result. I believe this is caused by the use of re.split in utilities.utils.highlight_string, as I could not find where the highlight string is being escaped for non-regex searches.

Observed Behavior

The following error is received:

<class 're.error'>

missing ), unterminated subpattern at position 0

Python version: 3.8.10
NetBox version: 3.4.2
Originally created by @sleepinggenius2 on GitHub (Jan 16, 2023). Originally assigned to: @kkthxbye-code, @jeremystretch on GitHub. ### NetBox version v3.4.2 ### Python version 3.8 ### Steps to Reproduce 1. Create a *Site* with Name = *"Foo (Bar)"* and Slug = *"foo-bar"* 2. Perform a default global search or explicit *Partial match* search for the string *"(Bar"* 3. Receive error ### Expected Behavior The *Site* should have been found and the string appropriately highlighted in the result. I believe this is caused by the use of `re.split` in `utilities.utils.highlight_string`, as I could not find where the highlight string is being escaped for non-regex searches. ### Observed Behavior The following error is received: ``` <class 're.error'> missing ), unterminated subpattern at position 0 Python version: 3.8.10 NetBox version: 3.4.2 ```
adam added the type: bugstatus: accepted labels 2025-12-29 20:24:43 +01:00
adam closed this issue 2025-12-29 20:24:43 +01:00
Author
Owner

@sleepinggenius2 commented on GitHub (Jan 18, 2023):

Looking at the solution that was merged, it seems like it is now escaping everything, including search strings that were supposed to be regex. Do we not still want to maintain that functionality for regex searches? It seems to make more sense to me to do something like:

93685d92a4/netbox/netbox/views/misc.py (L164)

to

highlight = re.escape(form.cleaned_data['q']) if lookup != LookupTypes.REGEX else form.cleaned_data['q']
@sleepinggenius2 commented on GitHub (Jan 18, 2023): Looking at the solution that was merged, it seems like it is now escaping everything, including search strings that were supposed to be regex. Do we not still want to maintain that functionality for regex searches? It seems to make more sense to me to do something like: https://github.com/netbox-community/netbox/blob/93685d92a40de88688b24ab7150d549275b236e9/netbox/netbox/views/misc.py#L164 to ```python highlight = re.escape(form.cleaned_data['q']) if lookup != LookupTypes.REGEX else form.cleaned_data['q'] ```
Author
Owner

@kkthxbye-code commented on GitHub (Jan 18, 2023):

@sleepinggenius2 - Good catch, I just totally forgot regex seach was added. Do you want to test your proposal and submit a PR?

@kkthxbye-code commented on GitHub (Jan 18, 2023): @sleepinggenius2 - Good catch, I just totally forgot regex seach was added. Do you want to test your proposal and submit a PR?
Author
Owner

@sleepinggenius2 commented on GitHub (Jan 19, 2023):

I can try, but it will take me a bit to get a development environment set back up again for 3.4. I'm also not quite sure how to go about formal testing, as I don't see where there are currently test cases defined for the search functionality and my knowledge of Python and Django is not at a level where I would even know how to begin to do that from scratch. I can tell you that changing that line (and adding import re to the file) on our server makes the error go away and seems to allow it to highlight properly for all the lookup types.

@sleepinggenius2 commented on GitHub (Jan 19, 2023): I can try, but it will take me a bit to get a development environment set back up again for 3.4. I'm also not quite sure how to go about formal testing, as I don't see where there are currently test cases defined for the search functionality and my knowledge of Python and Django is not at a level where I would even know how to begin to do that from scratch. I can tell you that changing that line (and adding `import re` to the file) on our server makes the error go away and seems to allow it to highlight properly for all the lookup types.
Author
Owner

@jeremystretch commented on GitHub (Jan 20, 2023):

I think I've fixed this in 591104177. Thanks for the catch.

@jeremystretch commented on GitHub (Jan 20, 2023): I think I've fixed this in 591104177. Thanks for the catch.
Author
Owner

@opericgithub commented on GitHub (Jan 20, 2023):

I still receive the same error in 3.4.3. I have device named "(9)123" (without quotes). When searching for "(9" without quotes in global search I get the error, but when searching for "(9)" I get regular search results.

@opericgithub commented on GitHub (Jan 20, 2023): I still receive the same error in 3.4.3. I have device named "(9)123" (without quotes). When searching for "(9" without quotes in global search I get the error, but when searching for "(9)" I get regular search results.
Author
Owner

@sleepinggenius2 commented on GitHub (Jan 20, 2023):

Yeah, I don't see where the non-regex path was actually impacted. I think this:

97723b1f96/netbox/netbox/views/misc.py (L171)

should have been this:

highlight = re.escape(form.cleaned_data['q'])

or the escaping from #11525 should have been kept here:

97723b1f96/netbox/utilities/utils.py (L530)

@sleepinggenius2 commented on GitHub (Jan 20, 2023): Yeah, I don't see where the non-regex path was actually impacted. I think this: https://github.com/netbox-community/netbox/blob/97723b1f96735de2c95d92af58a9292b7a69affd/netbox/netbox/views/misc.py#L171 should have been this: ```python highlight = re.escape(form.cleaned_data['q']) ``` or the escaping from #11525 should have been kept here: https://github.com/netbox-community/netbox/blob/97723b1f96735de2c95d92af58a9292b7a69affd/netbox/utilities/utils.py#L530
Author
Owner

@jeremystretch commented on GitHub (Jan 20, 2023):

Whoops. In introducing a new logic path for regex searches I seem to have lost the escape function.

@jeremystretch commented on GitHub (Jan 20, 2023): Whoops. In introducing a new logic path for regex searches I seem to have lost the escape function.
Author
Owner

@jeremystretch commented on GitHub (Jan 20, 2023):

I've opened #11555 for this.

@jeremystretch commented on GitHub (Jan 20, 2023): I've opened #11555 for this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7523