bson 4.0.4 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/bson.rb +1 -0
- data/lib/bson/config.rb +51 -0
- data/lib/bson/hash.rb +2 -2
- data/lib/bson/integer.rb +12 -2
- data/lib/bson/object.rb +1 -1
- data/lib/bson/string.rb +30 -1
- data/lib/bson/symbol.rb +2 -2
- data/lib/bson/version.rb +1 -1
- data/spec/bson/config_spec.rb +40 -0
- data/spec/bson/hash_spec.rb +48 -0
- data/spec/bson/string_spec.rb +43 -0
- data/spec/bson/symbol_spec.rb +27 -0
- data/spec/support/shared_examples.rb +57 -0
- metadata +12 -9
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47d29b1cc3748faca5e80d2fd891f3afef42a63f
|
4
|
+
data.tar.gz: c9cc5e18df9aa9bb2dc5ed6e007648adf05ac845
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 433674c25e326d02c357f2b2fad8861c63ee4d3de379bd6310ea0da84c208c433d3fe8f5366a97bf6c731ab174b27d17ace2fde7046f248bcf301182059ebf3c
|
7
|
+
data.tar.gz: 05a96d4fd7446788c97a8d2cb0254432e46d6d66c7e32d9e17ba345fdb1c6f18dbe088cb76920bfa8446436ea66d4b1d106ee27e29084ec5887b39c6e1029214
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/bson.rb
CHANGED
data/lib/bson/config.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Copyright (C) 2016 MongoDB Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module BSON
|
16
|
+
|
17
|
+
# Provides configuration options for the BSON library.
|
18
|
+
#
|
19
|
+
# @since 4.1.0
|
20
|
+
module Config
|
21
|
+
extend self
|
22
|
+
|
23
|
+
# Set the configuration option for BSON to validate keys or not.
|
24
|
+
#
|
25
|
+
# @example Set the config option.
|
26
|
+
# BSON::Config.validating_keys = true
|
27
|
+
#
|
28
|
+
# @param [ true, false ] value The value to set.
|
29
|
+
#
|
30
|
+
# @return [ true, false ] The value.
|
31
|
+
#
|
32
|
+
# @since 4.1.0
|
33
|
+
def validating_keys=(value)
|
34
|
+
@validating_keys = value
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns true if BSON will validate the document keys on serialization to
|
38
|
+
# determine if they contain invalid MongoDB values. Invalid keys start with
|
39
|
+
# '$' or contain a '.' in them.
|
40
|
+
#
|
41
|
+
# @example Is BSON validating keys?
|
42
|
+
# BSON::Config.validating_keys?
|
43
|
+
#
|
44
|
+
# @return [ true, false ] If BSON is validating keys?
|
45
|
+
#
|
46
|
+
# @since 4.1.0
|
47
|
+
def validating_keys?
|
48
|
+
!!@validating_keys
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/bson/hash.rb
CHANGED
@@ -37,12 +37,12 @@ module BSON
|
|
37
37
|
# @see http://bsonspec.org/#/specification
|
38
38
|
#
|
39
39
|
# @since 2.0.0
|
40
|
-
def to_bson(buffer = ByteBuffer.new)
|
40
|
+
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
|
41
41
|
position = buffer.length
|
42
42
|
buffer.put_int32(0)
|
43
43
|
each do |field, value|
|
44
44
|
buffer.put_byte(value.bson_type)
|
45
|
-
buffer.put_cstring(field.to_bson_key)
|
45
|
+
buffer.put_cstring(field.to_bson_key(validating_keys))
|
46
46
|
value.to_bson(buffer)
|
47
47
|
end
|
48
48
|
buffer.put_byte(NULL_BYTE)
|
data/lib/bson/integer.rb
CHANGED
@@ -145,8 +145,18 @@ module BSON
|
|
145
145
|
encoded << ((self >> 56) & 255)
|
146
146
|
end
|
147
147
|
|
148
|
-
|
149
|
-
|
148
|
+
# Convert the integer to a BSON string key.
|
149
|
+
#
|
150
|
+
# @example Convert the integer to a BSON key string.
|
151
|
+
# 1.to_bson_key
|
152
|
+
#
|
153
|
+
# @param [ true, false ] validating_keys If BSON should validate the key.
|
154
|
+
#
|
155
|
+
# @return [ String ] The string key.
|
156
|
+
#
|
157
|
+
# @since 2.0.0
|
158
|
+
def to_bson_key(validating_keys = Config.validating_keys?)
|
159
|
+
to_s.to_bson_key(validating_keys)
|
150
160
|
end
|
151
161
|
|
152
162
|
private
|
data/lib/bson/object.rb
CHANGED
data/lib/bson/string.rb
CHANGED
@@ -28,6 +28,11 @@ module BSON
|
|
28
28
|
# @since 2.0.0
|
29
29
|
BSON_TYPE = 2.chr.force_encoding(BINARY).freeze
|
30
30
|
|
31
|
+
# Regex for matching illegal BSON keys.
|
32
|
+
#
|
33
|
+
# @since 4.1.0
|
34
|
+
ILLEGAL_KEY = /(\A[$])|(\.)/.freeze
|
35
|
+
|
31
36
|
# Get the string as encoded BSON.
|
32
37
|
#
|
33
38
|
# @example Get the string as encoded BSON.
|
@@ -51,12 +56,18 @@ module BSON
|
|
51
56
|
#
|
52
57
|
# @raise [ EncodingError ] If the string is not UTF-8.
|
53
58
|
#
|
59
|
+
# @raise [ IllegalKey ] If validating keys and it contains a '.' or starts
|
60
|
+
# with '$'.
|
61
|
+
#
|
54
62
|
# @return [ String ] The encoded string.
|
55
63
|
#
|
56
64
|
# @see http://bsonspec.org/#/specification
|
57
65
|
#
|
58
66
|
# @since 2.0.0
|
59
|
-
def to_bson_key
|
67
|
+
def to_bson_key(validating_keys = Config.validating_keys?)
|
68
|
+
if validating_keys
|
69
|
+
raise IllegalKey.new(self) if ILLEGAL_KEY =~ self
|
70
|
+
end
|
60
71
|
self
|
61
72
|
end
|
62
73
|
|
@@ -89,6 +100,24 @@ module BSON
|
|
89
100
|
unpack("H*")[0]
|
90
101
|
end
|
91
102
|
|
103
|
+
# Raised when validating keys and a key is illegal in MongoDB
|
104
|
+
#
|
105
|
+
# @since 4.1.0
|
106
|
+
class IllegalKey < RuntimeError
|
107
|
+
|
108
|
+
# Instantiate the exception.
|
109
|
+
#
|
110
|
+
# @example Instantiate the exception.
|
111
|
+
# BSON::Object::IllegalKey.new(string)
|
112
|
+
#
|
113
|
+
# @param [ String ] string The illegal string.
|
114
|
+
#
|
115
|
+
# @since 4.1.0
|
116
|
+
def initialize(string)
|
117
|
+
super("'#{string}' is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.")
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
92
121
|
module ClassMethods
|
93
122
|
|
94
123
|
# Deserialize a string from BSON.
|
data/lib/bson/symbol.rb
CHANGED
@@ -70,8 +70,8 @@ module BSON
|
|
70
70
|
# @see http://bsonspec.org/#/specification
|
71
71
|
#
|
72
72
|
# @since 2.0.0
|
73
|
-
def to_bson_key
|
74
|
-
to_s.to_bson_key
|
73
|
+
def to_bson_key(validating_keys = Config.validating_keys?)
|
74
|
+
to_s.to_bson_key(validating_keys)
|
75
75
|
end
|
76
76
|
|
77
77
|
# Converts the symbol to a normalized key in a BSON document.
|
data/lib/bson/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe BSON::Config do
|
4
|
+
|
5
|
+
describe "#validating_keys?" do
|
6
|
+
|
7
|
+
context "when the default is used" do
|
8
|
+
|
9
|
+
it "returns false" do
|
10
|
+
expect(described_class).to_not be_validating_keys
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when configuring to false" do
|
15
|
+
|
16
|
+
before do
|
17
|
+
BSON::Config.validating_keys = false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns false" do
|
21
|
+
expect(described_class).to_not be_validating_keys
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when configuring to true" do
|
26
|
+
|
27
|
+
before do
|
28
|
+
BSON::Config.validating_keys = true
|
29
|
+
end
|
30
|
+
|
31
|
+
after do
|
32
|
+
BSON::Config.validating_keys = false
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns true" do
|
36
|
+
expect(described_class).to be_validating_keys
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/bson/hash_spec.rb
CHANGED
@@ -52,6 +52,54 @@ describe Hash do
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
context "when the hash has invalid keys" do
|
56
|
+
|
57
|
+
let(:obj) do
|
58
|
+
{ "$testing" => "value" }
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when validating keys" do
|
62
|
+
|
63
|
+
context "when validating globally" do
|
64
|
+
|
65
|
+
before do
|
66
|
+
BSON::Config.validating_keys = true
|
67
|
+
end
|
68
|
+
|
69
|
+
after do
|
70
|
+
BSON::Config.validating_keys = false
|
71
|
+
end
|
72
|
+
|
73
|
+
it "raises an error" do
|
74
|
+
expect {
|
75
|
+
obj.to_bson
|
76
|
+
}.to raise_error(BSON::String::IllegalKey)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "when validating locally" do
|
81
|
+
|
82
|
+
it "raises an error" do
|
83
|
+
expect {
|
84
|
+
obj.to_bson(BSON::ByteBuffer.new, true)
|
85
|
+
}.to raise_error(BSON::String::IllegalKey)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context "when not validating keys" do
|
91
|
+
|
92
|
+
let(:bson) do
|
93
|
+
"#{25.to_bson.to_s}#{String::BSON_TYPE}$testing#{BSON::NULL_BYTE}" +
|
94
|
+
"#{6.to_bson.to_s}value#{BSON::NULL_BYTE}#{BSON::NULL_BYTE}"
|
95
|
+
end
|
96
|
+
|
97
|
+
it "serializes the hash" do
|
98
|
+
expect(obj.to_bson.to_s).to eq(bson)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
55
103
|
context "when the hash is embedded" do
|
56
104
|
|
57
105
|
let(:obj) do
|
data/spec/bson/string_spec.rb
CHANGED
@@ -87,4 +87,47 @@ describe String do
|
|
87
87
|
expect(string.to_hex_string).to eq("74657374696e67313233")
|
88
88
|
end
|
89
89
|
end
|
90
|
+
|
91
|
+
describe "#to_bson_key" do
|
92
|
+
|
93
|
+
context "when validating keys" do
|
94
|
+
|
95
|
+
context "when validating globally" do
|
96
|
+
|
97
|
+
before do
|
98
|
+
BSON::Config.validating_keys = true
|
99
|
+
end
|
100
|
+
|
101
|
+
after do
|
102
|
+
BSON::Config.validating_keys = false
|
103
|
+
end
|
104
|
+
|
105
|
+
let(:validated) do
|
106
|
+
string.to_bson_key
|
107
|
+
end
|
108
|
+
|
109
|
+
it_behaves_like "a validated BSON key"
|
110
|
+
end
|
111
|
+
|
112
|
+
context "when validating locally" do
|
113
|
+
|
114
|
+
let(:validated) do
|
115
|
+
string.to_bson_key(true)
|
116
|
+
end
|
117
|
+
|
118
|
+
it_behaves_like "a validated BSON key"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "when allowing invalid keys" do
|
123
|
+
|
124
|
+
let(:string) do
|
125
|
+
"$testing.testing"
|
126
|
+
end
|
127
|
+
|
128
|
+
it "allows invalid keys" do
|
129
|
+
expect(string.to_bson_key).to eq(string)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
90
133
|
end
|
data/spec/bson/symbol_spec.rb
CHANGED
@@ -43,4 +43,31 @@ describe Symbol do
|
|
43
43
|
expect(symbol.to_bson_key).to eq(encoded)
|
44
44
|
end
|
45
45
|
end
|
46
|
+
|
47
|
+
describe "#to_bson_key" do
|
48
|
+
|
49
|
+
context "when validating keys" do
|
50
|
+
|
51
|
+
let(:symbol) do
|
52
|
+
:'$testing.testing'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises an exception" do
|
56
|
+
expect {
|
57
|
+
symbol.to_bson_key(true)
|
58
|
+
}.to raise_error(BSON::String::IllegalKey)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "when not validating keys" do
|
63
|
+
|
64
|
+
let(:symbol) do
|
65
|
+
:'$testing.testing'
|
66
|
+
end
|
67
|
+
|
68
|
+
it "allows invalid keys" do
|
69
|
+
expect(symbol.to_bson_key).to eq(symbol.to_s)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
46
73
|
end
|
@@ -96,3 +96,60 @@ shared_examples_for "a class which converts to Time" do
|
|
96
96
|
expect(described_class.new.bson_type).to eq(Time::BSON_TYPE)
|
97
97
|
end
|
98
98
|
end
|
99
|
+
|
100
|
+
shared_examples_for "a validated BSON key" do
|
101
|
+
|
102
|
+
context "when the string is valid" do
|
103
|
+
|
104
|
+
context "when the string has no invalid characters" do
|
105
|
+
|
106
|
+
let(:string) do
|
107
|
+
"testing"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "returns the key" do
|
111
|
+
expect(validated).to eq(string)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when the string contains a $" do
|
116
|
+
|
117
|
+
let(:string) do
|
118
|
+
"te$ting"
|
119
|
+
end
|
120
|
+
|
121
|
+
it "returns the key" do
|
122
|
+
expect(validated).to eq(string)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "when the string is invalid" do
|
128
|
+
|
129
|
+
context "when the string starts with $" do
|
130
|
+
|
131
|
+
let(:string) do
|
132
|
+
"$testing"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "raises an exception" do
|
136
|
+
expect {
|
137
|
+
validated
|
138
|
+
}.to raise_error(BSON::String::IllegalKey)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "when the string contains a ." do
|
143
|
+
|
144
|
+
let(:string) do
|
145
|
+
"testing.testing"
|
146
|
+
end
|
147
|
+
|
148
|
+
it "raises an exception" do
|
149
|
+
expect {
|
150
|
+
validated
|
151
|
+
}.to raise_error(BSON::String::IllegalKey)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.0
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Brock
|
@@ -16,7 +16,7 @@ cert_chain:
|
|
16
16
|
-----BEGIN CERTIFICATE-----
|
17
17
|
MIIDfDCCAmSgAwIBAgIBATANBgkqhkiG9w0BAQUFADBCMRQwEgYDVQQDDAtkcml2
|
18
18
|
ZXItcnVieTEVMBMGCgmSJomT8ixkARkWBTEwZ2VuMRMwEQYKCZImiZPyLGQBGRYD
|
19
|
-
|
19
|
+
Y29tMB4XDTE2MDQwODE0MTE0NVoXDTE3MDQwODE0MTE0NVowQjEUMBIGA1UEAwwL
|
20
20
|
ZHJpdmVyLXJ1YnkxFTATBgoJkiaJk/IsZAEZFgUxMGdlbjETMBEGCgmSJomT8ixk
|
21
21
|
ARkWA2NvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANFdSAa8fRm1
|
22
22
|
bAM9za6Z0fAH4g02bqM1NGnw8zJQrE/PFrFfY6IFCT2AsLfOwr1maVm7iU1+kdVI
|
@@ -27,14 +27,14 @@ cert_chain:
|
|
27
27
|
u8KAcPHm5KkCAwEAAaN9MHswCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
|
28
28
|
BBYEFFt3WbF+9JpUjAoj62cQBgNb8HzXMCAGA1UdEQQZMBeBFWRyaXZlci1ydWJ5
|
29
29
|
QDEwZ2VuLmNvbTAgBgNVHRIEGTAXgRVkcml2ZXItcnVieUAxMGdlbi5jb20wDQYJ
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
KoZIhvcNAQEFBQADggEBAKe478140EL5v2vJMjRSGCTtv9tD7jH7595aW267n9Tg
|
31
|
+
nuFFwHVC3cTz7xbWi8s7Dat3uC1qWhg7qPFjrF5QZymP7R3vC887XiXwJCnkilx4
|
32
|
+
tqIhdDCk/PU1G8RvS2GtCjxcDZLzr2sLNlqC1OCplJrL8YAY6vF8i46WKudH/189
|
33
|
+
HPLRJ8tx0aFZY7c6MO1NLqCtBs+wdB9sip7CvCA/w4Ly1MkLzVPXvzjkDJPwkoF1
|
34
|
+
yVPbgNJA2D/PnLe8ZFMhgJf+VIA1V4oybP/o+9aqlghTtNfYHJpRCAluUiaywwXl
|
35
|
+
KzD4mmIBpxtbWrWB3hx7tsVJmWx5zgO9aDAfvWnXfoo=
|
36
36
|
-----END CERTIFICATE-----
|
37
|
-
date: 2016-
|
37
|
+
date: 2016-04-14 00:00:00.000000000 Z
|
38
38
|
dependencies: []
|
39
39
|
description: A full featured BSON specification implementation, in Ruby
|
40
40
|
email:
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/bson/boolean.rb
|
60
60
|
- lib/bson/code.rb
|
61
61
|
- lib/bson/code_with_scope.rb
|
62
|
+
- lib/bson/config.rb
|
62
63
|
- lib/bson/date.rb
|
63
64
|
- lib/bson/date_time.rb
|
64
65
|
- lib/bson/document.rb
|
@@ -91,6 +92,7 @@ files:
|
|
91
92
|
- spec/bson/byte_buffer_spec.rb
|
92
93
|
- spec/bson/code_spec.rb
|
93
94
|
- spec/bson/code_with_scope_spec.rb
|
95
|
+
- spec/bson/config_spec.rb
|
94
96
|
- spec/bson/date_spec.rb
|
95
97
|
- spec/bson/date_time_spec.rb
|
96
98
|
- spec/bson/document_spec.rb
|
@@ -148,6 +150,7 @@ test_files:
|
|
148
150
|
- spec/bson/byte_buffer_spec.rb
|
149
151
|
- spec/bson/code_spec.rb
|
150
152
|
- spec/bson/code_with_scope_spec.rb
|
153
|
+
- spec/bson/config_spec.rb
|
151
154
|
- spec/bson/date_spec.rb
|
152
155
|
- spec/bson/date_time_spec.rb
|
153
156
|
- spec/bson/document_spec.rb
|
metadata.gz.sig
CHANGED
Binary file
|