Skip to content

Commit 7dcdd27

Browse files
authored
fix: verifyNotificationSignature timestamps are in seconds (#515)
1 parent 7e6dbe7 commit 7dcdd27

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

lib-es5/utils/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ function webhook_signature(data, timestamp) {
11251125
* Verifies the authenticity of a notification signature
11261126
*
11271127
* @param {string} body JSON of the request's body
1128-
* @param {number} timestamp Unix timestamp. Can be retrieved from the X-Cld-Timestamp header
1128+
* @param {number} timestamp Unix timestamp in seconds. Can be retrieved from the X-Cld-Timestamp header
11291129
* @param {string} signature Actual signature. Can be retrieved from the X-Cld-Signature header
11301130
* @param {number} [valid_for=7200] The desired time in seconds for considering the request valid
11311131
*
@@ -1135,7 +1135,7 @@ function verifyNotificationSignature(body, timestamp, signature) {
11351135
var valid_for = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 7200;
11361136

11371137
// verify that signature is valid for the given timestamp
1138-
if (timestamp < Date.now() - valid_for) {
1138+
if (timestamp < Math.round(Date.now() / 1000) - valid_for) {
11391139
return false;
11401140
}
11411141
var payload_hash = utils.webhook_signature(body, timestamp, {

lib/utils/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,15 +1036,15 @@ function webhook_signature(data, timestamp, options = {}) {
10361036
* Verifies the authenticity of a notification signature
10371037
*
10381038
* @param {string} body JSON of the request's body
1039-
* @param {number} timestamp Unix timestamp. Can be retrieved from the X-Cld-Timestamp header
1039+
* @param {number} timestamp Unix timestamp in seconds. Can be retrieved from the X-Cld-Timestamp header
10401040
* @param {string} signature Actual signature. Can be retrieved from the X-Cld-Signature header
10411041
* @param {number} [valid_for=7200] The desired time in seconds for considering the request valid
10421042
*
10431043
* @return {boolean}
10441044
*/
10451045
function verifyNotificationSignature(body, timestamp, signature, valid_for = 7200) {
10461046
// verify that signature is valid for the given timestamp
1047-
if (timestamp < Date.now() - valid_for) {
1047+
if (timestamp < Math.round(Date.now() / 1000) - valid_for) {
10481048
return false;
10491049
}
10501050
const payload_hash = utils.webhook_signature(body, timestamp, {

test/utils/utils_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,8 +1459,8 @@ describe("utils", function () {
14591459
'width': 100,
14601460
'height': 100
14611461
};
1462-
valid_response_timestamp = Date.now() - 5000;
1463-
invalid_response_timestamp = Date.now() - 50 * 1000;
1462+
valid_response_timestamp = (Date.now()/1000) - 5000;
1463+
invalid_response_timestamp = (Date.now()/1000) - 10000;
14641464
response_json = JSON.stringify(expected_parameters);
14651465
unexpected_response_json = JSON.stringify(unexpected_parameters);
14661466
});

0 commit comments

Comments
 (0)