Skip to content

Commit

Permalink
make sure we don't leak internal state via as_document (#5899)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis authored Nov 18, 2024
1 parent f3dbfa7 commit c2f078f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ def to_key
#
# @return [ Hash ] A hash of all attributes in the hierarchy.
def as_document
BSON::Document.new(as_attributes)
attrs = as_attributes

# legacy attributes have a tendency to leak internal state via
# `as_document`; we have to deep_dup the attributes here to prevent
# that.
attrs = attrs.deep_dup if Mongoid.legacy_attributes

BSON::Document.new(attrs)
end

# Calls #as_json on the document with additional, Mongoid-specific options.
Expand Down
27 changes: 27 additions & 0 deletions spec/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,33 @@ class << self; attr_accessor :name; end
expect(person.as_document["addresses"].first).to have_key(:locations)
end

context 'when modifying the returned object' do
let(:record) do
RootCategory.create(categories: [{ name: 'tests' }]).reload
end

shared_examples_for 'an object with protected internal state' do
it 'does not expose internal state' do
before_change = record.as_document.dup
record.categories.first.name = 'things'
after_change = record.as_document
expect(before_change['categories'].first['name']).not_to eq('things')
end
end

context 'when legacy_attributes is true' do
config_override :legacy_attributes, true

it_behaves_like 'an object with protected internal state'
end

context 'when legacy_attributes is false' do
config_override :legacy_attributes, false

it_behaves_like 'an object with protected internal state'
end
end

context "with relation define store_as option in embeded_many" do

let!(:phone) do
Expand Down

0 comments on commit c2f078f

Please sign in to comment.