wso2 / wso2/reference-implementation-openhie

Unchecked type casts will panic on malformed HL7 messages

Open Beginner friendly
#9 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ballerina
Stars
1
Forks
5
PR merge metrics
No merged PRs in 30d

Description

Unchecked type casts will panic on malformed HL7 messages, bypassing the error return

<map<anydata>>hl7Message["msh"] and the subsequent casts on lines 34–37 panic at runtime if any field is absent or not a map. Because these are type-cast panics (not errors), they are not catchable via the function's returns TcpRequestContext|error contract or an on fail block—callers will see an unhandled runtime panic instead of a manageable error.

Use ensureType() with check so failures surface as error values:

🐛 Proposed fix
-    map<anydata> msh = <map<anydata>>hl7Message["msh"];
-    map<anydata> hd3 = <map<anydata>>msh["msh3"];
-    map<anydata> hd4 = <map<anydata>>msh["msh4"];
-    map<anydata> hd5 = <map<anydata>>msh["msh5"];
-    map<anydata> hd6 = <map<anydata>>msh["msh6"];
+    map<anydata> msh = check hl7Message["msh"].ensureType();
+    map<anydata> hd3 = check msh["msh3"].ensureType();
+    map<anydata> hd4 = check msh["msh4"].ensureType();
+    map<anydata> hd5 = check msh["msh5"].ensureType();
+    map<anydata> hd6 = check msh["msh6"].ensureType();

Additionally, note that username (line 41) and sendingApplication (line 48) both resolve to hd3["hd1"] (MSH-3, Sending Application). This is likely intentional given the existing TODO: extract user details comment, but worth confirming.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@iol/iol-core/context_builder.bal` around lines 33 - 50, The code is doing
unchecked casts like map<anydata> msh = <map<anydata>>hl7Message["msh"] and
similar for hd3/hd4/hd5/hd6 and hd1 fields; change each unchecked cast/access to
use check ensureType(...) so failures become returned errors (e.g., msh := check
ensureType(hl7Message["msh"], map<anydata>), hd3 := check
ensureType(msh["msh3"], map<anydata>), and for leaf values use username := check
ensureType(hd3["hd1"], string) instead of hd3["hd1"].toString()); apply this for
msh, hd3, hd4, hd5, hd6 and the hd1 fields used for username, sendingFacility,
receivingFacility, sendingApplication and receivingApplication so the function
keeps its TcpRequestContext|error return behavior; also confirm whether using
hd3["hd1"] for both username and sendingApplication is intentional.

Originally posted by @coderabbitai[bot] in https://github.com/wso2/reference-implementation-openhie/pull/7#discussion_r3201537409

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in @iol/iol-core/context_builder.bal around lines 33–50 and inspect the unchecked casts and hd1 field accesses. Confirm malformed HL7 input produces an error through the existing TcpRequestContext|error contract, and verify whether using hd3["hd1"] for both username and sendingApplication is intentional.

Written by the indexing model from the issue text.

Assessment

Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.