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

Cumulative Fixes and Improvements #2255

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions .github/scripts/read-binary-content-via-json-invalid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash -e

# This script creates a binary resource with invalid data and verifies that,
# when trying to read its binary content, an error is generated.

BASE="http://localhost:8080/fhir"

# 10 KiB of random data, base64 encoded
ORIGINAL_DATA="$(openssl rand -base64 10240 | tr -d '\n')"


# Higher-order function to apply a transformation based on a condition
apply_generic_transformation() {
local data="$1"
local transform_function="$2"

local result=""

# Iterate over the data and apply the transform function
for i in $(seq 0 ${#data}-1); do
result+=$(eval "$transform_function \"${data:$i:1}\" $i")
done

echo "$result"
}

remove_parts_transform() {
local char="$1"
local index="$2"

# Randomly decide whether to remove this character (skip it)
if (( RANDOM % 10 == 0 )); then
echo "" # Skip (remove this character)
else
echo "$char" # Keep the character
fi
}

insert_non_base64_transform() {
local char="$1"
local index="$2"

# Every 5th character, insert a random non-base64 character
if (( index % 5 == 0 )); then
echo "$((RANDOM % 10))" # Insert a non-base64 digit
else
echo "$char" # Keep the character
fi
}

# Apply Remove Parts of the Data
FAULTY_DATA=$(apply_generic_transformation "$ORIGINAL_DATA" "remove_parts_transform")
# echo "Faulty Data (Removed Parts): $FAULTY_DATA"

# Apply Insert Non-Base64 Characters
FAULTY_DATA=$(apply_generic_transformation "$FAULTY_DATA" "insert_non_base64_transform")
# echo "Faulty Data (also with Non-Base64 Characters): $FAULTY_DATA"

DATA=$FAULTY_DATA


binary() {
cat <<END
{
"resourceType": "Binary",
"contentType": "application/pdf",
"data": "$DATA"
}
END
}

# Create a Binary resource that contains that faulty data, and get its ID (via JSON)
ID_VIA_JSON=$(curl -s -H 'Content-Type: application/fhir+json' -d "$(binary)" "$BASE/Binary" | jq -r '.id')

echo "Created Binary resource that contains the Random Data"
echo " - via JSON, with ID: $ID_VIA_JSON"


# Retrieve the Binary resource, and Base64 encode it so it can be safely handled by Bash (JSON)
BASE64_ENCODED_BINARY_RESOURCE_VIA_JSON=$(curl -s -H 'Accept: application/pdf' "$BASE/Binary/$ID_VIA_JSON" | base64 | tr -d '\n')


echo "Binary data retrieved. Verifying content... (JSON version)"

if [ "$DATA" = "$BASE64_ENCODED_BINARY_RESOURCE_VIA_JSON" ]; then
echo "✅ Base64 encoding of both the Original Data and the Retrieved Resource Data match (JSON)"
else
echo "🆘 Base64 encoding of both the Original Data and the Retrieved Resource Data are different (JSON)"
exit 1
fi
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,9 @@ jobs:
- name: Binary Content Download - not found
run: .github/scripts/read-binary-content-not-found.sh

- name: Binary Content Download - invalid (via JSON)
run: .github/scripts/read-binary-content-via-json-invalid.sh

- name: Binary Content Download - found (via JSON)
run: .github/scripts/read-binary-content-via-json-found.sh

Expand Down
8 changes: 4 additions & 4 deletions modules/rest-util/src/blaze/middleware/fhir/output.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

(def ^:private parse-accept (parse/fast-memoize 1000 parse/parse-accept))

(defn- generate-error [generation-fn ex]
(-> ex
(defn- generate-error-payload [generation-fn e]
(-> e
ba/anomaly
handler-util/operation-outcome
generation-fn))
Expand All @@ -51,7 +51,7 @@
(update response :body generate-xml**)
(catch Throwable e
(assoc response
:body (generate-error generate-xml** e)
:body (generate-error-payload generate-xml** e)
:status 500))))

(defn- generate-xml [response]
Expand All @@ -68,7 +68,7 @@
(update response :body generate-binary**)
(catch Throwable e
(assoc response
:body (generate-error identity e)
:body (generate-error-payload generate-json e)
:status 500))))

(defn- generate-binary [response]
Expand Down
6 changes: 3 additions & 3 deletions modules/rest-util/test/blaze/middleware/fhir/output_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@
[:headers "Content-Type"] := nil
:body := nil)))

(testing "failing binary emit (with invalid data)"
(testing "failing binary emit (with invalid data, using the JSON default)"
(given (call (binary-resource-handler-200 {:content-type "application/pdf" :data "MTANjECg=="}) {:headers {"accept" "text/plain"}})
:status := 500
[:headers "Content-Type"] := "application/pdf"
[:body :fhir/type] := :fhir/OperationOutcome
[:body :issue 0 :diagnostics] := "Input byte array has wrong 4-byte ending unit"))))
[:body parse-json :fhir/type] := :fhir/OperationOutcome
[:body parse-json :issue 0 :diagnostics] := "Input byte array has wrong 4-byte ending unit"))))

(deftest not-acceptable-test
(is (nil? (call resource-handler-200-with-patient {:headers {"accept" "text/plain"}}))))
Loading