Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix information expsure through error message #2993

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions containers/message-refiner/app/refine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import logging
from typing import Dict
from typing import List
from typing import Optional
from typing import Union

from lxml import etree

# read json that contains details for refining and is the base of what drives `refine`
from app.utils import read_json_from_assets

log = logging.getLogger(__name__).error


# read json that contains details for refining and is the base of what drives `refine`
REFINER_DETAILS = read_json_from_assets("refiner_details.json")

# extract section LOINC codes from the REFINER_DETAILS dictionary
Expand All @@ -34,8 +38,9 @@ def validate_message(raw_message: str) -> tuple[bytes | None, str]:
validated_message = etree.fromstring(raw_message)
return (validated_message, error_message)
except etree.XMLSyntaxError as error:
error_message = f"XMLSyntaxError: {error}"
return (None, str(error_message))
error_message = "Invalid XML format."
log(f"XMLSyntaxError: {error}")
return (None, error_message)


def validate_sections_to_include(sections_to_include: str | None) -> tuple[list, str]:
Expand All @@ -57,7 +62,8 @@ def validate_sections_to_include(sections_to_include: str | None) -> tuple[list,
sections = sections_to_include.split(",")
for section in sections:
if section not in SECTION_LOINCS:
error_message = f"{section} is invalid. Please provide a valid section."
error_message = "Invalid section provided."
log(f"Invalid section: {section}")
return (section_loincs, error_message)
section_loincs.append(section)

Expand Down
4 changes: 2 additions & 2 deletions containers/message-refiner/tests/test_refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def normalize_xml(xml_string):
# Test case: invalid sections_to_include
(
"blah blah blah",
([], "blah blah blah is invalid. Please provide a valid section."),
([], "Invalid section provided."),
),
],
)
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_validate_message():
raw_message = "this is not a valid XML"
actual_response, error_message = validate_message(raw_message)
assert actual_response is None
assert "XMLSyntaxError" in error_message
assert "Invalid XML format." in error_message


def test_generate_combined_xpath():
Expand Down
4 changes: 2 additions & 2 deletions containers/message-refiner/tests/test_refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_ecr_refiner():
assert actual_flattened == expected_flattened

# Test case: sections_to_include is invalid
expected_response = "blah blah blah is invalid. Please provide a valid section."
expected_response = "Invalid section provided."
content = test_eICR_xml
sections_to_include = "blah blah blah"
endpoint = f"/ecr/?sections_to_include={sections_to_include}"
Expand All @@ -142,7 +142,7 @@ def test_ecr_refiner():
endpoint = "/ecr/"
actual_response = client.post(endpoint, content=content)
assert actual_response.status_code == 400
assert "XMLSyntaxError" in actual_response.content.decode()
assert "Invalid XML format." in actual_response.content.decode()


@pytest.mark.asyncio
Expand Down
Loading