Skip to content

Commit cbe8126

Browse files
authored
fix(s3-request-presigner): not mutate client mw stack (#3751)
1 parent c2db32b commit cbe8126

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

packages/s3-request-presigner/src/getSignedUrl.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ describe("getSignedUrl", () => {
4242
Bucket: "Bucket",
4343
Key: "Key",
4444
});
45-
const signed = await getSignedUrl(client, command);
45+
const presignPromise = getSignedUrl(client, command);
46+
// do not mutate to the client or command
47+
expect(client.middlewareStack.remove("presignInterceptMiddleware")).toBe(false);
48+
expect(command.middlewareStack.remove("presignInterceptMiddleware")).toBe(false);
49+
50+
const signed = await presignPromise;
4651
expect(signed).toBe(mockPresigned);
4752
expect(mockPresign).toBeCalled();
4853
expect(mockV4Presign).not.toBeCalled();

packages/s3-request-presigner/src/getSignedUrl.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const getSignedUrl = async <
2626
delete request.headers["amz-sdk-request"];
2727
// User agent header would leak sensitive information
2828
delete request.headers["x-amz-user-agent"];
29-
3029
const presigned = await s3Presigner.presign(request, {
3130
...options,
3231
signingRegion: options.signingRegion ?? context["signing_region"],
@@ -42,21 +41,18 @@ export const getSignedUrl = async <
4241
} as any;
4342
};
4443
const middlewareName = "presignInterceptMiddleware";
45-
client.middlewareStack.addRelativeTo(presignInterceptMiddleware, {
44+
const clientStack = client.middlewareStack.clone();
45+
clientStack.addRelativeTo(presignInterceptMiddleware, {
4646
name: middlewareName,
4747
relation: "before",
4848
toMiddleware: "awsAuthMiddleware",
4949
override: true,
5050
});
5151

52-
let presigned: HttpRequest;
53-
try {
54-
const output = await client.send(command);
55-
//@ts-ignore the output is faked, so it's not actually OutputType
56-
presigned = output.presigned;
57-
} finally {
58-
client.middlewareStack.remove(middlewareName);
59-
}
52+
const handler = command.resolveMiddleware(clientStack, client.config, {});
53+
const { output } = await handler({ input: command.input });
54+
//@ts-ignore the output is faked, so it's not actually OutputType
55+
const { presigned } = output;
6056

6157
return formatUrl(presigned);
6258
};

0 commit comments

Comments
 (0)