Sequential relative URLs with colons in the text not rendering correctly using the render_markdown templatetag #6247

Closed
opened 2025-12-29 19:38:28 +01:00 by adam · 2 comments
Owner

Originally created by @drygdryg on GitHub (Mar 23, 2022).

Originally assigned to: @kkthxbye-code on GitHub.

NetBox version

v3.1.9

Python version

3.10

Steps to Reproduce

  1. Run NetBox shell and import the render_markdown templatetag:
$ /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py nbshell
### NetBox interactive shell (ubuntuserver)
### Python 3.10.2+ | Django 3.2.12 | NetBox 3.1.9
### lsmodels() will show available models. Use help(<model>) for more info.
>>> from utilities.templatetags.helpers import render_markdown
  1. Try to render test case URLs:
>>> render_markdown('[TEST](/dcim/devices/1/)')
'<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST</a></p></div>'  # Correct
>>> render_markdown('[TEST:](/dcim/devices/1/)')
'<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST:</a></p></div>'  # Correct
>>> render_markdown('[TEST](/dcim/devices/1/) [TEST](/dcim/devices/1/)')
'<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST</a> <a href="/dcim/devices/1/">TEST</a></p></div>'  # Correct
>>> render_markdown('[TEST](/dcim/devices/1/) [TEST:](/dcim/devices/1/)')
'<div class="rendered-markdown"><p>[TEST](](/dcim/devices/1/)</p></div>'  # Incorrect
>>> render_markdown('[TEST](https://netbox.site/dcim/devices/1/) [TEST:](https://netbox.site/dcim/devices/1/)')
'<div class="rendered-markdown"><p><a href="https://netbox.site/dcim/devices/1/">TEST</a> <a href="https://netbox.site/dcim/devices/1/">TEST:</a></p></div>'  # Correct

Expected Behavior

The string

[TEST](/dcim/devices/1/) [TEST:](/dcim/devices/1/)

should be rendered as

<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST</a> <a href="/dcim/devices/1/">TEST:</a></p></div>

Observed Behavior

The string

[TEST](/dcim/devices/1/) [TEST:](/dcim/devices/1/)

was rendered as

<div class="rendered-markdown"><p>[TEST](](/dcim/devices/1/)</p></div>
Originally created by @drygdryg on GitHub (Mar 23, 2022). Originally assigned to: @kkthxbye-code on GitHub. ### NetBox version v3.1.9 ### Python version 3.10 ### Steps to Reproduce 1. Run NetBox shell and import the `render_markdown` templatetag: ``` $ /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py nbshell ### NetBox interactive shell (ubuntuserver) ### Python 3.10.2+ | Django 3.2.12 | NetBox 3.1.9 ### lsmodels() will show available models. Use help(<model>) for more info. >>> from utilities.templatetags.helpers import render_markdown ``` 2. Try to render test case URLs: ```python >>> render_markdown('[TEST](/dcim/devices/1/)') '<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST</a></p></div>' # Correct >>> render_markdown('[TEST:](/dcim/devices/1/)') '<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST:</a></p></div>' # Correct >>> render_markdown('[TEST](/dcim/devices/1/) [TEST](/dcim/devices/1/)') '<div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST</a> <a href="/dcim/devices/1/">TEST</a></p></div>' # Correct >>> render_markdown('[TEST](/dcim/devices/1/) [TEST:](/dcim/devices/1/)') '<div class="rendered-markdown"><p>[TEST](](/dcim/devices/1/)</p></div>' # Incorrect >>> render_markdown('[TEST](https://netbox.site/dcim/devices/1/) [TEST:](https://netbox.site/dcim/devices/1/)') '<div class="rendered-markdown"><p><a href="https://netbox.site/dcim/devices/1/">TEST</a> <a href="https://netbox.site/dcim/devices/1/">TEST:</a></p></div>' # Correct ``` ### Expected Behavior The string ``` [TEST](/dcim/devices/1/) [TEST:](/dcim/devices/1/) ``` should be rendered as ``` <div class="rendered-markdown"><p><a href="/dcim/devices/1/">TEST</a> <a href="/dcim/devices/1/">TEST:</a></p></div> ``` ### Observed Behavior The string ``` [TEST](/dcim/devices/1/) [TEST:](/dcim/devices/1/) ``` was rendered as ``` <div class="rendered-markdown"><p>[TEST](](/dcim/devices/1/)</p></div> ```
adam added the type: bugstatus: accepted labels 2025-12-29 19:38:28 +01:00
adam closed this issue 2025-12-29 19:38:28 +01:00
Author
Owner

@drygdryg commented on GitHub (Mar 23, 2022):

I think there is a problem with the regular expression that captures colons in the URL text
6807db4967/netbox/utilities/templatetags/helpers.py (L52)
Possible the right change (just allow only characters allowed for URL scheme):

    pattern = fr'\[([^\]]+)\]\((?!({schemes}))[A-Za-z0-9\.+-]*:(.+)\)' 

Running the corrected regex in Regex101.com:
image

@drygdryg commented on GitHub (Mar 23, 2022): I think there is a problem with the regular expression that captures colons in the URL text https://github.com/netbox-community/netbox/blob/6807db4967934e2df87762bdf4255f672bf9e6f1/netbox/utilities/templatetags/helpers.py#L52 Possible the right change (just allow only characters [allowed for URL scheme](https://stackoverflow.com/a/3641782/9310156)): ```python pattern = fr'\[([^\]]+)\]\((?!({schemes}))[A-Za-z0-9\.+-]*:(.+)\)' ``` Running the corrected regex in Regex101.com: ![image](https://user-images.githubusercontent.com/43933400/159667432-4fca1744-b2b4-4816-a02e-43b298b35b37.png)
Author
Owner

@github-actions[bot] commented on GitHub (May 23, 2022):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@github-actions[bot] commented on GitHub (May 23, 2022): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6247