File: CVE-2014-0474.patch

package info (click to toggle)
python-django 1.2.3-3%2Bsqueeze15
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 29,720 kB
  • ctags: 21,538
  • sloc: python: 101,631; xml: 574; makefile: 149; sh: 121; sql: 7
file content (230 lines) | stat: -rw-r--r-- 10,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
Description: Fixed queries that may return unexpected results on MySQL due to typecasting.
Origin: backport, https://github.com/django/django/commit/aa80f498de6d687e613860933ac58433ab71ea4b
Forwarded: not-needed
Author: Salvatore Bonaccorso <carnil@debian.org>
Last-Update: 2014-05-11

Index: python-django/tests/regressiontests/model_fields/tests.py
===================================================================
--- python-django.orig/tests/regressiontests/model_fields/tests.py
+++ python-django/tests/regressiontests/model_fields/tests.py
@@ -6,7 +6,14 @@ import django.test
 from django import forms
 from django.db import models
 from django.core.exceptions import ValidationError
-
+from django.db.models.fields import (
+        AutoField, BigIntegerField, BooleanField, CharField,
+        CommaSeparatedIntegerField, DateField, DateTimeField, DecimalField,
+        EmailField, FilePathField, FloatField, IntegerField, IPAddressField,
+        NullBooleanField, PositiveIntegerField,
+        PositiveSmallIntegerField, SlugField, SmallIntegerField, TextField,
+        TimeField, URLField)
+from django.db.models.fields.files import FileField, ImageField, FieldFile
 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel
 
 # If PIL available, do these tests.
