mongoid 7.6.1 → 7.6.3
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 +105 -4
- data/lib/mongoid/attributes/readonly.rb +33 -3
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/config.rb +62 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +8 -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 +124 -10
- 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/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 +94 -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 +202 -8
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +286 -0
- data/spec/mongoid/attributes/readonly_spec.rb +387 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -0
- data/spec/mongoid/contextual/memory_spec.rb +196 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +29 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +200 -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/shared/lib/mrss/cluster_config.rb +5 -40
- data/spec/shared/lib/mrss/constraints.rb +7 -46
- data/spec/shared/lib/mrss/docker_runner.rb +10 -21
- data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
- data/spec/shared/lib/mrss/release/candidate.rb +18 -12
- data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
- data/spec/shared/share/Dockerfile.erb +6 -8
- data/spec/shared/shlib/server.sh +0 -50
- data/spec/shared/shlib/set_env.sh +4 -10
- 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 +1817 -1781
|
@@ -438,6 +438,32 @@ 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
|
+
|
|
444
|
+
let!(:person) do
|
|
445
|
+
Person.create!(ssn: "secret-ssn")
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
let!(:address) do
|
|
449
|
+
person.addresses.create!(street: "hobrecht")
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
let(:criteria) do
|
|
453
|
+
Address.all.tap do |crit|
|
|
454
|
+
crit.documents = [ address ]
|
|
455
|
+
end
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
let(:context) do
|
|
459
|
+
described_class.new(criteria)
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
it "does not call the method" do
|
|
463
|
+
expect(address).not_to receive(:destroy)
|
|
464
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
465
|
+
end
|
|
466
|
+
end
|
|
441
467
|
end
|
|
442
468
|
|
|
443
469
|
context "when legacy_pluck_distinct is false" do
|
|
@@ -632,6 +658,28 @@ describe Mongoid::Contextual::Memory do
|
|
|
632
658
|
expect(context.distinct("label.sales")).to eq([ BigDecimal("1E2") ])
|
|
633
659
|
end
|
|
634
660
|
end
|
|
661
|
+
|
|
662
|
+
context "when the field name is a method name" do
|
|
663
|
+
|
|
664
|
+
let!(:person) do
|
|
665
|
+
Person.create!(ssn: "secret-ssn")
|
|
666
|
+
end
|
|
667
|
+
|
|
668
|
+
let!(:address) do
|
|
669
|
+
person.addresses.create!(street: "hobrecht")
|
|
670
|
+
end
|
|
671
|
+
|
|
672
|
+
let(:criteria) do
|
|
673
|
+
Address.all.tap do |crit|
|
|
674
|
+
crit.documents = [ address ]
|
|
675
|
+
end
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
it "does not call the method" do
|
|
679
|
+
expect(address).not_to receive(:destroy)
|
|
680
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
681
|
+
end
|
|
682
|
+
end
|
|
635
683
|
end
|
|
636
684
|
end
|
|
637
685
|
|
|
@@ -1747,6 +1795,121 @@ describe Mongoid::Contextual::Memory do
|
|
|
1747
1795
|
])
|
|
1748
1796
|
end
|
|
1749
1797
|
end
|
|
1798
|
+
|
|
1799
|
+
context "when plucking a dynamic attribute" do
|
|
1800
|
+
|
|
1801
|
+
let(:criteria) do
|
|
1802
|
+
Band.all.tap do |crit|
|
|
1803
|
+
crit.documents = [ Band.create!(name: "Depeche Mode", mood: "dark") ]
|
|
1804
|
+
end
|
|
1805
|
+
end
|
|
1806
|
+
|
|
1807
|
+
it "returns the value from the attributes" do
|
|
1808
|
+
expect(context.pluck(:mood)).to eq([ "dark" ])
|
|
1809
|
+
end
|
|
1810
|
+
end
|
|
1811
|
+
|
|
1812
|
+
context "when the field name is a method name" do
|
|
1813
|
+
|
|
1814
|
+
let!(:person) do
|
|
1815
|
+
Person.create!(ssn: "secret-ssn")
|
|
1816
|
+
end
|
|
1817
|
+
|
|
1818
|
+
let!(:address) do
|
|
1819
|
+
person.addresses.create!(street: "hobrecht")
|
|
1820
|
+
end
|
|
1821
|
+
|
|
1822
|
+
let(:criteria) do
|
|
1823
|
+
Address.all.tap do |crit|
|
|
1824
|
+
crit.documents = [ address ]
|
|
1825
|
+
end
|
|
1826
|
+
end
|
|
1827
|
+
|
|
1828
|
+
it "does not call the method" do
|
|
1829
|
+
expect(address).not_to receive(:destroy)
|
|
1830
|
+
expect(context.pluck(:destroy)).to eq([ nil ])
|
|
1831
|
+
end
|
|
1832
|
+
|
|
1833
|
+
it "does not return the attributes hash" do
|
|
1834
|
+
expect(context.pluck(:attributes)).to eq([ nil ])
|
|
1835
|
+
end
|
|
1836
|
+
|
|
1837
|
+
it "does not traverse to the parent document" do
|
|
1838
|
+
expect(context.pluck("_root.ssn")).to eq([ nil ])
|
|
1839
|
+
end
|
|
1840
|
+
|
|
1841
|
+
it "does not destroy the parent document" do
|
|
1842
|
+
expect(context.pluck("_root.destroy")).to eq([ nil ])
|
|
1843
|
+
expect(Person.where(_id: person.id).count).to eq(1)
|
|
1844
|
+
end
|
|
1845
|
+
end
|
|
1846
|
+
|
|
1847
|
+
context "when plucking a belongs_to association" do
|
|
1848
|
+
|
|
1849
|
+
let!(:band) do
|
|
1850
|
+
Band.create!(name: "Depeche Mode")
|
|
1851
|
+
end
|
|
1852
|
+
|
|
1853
|
+
let!(:artist) do
|
|
1854
|
+
Artist.create!(band: band)
|
|
1855
|
+
end
|
|
1856
|
+
|
|
1857
|
+
let(:criteria) do
|
|
1858
|
+
Artist.all.tap do |crit|
|
|
1859
|
+
crit.documents = [ artist ]
|
|
1860
|
+
end
|
|
1861
|
+
end
|
|
1862
|
+
|
|
1863
|
+
it "traverses to the associated document" do
|
|
1864
|
+
expect(context.pluck("band.name")).to eq([ "Depeche Mode" ])
|
|
1865
|
+
end
|
|
1866
|
+
|
|
1867
|
+
it "returns the document for the association name" do
|
|
1868
|
+
expect(context.pluck(:band)).to eq([ band ])
|
|
1869
|
+
end
|
|
1870
|
+
|
|
1871
|
+
it "returns the id for the foreign key" do
|
|
1872
|
+
expect(context.pluck(:band_id)).to eq([ band.id ])
|
|
1873
|
+
end
|
|
1874
|
+
end
|
|
1875
|
+
|
|
1876
|
+
context "when plucking the foreign key of a many to many association" do
|
|
1877
|
+
|
|
1878
|
+
let!(:preference) do
|
|
1879
|
+
Preference.create!(name: "nature")
|
|
1880
|
+
end
|
|
1881
|
+
|
|
1882
|
+
let!(:person) do
|
|
1883
|
+
Person.create!(preferences: [ preference ])
|
|
1884
|
+
end
|
|
1885
|
+
|
|
1886
|
+
let(:criteria) do
|
|
1887
|
+
Person.all.tap do |crit|
|
|
1888
|
+
crit.documents = [ person ]
|
|
1889
|
+
end
|
|
1890
|
+
end
|
|
1891
|
+
|
|
1892
|
+
it "returns the stored ids" do
|
|
1893
|
+
expect(context.pluck(:preference_ids)).to eq([ [ preference.id ] ])
|
|
1894
|
+
end
|
|
1895
|
+
end
|
|
1896
|
+
|
|
1897
|
+
context "when the field path has an empty segment" do
|
|
1898
|
+
|
|
1899
|
+
let!(:band) do
|
|
1900
|
+
Band.create!(name: "Depeche Mode", label: Label.new(name: "Mute"))
|
|
1901
|
+
end
|
|
1902
|
+
|
|
1903
|
+
let(:criteria) do
|
|
1904
|
+
Band.all.tap do |crit|
|
|
1905
|
+
crit.documents = [ band ]
|
|
1906
|
+
end
|
|
1907
|
+
end
|
|
1908
|
+
|
|
1909
|
+
it "returns nil" do
|
|
1910
|
+
expect(context.pluck("label..name")).to eq([ nil ])
|
|
1911
|
+
end
|
|
1912
|
+
end
|
|
1750
1913
|
end
|
|
1751
1914
|
end
|
|
1752
1915
|
|
|
@@ -1769,6 +1932,39 @@ describe Mongoid::Contextual::Memory do
|
|
|
1769
1932
|
end
|
|
1770
1933
|
end
|
|
1771
1934
|
|
|
1935
|
+
describe "#tally" do
|
|
1936
|
+
|
|
1937
|
+
let(:context) do
|
|
1938
|
+
described_class.new(criteria)
|
|
1939
|
+
end
|
|
1940
|
+
|
|
1941
|
+
context "when the field name is a method name" do
|
|
1942
|
+
|
|
1943
|
+
let!(:person) do
|
|
1944
|
+
Person.create!(ssn: "secret-ssn")
|
|
1945
|
+
end
|
|
1946
|
+
|
|
1947
|
+
let!(:address) do
|
|
1948
|
+
person.addresses.create!(street: "hobrecht")
|
|
1949
|
+
end
|
|
1950
|
+
|
|
1951
|
+
let(:criteria) do
|
|
1952
|
+
Address.all.tap do |crit|
|
|
1953
|
+
crit.documents = [ address ]
|
|
1954
|
+
end
|
|
1955
|
+
end
|
|
1956
|
+
|
|
1957
|
+
it "does not call the method" do
|
|
1958
|
+
expect(address).not_to receive(:destroy)
|
|
1959
|
+
expect(context.tally(:destroy)).to eq(nil => 1)
|
|
1960
|
+
end
|
|
1961
|
+
|
|
1962
|
+
it "does not disclose the parent document fields" do
|
|
1963
|
+
expect(context.tally("_root.ssn")).to eq(nil => 1)
|
|
1964
|
+
end
|
|
1965
|
+
end
|
|
1966
|
+
end
|
|
1967
|
+
|
|
1772
1968
|
describe "#skip" do
|
|
1773
1969
|
|
|
1774
1970
|
let(:hobrecht) do
|
|
@@ -53,12 +53,30 @@ 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([])
|
|
59
67
|
end
|
|
60
68
|
end
|
|
61
69
|
|
|
70
|
+
describe '#update' do
|
|
71
|
+
it 'returns false without attributes and nil with them, updating nothing' do
|
|
72
|
+
expect(context.update).to be(false)
|
|
73
|
+
expect(context.update(name: 'The Cure')).to be_nil
|
|
74
|
+
expect(context.update_all).to be(false)
|
|
75
|
+
expect(context.update_all(name: 'The Cure')).to be_nil
|
|
76
|
+
expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
62
80
|
describe "#first" do
|
|
63
81
|
it "returns nil" do
|
|
64
82
|
expect(context.first).to be_nil
|
|
@@ -1344,6 +1344,33 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
|
1344
1344
|
)
|
|
1345
1345
|
end
|
|
1346
1346
|
end
|
|
1347
|
+
|
|
1348
|
+
context 'when any_of has multiple arguments' do
|
|
1349
|
+
|
|
1350
|
+
let(:selection) do
|
|
1351
|
+
query.or(field: [ 1, 2 ]).where(foo: 'bar').any_of({a: 1}, {b: 2})
|
|
1352
|
+
end
|
|
1353
|
+
|
|
1354
|
+
it 'adds the new conditions to the existing top-level $or' do
|
|
1355
|
+
expect(selection.selector).to eq(
|
|
1356
|
+
'$or' => [{'field' => [1, 2]}, {'a' => 1}, {'b' => 2}],
|
|
1357
|
+
'foo' => 'bar',
|
|
1358
|
+
)
|
|
1359
|
+
end
|
|
1360
|
+
|
|
1361
|
+
context 'when query already has a top-level $and' do
|
|
1362
|
+
let(:selection) do
|
|
1363
|
+
query.or(field: [ 1, 2 ]).where('$and' => [foo: 'bar']).any_of({a: 1}, {b: 2})
|
|
1364
|
+
end
|
|
1365
|
+
|
|
1366
|
+
it 'adds the new conditions to the existing top-level $or' do
|
|
1367
|
+
expect(selection.selector).to eq(
|
|
1368
|
+
'$or' => [{'field' => [1, 2]}, {'a' => 1}, {'b' => 2}],
|
|
1369
|
+
'$and' => [{'foo' => 'bar'}],
|
|
1370
|
+
)
|
|
1371
|
+
end
|
|
1372
|
+
end
|
|
1373
|
+
end
|
|
1347
1374
|
end
|
|
1348
1375
|
|
|
1349
1376
|
context "when provided multiple criteria" do
|
|
@@ -1801,6 +1828,8 @@ describe Mongoid::Criteria::Queryable::Selectable do
|
|
|
1801
1828
|
end
|
|
1802
1829
|
|
|
1803
1830
|
context 'when the following criteria uses string were form' do
|
|
1831
|
+
# String criteria compile to $where, which requires the opt-in.
|
|
1832
|
+
config_override :allow_unsafe_query_operators, true
|
|
1804
1833
|
|
|
1805
1834
|
let(:selection) do
|
|
1806
1835
|
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,202 @@ 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
|
|
589
789
|
end
|
|
@@ -2622,6 +2622,23 @@ describe Mongoid::Criteria do
|
|
|
2622
2622
|
it "returns the same criteria" do
|
|
2623
2623
|
expect(merged).to equal(criteria)
|
|
2624
2624
|
end
|
|
2625
|
+
|
|
2626
|
+
context 'when merging a none criteria into a criteria with a cached context' do
|
|
2627
|
+
let(:criteria) { Band.where(name: 'Depeche Mode') }
|
|
2628
|
+
let(:mergeable) { Band.none }
|
|
2629
|
+
|
|
2630
|
+
before do
|
|
2631
|
+
Band.create!(name: 'Depeche Mode')
|
|
2632
|
+
end
|
|
2633
|
+
|
|
2634
|
+
it 'does not use the cached context' do
|
|
2635
|
+
expect(criteria.count).to eq(1)
|
|
2636
|
+
|
|
2637
|
+
merged
|
|
2638
|
+
|
|
2639
|
+
expect(criteria.count).to eq(0)
|
|
2640
|
+
end
|
|
2641
|
+
end
|
|
2625
2642
|
end
|
|
2626
2643
|
|
|
2627
2644
|
describe "#min" do
|
|
@@ -3695,6 +3712,8 @@ describe Mongoid::Criteria do
|
|
|
3695
3712
|
end
|
|
3696
3713
|
|
|
3697
3714
|
context "when the criteria is not embedded" do
|
|
3715
|
+
# String criteria compile to $where, which requires the opt-in.
|
|
3716
|
+
config_override :allow_unsafe_query_operators, true
|
|
3698
3717
|
|
|
3699
3718
|
let(:criteria) do
|
|
3700
3719
|
Band.where("this.name == 'Depeche Mode'")
|
|
@@ -469,6 +469,20 @@ describe Mongoid::Findable do
|
|
|
469
469
|
expect(criteria.size).to eq(0)
|
|
470
470
|
end
|
|
471
471
|
end
|
|
472
|
+
|
|
473
|
+
context 'when none is called on a criteria with a cached context' do
|
|
474
|
+
let(:criteria) do
|
|
475
|
+
Band.where(name: 'Depeche Mode')
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
it 'does not use the cached context' do
|
|
479
|
+
expect(criteria.count).to eq(1)
|
|
480
|
+
|
|
481
|
+
criteria.none
|
|
482
|
+
|
|
483
|
+
expect(criteria.count).to eq(0)
|
|
484
|
+
end
|
|
485
|
+
end
|
|
472
486
|
end
|
|
473
487
|
|
|
474
488
|
describe ".pluck" do
|