Skip to content

Commit d6bc619

Browse files
committed
CCM-11602: Add validation for limit parameter
1 parent 03d7f0c commit d6bc619

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lambdas/api-handler/src/handlers/__tests__/get-letters.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,36 @@ describe('API Lambda handler', () => {
7676
});
7777
});
7878

79+
it("returns 400 if the limit parameter is not a number", async () => {
80+
const event = makeApiGwEvent({
81+
path: "/letters",
82+
queryStringParameters: { limit: "1%" },
83+
});
84+
const context = mockDeep<Context>();
85+
const callback = jest.fn();
86+
const result = await getLetters(event, context, callback);
87+
88+
expect(result).toEqual({
89+
statusCode: 400,
90+
body: "Bad Request: limit parameter is not a number",
91+
});
92+
});
93+
94+
it("returns 400 if the limit parameter is not positive", async () => {
95+
const event = makeApiGwEvent({
96+
path: "/letters",
97+
queryStringParameters: { limit: "-1" },
98+
});
99+
const context = mockDeep<Context>();
100+
const callback = jest.fn();
101+
const result = await getLetters(event, context, callback);
102+
103+
expect(result).toEqual({
104+
statusCode: 400,
105+
body: "Bad Request: limit parameter is not positive",
106+
});
107+
});
108+
79109
it('returns 400 for missing supplier ID (empty headers)', async () => {
80110
const event = makeApiGwEvent({ path: "/letters", headers: {} });
81111
const context = mockDeep<Context>();

lambdas/api-handler/src/handlers/get-letters.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,34 @@ export const getLetters: APIGatewayProxyHandler = async (event) => {
3030
limit = "10";
3131
}
3232

33+
let limitNumber = Number(limit);
34+
35+
if (isNaN(limitNumber)) {
36+
log.info({
37+
description: "limit parameter is not a number",
38+
limit,
39+
});
40+
return {
41+
statusCode: 400,
42+
body: "Bad Request: limit parameter is not a number",
43+
};
44+
}
45+
46+
if (limitNumber < 0) {
47+
log.info({
48+
description: "limit parameter is not positive",
49+
limit,
50+
});
51+
return {
52+
statusCode: 400,
53+
body: "Bad Request: limit parameter is not positive",
54+
};
55+
}
56+
3357
const letters = await getLettersForSupplier(
3458
supplierId,
3559
status,
36-
Number(limit),
60+
limitNumber,
3761
letterRepo,
3862
);
3963

0 commit comments

Comments
 (0)