From f640b9f3be885ebc4f7652e2952872cdcda3507d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 1 Aug 2015 15:20:11 +0300 Subject: [PATCH] Make ua column wider, and truncate its values for very long UA strings --- hc/api/migrations/0008_auto_20150801_1213.py | 19 ++++++++++++++ hc/api/models.py | 2 +- hc/api/tests/test_ping.py | 27 +++++++++++++++++++- hc/api/views.py | 3 ++- 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 hc/api/migrations/0008_auto_20150801_1213.py diff --git a/hc/api/migrations/0008_auto_20150801_1213.py b/hc/api/migrations/0008_auto_20150801_1213.py new file mode 100644 index 00000000..3858cbac --- /dev/null +++ b/hc/api/migrations/0008_auto_20150801_1213.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0007_ping'), + ] + + operations = [ + migrations.AlterField( + model_name='ping', + name='ua', + field=models.CharField(max_length=200, blank=True), + ), + ] diff --git a/hc/api/models.py b/hc/api/models.py index d463e537..9ffd3e2a 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -65,5 +65,5 @@ class Ping(models.Model): created = models.DateTimeField(auto_now_add=True) remote_addr = models.GenericIPAddressField() method = models.CharField(max_length=10) - ua = models.CharField(max_length=100, blank=True) + ua = models.CharField(max_length=200, blank=True) body = models.TextField(blank=True) diff --git a/hc/api/tests/test_ping.py b/hc/api/tests/test_ping.py index 8a226db4..0b36b85e 100644 --- a/hc/api/tests/test_ping.py +++ b/hc/api/tests/test_ping.py @@ -1,6 +1,6 @@ from django.test import Client, TestCase -from hc.api.models import Check +from hc.api.models import Check, Ping class PingTestCase(TestCase): @@ -26,3 +26,28 @@ class PingTestCase(TestCase): def test_it_handles_bad_uuid(self): r = self.client.get("/ping/not-uuid/") assert r.status_code == 400 + + def test_it_handles_120_char_ua(self): + ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/44.0.2403.89 Safari/537.36") + + check = Check() + check.save() + r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua) + assert r.status_code == 200 + + pings = list(Ping.objects.all()) + assert pings[0].ua == ua + + def test_it_truncates_long_ua(self): + ua = "01234567890" * 30 + + check = Check() + check.save() + r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua) + assert r.status_code == 200 + + pings = list(Ping.objects.all()) + assert len(pings[0].ua) == 200 + assert ua.startswith(pings[0].ua) diff --git a/hc/api/views.py b/hc/api/views.py index b481457a..e775b860 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -27,7 +27,8 @@ def ping(request, code): headers = request.META ping.remote_addr = headers.get("HTTP_X_REAL_IP", headers["REMOTE_ADDR"]) ping.method = headers["REQUEST_METHOD"] - ping.ua = headers.get("HTTP_USER_AGENT", "") + # If User-Agent is longer than 200 characters, truncate it: + ping.ua = headers.get("HTTP_USER_AGENT", "")[:200] ping.body = request.body ping.save()