Skip to content

Commit 08ee090

Browse files
author
Jon Elverkilde
authored
Merge pull request pusher#127 from pusher/consistent-encoding-for-shared-secret
Consistent encoding for shared secret
2 parents 93248ce + 7a639b7 commit 08ee090

File tree

6 files changed

+15
-18
lines changed

6 files changed

+15
-18
lines changed

examples/typescript/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import * as Pusher from "pusher"
44

55
const pusher = Pusher.forURL(process.env.PUSHER_URL, {
66
encryptionMasterKeyBase64: Buffer.from(
7-
"01234567890123456789012345678901",
8-
"binary"
7+
"01234567890123456789012345678901"
98
).toString("base64"),
109
agent: new Agent({ keepAlive: true }),
1110
})

lib/config.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function Config(options) {
4949
)
5050
}
5151

52-
this.encryptionMasterKey = options.encryptionMasterKey
52+
this.encryptionMasterKey = Buffer.from(options.encryptionMasterKey)
5353
}
5454

5555
// Handle base64 encoded 32 byte key to encourage use of the full range of byte values
@@ -61,10 +61,7 @@ function Config(options) {
6161
throw new Error("encryptionMasterKeyBase64 must be valid base64")
6262
}
6363

64-
const decodedKey = Buffer.from(
65-
options.encryptionMasterKeyBase64,
66-
"base64"
67-
).toString("binary")
64+
const decodedKey = Buffer.from(options.encryptionMasterKeyBase64, "base64")
6865
if (decodedKey.length !== 32) {
6966
throw new Error(
7067
"encryptionMasterKeyBase64 must decode to 32 bytes, but the string " +

lib/pusher.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ Pusher.prototype.createSignedQueryString = function (options) {
234234
Pusher.prototype.channelSharedSecret = function (channel) {
235235
return crypto
236236
.createHash("sha256")
237-
.update(channel + this.config.encryptionMasterKey)
237+
.update(
238+
Buffer.concat([Buffer.from(channel), this.config.encryptionMasterKey])
239+
)
238240
.digest()
239241
}
240242

tests/integration/pusher/authenticate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ describe("Pusher", function () {
149149
describe("Pusher with encryptionMasterKey", function () {
150150
let pusher
151151

152-
const testMasterKey = "01234567890123456789012345678901"
152+
const testMasterKeyBase64 = "zyrm8pvV2C9fJcBfhyXzvxbJVN/H7QLmbe0xJi1GhPU="
153153

154154
beforeEach(function () {
155155
pusher = new Pusher({
156156
appId: 1234,
157157
key: "f00d",
158158
secret: "tofu",
159-
encryptionMasterKey: testMasterKey,
159+
encryptionMasterKeyBase64: testMasterKeyBase64,
160160
})
161161
})
162162

@@ -168,7 +168,7 @@ describe("Pusher with encryptionMasterKey", function () {
168168
auth:
169169
"f00d:962c48b78bf93d98ff4c92ee7dff04865821455b7b401e9d60a9e0a90af2c105",
170170
channel_data: '"foo"',
171-
shared_secret: "BYBsePpRCQkGPvbWu/5j8x+MmUF5sgPH5DmNBwkTzYs=",
171+
shared_secret: "nlr49ISQHz91yS3cy/yWmW8wFMNeTnNL5tNHnbPJcLQ=",
172172
})
173173
})
174174
it("should not return a shared_secret for non-encrypted channels", function () {

tests/integration/pusher/constructor.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("Pusher", function () {
102102
it("should support `encryptionMasterKey` of 32 bytes", function () {
103103
const key = "01234567890123456789012345678901"
104104
const pusher = new Pusher({ encryptionMasterKey: key })
105-
expect(pusher.config.encryptionMasterKey).to.equal(key)
105+
expect(pusher.config.encryptionMasterKey.toString()).to.equal(key)
106106
})
107107

108108
it("should reject `encryptionMasterKey` of 31 bytes", function () {
@@ -121,22 +121,22 @@ describe("Pusher", function () {
121121

122122
it("should support `encryptionMasterKeyBase64` which decodes to 32 bytes", function () {
123123
const key = "01234567890123456789012345678901"
124-
const keyBase64 = Buffer.from(key, "binary").toString("base64")
124+
const keyBase64 = Buffer.from(key).toString("base64")
125125
const pusher = new Pusher({ encryptionMasterKeyBase64: keyBase64 })
126-
expect(pusher.config.encryptionMasterKey).to.equal(key)
126+
expect(pusher.config.encryptionMasterKey.toString()).to.equal(key)
127127
})
128128

129129
it("should reject `encryptionMasterKeyBase64` which decodes to 31 bytes", function () {
130130
const key = "0123456789012345678901234567890"
131-
const keyBase64 = Buffer.from(key, "binary").toString("base64")
131+
const keyBase64 = Buffer.from(key).toString("base64")
132132
expect(function () {
133133
new Pusher({ encryptionMasterKeyBase64: keyBase64 })
134134
}).to.throwException(/31 bytes/)
135135
})
136136

137137
it("should reject `encryptionMasterKeyBase64` which decodes to 33 bytes", function () {
138138
const key = "012345678901234567890123456789012"
139-
const keyBase64 = Buffer.from(key, "binary").toString("base64")
139+
const keyBase64 = Buffer.from(key).toString("base64")
140140
expect(function () {
141141
new Pusher({ encryptionMasterKeyBase64: keyBase64 })
142142
}).to.throwException(/33 bytes/)

tests/integration/pusher/trigger.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ describe("Pusher with encryptionMasterKey", function () {
447447
let pusher
448448

449449
const testMasterKey = Buffer.from(
450-
"01234567890123456789012345678901",
451-
"binary"
450+
"01234567890123456789012345678901"
452451
).toString("base64")
453452

454453
beforeEach(function () {

0 commit comments

Comments
 (0)