Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions packages/middleware-recursion-detection/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ describe(recursionDetectionMiddleware.name, () => {
expect(request.headers[TRACE_ID_HEADER_NAME]).toBe("some-real-trace-id");
});

it(`should NOT set ${TRACE_ID_HEADER_NAME} header when the header is already set in all lowercase`, async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

"is already set in all lowercase" should be "is already set with some other casing"

add a couple of test cases with inconsistent casing like x-AmZn-TrAcE-iD

process.env = {
AWS_LAMBDA_FUNCTION_NAME: "some-function",
_X_AMZN_TRACE_ID: "some-trace-id",
};
const handler = recursionDetectionMiddleware({ runtime: "node" })(mockNextHandler, {} as any);
await handler({
input: {},
request: new HttpRequest({
headers: {
[TRACE_ID_HEADER_NAME.toLowerCase()]: "some-real-trace-id",
},
}),
});

const { calls } = (mockNextHandler as any).mock;
expect(calls.length).toBe(1);
const { request } = mockNextHandler.mock.calls[0][0];
expect(request.headers[TRACE_ID_HEADER_NAME.toLowerCase()]).toBe("some-real-trace-id");
});

it("has no effect for browser runtime", async () => {
process.env = {
AWS_LAMBDA_FUNCTION_NAME: "some-function",
Expand Down
12 changes: 7 additions & 5 deletions packages/middleware-recursion-detection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export const recursionDetectionMiddleware =
<Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (
!HttpRequest.isInstance(request) ||
options.runtime !== "node" ||
request.headers.hasOwnProperty(TRACE_ID_HEADER_NAME)
) {
if (!HttpRequest.isInstance(request) || options.runtime !== "node") {
return next(args);
}
const traceIdHeader =
Object.keys(request.headers ?? {}).find((h) => h.toLowerCase() === TRACE_ID_HEADER_NAME.toLowerCase()) ??
TRACE_ID_HEADER_NAME;

if (request.headers.hasOwnProperty(traceIdHeader)) {
return next(args);
}
const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME];
const traceId = process.env[ENV_TRACE_ID];
const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0;
Expand Down
Loading