mongoid 8.0.12 → 8.0.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 +387 -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 +18 -0
- 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 +1862 -1826
|
@@ -438,6 +438,31 @@ describe Mongoid::Contextual::Memory do
|
|
|
438
438
|
}.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
|
|
439
439
|
end
|
|
440
440
|
end
|
|
441
|
+
|
|
442
|
+
context "when the field name is a method name" do
|
|
443
|
+
let!(:person) do
|
|
444
|
+
Person.create!(ssn: "secret-ssn")
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
let!(:address) do
|
|
448
|
+
person.addresses.create!(street: "hobrecht")
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
let(:criteria) do
|
|
452
|
+
Address.all.tap do |crit|
|
|
453
|
+
crit.documents = [ address ]
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
let(:context) do
|
|
458
|
+
described_class.new(criteria)
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
it "does not call the method" do
|
|
462
|
+
expect(address).not_to receive(:destroy)
|
|
463
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
464
|
+
end
|
|
465
|
+
end
|
|
441
466
|
end
|
|
442
467
|
|
|
443
468
|
context "when legacy_pluck_distinct is false" do
|
|
@@ -633,6 +658,27 @@ describe Mongoid::Contextual::Memory do
|
|
|
633
658
|
end
|
|
634
659
|
end
|
|
635
660
|
end
|
|
661
|
+
|
|
662
|
+
context 'when the field name is a method name' do
|
|
663
|
+
let!(:person) do
|
|
664
|
+
Person.create!(ssn: 'secret-ssn')
|
|
665
|
+
end
|
|
666
|
+
|
|
667
|
+
let!(:address) do
|
|
668
|
+
person.addresses.create!(street: 'hobrecht')
|
|
669
|
+
end
|
|
670
|
+
|
|
671
|
+
let(:criteria) do
|
|
672
|
+
Address.all.tap do |crit|
|
|
673
|
+
crit.documents = [ address ]
|
|
674
|
+
end
|
|
675
|
+
end
|
|
676
|
+
|
|
677
|
+
it 'does not call the method' do
|
|
678
|
+
expect(address).not_to receive(:destroy)
|
|
679
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
680
|
+
end
|
|
681
|
+
end
|
|
636
682
|
end
|
|
637
683
|
|
|
638
684
|
describe "#each" do
|
|
@@ -1743,6 +1789,116 @@ describe Mongoid::Contextual::Memory do
|
|
|
1743
1789
|
end
|
|
1744
1790
|
end
|
|
1745
1791
|
end
|
|
1792
|
+
|
|
1793
|
+
context 'when plucking a dynamic attribute' do
|
|
1794
|
+
let(:criteria) do
|
|
1795
|
+
Band.all.tap do |crit|
|
|
1796
|
+
crit.documents = [ Band.create!(name: 'Depeche Mode', mood: 'dark') ]
|
|
1797
|
+
end
|
|
1798
|
+
end
|
|
1799
|
+
|
|
1800
|
+
it 'returns the value from the attributes' do
|
|
1801
|
+
expect(context.pluck(:mood)).to eq([ 'dark' ])
|
|
1802
|
+
end
|
|
1803
|
+
end
|
|
1804
|
+
|
|
1805
|
+
context 'when the field name is a method name' do
|
|
1806
|
+
let!(:person) do
|
|
1807
|
+
Person.create!(ssn: 'secret-ssn')
|
|
1808
|
+
end
|
|
1809
|
+
|
|
1810
|
+
let!(:address) do
|
|
1811
|
+
person.addresses.create!(street: 'hobrecht')
|
|
1812
|
+
end
|
|
1813
|
+
|
|
1814
|
+
let(:criteria) do
|
|
1815
|
+
Address.all.tap do |crit|
|
|
1816
|
+
crit.documents = [ address ]
|
|
1817
|
+
end
|
|
1818
|
+
end
|
|
1819
|
+
|
|
1820
|
+
it 'does not call the method' do
|
|
1821
|
+
expect(address).not_to receive(:destroy)
|
|
1822
|
+
expect(context.pluck(:destroy)).to eq([ nil ])
|
|
1823
|
+
end
|
|
1824
|
+
|
|
1825
|
+
it 'does not return the attributes hash' do
|
|
1826
|
+
expect(context.pluck(:attributes)).to eq([ nil ])
|
|
1827
|
+
end
|
|
1828
|
+
|
|
1829
|
+
it 'does not traverse to the parent document' do
|
|
1830
|
+
expect(context.pluck('_root.ssn')).to eq([ nil ])
|
|
1831
|
+
end
|
|
1832
|
+
|
|
1833
|
+
it 'does not destroy the parent document' do
|
|
1834
|
+
expect(context.pluck('_root.destroy')).to eq([ nil ])
|
|
1835
|
+
expect(Person.where(_id: person.id).count).to eq(1)
|
|
1836
|
+
end
|
|
1837
|
+
end
|
|
1838
|
+
|
|
1839
|
+
context 'when plucking a belongs_to association' do
|
|
1840
|
+
let!(:band) do
|
|
1841
|
+
Band.create!(name: 'Depeche Mode')
|
|
1842
|
+
end
|
|
1843
|
+
|
|
1844
|
+
let!(:artist) do
|
|
1845
|
+
Artist.create!(band: band)
|
|
1846
|
+
end
|
|
1847
|
+
|
|
1848
|
+
let(:criteria) do
|
|
1849
|
+
Artist.all.tap do |crit|
|
|
1850
|
+
crit.documents = [ artist ]
|
|
1851
|
+
end
|
|
1852
|
+
end
|
|
1853
|
+
|
|
1854
|
+
it 'traverses to the associated document' do
|
|
1855
|
+
expect(context.pluck('band.name')).to eq([ 'Depeche Mode' ])
|
|
1856
|
+
end
|
|
1857
|
+
|
|
1858
|
+
it 'returns the document for the association name' do
|
|
1859
|
+
expect(context.pluck(:band)).to eq([ band ])
|
|
1860
|
+
end
|
|
1861
|
+
|
|
1862
|
+
it 'returns the id for the foreign key' do
|
|
1863
|
+
expect(context.pluck(:band_id)).to eq([ band.id ])
|
|
1864
|
+
end
|
|
1865
|
+
end
|
|
1866
|
+
|
|
1867
|
+
context 'when plucking the foreign key of a many to many association' do
|
|
1868
|
+
let!(:preference) do
|
|
1869
|
+
Preference.create!(name: 'nature')
|
|
1870
|
+
end
|
|
1871
|
+
|
|
1872
|
+
let!(:person) do
|
|
1873
|
+
Person.create!(preferences: [ preference ])
|
|
1874
|
+
end
|
|
1875
|
+
|
|
1876
|
+
let(:criteria) do
|
|
1877
|
+
Person.all.tap do |crit|
|
|
1878
|
+
crit.documents = [ person ]
|
|
1879
|
+
end
|
|
1880
|
+
end
|
|
1881
|
+
|
|
1882
|
+
it 'returns the stored ids' do
|
|
1883
|
+
expect(context.pluck(:preference_ids)).to eq([ [ preference.id ] ])
|
|
1884
|
+
end
|
|
1885
|
+
end
|
|
1886
|
+
|
|
1887
|
+
context 'when the field path has an empty segment' do
|
|
1888
|
+
let!(:band) do
|
|
1889
|
+
Band.create!(name: 'Depeche Mode', label: Label.new(name: 'Mute'))
|
|
1890
|
+
end
|
|
1891
|
+
|
|
1892
|
+
let(:criteria) do
|
|
1893
|
+
Band.all.tap do |crit|
|
|
1894
|
+
crit.documents = [ band ]
|
|
1895
|
+
end
|
|
1896
|
+
end
|
|
1897
|
+
|
|
1898
|
+
it 'returns nil' do
|
|
1899
|
+
expect(context.pluck('label..name')).to eq([ nil ])
|
|
1900
|
+
end
|
|
1901
|
+
end
|
|
1746
1902
|
end
|
|
1747
1903
|
|
|
1748
1904
|
describe "#pick" do
|
|
@@ -1803,6 +1959,27 @@ describe Mongoid::Contextual::Memory do
|
|
|
1803
1959
|
expect(picked).to be_nil
|
|
1804
1960
|
end
|
|
1805
1961
|
end
|
|
1962
|
+
|
|
1963
|
+
context 'when the field name is a method name' do
|
|
1964
|
+
let!(:person) do
|
|
1965
|
+
Person.create!(ssn: 'secret-ssn')
|
|
1966
|
+
end
|
|
1967
|
+
|
|
1968
|
+
let!(:address) do
|
|
1969
|
+
person.addresses.create!(street: 'hobrecht')
|
|
1970
|
+
end
|
|
1971
|
+
|
|
1972
|
+
let(:criteria) do
|
|
1973
|
+
Address.all.tap do |crit|
|
|
1974
|
+
crit.documents = [ address ]
|
|
1975
|
+
end
|
|
1976
|
+
end
|
|
1977
|
+
|
|
1978
|
+
it 'does not call the method' do
|
|
1979
|
+
expect(address).not_to receive(:destroy)
|
|
1980
|
+
expect(context.pick(:destroy)).to be_nil
|
|
1981
|
+
end
|
|
1982
|
+
end
|
|
1806
1983
|
end
|
|
1807
1984
|
|
|
1808
1985
|
describe "#tally" do
|
|
@@ -2254,6 +2431,31 @@ describe Mongoid::Contextual::Memory do
|
|
|
2254
2431
|
)
|
|
2255
2432
|
end
|
|
2256
2433
|
end
|
|
2434
|
+
|
|
2435
|
+
context 'when the field name is a method name' do
|
|
2436
|
+
let!(:person) do
|
|
2437
|
+
Person.create!(ssn: 'secret-ssn')
|
|
2438
|
+
end
|
|
2439
|
+
|
|
2440
|
+
let!(:address) do
|
|
2441
|
+
person.addresses.create!(street: 'hobrecht')
|
|
2442
|
+
end
|
|
2443
|
+
|
|
2444
|
+
let(:criteria) do
|
|
2445
|
+
Address.all.tap do |crit|
|
|
2446
|
+
crit.documents = [ address ]
|
|
2447
|
+
end
|
|
2448
|
+
end
|
|
2449
|
+
|
|
2450
|
+
it 'does not call the method' do
|
|
2451
|
+
expect(address).not_to receive(:destroy)
|
|
2452
|
+
expect(context.tally(:destroy)).to eq(nil => 1)
|
|
2453
|
+
end
|
|
2454
|
+
|
|
2455
|
+
it 'does not disclose the parent document fields' do
|
|
2456
|
+
expect(context.tally('_root.ssn')).to eq(nil => 1)
|
|
2457
|
+
end
|
|
2458
|
+
end
|
|
2257
2459
|
end
|
|
2258
2460
|
|
|
2259
2461
|
describe '#inc' do
|
|
@@ -53,6 +53,14 @@ describe Mongoid::Contextual::None do
|
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
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
|
|
63
|
+
|
|
56
64
|
describe "#pluck" do
|
|
57
65
|
it "returns an empty array" do
|
|
58
66
|
expect(context.pluck(:id)).to eq([])
|
|
@@ -71,6 +79,16 @@ describe Mongoid::Contextual::None do
|
|
|
71
79
|
end
|
|
72
80
|
end
|
|
73
81
|
|
|
82
|
+
describe '#update' do
|
|
83
|
+
it 'returns false without attributes and nil with them, updating nothing' do
|
|
84
|
+
expect(context.update).to be(false)
|
|
85
|
+
expect(context.update(name: 'The Cure')).to be_nil
|
|
86
|
+
expect(context.update_all).to be(false)
|
|
87
|
+
expect(context.update_all(name: 'The Cure')).to be_nil
|
|
88
|
+
expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
74
92
|
describe "#first" do
|
|
75
93
|
it "returns nil" do
|
|
76
94
|
expect(context.first).to be_nil
|
|
@@ -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 is not available on this branch; it landed in 8.1 via MONGOID-5453.
|
|
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
|
|
@@ -2663,6 +2680,8 @@ describe Mongoid::Criteria do
|
|
|
2663
2680
|
end
|
|
2664
2681
|
|
|
2665
2682
|
context "when the criteria is not embedded" do
|
|
2683
|
+
# String criteria compile to $where, which requires the opt-in.
|
|
2684
|
+
config_override :allow_unsafe_query_operators, true
|
|
2666
2685
|
|
|
2667
2686
|
let(:criteria) do
|
|
2668
2687
|
Band.where("this.name == 'Depeche Mode'")
|
|
@@ -457,6 +457,20 @@ describe Mongoid::Findable do
|
|
|
457
457
|
expect(criteria.size).to eq(0)
|
|
458
458
|
end
|
|
459
459
|
end
|
|
460
|
+
|
|
461
|
+
context 'when none is called on a criteria with a cached context' do
|
|
462
|
+
let(:criteria) do
|
|
463
|
+
Band.where(name: 'Depeche Mode')
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
it 'does not use the cached context' do
|
|
467
|
+
expect(criteria.count).to eq(1)
|
|
468
|
+
|
|
469
|
+
criteria.none
|
|
470
|
+
|
|
471
|
+
expect(criteria.count).to eq(0)
|
|
472
|
+
end
|
|
473
|
+
end
|
|
460
474
|
end
|
|
461
475
|
|
|
462
476
|
describe ".pluck" do
|