blind_index 1.0.2 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f39918c983140363af00fe1afeda800a7476a5727fd2c676c713b92222c1f35
4
- data.tar.gz: b83995b7b33eddacb2988e7fdb8816a1b2fc644161114b5e191f3d9bb2de2bd2
3
+ metadata.gz: cca7398adad9e03afd11dd268f4e69d99abe9ef767d648525ceeaee02aa37ba2
4
+ data.tar.gz: 9ebc3c610a67aace050bffc6724cf7067f734bfe142ff2e0409cd73be66cbbb0
5
5
  SHA512:
6
- metadata.gz: b2d03f17acbac80b5ecf8ac1f97321f3c68b0c689db1e50d95cc81cb3769086a5d8da6f8f0386325555c8eb715d46555281e00af38804cd240cadbefa45e86a4
7
- data.tar.gz: 59092061d3b415d399f1a20232d680591ccc832af79387945d23d9309062bf10a4b8105062ea277af2fa06b454cd75f9ad3d49a44dabac1a990e2856bc700849
6
+ metadata.gz: d2bc3c733e12261e561cfb0e82a171c9c995079d20f032dba22f757b07dc23dddadbcc87a20e7c83f7a683f6c6904b2227235aee5e69523168d3df2cb50551ec
7
+ data.tar.gz: ccea3ba949b1643cbbe86f8a2c5fdbd8dc1837334d9db1055bd8cfc0b2f16ea00133029483ea876ddef1c44359f9dec8e1313cbfcd5f727c54f14e803b2110fe
data/CHANGELOG.md CHANGED
@@ -1,3 +1,55 @@
1
+ ## 2.5.0 (2024-06-03)
2
+
3
+ - Added support for Mongoid 9
4
+ - Dropped support for Ruby < 3.1
5
+
6
+ ## 2.4.0 (2023-07-02)
7
+
8
+ - Dropped support for Ruby < 3 and Rails < 6.1
9
+ - Dropped support for Mongoid < 7
10
+
11
+ ## 2.3.2 (2023-04-26)
12
+
13
+ - Added `key_table` and `key_attribute` options
14
+
15
+ ## 2.3.1 (2022-09-06)
16
+
17
+ - Fixed error with `backfill` when `bidx_attribute` is a symbol
18
+
19
+ ## 2.3.0 (2022-01-16)
20
+
21
+ - Added blind indexes to `filter_attributes`
22
+ - Dropped support for Ruby < 2.6 and Rails < 5.2
23
+
24
+ ## 2.2.0 (2020-09-07)
25
+
26
+ - Added support for `where` with table in Active Record 5.2+
27
+
28
+ ## 2.1.1 (2020-08-14)
29
+
30
+ - Fixed `version` option
31
+
32
+ ## 2.1.0 (2020-07-06)
33
+
34
+ - Improved performance of uniqueness validations
35
+ - Fixed deprecation warnings in Ruby 2.7 with Mongoid
36
+
37
+ ## 2.0.2 (2020-06-01)
38
+
39
+ - Improved error message for bad key length
40
+ - Fixed `backfill` method with relations for Mongoid
41
+
42
+ ## 2.0.1 (2020-02-14)
43
+
44
+ - Added `BlindIndex.backfill` method
45
+
46
+ ## 2.0.0 (2020-02-10)
47
+
48
+ - Blind indexes are updated immediately instead of in a `before_validation` callback
49
+ - Better Lockbox integration - no need to generate a separate key
50
+ - The `argon2` gem has been replaced with `argon2-kdf` for less dependencies and Windows support
51
+ - Removed deprecated `compute_email_bidx`
52
+
1
53
  ## 1.0.2 (2019-12-26)
2
54
 
3
55
  - Fixed `OpenSSL::KDF` error on some platforms
@@ -30,7 +82,7 @@ Breaking changes
30
82
 
31
83
  - Added `size` option
32
84
  - Added sanity checks for Argon2 cost parameters
33
- - Fixed ActiveRecord callback issues introduced in 0.3.3
85
+ - Fixed Active Record callback issues introduced in 0.3.3
34
86
 
35
87
  ## 0.3.3 (2018-11-12)
36
88
 
@@ -59,13 +111,13 @@ Breaking changes
59
111
 
60
112
  ## 0.2.0 (2018-05-11)
61
113
 
62
- - Added support for ActiveRecord 4.2
114
+ - Added support for Active Record 4.2
63
115
  - Improved validation support when multiple blind indexes
64
116
  - Fixed `nil` handling
65
117
 
66
118
  ## 0.1.1 (2018-04-09)
67
119
 
68
- - Added support for ActiveRecord 5.2
120
+ - Added support for Active Record 5.2
69
121
  - Added `callback` option
70
122
  - Added support for `key` proc
