returning 404 on every error which is caused by adding an attachment via

providing URL

changed message to "Page not found" if 404 with an HTML page is returned
This commit is contained in:
Piotr Zalewa 2012-10-02 10:21:37 +02:00
Родитель cacd6f2d11
Коммит d8c126184d
3 изменённых файлов: 22 добавлений и 9 удалений

Просмотреть файл

@ -300,12 +300,12 @@ class TestViews(TestCase):
response = self.client.post(self.get_add_url(revision), {
"filename": "some.txt",
"url": "abc"})
eq_(response.status_code, 400)
eq_(response.status_code, 404)
# not existing url
response = self.client.post(self.get_add_url(revision), {
"filename": "some.txt",
"url": "http://notexistingurl.pl/some.txt"})
eq_(response.status_code, 400)
eq_(response.status_code, 404)
# malicious input
response = self.client.post(self.get_add_url(revision), {
"filename": "",

Просмотреть файл

@ -691,12 +691,16 @@ def revision_add_attachment(request, pk):
except ValidationError, err:
log.warning('[%s] Invalid url provided\n%s' % (url,
'\n'.join(err.messages)))
return HttpResponseBadRequest(("Loading attachment failed\n"
"%s") % parse_validation_messages(err))
raise Http404()
except Exception, err:
log.warning('[%s] Exception raised\n%s' % (url, str(err)))
return HttpResponseBadRequest(str(err))
raise Http404()
try:
att = urllib2.urlopen(url, timeout=settings.URLOPEN_TIMEOUT)
except Exception, err:
log.warning('[%s] Exception raised by opening url\n%s' % (url, str(err)))
raise Http404()
# validate filesize
att_info = att.info()
if 'content-length' in att_info.dict:
@ -707,7 +711,12 @@ def revision_add_attachment(request, pk):
"File is too big")
# download attachment's content
log.debug('[%s] Downloading' % url)
try:
content = att.read(settings.ATTACHMENT_MAX_FILESIZE + 1)
except Exception, err:
log.warning('[%s] Exception raised by reading url\n%s' % (url, str(err)))
raise Http404()
# work out the contenttype
basename, ext = os.path.splitext(filename)
unicode_contenttypes = ('utf-8',)

Просмотреть файл

@ -21,8 +21,12 @@ var defaultFailure = function(text) {
log.warn('Response error is not valid JSON');
if (text.indexOf('<html') !== -1) {
// We somehow got a full HTML page. Bad!
if (this.status == 404) {
response = 'Page not found';
} else {
log.error('Response is an HTML page!');
response = 'Something aweful happened.';
response = 'Something aweful happened.' + this.status;
}
} else {
// A simple text message
response = text;