-
Notifications
You must be signed in to change notification settings - Fork 647
fix(middleware-sdk-transcribe-streaming): unsign the non host headers #1336
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.