Skip to content

Consistent encoding for shared secret #127

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

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import * as Pusher from "pusher"

const pusher = Pusher.forURL(process.env.PUSHER_URL, {
encryptionMasterKeyBase64: Buffer.from(
"01234567890123456789012345678901",
"binary"
"01234567890123456789012345678901"
).toString("base64"),
agent: new Agent({ keepAlive: true }),
})
Expand Down
7 changes: 2 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Config(options) {
)
}

this.encryptionMasterKey = options.encryptionMasterKey
this.encryptionMasterKey = Buffer.from(options.encryptionMasterKey)
}

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

const decodedKey = Buffer.from(
options.encryptionMasterKeyBase64,
"base64"
).toString("binary")
const decodedKey = Buffer.from(options.encryptionMasterKeyBase64, "base64")
if (decodedKey.length !== 32) {
throw new Error(
"encryptionMasterKeyBase64 must decode to 32 bytes, but the string " +
Expand Down
4 changes: 3 additions & 1 deletion lib/pusher.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ Pusher.prototype.createSignedQueryString = function (options) {
Pusher.prototype.channelSharedSecret = function (channel) {
return crypto
.createHash("sha256")
.update(channel + this.config.encryptionMasterKey)
.update(
Buffer.concat([Buffer.from(channel), this.config.encryptionMasterKey])
)
.digest()
}

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/pusher/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ describe("Pusher", function () {
describe("Pusher with encryptionMasterKey", function () {
let pusher

const testMasterKey = "01234567890123456789012345678901"
const testMasterKeyBase64 = "zyrm8pvV2C9fJcBfhyXzvxbJVN/H7QLmbe0xJi1GhPU="

beforeEach(function () {
pusher = new Pusher({
appId: 1234,
key: "f00d",
secret: "tofu",
encryptionMasterKey: testMasterKey,
encryptionMasterKeyBase64: testMasterKeyBase64,
})
})

Expand All @@ -168,7 +168,7 @@ describe("Pusher with encryptionMasterKey", function () {
auth:
"f00d:962c48b78bf93d98ff4c92ee7dff04865821455b7b401e9d60a9e0a90af2c105",
channel_data: '"foo"',
shared_secret: "BYBsePpRCQkGPvbWu/5j8x+MmUF5sgPH5DmNBwkTzYs=",
shared_secret: "nlr49ISQHz91yS3cy/yWmW8wFMNeTnNL5tNHnbPJcLQ=",
})
})
it("should not return a shared_secret for non-encrypted channels", function () {
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/pusher/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe("Pusher", function () {
it("should support `encryptionMasterKey` of 32 bytes", function () {
const key = "01234567890123456789012345678901"
const pusher = new Pusher({ encryptionMasterKey: key })
expect(pusher.config.encryptionMasterKey).to.equal(key)
expect(pusher.config.encryptionMasterKey.toString()).to.equal(key)
})

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

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

it("should reject `encryptionMasterKeyBase64` which decodes to 31 bytes", function () {
const key = "0123456789012345678901234567890"
const keyBase64 = Buffer.from(key, "binary").toString("base64")
const keyBase64 = Buffer.from(key).toString("base64")
expect(function () {
new Pusher({ encryptionMasterKeyBase64: keyBase64 })
}).to.throwException(/31 bytes/)
})

it("should reject `encryptionMasterKeyBase64` which decodes to 33 bytes", function () {
const key = "012345678901234567890123456789012"
const keyBase64 = Buffer.from(key, "binary").toString("base64")
const keyBase64 = Buffer.from(key).toString("base64")
expect(function () {
new Pusher({ encryptionMasterKeyBase64: keyBase64 })
}).to.throwException(/33 bytes/)
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/pusher/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,7 @@ describe("Pusher with encryptionMasterKey", function () {
let pusher

const testMasterKey = Buffer.from(
"01234567890123456789012345678901",
"binary"
"01234567890123456789012345678901"
).toString("base64")

beforeEach(function () {
Expand Down