@@ -311,3 +318,83 @@ class TypeCoercionTests(django.test.Test
     def test_lookup_integer_in_textfield(self):
         self.assertEquals(Post.objects.filter(body=24).count(), 0)
 
+class PrepValueTest(django.test.TestCase):
+    def test_AutoField(self):
+        self.assertEqual(isinstance(AutoField(primary_key=True).get_prep_value(1), int), True)
+
+    def test_BigIntegerField(self):
+        self.assertEqual(isinstance(BigIntegerField().get_prep_value(long(9999999999999999999)), long), True)
+
+    def test_BooleanField(self):
+        self.assertEqual(isinstance(BooleanField().get_prep_value(True), bool), True)
+
+    def test_CharField(self):
+        self.assertEqual(isinstance(CharField().get_prep_value(''), str), True)
+        self.assertEqual(isinstance(CharField().get_prep_value(0), unicode), True)
+
+    def test_CommaSeparatedIntegerField(self):
+        self.assertEqual(isinstance(CommaSeparatedIntegerField().get_prep_value('1,2'), str), True)
+        self.assertEqual(isinstance(CommaSeparatedIntegerField().get_prep_value(0), unicode), True)
+
+    def test_DateField(self):
+        self.assertEqual(isinstance(DateField().get_prep_value(datetime.date.today()), datetime.date), True)
+
+    def test_DateTimeField(self):
+        self.assertEqual(isinstance(DateTimeField().get_prep_value(datetime.datetime.now()), datetime.datetime), True)
+
+    def test_DecimalField(self):
+        self.assertEqual(isinstance(DecimalField().get_prep_value(Decimal('1.2')), Decimal), True)
+
+    def test_EmailField(self):
+        self.assertEqual(isinstance(EmailField().get_prep_value('mailbox@domain.com'), str), True)
+
+    def test_FileField(self):
+        self.assertEqual(isinstance(FileField().get_prep_value('filename.ext'), unicode), True)
+        self.assertEqual(isinstance(FileField().get_prep_value(0), unicode), True)
+
+    def test_FilePathField(self):
+        self.assertEqual(isinstance(FilePathField().get_prep_value('tests.py'), unicode), True)
+        self.assertEqual(isinstance(FilePathField().get_prep_value(0), unicode), True)
+
+    def test_FloatField(self):
+        self.assertEqual(isinstance(FloatField().get_prep_value(1.2), float), True)
+
+    def test_ImageField(self):
+        self.assertEqual(isinstance(ImageField().get_prep_value('filename.ext'), unicode), True)
+
+    def test_IntegerField(self):
+        self.assertEqual(isinstance(IntegerField().get_prep_value(1), int), True)
+
+    def test_IPAddressField(self):
+        self.assertEqual(isinstance(IPAddressField().get_prep_value('127.0.0.1'), unicode), True)
+        self.assertEqual(isinstance(IPAddressField().get_prep_value(0), unicode), True)
+
+    def test_NullBooleanField(self):
+        self.assertEqual(isinstance(NullBooleanField().get_prep_value(True), bool), True)
+
+    def test_PositiveIntegerField(self):
+        self.assertEqual(isinstance(PositiveIntegerField().get_prep_value(1), int), True)
+
+    def test_PositiveSmallIntegerField(self):
+        self.assertEqual(isinstance(PositiveSmallIntegerField().get_prep_value(1), int), True)
+
+    def test_SlugField(self):
+        self.assertEqual(isinstance(SlugField().get_prep_value('slug'), str), True)
+        self.assertEqual(isinstance(SlugField().get_prep_value(0), unicode), True)
+
+    def test_SmallIntegerField(self):
+        self.assertEqual(isinstance(SmallIntegerField().get_prep_value(1), int), True)
+
+    def test_TextField(self):
+        self.assertEqual(isinstance(TextField().get_prep_value('Abc'), str), True)
+        self.assertEqual(isinstance(TextField().get_prep_value(0), unicode), True)
+
+    def test_TimeField(self):
+        self.assertEqual(isinstance(
+            TimeField().get_prep_value(datetime.datetime.now().time()),
+            datetime.time),
+            True)
+
+    def test_URLField(self):
+        self.assertEqual(isinstance(URLField().get_prep_value('http://domain.com'), str), True)
+
Index: python-django/django/db/models/fields/__init__.py
===================================================================
--- python-django.orig/django/db/models/fields/__init__.py
+++ python-django/django/db/models/fields/__init__.py
@@ -814,6 +814,12 @@ class FilePathField(Field):
         kwargs['max_length'] = kwargs.get('max_length', 100)
         Field.__init__(self, verbose_name, name, **kwargs)
 
+    def get_prep_value(self, value):
+        value = super(FilePathField, self).get_prep_value(value)
+        if value is None:
+            return None
+        return smart_unicode(value)
+
     def formfield(self, **kwargs):
         defaults = {
             'path': self.path,
@@ -909,6 +915,12 @@ class IPAddressField(Field):
         kwargs['max_length'] = 15
         Field.__init__(self, *args, **kwargs)
 
+    def get_prep_value(self, value):
+        value = super(IPAddressField, self).get_prep_value(value)
+        if value is None:
+            return None
+        return smart_unicode(value)
+
     def get_internal_type(self):
         return "IPAddressField"
 
Index: python-django/docs/howto/custom-model-fields.txt
===================================================================
--- python-django.orig/docs/howto/custom-model-fields.txt
+++ python-django/docs/howto/custom-model-fields.txt
@@ -472,6 +472,16 @@ For example::
             return ''.join([''.join(l) for l in (value.north,
                     value.east, value.south, value.west)])
 
+.. warning::
+
+    If your custom field uses the ``CHAR``, ``VARCHAR`` or ``TEXT``
+    types for MySQL, you must make sure that :meth:`.get_prep_value`
+    always returns a string type. MySQL performs flexible and unexpected
+    matching when a query is performed on these types and the provided
+    value is an integer, which can cause queries to include unexpected
+    objects in their results. This problem cannot occur if you always
+    return a string type from :meth:`.get_prep_value`.
+
 Converting query values to database values
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Index: python-django/docs/ref/databases.txt
===================================================================
--- python-django.orig/docs/ref/databases.txt
+++ python-django/docs/ref/databases.txt
@@ -343,6 +343,22 @@ Furthermore, if you are using a version
 column types have a maximum length restriction of 255 characters, regardless
 of whether ``unique=True`` is specified or not.
 
+Automatic typecasting can cause unexpected results
+--------------------------------------------------
+
+When performing a query on a string type, but with an integer value, MySQL will
+coerce the types of all values in the table to an integer before performing the
+comparison. If your table contains the values ``'abc'``, ``'def'`` and you
+query for ``WHERE mycolumn=0``, both rows will match. Similarly, ``WHERE mycolumn=1``
+will match the value ``'abc1'``. Therefore, string type fields included in Django
+will always cast the value to a string before using it in a query.
+
+If you implement custom model fields that inherit from :class:`~django.db.models.Field`
+directly, are overriding :meth:`~django.db.models.Field.get_prep_value`, or use
+:meth:`extra() <django.db.models.query.QuerySet.extra>` or
+:meth:`raw() <django.db.models.Manager.raw>`, you should ensure that you
+perform the appropriate typecasting.
+
 .. _sqlite-notes:
 
 SQLite notes
Index: python-django/docs/ref/models/querysets.txt
===================================================================
--- python-django.orig/docs/ref/models/querysets.txt
+++ python-django/docs/ref/models/querysets.txt
@@ -835,6 +835,16 @@ of the arguments is required, but you sh
 
         Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
 
+.. warning::
+
+    If you are performing queries on MySQL, note that MySQL's silent type coercion
+    may cause unexpected results when mixing types. If you query on a string
+    type column, but with an integer value, MySQL will coerce the types of all values
+    in the table to an integer before performing the comparison. For example, if your
+    table contains the values ``'abc'``, ``'def'`` and you query for ``WHERE mycolumn=0``,
+    both rows will match. To prevent this, perform the correct typecasting
+    before using the value in a query.
+
 ``defer(*fields)``
 ~~~~~~~~~~~~~~~~~~
 
Index: python-django/docs/topics/db/sql.txt
===================================================================
--- python-django.orig/docs/topics/db/sql.txt
+++ python-django/docs/topics/db/sql.txt
@@ -56,6 +56,16 @@ You could then execute custom SQL like s
     :attr:`~Options.db_table` option, which also lets you manually set the
     database table name.
 
+.. warning::
+
+    If you are performing queries on MySQL, note that MySQL's silent type coercion
+    may cause unexpected results when mixing types. If you query on a string
+    type column, but with an integer value, MySQL will coerce the types of all values
+    in the table to an integer before performing the comparison. For example, if your
+    table contains the values ``'abc'``, ``'def'`` and you query for ``WHERE mycolumn=0``,
+    both rows will match. To prevent this, perform the correct typecasting
+    before using the value in a query.
+
 Of course, this example isn't very exciting -- it's exactly the same as
 running ``Person.objects.all()``. However, ``raw()`` has a bunch of other
 options that make it very powerful.