Skip to content

Commit 8f28179

Browse files
authored
Merge pull request #7326 from BitGo/ANT-1033-fix
fix: Do not append text to HMAC subject if undefined
2 parents 7d1d3d8 + c9755e7 commit 8f28179

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

modules/sdk-hmac/src/hmac.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ export function calculateHMACSubject<T extends string | Buffer = string>({
5757
? [method.toUpperCase(), timestamp, '3.0', queryPath].join('|')
5858
: [timestamp, queryPath].join('|');
5959
}
60-
prefixedText += '|';
6160

6261
const isBuffer = Buffer.isBuffer(text);
6362
if (isBuffer) {
64-
return Buffer.concat([Buffer.from(prefixedText, 'utf-8'), text]) as T;
63+
return Buffer.concat([Buffer.from(prefixedText + '|', 'utf-8'), text]) as T;
6564
}
66-
return (prefixedText + text) as T;
65+
return [prefixedText, text].join('|') as T;
6766
}
6867

6968
/**

modules/sdk-hmac/test/hmac.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ describe('HMAC Utility Functions', () => {
7575
).to.equal(expectedSubject);
7676
});
7777

78+
it('should not include undefined for a response when text is undefined', () => {
79+
const expectedSubject = 'GET|1672531200000|/api/test|200|';
80+
expect(
81+
calculateHMACSubject({
82+
urlPath: '/api/test',
83+
text: undefined as unknown as string,
84+
timestamp: MOCK_TIMESTAMP,
85+
statusCode: 200,
86+
method: 'get',
87+
authVersion: 3,
88+
})
89+
).to.equal(expectedSubject);
90+
});
91+
7892
it('should handle Buffer text input and return a Buffer for requests', () => {
7993
const buffer = Buffer.from('binary-data-content');
8094
const result = calculateHMACSubject({
@@ -96,6 +110,22 @@ describe('HMAC Utility Functions', () => {
96110
expect(result).to.deep.equal(expectedBuffer);
97111
});
98112

113+
it('should handle Buffer undefined text input and return a Buffer for requests', () => {
114+
const buffer = undefined as unknown as Buffer;
115+
const result = calculateHMACSubject({
116+
urlPath: '/api/test',
117+
text: buffer,
118+
timestamp: MOCK_TIMESTAMP,
119+
method: 'get',
120+
authVersion: 3,
121+
});
122+
123+
expect(Buffer.isBuffer(result)).to.be.false;
124+
125+
const expectedSubject = 'GET|1672531200000|3.0|/api/test|';
126+
expect(result).to.deep.equal(expectedSubject);
127+
});
128+
99129
it('should handle Buffer text input and return a Buffer for responses', () => {
100130
const buffer = Buffer.from('binary-response-data');
101131
const result = calculateHMACSubject({

0 commit comments

Comments
 (0)