Skip to content

Commit 6c2c84d

Browse files
authored
RUBY-3156 Error if RewrapManyDataKey is called with masterKey and without provider (#2720)
* RUBY 3156 enforce that masterKey requires provider * reference the correct variable * rubocop work is being done in a separate branch
1 parent bafb129 commit 6c2c84d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/mongo/crypt/explicit_encrypter.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ def remove_key_alt_name(id, key_alt_name)
257257
#
258258
# @return [ Crypt::RewrapManyDataKeyResult ] Result of the operation.
259259
def rewrap_many_data_key(filter, opts = {})
260+
validate_rewrap_options!(opts)
261+
260262
master_key_document = if opts[:provider]
261263
options = opts.dup
262264
provider = options.delete(:provider)
@@ -291,6 +293,18 @@ def rewrap_many_data_key(filter, opts = {})
291293
@encryption_io.update_data_keys(updates)
292294
)
293295
end
296+
297+
# Ensures the consistency of the options passed to #rewrap_many_data_keys.
298+
#
299+
# @param [Hash] opts the options hash to validate
300+
#
301+
# @raise [ ArgumentError ] if the options are not consistent or
302+
# compatible.
303+
def validate_rewrap_options!(opts)
304+
if opts.key?(:master_key) && !opts.key?(:provider)
305+
raise ArgumentError, 'If :master_key is specified, :provider must also be given'
306+
end
307+
end
294308
end
295309
end
296310
end

spec/integration/client_side_encryption/rewrap_prose_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@
101101
expect(client_encryption1.decrypt(ciphertext)).to eq('test')
102102
expect(client_encryption2.decrypt(ciphertext)).to eq('test')
103103
end
104+
105+
context 'when master_key is present without provider' do
106+
it 'raises an exception' do
107+
expect { client_encryption1.rewrap_many_data_key({}, master_key: {}) }
108+
.to raise_error(ArgumentError, /provider/)
109+
end
110+
end
104111
end
105112
end
106113
end

0 commit comments

Comments
 (0)