Skip to content

Fix: Rx note giving an error if pharmacy name contains double quotes#144

Merged
lacarmen merged 4 commits into
MagentaHealth:release/2026-03-17from
openo-beta:bug/rx-note-cant-be-added-if-pharamacy-has-double-quotes-base-release
Jun 5, 2026
Merged

Fix: Rx note giving an error if pharmacy name contains double quotes#144
lacarmen merged 4 commits into
MagentaHealth:release/2026-03-17from
openo-beta:bug/rx-note-cant-be-added-if-pharamacy-has-double-quotes-base-release

Conversation

@LiamStanziani
Copy link
Copy Markdown
Collaborator

@LiamStanziani LiamStanziani commented Jun 5, 2026

Summary

A double quote in a pharmacy name broke the Print & Add to encounter note button —
clicking it did nothing. The fix applies context-appropriate OWASP encoding at each JSP
output point
so pharmacy/provider values are safe in both the JavaScript onClick handlers
and the HTML hidden inputs that get submitted to the prescription PDF servlet.

Files changed:

  • src/main/java/ca/openosp/openo/rx/RxPrintPreview2Action.java
  • src/main/webapp/oscarRx/printRx/PrintPreview.jsp
  • src/main/webapp/oscarRx/printRx/PreviewContent.jsp

Original PR: openo-beta#2459

Problem

When a patient's preferred pharmacy name contained a double quote ("), clicking
Print & Add to encounter note (and the Fax + paste button) did nothing — neither the
print dialog nor the encounter window opened.

Root cause: in PrintPreview.jsp, the pharmacy name was interpolated raw into a
double-quoted onClick HTML attribute:

onClick="printPasteToParent('${ctx}', ..., '${requestScope.prefPharmacy}', ...);">

The value reaching the JSP was only escaped for single quotes in the action
(pharmacy.getName().replace("'", "\\'")), so a " passed through raw, closed the
onClick="..." attribute early, and left a truncated, malformed handler — making the button
a no-op.

A second, related defect surfaced during review: pharmacyName and pharmacyFax are
dual-context — they are used in the onClick (JavaScript string) and in hidden inputs
in PreviewContent.jsp that are submitted to FrmCustomedPDFServlet (pharmaName/pharmaFax).
JavaScript-escaping these at the Java source would submit escaped literals
(e.g. Test \x22Quote\x22 Pharmacy) to the PDF servlet instead of the real name.

Solution

Encode at the output, per context, rather than at the Java source — matching the pattern
already used by the sibling hidden inputs (patientNameEncode.forHtml, pharmacyInfo
Encode.forHtmlAttribute).

RxPrintPreview2Action.java — store the raw (null-guarded) values; no source-side
encoding:

  • providerName, prefPharmacy, pharmacyName, pharmacyFax

PrintPreview.jsp — JavaScript-string context (both the print and fax onClick
handlers): wrap each of the four values in ${e:forJavaScript(...)}. forJavaScript escapes
" (→ \x22), ', \, /, <, >, &, so the value is safe as a JS string literal and
cannot break out of the surrounding double-quoted attribute. The browser decodes the escapes
back to the literal characters at runtime, so the encounter note shows the real name.

PreviewContent.jsp — HTML-attribute context (hidden inputs submitted to the PDF servlet):
pharmaName/pharmaFax use <%= Encode.forHtmlAttribute(...) %>. HTML-attribute encoding
decodes back to the real value on form submit, so FrmCustomedPDFServlet receives
Test "Quote" Pharmacy, not the escaped literal.

Why output-side encoding

Encoding at the Java source is the wrong layer for a value rendered into more than one context:
the same string was JS-escaped once but consumed as both JavaScript and an HTML attribute, so
one of the two contexts was always wrong. Encoding at each output point lets every consumer
apply the encoding its context requires. Values are coalesced to "" before being stored so
the request attributes are never null.

Summary by Sourcery

Apply context-appropriate output encoding for provider and pharmacy data in Rx print preview to prevent broken buttons and ensure safe values in both JavaScript handlers and PDF form submissions.

Bug Fixes:

  • Fix Rx print and fax encounter-note actions failing when pharmacy or provider fields contain double quotes or other special characters.

