Skip to content
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
42 changes: 42 additions & 0 deletions packages/middleware-sdk-transcribe-streaming/src/signer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { HttpRequest } from "@aws-sdk/protocol-http";
import { SignatureV4 } from "./signer";
describe("transcribe streaming", () => {
describe("WebSocket request signer", () => {
it("should invoke base SigV4 signer correctly", async () => {
expect.assertions(5);
const toSign = new HttpRequest({
headers: {
"x-amz-foo": "foo",
bar: "bar",
"amz-sdk-invocation-id": "123",
"amz-sdk-request": "attempt=1",
host: "aws.amazon.com"
},
body: "hello world",
query: {
prop1: "A",
prop2: "B"
}
});
const mockBaseSigner = {
presign: jest
.fn()
.mockImplementation(request => Promise.resolve(request))
};
const signer = new SignatureV4({ signer: mockBaseSigner as any });
const signed = await signer.sign(toSign);
expect(toSign).toMatchObject(signed);
Copy link
Member

Choose a reason for hiding this comment

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

presign is mocked, so it should return undefined. So I'm not sure how this expect is successful.
Add a mockImplementation for presign function, and check the value returned with signed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is fixed in 6e86721

expect(mockBaseSigner.presign).toBeCalled();
// The request's body should not be presigned
expect(mockBaseSigner.presign.mock.calls[0][0].body).toEqual("");
expect(
mockBaseSigner.presign.mock.calls[0][1]!.unsignableHeaders
).toBeDefined();
const unsignableHeaders: Set<string> = mockBaseSigner.presign.mock
.calls[0][1]!.unsignableHeaders;
expect([...unsignableHeaders.entries()].map(([value]) => value)).toEqual(
Object.keys(toSign.headers).filter(a => a !== "host")
);
});
});
});
18 changes: 14 additions & 4 deletions packages/middleware-sdk-transcribe-streaming/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ export class SignatureV4 implements RequestSigner, RequestPresigner {
): Promise<IHttpRequest> {
if (HttpRequest.isInstance(toSign)) {
// Presign the endpoint url with empty body, otherwise
// the payload hash would be UNSINGED_PAYLOAD
const signedRequest = await this.signer.presign({ ...toSign, body: "" }, {
expiresIn: 5 * 60 // presigned url must be expired within 5 mins
} as any);
// the payload hash would be UNSINGED-PAYLOAD
Copy link
Member

Choose a reason for hiding this comment

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

nit

Suggested change
// the payload hash would be UNSINGED-PAYLOAD
// the payload hash would be UNSIGNED-PAYLOAD

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fixed

const signedRequest = await this.signer.presign(
{ ...toSign, body: "" },
{
// presigned url must be expired within 5 mins.
expiresIn: 5 * 60,
// Not to sign headers. Transcribe-streaming WebSocket
// request omits headers except for required 'host' header. If we sign
// the other headers, the signature could be mismatch.
unsignableHeaders: new Set(
Object.keys(toSign.headers).filter(header => header !== "host")
)
}
);
return {
...signedRequest,
body: toSign.body
Expand Down