Skip to content

Commit 3085447

Browse files
authored
Update to use ensure block for closing PHI access (#73)
* Update to use ensure block for closing PHI access * Update build to encompass newer versions of ruby and rails * Restore version check for legacy_connection_handling * Drop deprecated ruby version support * Add spec for bug * Fix method call in new spec * Update both versions of get_phi, add specs for both * Remove useless changelog * Bump version
1 parent 7c074de commit 3085447

File tree

11 files changed

+59
-68
lines changed

11 files changed

+59
-68
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ jobs:
88
name: Ruby ${{ matrix.ruby }}
99
strategy:
1010
matrix:
11-
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
12-
ruby: ['2.7.2', '3.0', 3.1]
11+
ruby: [3.1, 3.2, 3.3]
1312
env:
1413
RUBY_VERSION: ${{ matrix.ruby }}
1514
steps:

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
/spec/dummy/tmp/
1515
/spec/dummy/db/schema.rb
16-
*.sqlite
17-
*.sqlite3
16+
*.sqlite*
17+
*.sqlite3*
1818
*.log
1919
.rspec_status
2020
gemfiles/*.gemfile.lock
21+
gemfiles/.bundle
2122

2223
# Macs. Ugh.
2324
.DS_Store

CHANGELOG.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ WORKDIR $APP_HOME
1414
COPY . $APP_HOME/
1515

1616
RUN gem update --system
17-
RUN bundle config set force_ruby_platform true
1817

1918
EXPOSE 3000
2019

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ To release a new version, update the version number in `version.rb`, and then ru
463463

464464
Bug reports and pull requests are welcome on GitHub at https://github.com/apsislabs/phi_attrs.
465465

466-
Any PRs should be accompanied with documentation in `README.md`, and changes documented in [`CHANGELOG.md`](https://keepachangelog.com/).
466+
Any PRs should be accompanied with documentation in `README.md`.
467467

468468
## License
469469

lib/phi_attrs/phi_record.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,32 @@ def get_phi(user_id = nil, reason = nil, allow_only: nil)
142142
# Save this so we don't revoke access previously extended outside the block
143143
frozen_instances = __instances_with_extended_phi.index_with { |obj| obj.instance_variable_get(:@__phi_relations_extended).clone }
144144

145-
if allow_only.nil?
146-
allow_phi!(user_id, reason)
147-
else
148-
allow_only.each { |t| t.allow_phi!(user_id, reason) }
149-
end
145+
begin
146+
if allow_only.nil?
147+
allow_phi!(user_id, reason)
148+
else
149+
allow_only.each { |t| t.allow_phi!(user_id, reason) }
150+
end
150151

151-
result = yield if block_given?
152+
return yield
153+
ensure
154+
__instances_with_extended_phi.each do |obj|
155+
if frozen_instances.include?(obj)
156+
old_extensions = frozen_instances[obj]
157+
new_extensions = obj.instance_variable_get(:@__phi_relations_extended) - old_extensions
158+
obj.send(:revoke_extended_phi!, new_extensions) if new_extensions.any?
159+
else
160+
obj.send(:revoke_extended_phi!) # Instance is new to the set, so revoke everything
161+
end
162+
end
152163

153-
__instances_with_extended_phi.each do |obj|
154-
if frozen_instances.include?(obj)
155-
old_extensions = frozen_instances[obj]
156-
new_extensions = obj.instance_variable_get(:@__phi_relations_extended) - old_extensions
157-
obj.send(:revoke_extended_phi!, new_extensions) if new_extensions.any?
164+
if allow_only.nil?
165+
disallow_last_phi!
158166
else
159-
obj.send(:revoke_extended_phi!) # Instance is new to the set, so revoke everything
167+
allow_only.each { |t| t.disallow_last_phi!(preserve_extensions: true) }
168+
# We've handled any newly extended allowances ourselves above
160169
end
161170
end
162-
163-
if allow_only.nil?
164-
disallow_last_phi!
165-
else
166-
allow_only.each { |t| t.disallow_last_phi!(preserve_extensions: true) }
167-
# We've handled any newly extended allowances ourselves above
168-
end
169-
170-
result
171171
end
172172

173173
# Explicitly disallow phi access in a specific area of code. This does not
@@ -385,15 +385,15 @@ def get_phi(user_id = nil, reason = nil)
385385
raise ArgumentError, 'block required' unless block_given?
386386

387387
extended_instances = @__phi_relations_extended.clone
388-
allow_phi!(user_id, reason)
389-
390-
result = yield if block_given?
391-
392-
new_extensions = @__phi_relations_extended - extended_instances
393-
disallow_last_phi!(preserve_extensions: true)
394-
revoke_extended_phi!(new_extensions) if new_extensions.any?
395-
396-
result
388+
begin
389+
allow_phi!(user_id, reason)
390+
391+
return yield
392+
ensure
393+
new_extensions = @__phi_relations_extended - extended_instances
394+
disallow_last_phi!(preserve_extensions: true)
395+
revoke_extended_phi!(new_extensions) if new_extensions.any?
396+
end
397397
end
398398

399399
# Revoke all PHI access for a single instance of this class.

lib/phi_attrs/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module PhiAttrs
4-
VERSION = '0.3.1'
4+
VERSION = '0.3.2'
55
end

spec/dummy/application.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class Application < Rails::Application
2525

2626
if Rails.version.match?(/^6.0/)
2727
config.active_record.sqlite3.represent_boolean_as_integer = true
28-
else
29-
config.active_record.legacy_connection_handling = false
3028
end
3129

3230
def require_environment!

spec/dummy/db/schema.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.0].define(version: 2017_02_14_100255) do
13+
ActiveRecord::Schema[7.2].define(version: 2017_02_14_100255) do
1414
create_table "addresses", force: :cascade do |t|
1515
t.integer "patient_info_id"
1616
t.string "address"
@@ -23,16 +23,6 @@
2323
t.index ["patient_info_id"], name: "index_health_records_on_patient_info_id"
2424
end
2525

26-
create_table "missing_attribute_model", force: :cascade do |t|
27-
t.datetime "created_at", precision: nil, null: false
28-
t.datetime "updated_at", precision: nil, null: false
29-
end
30-
31-
create_table "missing_extend_model", force: :cascade do |t|
32-
t.datetime "created_at", precision: nil, null: false
33-
t.datetime "updated_at", precision: nil, null: false
34-
end
35-
3626
create_table "patient_details", force: :cascade do |t|
3727
t.integer "patient_info_id"
3828
t.string "detail"
@@ -48,5 +38,4 @@
4838
t.datetime "created_at", precision: nil, null: false
4939
t.datetime "updated_at", precision: nil, null: false
5040
end
51-
5241
end

spec/phi_attrs/phi_record/class__allow_phi_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@
111111
it 'get_phi with block returns value' do |t|
112112
expect(PatientInfo.get_phi(file_name, t.full_description) { patient_jane.first_name }).to eq('Jane')
113113
end
114+
115+
it 'does not leak phi allowance if get_phi returns', :aggregate_failures do |t|
116+
def name_getter(reason, description)
117+
PatientInfo.get_phi(reason, description) { return patient_jane.first_name }
118+
end
119+
120+
expect(patient_jane.phi_allowed?).to be false
121+
first_name = name_getter(file_name, t.full_description)
122+
expect(first_name).to eq('Jane')
123+
expect(patient_jane.phi_allowed?).to be false
124+
end
114125
end
115126

116127
context 'extended authorization' do

spec/phi_attrs/phi_record/instance__allow_phi_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@
124124
it 'get_phi with block returns value' do |t|
125125
expect(patient_jane.get_phi(file_name, t.full_description) { patient_jane.first_name }).to eq('Jane')
126126
end
127+
128+
it 'does not leak phi allowance if get_phi returns', :aggregate_failures do |t|
129+
def name_getter(reason, description)
130+
patient_jane.get_phi(reason, description) { return patient_jane.first_name }
131+
end
132+
133+
expect(patient_jane.phi_allowed?).to be false
134+
first_name = name_getter(file_name, t.full_description)
135+
expect(first_name).to eq('Jane')
136+
expect(patient_jane.phi_allowed?).to be false
137+
end
127138
end
128139

129140
context 'collection' do

0 commit comments

Comments
 (0)