mongoid 8.1.12 → 8.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/config/locales/en.yml +26 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/nested/many.rb +38 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +103 -2
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/config.rb +62 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
- data/lib/mongoid/contextual/memory.rb +19 -8
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
- data/lib/mongoid/matcher/regex.rb +8 -9
- data/lib/mongoid/matcher/regexp_budget.rb +389 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +3 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +1 -0
- data/spec/integration/app_spec.rb +8 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
- data/spec/integration/dots_and_dollars_spec.rb +12 -2
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
- data/spec/mongoid/attributes/nested_spec.rb +204 -10
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
- data/spec/mongoid/attributes/readonly_spec.rb +389 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/config_spec.rb +7 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
- data/spec/mongoid/contextual/memory_spec.rb +202 -0
- data/spec/mongoid/contextual/none_spec.rb +22 -5
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
- data/spec/mongoid/criteria_spec.rb +19 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- data/spec/spec_helper.rb +8 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- metadata +1900 -1860
|
@@ -468,6 +468,31 @@ describe Mongoid::Contextual::Memory do
|
|
|
468
468
|
}.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
|
|
469
469
|
end
|
|
470
470
|
end
|
|
471
|
+
|
|
472
|
+
context "when the field name is a method name" do
|
|
473
|
+
let!(:person) do
|
|
474
|
+
Person.create!(ssn: "secret-ssn")
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
let!(:address) do
|
|
478
|
+
person.addresses.create!(street: "hobrecht")
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
let(:criteria) do
|
|
482
|
+
Address.all.tap do |crit|
|
|
483
|
+
crit.documents = [ address ]
|
|
484
|
+
end
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
let(:context) do
|
|
488
|
+
described_class.new(criteria)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
it "does not call the method" do
|
|
492
|
+
expect(address).not_to receive(:destroy)
|
|
493
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
494
|
+
end
|
|
495
|
+
end
|
|
471
496
|
end
|
|
472
497
|
|
|
473
498
|
context "when legacy_pluck_distinct is false" do
|
|
@@ -653,6 +678,27 @@ describe Mongoid::Contextual::Memory do
|
|
|
653
678
|
end
|
|
654
679
|
end
|
|
655
680
|
end
|
|
681
|
+
|
|
682
|
+
context 'when the field name is a method name' do
|
|
683
|
+
let!(:person) do
|
|
684
|
+
Person.create!(ssn: 'secret-ssn')
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
let!(:address) do
|
|
688
|
+
person.addresses.create!(street: 'hobrecht')
|
|
689
|
+
end
|
|
690
|
+
|
|
691
|
+
let(:criteria) do
|
|
692
|
+
Address.all.tap do |crit|
|
|
693
|
+
crit.documents = [ address ]
|
|
694
|
+
end
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
it 'does not call the method' do
|
|
698
|
+
expect(address).not_to receive(:destroy)
|
|
699
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
700
|
+
end
|
|
701
|
+
end
|
|
656
702
|
end
|
|
657
703
|
|
|
658
704
|
describe "#each" do
|
|
@@ -1996,6 +2042,116 @@ describe Mongoid::Contextual::Memory do
|
|
|
1996
2042
|
end
|
|
1997
2043
|
end
|
|
1998
2044
|
end
|
|
2045
|
+
|
|
2046
|
+
context 'when plucking a dynamic attribute' do
|
|
2047
|
+
let(:criteria) do
|
|
2048
|
+
Band.all.tap do |crit|
|
|
2049
|
+
crit.documents = [ Band.create!(name: 'Depeche Mode', mood: 'dark') ]
|
|
2050
|
+
end
|
|
2051
|
+
end
|
|
2052
|
+
|
|
2053
|
+
it 'returns the value from the attributes' do
|
|
2054
|
+
expect(context.pluck(:mood)).to eq([ 'dark' ])
|
|
2055
|
+
end
|
|
2056
|
+
end
|
|
2057
|
+
|
|
2058
|
+
context 'when the field name is a method name' do
|
|
2059
|
+
let!(:person) do
|
|
2060
|
+
Person.create!(ssn: 'secret-ssn')
|
|
2061
|
+
end
|
|
2062
|
+
|
|
2063
|
+
let!(:address) do
|
|
2064
|
+
person.addresses.create!(street: 'hobrecht')
|
|
2065
|
+
end
|
|
2066
|
+
|
|
2067
|
+
let(:criteria) do
|
|
2068
|
+
Address.all.tap do |crit|
|
|
2069
|
+
crit.documents = [ address ]
|
|
2070
|
+
end
|
|
2071
|
+
end
|
|
2072
|
+
|
|
2073
|
+
it 'does not call the method' do
|
|
2074
|
+
expect(address).not_to receive(:destroy)
|
|
2075
|
+
expect(context.pluck(:destroy)).to eq([ nil ])
|
|
2076
|
+
end
|
|
2077
|
+
|
|
2078
|
+
it 'does not return the attributes hash' do
|
|
2079
|
+
expect(context.pluck(:attributes)).to eq([ nil ])
|
|
2080
|
+
end
|
|
2081
|
+
|
|
2082
|
+
it 'does not traverse to the parent document' do
|
|
2083
|
+
expect(context.pluck('_root.ssn')).to eq([ nil ])
|
|
2084
|
+
end
|
|
2085
|
+
|
|
2086
|
+
it 'does not destroy the parent document' do
|
|
2087
|
+
expect(context.pluck('_root.destroy')).to eq([ nil ])
|
|
2088
|
+
expect(Person.where(_id: person.id).count).to eq(1)
|
|
2089
|
+
end
|
|
2090
|
+
end
|
|
2091
|
+
|
|
2092
|
+
context 'when plucking a belongs_to association' do
|
|
2093
|
+
let!(:band) do
|
|
2094
|
+
Band.create!(name: 'Depeche Mode')
|
|
2095
|
+
end
|
|
2096
|
+
|
|
2097
|
+
let!(:artist) do
|
|
2098
|
+
Artist.create!(band: band)
|
|
2099
|
+
end
|
|
2100
|
+
|
|
2101
|
+
let(:criteria) do
|
|
2102
|
+
Artist.all.tap do |crit|
|
|
2103
|
+
crit.documents = [ artist ]
|
|
2104
|
+
end
|
|
2105
|
+
end
|
|
2106
|
+
|
|
2107
|
+
it 'traverses to the associated document' do
|
|
2108
|
+
expect(context.pluck('band.name')).to eq([ 'Depeche Mode' ])
|
|
2109
|
+
end
|
|
2110
|
+
|
|
2111
|
+
it 'returns the document for the association name' do
|
|
2112
|
+
expect(context.pluck(:band)).to eq([ band ])
|
|
2113
|
+
end
|
|
2114
|
+
|
|
2115
|
+
it 'returns the id for the foreign key' do
|
|
2116
|
+
expect(context.pluck(:band_id)).to eq([ band.id ])
|
|
2117
|
+
end
|
|
2118
|
+
end
|
|
2119
|
+
|
|
2120
|
+
context 'when plucking the foreign key of a many to many association' do
|
|
2121
|
+
let!(:preference) do
|
|
2122
|
+
Preference.create!(name: 'nature')
|
|
2123
|
+
end
|
|
2124
|
+
|
|
2125
|
+
let!(:person) do
|
|
2126
|
+
Person.create!(preferences: [ preference ])
|
|
2127
|
+
end
|
|
2128
|
+
|
|
2129
|
+
let(:criteria) do
|
|
2130
|
+
Person.all.tap do |crit|
|
|
2131
|
+
crit.documents = [ person ]
|
|
2132
|
+
end
|
|
2133
|
+
end
|
|
2134
|
+
|
|
2135
|
+
it 'returns the stored ids' do
|
|
2136
|
+
expect(context.pluck(:preference_ids)).to eq([ [ preference.id ] ])
|
|
2137
|
+
end
|
|
2138
|
+
end
|
|
2139
|
+
|
|
2140
|
+
context 'when the field path has an empty segment' do
|
|
2141
|
+
let!(:band) do
|
|
2142
|
+
Band.create!(name: 'Depeche Mode', label: Label.new(name: 'Mute'))
|
|
2143
|
+
end
|
|
2144
|
+
|
|
2145
|
+
let(:criteria) do
|
|
2146
|
+
Band.all.tap do |crit|
|
|
2147
|
+
crit.documents = [ band ]
|
|
2148
|
+
end
|
|
2149
|
+
end
|
|
2150
|
+
|
|
2151
|
+
it 'returns nil' do
|
|
2152
|
+
expect(context.pluck('label..name')).to eq([ nil ])
|
|
2153
|
+
end
|
|
2154
|
+
end
|
|
1999
2155
|
end
|
|
2000
2156
|
|
|
2001
2157
|
describe "#pick" do
|
|
@@ -2056,6 +2212,27 @@ describe Mongoid::Contextual::Memory do
|
|
|
2056
2212
|
expect(picked).to be_nil
|
|
2057
2213
|
end
|
|
2058
2214
|
end
|
|
2215
|
+
|
|
2216
|
+
context 'when the field name is a method name' do
|
|
2217
|
+
let!(:person) do
|
|
2218
|
+
Person.create!(ssn: 'secret-ssn')
|
|
2219
|
+
end
|
|
2220
|
+
|
|
2221
|
+
let!(:address) do
|
|
2222
|
+
person.addresses.create!(street: 'hobrecht')
|
|
2223
|
+
end
|
|
2224
|
+
|
|
2225
|
+
let(:criteria) do
|
|
2226
|
+
Address.all.tap do |crit|
|
|
2227
|
+
crit.documents = [ address ]
|
|
2228
|
+
end
|
|
2229
|
+
end
|
|
2230
|
+
|
|
2231
|
+
it 'does not call the method' do
|
|
2232
|
+
expect(address).not_to receive(:destroy)
|
|
2233
|
+
expect(context.pick(:destroy)).to be_nil
|
|
2234
|
+
end
|
|
2235
|
+
end
|
|
2059
2236
|
end
|
|
2060
2237
|
|
|
2061
2238
|
describe "#tally" do
|
|
@@ -2510,6 +2687,31 @@ describe Mongoid::Contextual::Memory do
|
|
|
2510
2687
|
)
|
|
2511
2688
|
end
|
|
2512
2689
|
end
|
|
2690
|
+
|
|
2691
|
+
context 'when the field name is a method name' do
|
|
2692
|
+
let!(:person) do
|
|
2693
|
+
Person.create!(ssn: 'secret-ssn')
|
|
2694
|
+
end
|
|
2695
|
+
|
|
2696
|
+
let!(:address) do
|
|
2697
|
+
person.addresses.create!(street: 'hobrecht')
|
|
2698
|
+
end
|
|
2699
|
+
|
|
2700
|
+
let(:criteria) do
|
|
2701
|
+
Address.all.tap do |crit|
|
|
2702
|
+
crit.documents = [ address ]
|
|
2703
|
+
end
|
|
2704
|
+
end
|
|
2705
|
+
|
|
2706
|
+
it 'does not call the method' do
|
|
2707
|
+
expect(address).not_to receive(:destroy)
|
|
2708
|
+
expect(context.tally(:destroy)).to eq(nil => 1)
|
|
2709
|
+
end
|
|
2710
|
+
|
|
2711
|
+
it 'does not disclose the parent document fields' do
|
|
2712
|
+
expect(context.tally('_root.ssn')).to eq(nil => 1)
|
|
2713
|
+
end
|
|
2714
|
+
end
|
|
2513
2715
|
end
|
|
2514
2716
|
|
|
2515
2717
|
describe '#inc' do
|
|
@@ -53,10 +53,17 @@ describe Mongoid::Contextual::None do
|
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
-
describe
|
|
56
|
+
describe '#destroy' do
|
|
57
|
+
it 'returns zero and destroys nothing' do
|
|
58
|
+
expect(context.destroy).to eq(0)
|
|
59
|
+
expect(context.destroy_all).to eq(0)
|
|
60
|
+
expect(Band.where(name: 'Depeche Mode').count).to eq(1)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
describe '#pluck' do
|
|
65
|
+
context 'when plucking one field' do
|
|
66
|
+
it 'returns an empty array' do
|
|
60
67
|
expect(context.pluck(:id)).to eq([])
|
|
61
68
|
end
|
|
62
69
|
end
|
|
@@ -80,8 +87,18 @@ describe Mongoid::Contextual::None do
|
|
|
80
87
|
end
|
|
81
88
|
end
|
|
82
89
|
|
|
83
|
-
describe
|
|
84
|
-
it
|
|
90
|
+
describe '#update' do
|
|
91
|
+
it 'returns false without attributes and nil with them, updating nothing' do
|
|
92
|
+
expect(context.update).to be(false)
|
|
93
|
+
expect(context.update(name: 'The Cure')).to be_nil
|
|
94
|
+
expect(context.update_all).to be(false)
|
|
95
|
+
expect(context.update_all(name: 'The Cure')).to be_nil
|
|
96
|
+
expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
describe '#first' do
|
|
101
|
+
it 'returns nil' do
|
|
85
102
|
expect(context.first).to be_nil
|
|
86
103
|
end
|
|
87
104
|
|
|
@@ -1829,6 +1829,8 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
|
1829
1829
|
end
|
|
1830
1830
|
|
|
1831
1831
|
context 'when the following criteria uses string were form' do
|
|
1832
|
+
# String criteria compile to $where, which requires the opt-in.
|
|
1833
|
+
config_override :allow_unsafe_query_operators, true
|
|
1832
1834
|
|
|
1833
1835
|
let(:selection) do
|
|
1834
1836
|
query.not.where('hello world')
|
|
@@ -35,6 +35,8 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
|
35
35
|
it_behaves_like 'requires a non-nil argument'
|
|
36
36
|
|
|
37
37
|
context "when provided a string" do
|
|
38
|
+
# String criteria compile to $where, which requires the opt-in.
|
|
39
|
+
config_override :allow_unsafe_query_operators, true
|
|
38
40
|
|
|
39
41
|
let(:selection) do
|
|
40
42
|
query.where("this.value = 10")
|
|
@@ -586,4 +588,237 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
|
586
588
|
end
|
|
587
589
|
end
|
|
588
590
|
end
|
|
591
|
+
|
|
592
|
+
describe 'top-level operator injection guard' do
|
|
593
|
+
context 'when allow_unsafe_query_operators is true' do
|
|
594
|
+
config_override :allow_unsafe_query_operators, true
|
|
595
|
+
|
|
596
|
+
context 'when passing $where' do
|
|
597
|
+
it 'does not raise' do
|
|
598
|
+
expect do
|
|
599
|
+
query.where('$where' => 'this.name == "admin"')
|
|
600
|
+
end.not_to raise_error
|
|
601
|
+
end
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
context 'when passing a string criterion' do
|
|
605
|
+
it 'does not raise' do
|
|
606
|
+
expect do
|
|
607
|
+
query.where('this.name == "admin"')
|
|
608
|
+
end.not_to raise_error
|
|
609
|
+
end
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
context 'when passing a string criterion to a negated query' do
|
|
613
|
+
it 'does not raise' do
|
|
614
|
+
expect do
|
|
615
|
+
query.not.where('this.name == "admin"')
|
|
616
|
+
end.not_to raise_error
|
|
617
|
+
end
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
context 'when passing $function' do
|
|
621
|
+
it 'does not raise' do
|
|
622
|
+
expect do
|
|
623
|
+
query.where('$function' => { body: 'function() { return true; }', args: [], lang: 'js' })
|
|
624
|
+
end.not_to raise_error
|
|
625
|
+
end
|
|
626
|
+
end
|
|
627
|
+
end
|
|
628
|
+
|
|
629
|
+
context 'when allow_unsafe_query_operators is false' do
|
|
630
|
+
config_override :allow_unsafe_query_operators, false
|
|
631
|
+
|
|
632
|
+
context 'when passing $where' do
|
|
633
|
+
it 'raises InvalidQuery' do
|
|
634
|
+
expect do
|
|
635
|
+
query.where('$where' => 'this.name == "admin"')
|
|
636
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /\$where/)
|
|
637
|
+
end
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
context 'when passing $function' do
|
|
641
|
+
it 'raises InvalidQuery' do
|
|
642
|
+
expect do
|
|
643
|
+
query.where('$function' => { body: 'function() { return true; }', args: [], lang: 'js' })
|
|
644
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /\$function/)
|
|
645
|
+
end
|
|
646
|
+
end
|
|
647
|
+
|
|
648
|
+
context 'when the error mentions allow_unsafe_query_operators' do
|
|
649
|
+
it 'includes the config opt-in in the message' do
|
|
650
|
+
expect do
|
|
651
|
+
query.where('$where' => 'true')
|
|
652
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /allow_unsafe_query_operators/)
|
|
653
|
+
end
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
context 'when passing a string criterion' do
|
|
657
|
+
it 'raises InvalidQuery' do
|
|
658
|
+
expect do
|
|
659
|
+
query.where('this.name == "admin"')
|
|
660
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /\$where/)
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
it 'includes the config opt-in in the message' do
|
|
664
|
+
expect do
|
|
665
|
+
query.where('this.name == "admin"')
|
|
666
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /allow_unsafe_query_operators/)
|
|
667
|
+
end
|
|
668
|
+
end
|
|
669
|
+
|
|
670
|
+
context 'when passing a string criterion to a negated query' do
|
|
671
|
+
it 'raises InvalidQuery' do
|
|
672
|
+
expect do
|
|
673
|
+
query.not.where('this.name == "admin"')
|
|
674
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /\$where/)
|
|
675
|
+
end
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
%w[$and $or $nor $not $text $comment $expr $jsonSchema $alwaysFalse $alwaysTrue].each do |op|
|
|
679
|
+
context "when passing #{op} (allowlisted)" do
|
|
680
|
+
it 'does not raise' do
|
|
681
|
+
value = case op
|
|
682
|
+
when '$and', '$or', '$nor', '$not' then [ { 'x' => 1 } ]
|
|
683
|
+
when '$text' then { '$search' => 'hi' }
|
|
684
|
+
when '$expr' then { '$gt' => [ '$a', 1 ] }
|
|
685
|
+
when '$jsonSchema' then { 'required' => [ 'x' ] }
|
|
686
|
+
else true
|
|
687
|
+
end
|
|
688
|
+
expect { query.where(op => value) }.not_to raise_error
|
|
689
|
+
end
|
|
690
|
+
end
|
|
691
|
+
end
|
|
692
|
+
end
|
|
693
|
+
end
|
|
694
|
+
|
|
695
|
+
describe 'nested operator injection guard' do
|
|
696
|
+
js = 'this.name == "admin"'
|
|
697
|
+
|
|
698
|
+
function_expr = {
|
|
699
|
+
'$function' => { 'body' => 'function() { return true; }', 'args' => [], 'lang' => 'js' }
|
|
700
|
+
}.freeze
|
|
701
|
+
|
|
702
|
+
accumulator_expr = {
|
|
703
|
+
'$accumulator' => {
|
|
704
|
+
'init' => 'function() { return 0; }',
|
|
705
|
+
'accumulate' => 'function() { return 0; }',
|
|
706
|
+
'accumulateArgs' => [],
|
|
707
|
+
'merge' => 'function() { return 0; }',
|
|
708
|
+
'lang' => 'js'
|
|
709
|
+
}
|
|
710
|
+
}.freeze
|
|
711
|
+
|
|
712
|
+
# Every path into the selector that does not route through #expr_query,
|
|
713
|
+
# plus the nested forms that a top-level key check cannot see.
|
|
714
|
+
unsafe_queries = {
|
|
715
|
+
'and with a $where key' => ->(query) { query.and('$where' => js) },
|
|
716
|
+
'or with a $where key' => ->(query) { query.or('$where' => js) },
|
|
717
|
+
'nor with a $where key' => ->(query) { query.nor('$where' => js) },
|
|
718
|
+
'not with a $where key' => ->(query) { query.not('$where' => js) },
|
|
719
|
+
'any_of with a $where key' => ->(query) { query.any_of('$where' => js) },
|
|
720
|
+
'none_of with a $where key' => ->(query) { query.none_of('$where' => js) },
|
|
721
|
+
'elem_match with a nested $where' => ->(query) { query.elem_match(a: { '$where' => js }) },
|
|
722
|
+
'where with $where inside $or' => ->(query) { query.where('$or' => [ { '$where' => js } ]) },
|
|
723
|
+
'where with $function inside $expr' => ->(query) { query.where('$expr' => function_expr) },
|
|
724
|
+
'where with $accumulator inside $expr' => ->(query) { query.where('$expr' => accumulator_expr) },
|
|
725
|
+
'where with $where three levels deep' => lambda { |query|
|
|
726
|
+
query.where('$and' => [ { '$or' => [ { '$where' => js } ] } ])
|
|
727
|
+
},
|
|
728
|
+
'where with a symbol $where key inside $or' => ->(query) { query.where('$or' => [ { :$where => js } ]) }
|
|
729
|
+
}.freeze
|
|
730
|
+
|
|
731
|
+
context 'when allow_unsafe_query_operators is false' do
|
|
732
|
+
config_override :allow_unsafe_query_operators, false
|
|
733
|
+
|
|
734
|
+
unsafe_queries.each do |description, builder|
|
|
735
|
+
context "when querying with #{description}" do
|
|
736
|
+
it 'raises InvalidQuery' do
|
|
737
|
+
expect do
|
|
738
|
+
builder.call(query)
|
|
739
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /\$where|\$function|\$accumulator/)
|
|
740
|
+
end
|
|
741
|
+
end
|
|
742
|
+
end
|
|
743
|
+
|
|
744
|
+
it 'includes the config opt-in in the message' do
|
|
745
|
+
expect do
|
|
746
|
+
query.or('$where' => js)
|
|
747
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /allow_unsafe_query_operators/)
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
context 'when the nested expression is safe' do
|
|
751
|
+
it 'permits $or with field expressions' do
|
|
752
|
+
expect do
|
|
753
|
+
query.where('$or' => [ { 'a' => 1 }, { 'b' => { '$gt' => 2 } } ])
|
|
754
|
+
end.not_to raise_error
|
|
755
|
+
end
|
|
756
|
+
|
|
757
|
+
it 'permits $expr with aggregation operators' do
|
|
758
|
+
expect do
|
|
759
|
+
query.where('$expr' => { '$gt' => [ '$a', '$b' ] })
|
|
760
|
+
end.not_to raise_error
|
|
761
|
+
end
|
|
762
|
+
|
|
763
|
+
it 'permits a field whose name resembles a javascript operator' do
|
|
764
|
+
expect do
|
|
765
|
+
query.where('where' => js)
|
|
766
|
+
end.not_to raise_error
|
|
767
|
+
end
|
|
768
|
+
|
|
769
|
+
it 'permits a value that resembles a javascript operator' do
|
|
770
|
+
expect do
|
|
771
|
+
query.where('name' => '$where')
|
|
772
|
+
end.not_to raise_error
|
|
773
|
+
end
|
|
774
|
+
end
|
|
775
|
+
end
|
|
776
|
+
|
|
777
|
+
context 'when allow_unsafe_query_operators is true' do
|
|
778
|
+
config_override :allow_unsafe_query_operators, true
|
|
779
|
+
|
|
780
|
+
unsafe_queries.each do |description, builder|
|
|
781
|
+
context "when querying with #{description}" do
|
|
782
|
+
it 'does not raise' do
|
|
783
|
+
expect { builder.call(query) }.not_to raise_error
|
|
784
|
+
end
|
|
785
|
+
end
|
|
786
|
+
end
|
|
787
|
+
end
|
|
788
|
+
end
|
|
789
|
+
|
|
790
|
+
describe 'operator guard on chained override methods' do
|
|
791
|
+
# On this branch overwrite_chained_operators defaults to false, which
|
|
792
|
+
# routes elem_match through the guarded and_with_operator funnel. Force
|
|
793
|
+
# the override path so the guard in Mergeable#__override__ is exercised.
|
|
794
|
+
config_override :overwrite_chained_operators, true
|
|
795
|
+
|
|
796
|
+
let(:js) { 'this.name == "admin"' }
|
|
797
|
+
|
|
798
|
+
context 'when allow_unsafe_query_operators is false' do
|
|
799
|
+
config_override :allow_unsafe_query_operators, false
|
|
800
|
+
|
|
801
|
+
it 'rejects a $where nested in elem_match' do
|
|
802
|
+
expect do
|
|
803
|
+
query.elem_match(a: { '$where' => js })
|
|
804
|
+
end.to raise_error(Mongoid::Errors::InvalidQuery, /executes server-side JavaScript/)
|
|
805
|
+
end
|
|
806
|
+
|
|
807
|
+
it 'permits an ordinary elem_match value' do
|
|
808
|
+
expect do
|
|
809
|
+
query.elem_match(a: { name: 'x' })
|
|
810
|
+
end.not_to raise_error
|
|
811
|
+
end
|
|
812
|
+
end
|
|
813
|
+
|
|
814
|
+
context 'when allow_unsafe_query_operators is true' do
|
|
815
|
+
config_override :allow_unsafe_query_operators, true
|
|
816
|
+
|
|
817
|
+
it 'permits a $where nested in elem_match' do
|
|
818
|
+
expect do
|
|
819
|
+
query.elem_match(a: { '$where' => js })
|
|
820
|
+
end.not_to raise_error
|
|
821
|
+
end
|
|
822
|
+
end
|
|
823
|
+
end
|
|
589
824
|
end
|
|
@@ -1507,6 +1507,23 @@ describe Mongoid::Criteria do
|
|
|
1507
1507
|
it "returns the same criteria" do
|
|
1508
1508
|
expect(merged).to equal(criteria)
|
|
1509
1509
|
end
|
|
1510
|
+
|
|
1511
|
+
context 'when merging a none criteria into a criteria with a cached context' do
|
|
1512
|
+
let(:criteria) { Band.where(name: 'Depeche Mode') }
|
|
1513
|
+
let(:mergeable) { Band.none }
|
|
1514
|
+
|
|
1515
|
+
before do
|
|
1516
|
+
Band.create!(name: 'Depeche Mode')
|
|
1517
|
+
end
|
|
1518
|
+
|
|
1519
|
+
it 'does not use the cached context' do
|
|
1520
|
+
expect(criteria.count).to eq(1)
|
|
1521
|
+
|
|
1522
|
+
merged
|
|
1523
|
+
|
|
1524
|
+
expect(criteria.count).to eq(0)
|
|
1525
|
+
end
|
|
1526
|
+
end
|
|
1510
1527
|
end
|
|
1511
1528
|
|
|
1512
1529
|
describe "#min" do
|
|
@@ -2659,6 +2676,8 @@ describe Mongoid::Criteria do
|
|
|
2659
2676
|
end
|
|
2660
2677
|
|
|
2661
2678
|
context "when the criteria is not embedded" do
|
|
2679
|
+
# String criteria compile to $where, which requires the opt-in.
|
|
2680
|
+
config_override :allow_unsafe_query_operators, true
|
|
2662
2681
|
|
|
2663
2682
|
let(:criteria) do
|
|
2664
2683
|
Band.where("this.name == 'Depeche Mode'")
|
|
@@ -817,6 +817,20 @@ describe Mongoid::Findable do
|
|
|
817
817
|
expect(criteria.size).to eq(0)
|
|
818
818
|
end
|
|
819
819
|
end
|
|
820
|
+
|
|
821
|
+
context 'when none is called on a criteria with a cached context' do
|
|
822
|
+
let(:criteria) do
|
|
823
|
+
Band.where(name: 'Depeche Mode')
|
|
824
|
+
end
|
|
825
|
+
|
|
826
|
+
it 'does not use the cached context' do
|
|
827
|
+
expect(criteria.count).to eq(1)
|
|
828
|
+
|
|
829
|
+
criteria.none
|
|
830
|
+
|
|
831
|
+
expect(criteria.count).to eq(0)
|
|
832
|
+
end
|
|
833
|
+
end
|
|
820
834
|
end
|
|
821
835
|
|
|
822
836
|
describe ".pluck" do
|