Skip to content

fix(middleware-apply-body-checksum): fix request copying with clone #1324

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

Merged
merged 3 commits into from
Jul 18, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/thick-hats-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/middleware-apply-body-checksum": patch
---

Fix request copying with `HttpRequest.clone()`.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ describe("applyMd5BodyChecksumMiddleware", () => {
expect(mockHashDigest.mock.calls.length).toBe(0);
expect(mockEncoder.mock.calls.length).toBe(0);
});

it("should clone the request when applying the checksum", async () => {
const handler = applyMd5BodyChecksumMiddleware({
md5: MockHash,
base64Encoder: mockEncoder,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
streamHasher: async (stream: ExoticStream) => new Uint8Array(5),
})(next, {} as any);

await handler({
input: {},
request: new HttpRequest({
body: body,
}),
});

expect(next.mock.calls.length).toBe(1);
const { request } = next.mock.calls[0][0];
// Assert that non-enumerable properties like the method `clone()` are preserved.
expect(request.clone).toBeDefined();
});
}

it("should use the supplied stream hasher to calculate the hash of a streaming body", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const applyMd5BodyChecksumMiddleware =
(options: Md5BodyChecksumResolvedConfig): BuildMiddleware<any, any> =>
<Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
let { request } = args;
const { request } = args;
if (HttpRequest.isInstance(request)) {
const { body, headers } = request;
if (!hasHeader("content-md5", headers)) {
Expand All @@ -30,19 +30,18 @@ export const applyMd5BodyChecksumMiddleware =
digest = options.streamHasher(options.md5, body);
}

request = {
...request,
headers: {
...headers,
"content-md5": options.base64Encoder(await digest),
},
const cloned = HttpRequest.clone(request);
cloned.headers = {
...headers,
"content-md5": options.base64Encoder(await digest),
};
return next({
...args,
request: cloned,
});
}
}
return next({
...args,
request,
});
return next(args);
};

export const applyMd5BodyChecksumMiddlewareOptions: BuildHandlerOptions = {
Expand Down
Loading