Skip to content

Commit b8ce98b

Browse files
committed
fix problem with spaces in request.path
1 parent 5166fac commit b8ce98b

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

peterbecom/base/fscache.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ def cache_request(request, response):
131131
# XXX TODO: Support JSON and xml
132132
"text/html" in response["Content-Type"]
133133
):
134+
# When you have url patterns like `/foo/(?P<oid>.*)` you can get requests
135+
# that match but if the URL is something like
136+
# "/foo/myslug%0Anextline" which translates to `/foo/myslug\nnextline`.
137+
# The Django view will match just fine but the request.path will have a
138+
# `\s` character in it.
139+
# If that's the case we don't want to cache this request.
140+
if "\n" in request.path or "\t" in request.path:
141+
return False
142+
134143
# let's iterate through some exceptions
135144
not_starts = (
136145
"/plog/edit/",

peterbecom/base/test_fscache.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ def test_cache_request():
5050
request._fscache_disable = True
5151
response = http.HttpResponse()
5252
assert not fscache.cache_request(request, response)
53+
54+
request = RequestFactory().get("/\nsomething")
55+
request.user = AnonymousUser()
56+
response = http.HttpResponse()
57+
assert not fscache.cache_request(request, response)

peterbecom/plog/tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,9 @@ def test_blog_post_ping(self):
188188

189189
hit, = BlogItemHit.objects.all()
190190
assert hit.blogitem == blog
191+
192+
def test_blog_post_with_newline_request_path(self):
193+
url = reverse("blog_post", args=["myoid"])
194+
url += "\nOtherstuff"
195+
response = self.client.get(url)
196+
assert response.status_code == 302

peterbecom/plog/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def _blog_post_key_prefixer(request):
134134
@cache_control(public=True, max_age=settings.DEBUG and ONE_HOUR or ONE_WEEK)
135135
@cache_page(settings.DEBUG and ONE_HOUR or ONE_WEEK, _blog_post_key_prefixer)
136136
def blog_post(request, oid):
137+
if '\n' in request.path:
138+
return redirect(reverse('blog_post', args=[oid]))
137139
if request.path.endswith("/ping"):
138140
# Sometimes this can happen when the URL parsing by Django
139141
# isn't working out.

0 commit comments

Comments
 (0)