Enhancements:

  • Align JSP hidden input encoding with OWASP recommendations by using HTML and HTML-attribute encoders for patient DOB and pharmacy fields.

@LiamStanziani LiamStanziani self-assigned this Jun 5, 2026
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 5, 2026

Reviewer's Guide

Ensures pharmacy/provider names are stored raw in the action and then safely rendered in both JavaScript onClick handlers and HTML hidden inputs using context-appropriate OWASP encoding to fix Rx print/fax buttons breaking when names contain double quotes.

File-Level Changes

Change Details Files
Store raw pharmacy/provider values in the action and avoid Java-side JavaScript escaping so they can be correctly encoded per output context in JSPs.
  • Add documentation comment explaining that providerName is stored raw and encoding happens in JSPs.
  • Remove single-quote replacement from preferred pharmacy name and instead trim and null-guard the raw name before storing it in the request.
  • Change pharmacyName and pharmacyFax request attributes to be raw, null-guarded strings instead of pre-JavaScript-escaped values, with comments about dual JavaScript/HTML contexts.
src/main/java/ca/openosp/openo/rx/RxPrintPreview2Action.java
Apply OWASP JavaScript encoding to all pharmacy/provider-related values used inside JavaScript onClick handlers to prevent malformed handlers from special characters like double quotes.
  • Wrap prefPharmacy, providerName, pharmacyName, and pharmacyFax in e:forJavaScript when passed into printPasteToParent onClick handler.
  • Wrap the same four values in e:forJavaScript when passed into faxPasteToParent onClick handler.
src/main/webapp/oscarRx/printRx/PrintPreview.jsp
HTML-encode pharmacy and patient values in preview content hidden inputs so the PDF servlet receives correct raw values while protecting against broken markup.
  • Switch patientDOB hidden input encoding from StringEscapeUtils.escapeHtml to Encode.forHtml.
  • Encode pharmacyFax and pharmacyName hidden input values with Encode.forHtmlAttribute, null-guarding the request attributes.
  • Keep pharmacyInfo using Encode.forHtmlAttribute for consistency with other hidden inputs.
src/main/webapp/oscarRx/printRx/PreviewContent.jsp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@LiamStanziani LiamStanziani marked this pull request as ready for review June 5, 2026 21:11
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In setupAdditionalAttributes, the null-coalescing and trimming for pharmacy name/fax is open-coded; consider extracting a small helper to normalize these values so you don’t duplicate this pattern if other pharmacy fields are added later.
  • In PreviewContent.jsp, patientDOB now uses Encode.forHtml while neighboring fields still use StringEscapeUtils.escapeHtml; it would be clearer and less error-prone to standardize on a single encoding library for all these patient-related attributes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `setupAdditionalAttributes`, the null-coalescing and trimming for pharmacy name/fax is open-coded; consider extracting a small helper to normalize these values so you don’t duplicate this pattern if other pharmacy fields are added later.
- In `PreviewContent.jsp`, `patientDOB` now uses `Encode.forHtml` while neighboring fields still use `StringEscapeUtils.escapeHtml`; it would be clearer and less error-prone to standardize on a single encoding library for all these patient-related attributes.

## Individual Comments

### Comment 1
<location path="src/main/webapp/oscarRx/printRx/PreviewContent.jsp" line_range="121-122" />
<code_context>
+                                <input type="hidden" name="patientDOB"
</code_context>
<issue_to_address>
**🚨 issue (security):** Use `Encode.forHtmlAttribute` for `patientDOB` since the value is in an HTML attribute context.

Since this value is rendered in an HTML attribute (`value="..."`), it should use attribute-safe encoding to prevent malformed attributes or potential XSS if the string ever contains quotes or other special characters. `Encode.forHtmlAttribute` is designed for this context, unlike `Encode.forHtml`, which targets body content.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/main/webapp/oscarRx/printRx/PreviewContent.jsp
@LiamStanziani LiamStanziani requested a review from lacarmen June 5, 2026 21:17
@lacarmen lacarmen merged commit b056deb into MagentaHealth:release/2026-03-17 Jun 5, 2026
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants