-
Notifications
You must be signed in to change notification settings - Fork 72
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
Verify Slack webhook tokens #776
base: develop
Are you sure you want to change the base?
Changes from 2 commits
4fdef9a
ab1aece
2d97794
4d312a3
e6d0359
fa0ec79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ export class SlackHookHandler extends BaseSlackHandler { | |
createServer = (cb) => httpsCreate(tlsOptions, cb); | ||
} | ||
return new Promise<void>((resolve, reject) => { | ||
const srv = createServer(this.onRequest.bind(this)); | ||
const srv = createServer(this._onRequest.bind(this)); | ||
srv.once("error", reject); | ||
srv.listen(port, () => { | ||
const protocol = tlsConfig ? "https" : "http"; | ||
|
@@ -85,7 +85,7 @@ export class SlackHookHandler extends BaseSlackHandler { | |
} | ||
} | ||
|
||
private onRequest(req: IncomingMessage, res: ServerResponse) { | ||
public _onRequest(req: IncomingMessage, res: ServerResponse) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems quite cheeky. If you're mocking stuff, why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting around the check seems dirtier to me, to be honest. I won't fight for it though, I'm fine with either. |
||
const HTTP_SERVER_ERROR = 500; | ||
const {method, url } = req; | ||
if (!method || !url) { | ||
|
@@ -234,6 +234,16 @@ export class SlackHookHandler extends BaseSlackHandler { | |
return; | ||
} | ||
|
||
if (params.token !== room.SlackWebhookToken) { | ||
log.warn(`Ignoring message for ${room.MatrixRoomId} due to webhook token mismatch`); | ||
|
||
response.writeHead(HTTP_CODES.FORBIDDEN); | ||
response.end(); | ||
|
||
endTimer({outcome: "dropped"}); | ||
return; | ||
} | ||
|
||
if (method === "POST" && path === "post") { | ||
try { | ||
if (!room) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright 2024 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
import { SlackHookHandler } from "../../src/SlackHookHandler"; | ||
import { FakeMain } from "../utils/fakeMain"; | ||
import { Main } from "../../src/Main"; | ||
import { expect } from "chai"; | ||
import * as httpMocks from "node-mocks-http"; | ||
import * as randomstring from "randomstring"; | ||
import { BridgedRoom } from "../../src/BridgedRoom"; | ||
|
||
const constructHarness = () => { | ||
const main = new FakeMain({ | ||
oauth2: false, | ||
teams: [ | ||
{ | ||
bot_token: "foo", | ||
id: "12345", | ||
name: "FakeTeam", | ||
domain: "fake-domain", | ||
user_id: "foo", | ||
bot_id: "bar", | ||
status: "ok", | ||
scopes: "", | ||
}, | ||
], | ||
}); | ||
const hooks = new SlackHookHandler(main as unknown as Main); | ||
return { hooks, main }; | ||
}; | ||
|
||
const DEFAULT_PAYLOAD = { | ||
team_id: 'T06Q92QGCLC', | ||
team_domain: 'mas', | ||
service_id: '6899401468119', | ||
channel_id: 'C06Q6525S71', | ||
channel_name: 'bridge-testing', | ||
timestamp: '1711628700.919889', | ||
user_id: 'U06QMMZQRH5', | ||
user_name: 'mario', | ||
text: 'incoming!' | ||
}; | ||
|
||
describe("WebhookTest", () => { | ||
let harness: { hooks: SlackHookHandler, main: FakeMain }; | ||
|
||
beforeEach(() => { | ||
harness = constructHarness(); | ||
}); | ||
|
||
async function checkResult(req: httpMocks.MockRequest<any>, expectations: (res: httpMocks.MockResponse<any>) => void): Promise<void> { | ||
const res = httpMocks.createResponse({ eventEmitter: require('events').EventEmitter }); | ||
const promise = new Promise<void>((resolve, reject) => { | ||
res.on('end', () => { | ||
try { | ||
expectations(res); | ||
resolve(); | ||
} catch (err: unknown) { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
|
||
harness.hooks._onRequest(req, res); | ||
|
||
req.emit('end'); | ||
|
||
return promise; | ||
} | ||
|
||
it("will ignore webhooks sent to unknown room", () => { | ||
const req = httpMocks.createRequest({ | ||
method: 'POST', | ||
url: 'http://foo.bar/webhooks/' + randomstring.generate(32), | ||
params: DEFAULT_PAYLOAD, | ||
}); | ||
|
||
return checkResult(req, res => { | ||
expect(res.statusCode).to.equal(200); | ||
}); | ||
}); | ||
|
||
it("will reject webhooks not containing a valid token", () => { | ||
let room = new BridgedRoom(harness.main as unknown as Main, { | ||
matrix_room_id: '!foo:bar.baz', | ||
inbound_id: randomstring.generate(32), | ||
slack_webhook_token: randomstring.generate(24), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about no token at all :). Or null, or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a few more in tadzik@4d312a3?diff=unified&w=1 :) |
||
slack_type: "channel", | ||
}); | ||
harness.main.rooms.upsertRoom(room); | ||
|
||
const req = httpMocks.createRequest({ | ||
method: 'POST', | ||
url: 'http://foo.bar/webhooks/' + room.InboundId, | ||
params: { | ||
token: 'invalid', | ||
...DEFAULT_PAYLOAD, | ||
}, | ||
}); | ||
|
||
return checkResult(req, res => { | ||
expect(res.statusCode).to.equal(403); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge fan of the underscore prefix, smells like snakes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in 2d97794?diff=unified&w=1