Skip to content

Commit

Permalink
[CRIMAPP-449] Redact additional information (#212)
Browse files Browse the repository at this point in the history
* Redact additional information

* Reduce cyclometric complexity
  • Loading branch information
hiboabd authored Feb 5, 2024
1 parent 29dca2a commit 143e74e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
28 changes: 18 additions & 10 deletions app/services/redacting/redact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ def process!
# Then we redact from this copy anything according to the rules
Rules.pii_attributes.each do |path, rules|
path = path.split('.')
details = redacted_payload.dig(*path)
payload_details = redacted_payload.dig(*path)

next if details.blank?
next if payload_details.blank?

fields = rules.fetch(:redact)
type = rules.fetch(:type, :object)

details = case type
when :object
details.slice(*fields).compact_blank
when :array
details.map { |item| item.slice(*fields).compact_blank }
else
raise "unknown rule path type: #{type}"
end
details = details_by_type(payload_details, type, fields)

merge_redacted(path, details)
end
Expand All @@ -55,6 +48,19 @@ def process_metadata!

private

def details_by_type(details, type, fields)
case type
when :object
details.slice(*fields).compact_blank
when :array
details.map { |item| item.slice(*fields).compact_blank }
when :string
details
else
raise "unknown rule path type: #{type}"
end
end

def merge_redacted(path, details)
redacted_payload.deep_merge!(
traverse(path, redact(details.dup))
Expand All @@ -71,6 +77,8 @@ def merge_redacted(path, details)
def redact(details)
if details.is_a?(Array)
details.map { |item| redact(item.dup) }
elsif details.is_a?(String)
REDACTED_KEYWORD
else
details.each_key { |key| details[key] = REDACTED_KEYWORD }
end
Expand Down
4 changes: 4 additions & 0 deletions app/services/redacting/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module Rules
redact: %w[s3_object_key filename],
type: :array # [{}, {}, ...]
},
'additional_information' => {
redact: :value,
type: :string
}
}.freeze

def self.pii_attributes
Expand Down
12 changes: 12 additions & 0 deletions spec/services/redacting/redact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@
)
end
end

context 'with additional information' do
let(:submitted_application) do
super().deep_merge('additional_information' => 'Additional information here')
end

let(:additional_information) { redacted_application['additional_information'] }

it 'redacts the expected attributes' do
expect(additional_information).to eq('__redacted__')
end
end
end

describe 'metadata attributes' do
Expand Down
1 change: 1 addition & 0 deletions spec/services/redacting/rules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
case_details.codefendants
interests_of_justice
supporting_evidence
additional_information
]
)
end
Expand Down

0 comments on commit 143e74e

Please sign in to comment.