Skip to content

Commit

Permalink
feat(@aws-amplify/core): Throw Error if body attribute passed to Sign… (
Browse files Browse the repository at this point in the history
aws-amplify#10137)

* feat(@aws-amplify/core): Throw Error if body attribute passed to Signer.sign()

Co-authored-by: Ahilash Sasidharan  ahilashs@yahoo.com

* Set space-before-function-paren to false for unit test

* Make changes in response to PR comments

* Make changes in response to PR comments

* Find issue with unit tests

* Move sign error tests into Signer-test

* Set space-before-function-paren to false for unit test

* Run test with space-before-function-paren set to true

* Fix space before function error

* Update tslint.json

Set "space-before-function-paren" back to default setting.

* Remove spaces before keyword "function"

Return original formatting rule of no-spaces before "function" keyword.

Co-authored-by: Ashika <35131273+ashika01@users.noreply.github.com>
  • Loading branch information
amehi0index and ashika01 authored Aug 4, 2022
1 parent dbe57a4 commit 360bde2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
57 changes: 57 additions & 0 deletions packages/core/__tests__/Signer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,60 @@ describe('Signer test', () => {
});
});
});
describe('Sign method error', () => {
test('Should throw an Error if body attribute is passed to sign method', () => {
const url = 'https://host/some/path';

const request_body = {
url,
headers: {},
body: {},
};

const access_info = {
session_token: 'session_token',
};

const spyon = jest
.spyOn(Date.prototype, 'toISOString')
.mockReturnValueOnce('0');

expect(() => {
Signer.sign(request_body, access_info, {
service: 'aservice',
region: 'aregion',
});
}).toThrowError(
'The attribute "body" was found on the request object. Please use the attribute "data" instead.'
);

spyon.mockClear();
});

test('Should NOT throw an Error if data attribute is passed to sign method', () => {
const url = 'https://host/some/path';

const request_data = {
url,
headers: {},
data: {},
};

const access_info = {
session_token: 'session_token',
};

const spyon = jest
.spyOn(Date.prototype, 'toISOString')
.mockReturnValueOnce('0');

expect(() => {
Signer.sign(request_data, access_info, {
service: 'aservice',
region: 'aregion',
});
}).not.toThrowError();

spyon.mockClear();
});
});
14 changes: 7 additions & 7 deletions packages/core/src/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ const hash = function(src) {
*/
const escape_RFC3986 = function(component) {
return component.replace(/[!'()*]/g, function(c) {
return (
'%' +
c
.charCodeAt(0)
.toString(16)
.toUpperCase()
);
return '%' + c.charCodeAt(0).toString(16).toUpperCase();
});
};

Expand Down Expand Up @@ -290,6 +284,12 @@ export class Signer {
static sign(request, access_info, service_info = null) {
request.headers = request.headers || {};

if (request.body && !request.data) {
throw new Error(
'The attribute "body" was found on the request object. Please use the attribute "data" instead.'
);
}

// datetime string and date string
const dt = DateUtils.getDateWithClockOffset(),
dt_str = dt.toISOString().replace(/[:\-]|\.\d{3}/g, ''),
Expand Down

0 comments on commit 360bde2

Please sign in to comment.