Skip to content

Commit 2170401

Browse files
committed
PYTHON-2680 Ensure $regularExpression options are a string when parsing JSON
Skip UUID hyphens test.
1 parent fb38fbe commit 2170401

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+108
-69
lines changed

bson/json_util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,12 @@ def _parse_canonical_regex(doc):
666666
if len(regex) != 2:
667667
raise TypeError('Bad $regularExpression must include only "pattern"'
668668
'and "options" components: %s' % (doc,))
669-
return Regex(regex['pattern'], regex['options'])
669+
670+
# Validate options, defer to Regex for pattern validation.
671+
options = regex['options']
672+
if not isinstance(options, str):
673+
raise TypeError("pattern must be a string, not %s" % type(options))
674+
return Regex(regex['pattern'], options)
670675

671676

672677
def _parse_canonical_dbref(doc):

test/bson_corpus/binary.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,20 @@
9494
"string": "{\"x\" : { \"$uuid\" : { \"data\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4\"}}}"
9595
},
9696
{
97-
"description": "$uuid invalid value",
97+
"description": "$uuid invalid value--too short",
9898
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-90e8-e7d1dfc035d4\"}}"
99+
},
100+
{
101+
"description": "$uuid invalid value--too long",
102+
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4-789e4\"}}"
103+
},
104+
{
105+
"description": "$uuid invalid value--misplaced hyphens",
106+
"string": "{\"x\" : { \"$uuid\" : \"73ff-d26444b-34c6-990e8e-7d1dfc035d4\"}}"
107+
},
108+
{
109+
"description": "$uuid invalid value--too many hyphens",
110+
"string": "{\"x\" : { \"$uuid\" : \"----d264-44b3-4--9-90e8-e7d1dfc0----\"}}"
99111
}
100112
]
101113
}

test/bson_corpus/code.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,48 @@
2020
},
2121
{
2222
"description": "two-byte UTF-8 (\u00e9)",
23-
"canonical_bson": "190000000261000D000000C3A9C3A9C3A9C3A9C3A9C3A90000",
24-
"canonical_extjson": "{\"a\" : \"\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\"}"
23+
"canonical_bson": "190000000D61000D000000C3A9C3A9C3A9C3A9C3A9C3A90000",
24+
"canonical_extjson": "{\"a\" : {\"$code\" : \"\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\"}}"
2525
},
2626
{
2727
"description": "three-byte UTF-8 (\u2606)",
28-
"canonical_bson": "190000000261000D000000E29886E29886E29886E298860000",
29-
"canonical_extjson": "{\"a\" : \"\\u2606\\u2606\\u2606\\u2606\"}"
28+
"canonical_bson": "190000000D61000D000000E29886E29886E29886E298860000",
29+
"canonical_extjson": "{\"a\" : {\"$code\" : \"\\u2606\\u2606\\u2606\\u2606\"}}"
3030
},
3131
{
3232
"description": "Embedded nulls",
33-
"canonical_bson": "190000000261000D0000006162006261620062616261620000",
34-
"canonical_extjson": "{\"a\" : \"ab\\u0000bab\\u0000babab\"}"
33+
"canonical_bson": "190000000D61000D0000006162006261620062616261620000",
34+
"canonical_extjson": "{\"a\" : {\"$code\" : \"ab\\u0000bab\\u0000babab\"}}"
3535
}
3636
],
3737
"decodeErrors": [
3838
{
3939
"description": "bad code string length: 0 (but no 0x00 either)",
40-
"bson": "0C0000000261000000000000"
40+
"bson": "0C0000000D61000000000000"
4141
},
4242
{
4343
"description": "bad code string length: -1",
44-
"bson": "0C000000026100FFFFFFFF00"
44+
"bson": "0C0000000D6100FFFFFFFF00"
4545
},
4646
{
4747
"description": "bad code string length: eats terminator",
48-
"bson": "10000000026100050000006200620000"
48+
"bson": "100000000D6100050000006200620000"
4949
},
5050
{
5151
"description": "bad code string length: longer than rest of document",
52-
"bson": "120000000200FFFFFF00666F6F6261720000"
52+
"bson": "120000000D00FFFFFF00666F6F6261720000"
5353
},
5454
{
5555
"description": "code string is not null-terminated",
56-
"bson": "1000000002610004000000616263FF00"
56+
"bson": "100000000D610004000000616263FF00"
5757
},
5858
{
5959
"description": "empty code string, but extra null",
60-
"bson": "0E00000002610001000000000000"
60+
"bson": "0E0000000D610001000000000000"
6161
},
6262
{
6363
"description": "invalid UTF-8",
64-
"bson": "0E00000002610002000000E90000"
64+
"bson": "0E0000000D610002000000E90000"
6565
}
6666
]
6767
}