71
123
  - Fixed error inheritance
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2017-2019 Andrew Kane
1
+ Copyright (c) 2017-2023 Andrew Kane
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -6,11 +6,11 @@ Works with [Lockbox](https://github.com/ankane/lockbox) ([full example](https://
6
6
 
7
7
  Learn more about [securing sensitive data in Rails](https://ankane.org/sensitive-data-rails)
8
8
 
9
- [![Build Status](https://travis-ci.org/ankane/blind_index.svg?branch=master)](https://travis-ci.org/ankane/blind_index)
9
+ [![Build Status](https://github.com/ankane/blind_index/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/blind_index/actions)
10
10
 
11
11
  ## How It Works
12
12
 
13
- We use [this approach](https://paragonie.com/blog/2017/05/building-searchable-encrypted-databases-with-php-and-sql) by Scott Arciszewski. To summarize, we compute a keyed hash of the sensitive data and store it in a column. To query, we apply the keyed hash function to the value we’re searching and then perform a database search. This results in performant queries for exact matches. `LIKE` queries are not possible, but you can index expressions.
13
+ We use [this approach](https://paragonie.com/blog/2017/05/building-searchable-encrypted-databases-with-php-and-sql) by Scott Arciszewski. To summarize, we compute a keyed hash of the sensitive data and store it in a column. To query, we apply the keyed hash function to the value we’re searching and then perform a database search. This results in performant queries for exact matches. Efficient `LIKE` queries are [not possible](#like-ilike-and-full-text-searching), but you can index expressions.
14
14
 
15
15
  ## Leakage
16
16
 
@@ -23,41 +23,17 @@ Here’s a [great article](https://blog.cryptographyengineering.com/2019/02/11/a
23
23
  Add this line to your application’s Gemfile:
24
24
 
25
25
  ```ruby
26
- gem 'blind_index'
26
+ gem "blind_index"
27
27
  ```
28
28
 
29
- On Windows, also add:
29
+ ## Prep
30
30
 
31
- ```ruby
32
- gem 'argon2', git: 'https://github.com/technion/ruby-argon2.git', submodules: true
33
- ```
31
+ Your model should already be set up with Lockbox or attr_encrypted. The examples are for a `User` model with `has_encrypted :email` or `attr_encrypted :email`. See the full examples for [Lockbox](https://ankane.org/securing-user-emails-lockbox) and [attr_encrypted](https://ankane.org/securing-user-emails-in-rails) if needed.
34
32
 
35
- Until `argon2 > 2.0.2` is released.
33
+ Also, if you use attr_encrypted, [generate a key](#key-generation).
36
34
 
37
35
  ## Getting Started
38
36
 
39
- > Note: Your model should already be set up with Lockbox or attr_encrypted. The examples are for a `User` model with `encrypts :email` or `attr_encrypted :email`. See the full examples for [Lockbox](https://ankane.org/securing-user-emails-lockbox) and [attr_encrypted](https://ankane.org/securing-user-emails-in-rails) if needed.
40
-
41
- First, generate a key
42
-
43
- ```ruby
44
- BlindIndex.generate_key
45
- ```
46
-
47
- Store the key with your other secrets. This is typically Rails credentials or an environment variable ([dotenv](https://github.com/bkeepers/dotenv) is great for this). Be sure to use different keys in development and production. Keys don’t need to be hex-encoded, but it’s often easier to store them this way.
48
-
49
- Set the following environment variable with your key (you can use this one in development)
50
-
51
- ```sh
52
- BLIND_INDEX_MASTER_KEY=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
53
- ```
54
-
55
- or create `config/initializers/blind_index.rb` with something like
56
-
57
- ```ruby
58
- BlindIndex.master_key = Rails.application.credentials.blind_index_master_key
59
- ```
60
-
61
37
  Create a migration to add a column for the blind index
62
38
 
63
39
  ```ruby
@@ -84,10 +60,7 @@ end
84
60
  Backfill existing records
85
61
 
86
62
  ```ruby
87
- User.unscoped.where(email_bidx: nil).find_each do |user|
88
- user.compute_email_bidx
89
- user.save(validate: false)
90
- end
63
+ BlindIndex.backfill(User)
91
64
  ```
92
65
 
93
66
  And query away
@@ -96,9 +69,19 @@ And query away
96
69
  User.where(email: "test@example.org")
97
70
  ```
98
71
 
72
+ ## Expressions
73
+
74
+ You can apply expressions to attributes before indexing and searching. This gives you the the ability to perform case-insensitive searches and more.
75
+
76
+ ```ruby
77
+ class User < ApplicationRecord
78
+ blind_index :email, expression: ->(v) { v.downcase }
79
+ end
80
+ ```
81
+
99
82
  ## Validations
100
83
 
101
- To prevent duplicates, use:
84
+ You can use blind indexes for uniqueness validations.
102
85
 
103
86
  ```ruby
104
87
  class User < ApplicationRecord
@@ -106,15 +89,27 @@ class User < ApplicationRecord
106
89
  end
107
90
  ```
108
91
 
109
- We also recommend adding a unique index to the blind index column through a database migration.
92
+ We recommend adding a unique index to the blind index column through a database migration.
110
93
 
111
- ## Expressions
94
+ ```ruby
95
+ add_index :users, :email_bidx, unique: true
96
+ ```
112
97
 
113
- You can apply expressions to attributes before indexing and searching. This gives you the the ability to perform case-insensitive searches and more.
98
+ For `allow_blank: true`, use:
99
+
100
+ ```ruby
101
+ class User < ApplicationRecord
102
+ blind_index :email, expression: ->(v) { v.presence }
103
+ validates :email, uniqueness: {allow_blank: true}
104
+ end
105
+ ```
106
+
107
+ For `case_sensitive: false`, use:
114
108
 
115
109
  ```ruby
116
110
  class User < ApplicationRecord
117
111
  blind_index :email, expression: ->(v) { v.downcase }
112
+ validates :email, uniqueness: true # for best performance, leave out {case_sensitive: false}
118
113
  end
119
114
  ```
120
115
 
@@ -139,10 +134,7 @@ end
139
134
  Backfill existing records
140
135
 
141
136
  ```ruby
142
- User.unscoped.where(email_ci_bidx: nil).find_each do |user|
143
- user.compute_email_ci_bidx
144
- user.save(validate: false)
145
- end
137
+ BlindIndex.backfill(User, columns: [:email_ci_bidx])
146
138
  ```
147
139
 
148
140
  And query away
@@ -169,18 +161,34 @@ You can also use virtual attributes to index data from multiple columns:
169
161
  ```ruby
170
162
  class User < ApplicationRecord
171
163
  attribute :initials, :string
164
+ blind_index :initials
172
165
 
173
- # must come before the blind_index method so it runs first
174
166
  before_validation :set_initials, if: -> { changes.key?(:first_name) || changes.key?(:last_name) }
175
167
 
176
- blind_index :initials
177
-
178
168
  def set_initials
179
169
  self.initials = "#{first_name[0]}#{last_name[0]}"
180
170
  end
181
171
  end
182
172
  ```
183
173
 
174
+ ## Migrating Data
175
+
176
+ If you’re encrypting a column and adding a blind index at the same time, use the `migrating` option.
177
+
178
+ ```ruby
179
+ class User < ApplicationRecord
180
+ blind_index :email, migrating: true
181
+ end
182
+ ```
183
+
184
+ This allows you to backfill records while still querying the unencrypted field.
185
+
186
+ ```ruby
187
+ BlindIndex.backfill(User)
188
+ ```
189
+
190
+ Once that completes, you can remove the `migrating` option.
191
+
184
192
  ## Key Rotation
185
193
 
186
194
  To rotate keys without downtime, add a new column:
@@ -201,10 +209,7 @@ end
201
209
  This will keep the new column synced going forward. Next, backfill the data:
202
210
 
203
211
  ```ruby
204
- User.unscoped.where(email_bidx_v2: nil).find_each do |user|
205
- user.compute_rotated_email_bidx
206
- user.save(validate: false)
207
- end
212
+ BlindIndex.backfill(User, columns: [:email_bidx_v2])
208
213
  ```
209
214
 
210
215
  Then update your model
@@ -219,17 +224,27 @@ Finally, drop the old column.
219
224
 
220
225
  ## Key Separation
221
226
 
222
- The master key is used to generate unique keys for each blind index. This technique comes from [CipherSweet](https://ciphersweet.paragonie.com/internals/key-hierarchy). The table name and blind index column name are both used in this process. If you need to rename a table with blind indexes, or a blind index column itself, get the key:
227
+ The master key is used to generate unique keys for each blind index. This technique comes from [CipherSweet](https://ciphersweet.paragonie.com/internals/key-hierarchy). The table name and blind index column name are both used in this process.
228
+
229
+ You can get an individual key with:
223
230
 
224
231
  ```ruby
225
232
  BlindIndex.index_key(table: "users", bidx_attribute: "email_bidx")
226
233
  ```
227
234
 
228
- And set it directly before renaming:
235
+ To rename a table with blind indexes, use:
229
236
 
230
237
  ```ruby
231
238
  class User < ApplicationRecord
232
- blind_index :email, key: ENV["USER_EMAIL_BLIND_INDEX_KEY"]
239
+ blind_index :email, key_table: "original_table"
240
+ end
241
+ ```
242
+
243
+ To rename a blind index column, use:
244
+
245
+ ```ruby
246
+ class User < ApplicationRecord
247
+ blind_index :email, key_attribute: "original_column"
233
248
  end
234
249
  ```
235
250
 
@@ -257,148 +272,128 @@ For Mongoid, use:
257
272
  ```ruby
258
273
  class User
259
274
  field :email_bidx, type: String
275
+ index({email_bidx: 1})
260
276
  end
261
277
  ```
262
278
 
263
- ## Reference
279
+ ## Key Generation
264
280
 
265
- Set default options in an initializer with:
266
-
267
- ```ruby
268
- BlindIndex.default_options = {algorithm: :pbkdf2_sha256}
269
- ```
281
+ This is optional for Lockbox, as its master key is used by default.
270
282
 
271
- By default, blind indexes are encoded in Base64. Set a different encoding with:
283
+ Generate a key with:
272
284
 
273
285
  ```ruby
274
- class User < ApplicationRecord
275
- blind_index :email, encode: ->(v) { [v].pack("H*") }
276
- end
286
+ BlindIndex.generate_key
277
287
  ```
278
288
 
279
- By default, blind indexes are 32 bytes. Set a smaller size with:
289
+ Store the key with your other secrets. This is typically Rails credentials or an environment variable ([dotenv](https://github.com/bkeepers/dotenv) is great for this). Be sure to use different keys in development and production. Keys don’t need to be hex-encoded, but it’s often easier to store them this way.
280
290
 
281
- ```ruby
282
- class User < ApplicationRecord
283
- blind_index :email, size: 16
284
- end
291
+ Set the following environment variable with your key (you can use this one in development)
292
+
293
+ ```sh
294
+ BLIND_INDEX_MASTER_KEY=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
285
295
  ```
286
296
 
287
- Set a key directly for an index with:
297
+ or create `config/initializers/blind_index.rb` with something like
288
298
 
289
299
  ```ruby
290
- class User < ApplicationRecord
291
- blind_index :email, key: ENV["USER_EMAIL_BLIND_INDEX_KEY"]
292
- end
300
+ BlindIndex.master_key = Rails.application.credentials.blind_index_master_key
293
301
  ```
294
302
 
295
- ## Alternatives
296
-
297
- One alternative to blind indexing is to use a deterministic encryption scheme, like [AES-SIV](https://github.com/miscreant/miscreant). In this approach, the encrypted data will be the same for matches. We recommend blind indexing over deterministic encryption because:
298
-
299
- 1. You can keep encryption consistent for all fields (both searchable and non-searchable)
300
- 2. Blind indexing supports expressions
301
-
302
- ## Upgrading
303
+ ## LIKE, ILIKE, and Full-Text Searching
303
304
 
304
- ### 1.0.0
305
+ Unfortunately, blind indexes can’t be used for `LIKE`, `ILIKE`, or full-text searching. Instead, records must be loaded, decrypted, and searched in memory.
305
306
 
306
- 1.0.0 brings a number of improvements. Here are a few to be aware of:
307
+ For `LIKE`, use:
307
308
 
308
- - Argon2id is the default algorithm for stronger security
309
- - You can use a master key instead of individual keys for each column
310
- - Columns no longer have an `encrypted_` prefix
309
+ ```ruby
310
+ User.select { |u| u.email.include?("value") }
311
+ ```
311
312
 
312
- For existing fields, add:
313
+ For `ILIKE`, use:
313
314
 
314
315
  ```ruby
315
- class User < ApplicationRecord
316
- blind_index :email, legacy: true
317
- end
316
+ User.select { |u| u.email =~ /value/i }
318
317
  ```
319
318
 
320
- #### Optional
321
-
322
- To rotate to new fields that use Argon2id and a master key, generate a master key:
319
+ For full-text or fuzzy searching, use a gem like [FuzzyMatch](https://github.com/seamusabshere/fuzzy_match):
323
320
 
324
321
  ```ruby
325
- BlindIndex.generate_key
322
+ FuzzyMatch.new(User.all, read: :email).find("value")
326
323
  ```
327
324
 
328
- And set `ENV["BLIND_INDEX_MASTER_KEY"]` or `BlindIndex.master_key`.
325
+ If the number of records is large, try to find a way to narrow it down. An [expression index](#expressions) is one way to do this, but leaks which records have the same value of the expression, so use it carefully.
326
+
327
+ ## Reference
329
328
 
330
- Add a new column without the `encrypted_` prefix:
329
+ Set default options in an initializer with:
331
330
 
332
331
  ```ruby
333
- add_column :users, :email_bidx, :string
334
- add_index :users, :email_bidx # unique: true if needed
332
+ BlindIndex.default_options = {algorithm: :pbkdf2_sha256}
335
333
  ```
336
334
 
337
- And add to your model
335
+ By default, blind indexes are encoded in Base64. Set a different encoding with:
338
336
 
339
337
  ```ruby
340
338
  class User < ApplicationRecord
341
- blind_index :email, key: ENV["USER_EMAIL_BLIND_INDEX_KEY"], legacy: true, rotate: {}
339
+ blind_index :email, encode: ->(v) { [v].pack("H*") }
342
340
  end
343
341
  ```
344
342
 
345
- > For more sensitive fields, use `rotate: {slow: true}`
346
-
347
- This will keep the new column synced going forward. Next, backfill the data:
343
+ By default, blind indexes are 32 bytes. Set a smaller size with:
348
344
 
349
345
  ```ruby
350
- User.unscoped.where(email_bidx: nil).find_each do |user|
351
- user.compute_rotated_email_bidx
352
- user.save(validate: false)
346
+ class User < ApplicationRecord
347
+ blind_index :email, size: 16
353
348
  end
354
349
  ```
355
350
 
356
- Then update your model
351
+ Set a key directly for an index with:
357
352
 
358
353
  ```ruby
359
354
  class User < ApplicationRecord
360
- blind_index :email
355
+ blind_index :email, key: ENV["USER_EMAIL_BLIND_INDEX_KEY"]
361
356
  end
362
357
  ```
363
358
 
364
- > For more sensitive fields, add `slow: true`
365
-
366
- Finally, drop the old column.
359
+ ## Compatibility
367
360
 
368
- ### 0.3.0
361
+ You can generate blind indexes from other languages as well. For Python, you can use [argon2-cffi](https://github.com/hynek/argon2-cffi).
369
362
 
370
- This version introduces a breaking change to enforce secure key generation. An error is thrown if your blind index key isn’t both binary and 32 bytes.
363
+ ```python
364
+ from argon2.low_level import Type, hash_secret_raw
365
+ from base64 import b64encode
371
366
 
372
- We recommend rotating your key if it doesn’t meet this criteria. You can generate a new key in the Rails console with:
367
+ key = '289737bab72fa97b1f4b081cef00d7b7d75034bcf3183c363feaf3e6441777bc'
368
+ value = 'test@example.org'
373
369
 
374
- ```ruby
375
- SecureRandom.hex(32)
370
+ bidx = b64encode(hash_secret_raw(
371
+ secret=value.encode(),
372
+ salt=bytes.fromhex(key),
373
+ time_cost=3,
374
+ memory_cost=2**12,
375
+ parallelism=1,
376
+ hash_len=32,
377
+ type=Type.ID
378
+ ))
376
379
  ```
377
380
 
378
- Update your model to convert the hex key to binary.
381
+ ## Alternatives
379
382
 
380
- ```ruby
381
- class User < ApplicationRecord
382
- blind_index :email, key: [ENV["USER_EMAIL_BLIND_INDEX_KEY"]].pack("H*")
383
- end
384
- ```
383
+ One alternative to blind indexing is to use a deterministic encryption scheme, like [AES-SIV](https://github.com/miscreant/miscreant). In this approach, the encrypted data will be the same for matches. We recommend blind indexing over deterministic encryption because:
385
384
 
386
- And recompute the blind index.
385
+ 1. You can keep encryption consistent for all fields (both searchable and non-searchable)
386
+ 2. Blind indexing supports expressions
387
387
 
388
- ```ruby
389
- User.unscoped.find_each do |user|
390
- user.compute_email_bidx
391
- user.save(validate: false)
392
- end
393
- ```
388
+ ## Upgrading
394
389
 
395
- To continue without rotating, set:
390
+ ### 2.0.0
396
391
 
397
- ```ruby
398
- class User < ApplicationRecord
399
- blind_index :email, insecure_key: true
400
- end
401
- ```
392
+ 2.0.0 brings a number of improvements.
393
+
394
+ - Blind indexes are updated immediately instead of in a `before_validation` callback
395
+ - Better Lockbox integration - no need to generate a separate key
396
+ - There’s a new gem for Argon2 that has no dependencies and (officially) supports Windows
402
397
 
403
398
  ## History
404
399
 
@@ -421,3 +416,5 @@ cd blind_index
421
416
  bundle install
422
417
  bundle exec rake test
423
418
  ```
419
+
420
+ For security issues, send an email to the address on [this page](https://github.com/ankane).
@@ -0,0 +1,117 @@
1
+ module BlindIndex
2
+ class Backfill
3
+ attr_reader :blind_indexes
4
+
5
+ def initialize(relation, batch_size:, columns:)
6
+ @relation = relation
7
+ @transaction = @relation.respond_to?(:transaction) && !mongoid_relation?(relation.all)
8
+ @batch_size = batch_size
9
+ @blind_indexes = @relation.blind_indexes
10
+ filter_columns!(columns) if columns
11
+ end
12
+
13
+ def perform
14
+ each_batch do |records|
15
+ backfill_records(records)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # modify in-place
22
+ def filter_columns!(columns)
23
+ columns = Array(columns).map(&:to_s)
24
+ blind_indexes.select! { |_, v| columns.include?(v[:bidx_attribute].to_s) }
25
+ bad_columns = columns - blind_indexes.map { |_, v| v[:bidx_attribute].to_s }
26
+ raise ArgumentError, "Bad column: #{bad_columns.first}" if bad_columns.any?
27
+ end
28
+
29
+ def build_relation
30
+ # build relation
31
+ relation = @relation
32
+
33
+ if defined?(ActiveRecord::Base) && relation.is_a?(ActiveRecord::Base)
34
+ relation = relation.unscoped
35
+ end
36
+
37
+ # convert from possible class to ActiveRecord::Relation or Mongoid::Criteria
38
+ relation = relation.all
39
+
40
+ attributes = blind_indexes.map { |_, v| v[:bidx_attribute] }
41
+
42
+ if defined?(ActiveRecord::Relation) && relation.is_a?(ActiveRecord::Relation)
43
+ base_relation = relation.unscoped
44
+ or_relation = relation.unscoped
45
+
46
+ attributes.each_with_index do |attribute, i|
47
+ or_relation =
48
+ if i == 0
49
+ base_relation.where(attribute => nil)
50
+ else
51
+ or_relation.or(base_relation.where(attribute => nil))
52
+ end
53
+ end
54
+
55
+ relation.merge(or_relation)
56
+ else
57
+ relation.merge(relation.unscoped.or(attributes.map { |a| {a => nil} }))
58
+ end
59
+ end
60
+
61
+ def each_batch
62
+ relation = build_relation
63
+
64
+ if relation.respond_to?(:find_in_batches)
65
+ relation.find_in_batches(batch_size: @batch_size) do |records|
66
+ yield records
67
+ end
68
+ else
69
+ # https://github.com/karmi/tire/blob/master/lib/tire/model/import.rb
70
+ # use cursor for Mongoid
71
+ records = []
72
+ relation.all.each do |record|
73
+ records << record
74
+ if records.length == @batch_size
75
+ yield records
76
+ records = []
77
+ end
78
+ end
79
+ yield records if records.any?
80
+ end
81
+ end
82
+
83
+ def backfill_records(records)
84
+ # do expensive blind index computation outside of transaction
85
+ records.each do |record|
86
+ blind_indexes.each do |k, v|
87
+ record.send("compute_#{k}_bidx") if !record.send(v[:bidx_attribute])
88
+ end
89
+ end
90
+
91
+ # don't need to save records that went from nil => nil
92
+ records.select! { |r| r.changed? }
93
+
94
+ if records.any?
95
+ with_transaction do
96
+ records.each do |record|
97
+ record.save!(validate: false)
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ def mongoid_relation?(relation)
104
+ defined?(Mongoid::Criteria) && relation.is_a?(Mongoid::Criteria)
105
+ end
106
+
107
+ def with_transaction
108
+ if @transaction
109
+ @relation.transaction do
110
+ yield
111
+ end
112
+ else
113
+ yield
114
+ end
115
+ end
116
+ end
117
+ end
@@ -1,25 +1,6 @@
1
1
  module BlindIndex
2
2
  module Extensions
3
- # ActiveRecord 5.0+
4
3
  module TableMetadata
5
- def resolve_column_aliases(hash)
6
- new_hash = super
7
- if has_blind_indexes?
8
- hash.each do |key, _|
9
- if key.respond_to?(:to_sym) && (bi = klass.blind_indexes[key.to_sym]) && !new_hash[key].is_a?(ActiveRecord::StatementCache::Substitute)
10
- value = new_hash.delete(key)
11
- new_hash[bi[:bidx_attribute]] =
12
- if value.is_a?(Array)
13
- value.map { |v| BlindIndex.generate_bidx(v, **bi) }
14
- else
15
- BlindIndex.generate_bidx(value, **bi)
16
- end
17
- end
18
- end
19
- end
20
- new_hash
21
- end
22
-
23
4
  # memoize for performance
24
5
  def has_blind_indexes?
25
6
  unless defined?(@has_blind_indexes)
@@ -29,54 +10,38 @@ module BlindIndex
29
10
  end
30
11
  end
31
12
 
32
- # ActiveRecord 4.2
33
13
  module PredicateBuilder
34
- def resolve_column_aliases(klass, hash)
35
- new_hash = super
36
- if has_blind_indexes?(klass)
37
- hash.each do |key, _|
38
- if key.respond_to?(:to_sym) && (bi = klass.blind_indexes[key.to_sym]) && !new_hash[key].is_a?(ActiveRecord::StatementCache::Substitute)
39
- value = new_hash.delete(key)
40
- new_hash[bi[:bidx_attribute]] =
41
- if value.is_a?(Array)
42
- value.map { |v| BlindIndex.generate_bidx(v, bi) }
43
- else
44
- BlindIndex.generate_bidx(value, bi)
45
- end
14
+ # https://github.com/rails/rails/commit/56f30962b84fc53b76001301fb830c1594fd377e
15
+ def build(attribute, value, *args)
16
+ if table.has_blind_indexes? && (bi = table.send(:klass).blind_indexes[attribute.name.to_sym]) && !value.is_a?(ActiveRecord::StatementCache::Substitute)
17
+ attribute = attribute.relation[bi[:bidx_attribute]]
18
+ value =
19
+ if value.is_a?(Array)
20
+ value.map { |v| BlindIndex.generate_bidx(v, **bi) }
21
+ else
22
+ BlindIndex.generate_bidx(value, **bi)
46
23
  end
47
- end
48
24
  end
49
- new_hash
50
- end
51
25
 
52
- @@blind_index_cache = {}
53
-
54
- # memoize for performance
55
- def has_blind_indexes?(klass)
56
- if @@blind_index_cache[klass].nil?
57
- @@blind_index_cache[klass] = klass.respond_to?(:blind_indexes)
58
- end
59
- @@blind_index_cache[klass]
26
+ super(attribute, value, *args)
60
27
  end
61
28
  end
62
29
 
63
30
  module UniquenessValidator
64
- if ActiveRecord::VERSION::STRING >= "5.2"
65
- def build_relation(klass, attribute, value)
66
- if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
67
- value = BlindIndex.generate_bidx(value, **bi)
68
- attribute = bi[:bidx_attribute]
69
- end
70
- super(klass, attribute, value)
31
+ def validate_each(record, attribute, value)
32
+ klass = record.class
33
+ if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
34
+ value = record.read_attribute_for_validation(bi[:bidx_attribute])
71
35
  end
72
- else
73
- def build_relation(klass, table, attribute, value)
74
- if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
75
- value = BlindIndex.generate_bidx(value, **bi)
76
- attribute = bi[:bidx_attribute]
77
- end
78
- super(klass, table, attribute, value)
36
+ super(record, attribute, value)
37
+ end
38
+
39
+ # change attribute name here instead of validate_each for better error message
40
+ def build_relation(klass, attribute, value)
41
+ if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
42
+ attribute = bi[:bidx_attribute]
79
43
  end
44
+ super(klass, attribute, value)
80
45
  end
81
46
  end
82
47
 
@@ -11,7 +11,7 @@ module BlindIndex
11
11
  raise ArgumentError, "Missing field for key generation" if bidx_attribute.to_s.empty?
12
12
 
13
13
  c = "\x7E"*32
14
- root_key = hkdf(BlindIndex.decode_key(@master_key), salt: table.to_s, info: "#{c}#{bidx_attribute}", length: 32, hash: "sha384")
14
+ root_key = hkdf(BlindIndex.decode_key(@master_key, name: "Master key"), salt: table.to_s, info: "#{c}#{bidx_attribute}", length: 32, hash: "sha384")
15
15
  hash_hmac("sha256", pack([table, bidx_attribute, bidx_attribute]), root_key)
16
16
  end
17
17
 
@@ -10,7 +10,7 @@ module BlindIndex
10
10
  # check here so we validate rotate options as well
11
11
  unknown_keywords = options.keys - [:algorithm, :attribute, :bidx_attribute,
12
12
  :callback, :cost, :encode, :expression, :insecure_key, :iterations, :key,
13
- :legacy, :master_key, :size, :slow]
13
+ :key_attribute, :key_table, :legacy, :master_key, :size, :slow, :version]
14
14
  raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any?
15
15
 
16
16
  attribute = options[:attribute] || name
@@ -33,9 +33,17 @@ module BlindIndex
33
33
  class_method_name = :"generate_#{name}_bidx"
34
34
 
35
35
  key = options[:key]
36
- key ||= -> { BlindIndex.index_key(table: try(:table_name) || collection_name.to_s, bidx_attribute: bidx_attribute, master_key: options[:master_key], encode: false) }
36
+ key ||= -> { BlindIndex.index_key(table: options[:key_table] || try(:table_name) || collection_name.to_s, bidx_attribute: options[:key_attribute] || bidx_attribute, master_key: options[:master_key], encode: false) }
37
37
 
38
38
  class_eval do
39
+ activerecord = defined?(ActiveRecord) && self < ActiveRecord::Base
40
+
41
+ if activerecord && ActiveRecord::VERSION::MAJOR >= 6
42
+ # blind index value isn't really sensitive
43
+ # but don't need to show it in the Rails console
44
+ self.filter_attributes += [/\A#{Regexp.escape(bidx_attribute)}\z/]
45
+ end
46
+
39
47
  @blind_indexes ||= {}
40
48
 
41
49
  unless respond_to?(:blind_indexes)
@@ -64,25 +72,28 @@ module BlindIndex
64
72
  BlindIndex.generate_bidx(value, **blind_indexes[name])
65
73
  end
66
74
 
67
- define_singleton_method method_name do |value|
68
- ActiveSupport::Deprecation.warn("Use #{class_method_name} instead")
69
- send(class_method_name, value)
70
- end
71
-
72
75
  define_method method_name do
73
- self.send("#{bidx_attribute}=", self.class.send(class_method_name, send(attribute)))
76
+ send("#{bidx_attribute}=", self.class.send(class_method_name, send(attribute)))
74
77
  end
75
78
 
76
79
  if callback
77
- if defined?(ActiveRecord) && self < ActiveRecord::Base
78
- # Active Record
79
- # prevent deprecation warnings
80
- before_validation method_name, if: -> { changes.key?(attribute.to_s) }
81
- else
82
- # Mongoid
83
- # Lockbox only supports attribute_changed?
84
- before_validation method_name, if: -> { send("#{attribute}_changed?") }
80
+ # TODO reuse module
81
+ m = Module.new do
82
+ define_method "#{attribute}=" do |value|
83
+ result = super(value)
84
+ send(method_name)
85
+ result
86
+ end
87
+
88
+ unless activerecord
89
+ define_method "reset_#{attribute}!" do
90
+ result = super()
91
+ send(method_name)
92
+ result
93
+ end
94
+ end
85
95
  end
96
+ prepend m
86
97
  end
87
98
 
88
99
  # use include so user can override
@@ -26,9 +26,9 @@ module BlindIndex
26
26
 
27
27
  criterion[bidx_key] =
28
28
  if value.is_a?(Array)
29
- value.map { |v| BlindIndex.generate_bidx(v, bi) }
29
+ value.map { |v| BlindIndex.generate_bidx(v, **bi) }
30
30
  else
31
- BlindIndex.generate_bidx(value, bi)
31
+ BlindIndex.generate_bidx(value, **bi)
32
32
  end
33
33
  end
34
34
  end
@@ -39,9 +39,18 @@ module BlindIndex
39
39
  end
40
40
 
41
41
  module UniquenessValidator
42
+ def validate_each(record, attribute, value)
43
+ klass = record.class
44
+ if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
45
+ value = record.read_attribute_for_validation(bi[:bidx_attribute])
46
+ end
47
+ super(record, attribute, value)
48
+ end
49
+
50
+ # change attribute name here instead of validate_each for better error message
42
51
  def create_criteria(base, document, attribute, value)
43
- if base.respond_to?(:blind_indexes) && (bi = base.blind_indexes[attribute])
44
- value = BlindIndex.generate_bidx(value, bi)
52
+ klass = document.class
53
+ if klass.respond_to?(:blind_indexes) && (bi = klass.blind_indexes[attribute])
45
54
  attribute = bi[:bidx_attribute]
46
55
  end
47
56
  super(base, document, attribute, value)
@@ -1,3 +1,3 @@
1
1
  module BlindIndex
2
- VERSION = "1.0.2"
2
+ VERSION = "2.5.0"
3
3
  end
data/lib/blind_index.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  # dependencies
2
2
  require "active_support"
3
+ require "argon2/kdf"
4
+
5
+ # stdlib
3
6
  require "openssl"
4
- require "argon2"
5
7
 
6
8
  # modules
7
- require "blind_index/key_generator"
8
- require "blind_index/model"
9
- require "blind_index/version"
9
+ require_relative "blind_index/backfill"
10
+ require_relative "blind_index/key_generator"
11
+ require_relative "blind_index/model"
12
+ require_relative "blind_index/version"
10
13
 
11
14
  module BlindIndex
12
15
  class Error < StandardError; end
@@ -18,7 +21,7 @@ module BlindIndex
18
21
  self.default_options = {}
19
22
 
20
23
  def self.master_key
21
- @master_key ||= ENV["BLIND_INDEX_MASTER_KEY"]
24
+ @master_key ||= ENV["BLIND_INDEX_MASTER_KEY"] || (defined?(Lockbox.master_key) && Lockbox.master_key)
22
25
  end
23
26
 
24
27
  def self.generate_bidx(value, key:, **options)
@@ -64,7 +67,7 @@ module BlindIndex
64
67
  # use same bounds as rbnacl
65
68
  raise BlindIndex::Error, "m must be between 3 and 22" if m < 3 || m > 22
66
69
 
67
- [Argon2::Engine.hash_argon2id(value, key, t, m, size)].pack("H*")
70
+ Argon2::KDF.argon2id(value, salt: key, t: t, m: m, p: 1, length: size)
68
71
  when :pbkdf2_sha256
69
72
  iterations = cost_options[:iterations] || options[:iterations] || (options[:slow] ? 100000 : 10000)
70
73
  OpenSSL::PKCS5.pbkdf2_hmac(value, key, iterations, size, "sha256")
@@ -78,7 +81,7 @@ module BlindIndex
78
81
  # use same bounds as rbnacl
79
82
  raise BlindIndex::Error, "m must be between 3 and 22" if m < 3 || m > 22
80
83
 
81
- [Argon2::Engine.hash_argon2i(value, key, t, m, size)].pack("H*")
84
+ Argon2::KDF.argon2i(value, salt: key, t: t, m: m, p: 1, length: size)
82
85
  when :scrypt
83
86
  n = cost_options[:n] || 4096
84
87
  r = cost_options[:r] || 8
@@ -116,42 +119,36 @@ module BlindIndex
116
119
  key
117
120
  end
118
121
 
119
- def self.decode_key(key)
122
+ def self.decode_key(key, name: "Key")
120
123
  # decode hex key
121
124
  if key.encoding != Encoding::BINARY && key =~ /\A[0-9a-f]{64}\z/i
122
125
  key = [key].pack("H*")
123
126
  end
124
127
 
125
- raise BlindIndex::Error, "Key must use binary encoding" if key.encoding != Encoding::BINARY
126
- raise BlindIndex::Error, "Key must be 32 bytes" if key.bytesize != 32
128
+ raise BlindIndex::Error, "#{name} must be 32 bytes (64 hex digits)" if key.bytesize != 32
129
+ raise BlindIndex::Error, "#{name} must use binary encoding" if key.encoding != Encoding::BINARY
127
130
 
128
131
  key
129
132
  end
133
+
134
+ def self.backfill(relation, columns: nil, batch_size: 1000)
135
+ Backfill.new(relation, columns: columns, batch_size: batch_size).perform
136
+ end
130
137
  end
131
138
 
132
139
  ActiveSupport.on_load(:active_record) do
133
- require "blind_index/extensions"
140
+ require_relative "blind_index/extensions"
134
141
  extend BlindIndex::Model
135
142
 
136
- if defined?(ActiveRecord::TableMetadata)
137
- ActiveRecord::TableMetadata.prepend(BlindIndex::Extensions::TableMetadata)
138
- else
139
- ActiveRecord::PredicateBuilder.singleton_class.prepend(BlindIndex::Extensions::PredicateBuilder)
140
- end
141
-
143
+ ActiveRecord::TableMetadata.prepend(BlindIndex::Extensions::TableMetadata)
142
144
  ActiveRecord::DynamicMatchers::Method.prepend(BlindIndex::Extensions::DynamicMatchers)
143
-
144
- unless ActiveRecord::VERSION::STRING.start_with?("5.1.")
145
- ActiveRecord::Validations::UniquenessValidator.prepend(BlindIndex::Extensions::UniquenessValidator)
146
- end
145
+ ActiveRecord::Validations::UniquenessValidator.prepend(BlindIndex::Extensions::UniquenessValidator)
146
+ ActiveRecord::PredicateBuilder.prepend(BlindIndex::Extensions::PredicateBuilder)
147
147
  end
148
148
 
149
- if defined?(Mongoid)
150
- # TODO find better ActiveModel hook
151
- require "active_model/callbacks"
152
- ActiveModel::Callbacks.include(BlindIndex::Model)
153
-
154
- require "blind_index/mongoid"
149
+ ActiveSupport.on_load(:mongoid) do
150
+ require_relative "blind_index/mongoid"
151
+ Mongoid::Document::ClassMethods.include(BlindIndex::Model)
155
152
  Mongoid::Criteria.prepend(BlindIndex::Mongoid::Criteria)
156
153
  Mongoid::Validatable::UniquenessValidator.prepend(BlindIndex::Mongoid::UniquenessValidator)
157
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blind_index
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2024-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,156 +16,30 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
19
+ version: '6.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5'
26
+ version: '6.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: argon2
28
+ name: argon2-kdf
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '2'
33
+ version: 0.1.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '2'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: minitest
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: attr_encrypted
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: activerecord
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: sqlite3
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: scrypt
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: benchmark-ips
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: lockbox
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0.2'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0.2'
167
- description:
168
- email: andrew@chartkick.com
40
+ version: 0.1.1
41
+ description:
42
+ email: andrew@ankane.org
169
43
  executables: []
170
44
  extensions: []
171
45
  extra_rdoc_files: []
@@ -174,6 +48,7 @@ files:
174
48
  - LICENSE.txt
175
49
  - README.md
176
50
  - lib/blind_index.rb
51
+ - lib/blind_index/backfill.rb
177
52
  - lib/blind_index/extensions.rb
178
53
  - lib/blind_index/key_generator.rb
179
54
  - lib/blind_index/model.rb
@@ -183,7 +58,7 @@ homepage: https://github.com/ankane/blind_index
183
58
  licenses:
184
59
  - MIT
185
60
  metadata: {}
186
- post_install_message:
61
+ post_install_message:
187
62
  rdoc_options: []
188
63
  require_paths:
189
64
  - lib
@@ -191,15 +66,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
191
66
  requirements:
192
67
  - - ">="
193
68
  - !ruby/object:Gem::Version
194
- version: '2.4'
69
+ version: '3.1'
195
70
  required_rubygems_version: !ruby/object:Gem::Requirement
196
71
  requirements:
197
72
  - - ">="
198
73
  - !ruby/object:Gem::Version
199
74
  version: '0'
200
75
  requirements: []
201
- rubygems_version: 3.1.2
202
- signing_key:
76
+ rubygems_version: 3.5.9
77
+ signing_key:
203
78
  specification_version: 4
204
79
  summary: Securely search encrypted database fields
205
80
  test_files: []