Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#20022 closed Bug (fixed)

reverse() breaks on URLs which include a ~ character

Reported by: mxheyeck@… Owned by: bmispelon
Component: Core (URLs) Version: 1.5
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Testing on my local machine through Apache, so the root URL is at:

http://localhost/~mxheyeck/sicariid/

Calling reverse on one of my views inside views.py, e.g.:

s = reverse(locate_task_enter, args=(task_id,))

Breaks with the cryptic message:

"a float is required"

A little examination reveals what's going on. Break is at line 391 of urlresolvers.py.

candidate = (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args))

prefix_norm is:

u'/%7Emxheyeck/sicariid/'

So line 391 is equivalent to:

u'/%7Emxheyeck/sicariid/locate_task_enter/%(task_id)s/' % {u'task_id': u'419'}

Where '%7E' is both a urlencoded tilde and, unfortunately, an upper-case floating-point exponential. So, boom.

I attach the local vars from the break at 391.

val		419L
pattern		'locate_task_enter/(?P<task_id>\\d+)/$'
self		<RegexURLResolver 'sitespecific.urls' (None:None) ^/>
args		(419L,)
_prefix		u'/~mxheyeck/sicariid/'
possibility	[(u'locate_task_enter/%(task_id)s/', [u'task_id'])]
unicode_args	[u'419']
possibilities	[([(u'locate_task_enter/%(task_id)s/', [u'task_id'])],  'locate_task_enter/(?P<task_id>\\d+)/$',  {})]
lookup_view	<function locate_task_enter at 0x1065f8668>
prefix_norm	u'/%7Emxheyeck/sicariid/'
prefix_args	[]
params		[u'task_id']
result		u'locate_task_enter/%(task_id)s/'
defaults	{}
kwargs		{}

Change History (5)

comment:1 Changed 2 years ago by aaugustin

  • Component changed from Uncategorized to Core (URLs)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

comment:2 Changed 2 years ago by bmispelon

  • Cc bmispelon@… added
  • Owner changed from nobody to bmispelon
  • Status changed from new to assigned

This seems to have been overlooked when fixing #18210.

The fix should be similar: escape any % present in the prefix.

Note that a temporary workaround is to used named arguments (kwargs) for reverse.

comment:3 Changed 2 years ago by Baptiste Mispelon <bmispelon@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 4fa7f3cdd9bcf50ec4c7f64a31c1dfc02c375f46:

Fix #20022: Correctly handle prefixes with url-unsafe characters in reverse().

comment:4 Changed 2 years ago by Jacob Kaplan-Moss <jacob@…>

In e4d9f8aed11e964885cd77f8abd01ba0dfa4aa13:

Merge pull request #900 from bmispelon/ticket-20022

Fix #20022: Correctly handle prefixes with url-unsafe characters in reverse()

comment:5 Changed 2 years ago by Florian Apolloner <florian@…>

In 1059da8de675442e84381d6366c0be254681753e:

Merge pull request #900 from bmispelon/ticket-20022

Fix #20022: Correctly handle prefixes with url-unsafe characters in reverse()

Note: See TracTickets for help on using tickets.
Back to Top