Skip to content

Commit c0bc9cb

Browse files
committed
fix(middleware-sdk-transcribe-streaming): unsign the non host headers
1 parent d018293 commit c0bc9cb

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { SignatureV4 } from "./signer";
2+
import { HttpRequest } from "@aws-sdk/protocol-http";
3+
describe("transcribe streaming", () => {
4+
describe("WebSocket request signer", () => {
5+
it("should invoke base SigV4 signer correctly", async () => {
6+
expect.assertions(4);
7+
const mockBaseSigner = {
8+
presign: jest.fn()
9+
};
10+
const signer = new SignatureV4({ signer: mockBaseSigner as any });
11+
const toSign = new HttpRequest({
12+
headers: {
13+
"x-amz-foo": "foo",
14+
bar: "bar",
15+
"amz-sdk-invocation-id": "123",
16+
"amz-sdk-request": "attempt=1",
17+
host: "aws.amazon.com"
18+
},
19+
body: "hello world",
20+
query: {
21+
prop1: "A",
22+
prop2: "B"
23+
}
24+
});
25+
const signed = await signer.sign(toSign);
26+
expect(toSign).toMatchObject(signed);
27+
expect(mockBaseSigner.presign).toBeCalled();
28+
// The request's body should not be presigned
29+
expect(mockBaseSigner.presign.mock.calls[0][0].body).toEqual("");
30+
expect(mockBaseSigner.presign.mock.calls[0][1]!.unsignableHeaders)
31+
.toBeDefined;
32+
const unsignableHeaders = mockBaseSigner.presign.mock.calls[0][1]!
33+
.unsignableHeaders;
34+
expect(unsignableHeaders).toEqual(
35+
new Set(Object.keys(toSign.headers).filter(a => a !== "host"))
36+
);
37+
});
38+
});
39+
});

packages/middleware-sdk-transcribe-streaming/src/signer.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ export class SignatureV4 implements RequestSigner, RequestPresigner {
2727
): Promise<IHttpRequest> {
2828
if (HttpRequest.isInstance(toSign)) {
2929
// Presign the endpoint url with empty body, otherwise
30-
// the payload hash would be UNSINGED_PAYLOAD
31-
const signedRequest = await this.signer.presign({ ...toSign, body: "" }, {
32-
expiresIn: 5 * 60 // presigned url must be expired within 5 mins
33-
} as any);
30+
// the payload hash would be UNSINGED-PAYLOAD
31+
const signedRequest = await this.signer.presign(
32+
{ ...toSign, body: "" },
33+
{
34+
// presigned url must be expired within 5 mins.
35+
expiresIn: 5 * 60,
36+
// Not to sign headers. Transcribe-streaming WebSocket
37+
// request omits headers except for required 'host' header. If we sign
38+
// the other headers, the signature could be mismatch.
39+
unsignableHeaders: new Set(
40+
Object.keys(toSign.headers).filter(header => header !== "host")
41+
)
42+
}
43+
);
3444
return {
3545
...signedRequest,
3646
body: toSign.body

0 commit comments

Comments
 (0)