Skip to content

Commit

Permalink
fix(s3-request-presigner): not mutate client mw stack (#3751)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored Jun 28, 2022
1 parent c2db32b commit cbe8126
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 6 additions & 1 deletion packages/s3-request-presigner/src/getSignedUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ describe("getSignedUrl", () => {
Bucket: "Bucket",
Key: "Key",
});
const signed = await getSignedUrl(client, command);
const presignPromise = getSignedUrl(client, command);
// do not mutate to the client or command
expect(client.middlewareStack.remove("presignInterceptMiddleware")).toBe(false);
expect(command.middlewareStack.remove("presignInterceptMiddleware")).toBe(false);

const signed = await presignPromise;
expect(signed).toBe(mockPresigned);
expect(mockPresign).toBeCalled();
expect(mockV4Presign).not.toBeCalled();
Expand Down
16 changes: 6 additions & 10 deletions packages/s3-request-presigner/src/getSignedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const getSignedUrl = async <
delete request.headers["amz-sdk-request"];
// User agent header would leak sensitive information
delete request.headers["x-amz-user-agent"];

const presigned = await s3Presigner.presign(request, {
...options,
signingRegion: options.signingRegion ?? context["signing_region"],
Expand All @@ -42,21 +41,18 @@ export const getSignedUrl = async <
} as any;
};
const middlewareName = "presignInterceptMiddleware";
client.middlewareStack.addRelativeTo(presignInterceptMiddleware, {
const clientStack = client.middlewareStack.clone();
clientStack.addRelativeTo(presignInterceptMiddleware, {
name: middlewareName,
relation: "before",
toMiddleware: "awsAuthMiddleware",
override: true,
});

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

return formatUrl(presigned);
};

0 comments on commit cbe8126

Please sign in to comment.