Skip to content

Commit 43329b2

Browse files
wildjameststephen-nhsanthony-nhs
authored
Chore: [AEA-5508] - Validate ODS code (#2267)
## Summary - Routine Change ### Details When we get ODS codes, makes sure that they are: - capitalised - trimmed of whitespace Then, checks that the ODS codes submitted for all data items are: - non-empty - consist of only numbers and capital letters if either of those two checks don't pass, then an error is returned. --------- Co-authored-by: tstephen-nhs <[email protected]> Co-authored-by: anthony-nhs <[email protected]>
1 parent f4f7285 commit 43329b2

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

packages/nhsNotifyLambda/src/utils/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function tokenExchange(
1919
])
2020

2121
const API_KEY = apiKeyRaw?.toString().trim()
22-
const PRIVATE_KEY = privateKeyRaw?.toString()
22+
const PRIVATE_KEY = privateKeyRaw?.toString().trim()
2323
const KID = kidRaw?.toString().trim()
2424

2525
if (!API_KEY || !PRIVATE_KEY || !KID) {

packages/nhsNotifyUpdateCallback/src/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export async function fetchSecrets(logger: Logger): Promise<void> {
6161
throw new Error("Failed to get secret values from the AWS secret manager")
6262
}
6363

64-
APP_ID = appIdValue.toString()
65-
API_KEY = apiKeyValue.toString()
64+
APP_ID = appIdValue.toString().trim()
65+
API_KEY = apiKeyValue.toString().trim()
6666

6767
// Check again to catch empty strings
6868
if (!appIdValue || !apiKeyValue) {

packages/updatePrescriptionStatus/src/updatePrescriptionStatus.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPro
111111

112112
const dataItems = buildDataItems(requestEntries, xRequestID, applicationName)
113113

114+
// If the dataItems contain any invalid ODS codes, then return an error
115+
const invalidODSCodes = dataItems
116+
.filter(item => {
117+
const odsCode = item.PharmacyODSCode
118+
if (!odsCode || !/^[A-Z0-9]+$/.test(odsCode)) return true
119+
return false
120+
})
121+
.map(it => it.PharmacyODSCode)
122+
if (invalidODSCodes.length) {
123+
logger.error("Received invalid ODS codes", {invalidODSCodes})
124+
responseEntries = [badRequest(`Received invalid ODS codes: ${JSON.stringify(invalidODSCodes)}`)]
125+
return response(400, responseEntries)
126+
}
127+
114128
// AEA-4317 (AEA-4365) - Intercept INT test prescriptions
115129
let testPrescription1Forced201 = false
116130
let testPrescriptionForcedError = false
@@ -318,7 +332,7 @@ export function buildDataItems(
318332
LastModified: task.lastModified!,
319333
LineItemID: task.focus!.identifier!.value!.toUpperCase(),
320334
PatientNHSNumber: task.for!.identifier!.value!,
321-
PharmacyODSCode: task.owner!.identifier!.value!.toUpperCase(),
335+
PharmacyODSCode: task.owner!.identifier!.value!.toUpperCase().trim(),
322336
PrescriptionID: task.basedOn![0].identifier!.value!.toUpperCase(),
323337
...(repeatNo !== undefined && {RepeatNo: repeatNo}),
324338
RequestID: xRequestID,

packages/updatePrescriptionStatus/tests/testHandler.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,33 @@ describe("Integration tests for updatePrescriptionStatus handler", () => {
217217
)
218218
})
219219

220+
const testInvalidODSCode = async (invalidODSCode: string, expectedErrorCode: string) => {
221+
const body = generateBody()
222+
const entryResource: any = body.entry?.[0]?.resource
223+
if (entryResource?.owner?.identifier) {
224+
entryResource.owner.identifier.value = invalidODSCode
225+
}
226+
227+
const event: APIGatewayProxyEvent = generateMockEvent(body)
228+
229+
const response: APIGatewayProxyResult = await handler(event, {})
230+
231+
expect(response.statusCode).toBe(400)
232+
expect(JSON.parse(response.body)).toEqual(
233+
bundleWrap([
234+
badRequest(`Received invalid ODS codes: ["${expectedErrorCode}"]`)
235+
])
236+
)
237+
}
238+
239+
it("When the ODS code contains a special character, the handler returns a 400 error", async () => {
240+
await testInvalidODSCode("AB1$%2", "AB1$%2")
241+
})
242+
243+
it("When the ODS code is a space character, the handler returns a 400 error", async () => {
244+
await testInvalidODSCode(" ", "")
245+
})
246+
220247
it("when dynamo call fails, expect 500 status code and internal server error message", async () => {
221248
const event = generateMockEvent(requestDispatched)
222249
dynamoDBMockSend.mockRejectedValue(new Error() as never)

0 commit comments

Comments
 (0)