Skip to content

Commit

Permalink
Extract character sanitization logic into remove_invalid_characters m…
Browse files Browse the repository at this point in the history
…ethod

Signed-off-by: Vanessa Fotso <[email protected]>
  • Loading branch information
vanessuniq committed Aug 26, 2024
1 parent a92789f commit a894c0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/inferno/dsl/fhir_resource_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,20 @@ def operation_outcome_from_hl7_wrapped_response(response_hash)
FHIR::OperationOutcome.new(issue: issues)
end

# @private
def remove_invalid_characters(string)
string.gsub(/[^[:print:]\r\n]+/, '')
end

# @private
def operation_outcome_from_validator_response(response, runnable)
# Sanitize the response body by removing non-printable/control characters
sanitized_body = response.body.gsub(/[^[:print:]\r\n]+/, '')
sanitized_body = remove_invalid_characters(response.body)

operation_outcome_from_hl7_wrapped_response(JSON.parse(sanitized_body))
rescue JSON::ParserError
runnable.add_message('error', "Validator Response: HTTP #{response.status}\n#{sanitized_body}")
raise Inferno::Exceptions::ErrorInValidatorException,
'Validator response was an unexpected format. '\
'Validator response was an unexpected format. ' \
'Review Messages tab or validator service logs for more information.'
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/inferno/dsl/fhir_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,20 @@ def call_validator(resource, profile_url)
).post('validate', resource.source_contents)
end

# @private
def remove_invalid_characters(string)
string.gsub(/[^[:print:]\r\n]+/, '')
end

# @private
def operation_outcome_from_validator_response(response, runnable)
# Sanitize the response body by removing non-printable/control characters
sanitized_body = response.body.gsub(/[^[:print:]\r\n]+/, '')
sanitized_body = remove_invalid_characters(response.body)

FHIR::OperationOutcome.new(JSON.parse(sanitized_body))
rescue JSON::ParserError
runnable.add_message('error', "Validator Response: HTTP #{response.status}\n#{sanitized_body}")
raise Inferno::Exceptions::ErrorInValidatorException,
'Validator response was an unexpected format. '\
'Validator response was an unexpected format. ' \
'Review Messages tab or validator service logs for more information.'
end
end
Expand Down

0 comments on commit a894c0c

Please sign in to comment.