From 143e74e95608f3a5320c0705d3a115f7b6a739c5 Mon Sep 17 00:00:00 2001 From: Hibo Abdilaahi <51047911+hiboabd@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:09:37 +0000 Subject: [PATCH] [CRIMAPP-449] Redact additional information (#212) * Redact additional information * Reduce cyclometric complexity --- app/services/redacting/redact.rb | 28 +++++++++++++++++--------- app/services/redacting/rules.rb | 4 ++++ spec/services/redacting/redact_spec.rb | 12 +++++++++++ spec/services/redacting/rules_spec.rb | 1 + 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/services/redacting/redact.rb b/app/services/redacting/redact.rb index 2814c40f..5cf9ea68 100644 --- a/app/services/redacting/redact.rb +++ b/app/services/redacting/redact.rb @@ -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 @@ -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)) @@ -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 diff --git a/app/services/redacting/rules.rb b/app/services/redacting/rules.rb index 55b8b75a..bd4da990 100644 --- a/app/services/redacting/rules.rb +++ b/app/services/redacting/rules.rb @@ -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 diff --git a/spec/services/redacting/redact_spec.rb b/spec/services/redacting/redact_spec.rb index 23f3a91b..faa04ff6 100644 --- a/spec/services/redacting/redact_spec.rb +++ b/spec/services/redacting/redact_spec.rb @@ -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 diff --git a/spec/services/redacting/rules_spec.rb b/spec/services/redacting/rules_spec.rb index 139a0506..d5fc787e 100644 --- a/spec/services/redacting/rules_spec.rb +++ b/spec/services/redacting/rules_spec.rb @@ -15,6 +15,7 @@ case_details.codefendants interests_of_justice supporting_evidence + additional_information ] ) end