Unchecked type casts will panic on malformed HL7 messages
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Domain
- backend
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.
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
- Dominant language
- Ballerina
- Stars
- 1
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from wso2/reference-implementation-openhie
-
Potential issue iol Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
All issues in wso2/reference-implementation-openhie
Similar issues
-
bug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100