test/bson_corpus/dbref.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "DBRef",
2+
"description": "Document type (DBRef sub-documents)",
33
"bson_type": "0x03",
44
"valid": [
55
{
@@ -26,6 +26,26 @@
2626
"description": "Document with key names similar to those of a DBRef",
2727
"canonical_bson": "3e0000000224726566000c0000006e6f742d612d646272656600072469640058921b3e6e32ab156a22b59e022462616e616e6100050000007065656c0000",
2828
"canonical_extjson": "{\"$ref\": \"not-a-dbref\", \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}, \"$banana\": \"peel\"}"
29+
},
30+
{
31+
"description": "DBRef with additional dollar-prefixed and dotted fields",
32+
"canonical_bson": "48000000036462726566003c0000000224726566000b000000636f6c6c656374696f6e00072469640058921b3e6e32ab156a22b59e10612e62000100000010246300010000000000",
33+
"canonical_extjson": "{\"dbref\": {\"$ref\": \"collection\", \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}, \"a.b\": {\"$numberInt\": \"1\"}, \"$c\": {\"$numberInt\": \"1\"}}}"
34+
},
35+
{
36+
"description": "Sub-document resembles DBRef but $id is missing",
37+
"canonical_bson": "26000000036462726566001a0000000224726566000b000000636f6c6c656374696f6e000000",
38+
"canonical_extjson": "{\"dbref\": {\"$ref\": \"collection\"}}"
39+
},
40+
{
41+
"description": "Sub-document resembles DBRef but $ref is not a string",
42+
"canonical_bson": "2c000000036462726566002000000010247265660001000000072469640058921b3e6e32ab156a22b59e0000",
43+
"canonical_extjson": "{\"dbref\": {\"$ref\": {\"$numberInt\": \"1\"}, \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}}}"
44+
},
45+
{
46+
"description": "Sub-document resembles DBRef but $db is not a string",
47+
"canonical_bson": "4000000003646272656600340000000224726566000b000000636f6c6c656374696f6e00072469640058921b3e6e32ab156a22b59e1024646200010000000000",
48+
"canonical_extjson": "{\"dbref\": {\"$ref\": \"collection\", \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}, \"$db\": {\"$numberInt\": \"1\"}}}"
2949
}
3050
]
3151
}

test/bson_corpus/symbol.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,31 @@
5050
"decodeErrors": [
5151
{
5252
"description": "bad symbol length: 0 (but no 0x00 either)",
53-
"bson": "0C0000000261000000000000"
53+
"bson": "0C0000000E61000000000000"
5454
},
5555
{
5656
"description": "bad symbol length: -1",
57-
"bson": "0C000000026100FFFFFFFF00"
57+
"bson": "0C0000000E6100FFFFFFFF00"
5858
},
5959
{
6060
"description": "bad symbol length: eats terminator",
61-
"bson": "10000000026100050000006200620000"
61+
"bson": "100000000E6100050000006200620000"
6262
},
6363
{
6464
"description": "bad symbol length: longer than rest of document",
65-
"bson": "120000000200FFFFFF00666F6F6261720000"
65+
"bson": "120000000E00FFFFFF00666F6F6261720000"
6666
},
6767
{
6868
"description": "symbol is not null-terminated",
69-
"bson": "1000000002610004000000616263FF00"
69+
"bson": "100000000E610004000000616263FF00"
7070
},
7171
{
7272
"description": "empty symbol, but extra null",
73-
"bson": "0E00000002610001000000000000"
73+
"bson": "0E0000000E610001000000000000"
7474
},
7575
{
7676
"description": "invalid UTF-8",
77-
"bson": "0E00000002610002000000E90000"
77+
"bson": "0E0000000E610002000000E90000"
7878
}
7979
]
8080
}

test/bson_corpus/top.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@
9292
},
9393
{
9494
"description": "Bad $regularExpression (pattern is number, not string)",
95-
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": 42, \"$options\" : \"\"}}}"
95+
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": 42, \"options\" : \"\"}}}"
9696
},
9797
{
9898
"description": "Bad $regularExpression (options are number, not string)",
99-
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": \"a\", \"$options\" : 0}}}"
99+
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": \"a\", \"options\" : 0}}}"
100100
},
101101
{
102102
"description" : "Bad $regularExpression (missing pattern field)",

test/crud/unified/aggregate-merge.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "aggregate-merge",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.1.11"

test/crud/unified/bulkWrite-arrayFilters-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-arrayFilters-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "3.5.5"

test/crud/unified/bulkWrite-arrayFilters.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-arrayFilters",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "3.5.6"

test/crud/unified/bulkWrite-delete-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-delete-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "3.3.99"

test/crud/unified/bulkWrite-delete-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-delete-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "3.4.0",

test/crud/unified/bulkWrite-delete-hint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-delete-hint",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.3.4"

test/crud/unified/bulkWrite-update-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-update-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "3.3.99"

test/crud/unified/bulkWrite-update-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-update-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "3.4.0",

test/crud/unified/bulkWrite-update-hint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "bulkWrite-update-hint",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.2.0"

test/crud/unified/deleteMany-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "deleteMany-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "3.3.99"

test/crud/unified/deleteMany-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "deleteMany-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "3.4.0",

test/crud/unified/deleteMany-hint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "deleteMany-hint",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.3.4"

test/crud/unified/deleteOne-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "deleteOne-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "3.3.99"

test/crud/unified/deleteOne-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "deleteOne-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "3.4.0",

test/crud/unified/deleteOne-hint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "deleteOne-hint",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.3.4"

test/crud/unified/find-allowdiskuse-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "find-allowdiskuse-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "3.0.99"

test/crud/unified/find-allowdiskuse-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "find-allowdiskuse-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "3.2",

test/crud/unified/find-allowdiskuse.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "find-allowdiskuse",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.3.1"

test/crud/unified/findOneAndDelete-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndDelete-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "4.0.99"

test/crud/unified/findOneAndDelete-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndDelete-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.2.0",

test/crud/unified/findOneAndDelete-hint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndDelete-hint",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.3.4"

test/crud/unified/findOneAndReplace-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndReplace-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "4.0.99"

test/crud/unified/findOneAndReplace-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndReplace-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.2.0",

test/crud/unified/findOneAndReplace-hint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndReplace-hint",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.3.1"

test/crud/unified/findOneAndUpdate-hint-clientError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndUpdate-hint-clientError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"maxServerVersion": "4.0.99"

test/crud/unified/findOneAndUpdate-hint-serverError.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "findOneAndUpdate-hint-serverError",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.0",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.2.0",

0 commit comments

Comments
 (0)