Skip to content

Commit e5276ee

Browse files
Tweak some things
1 parent 1adf207 commit e5276ee

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ receivers:
4040
- url: 'https://my-matrix-alertmanager.tld/alerts'
4141
http_config:
4242
authorization:
43+
type: Bearer
4344
credentials: 'veryverysecretkeyhere'
4445
```
4546

src/routes.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
const client = require('./client')
22
const utils = require('./utils')
33

4+
const crypto = require('crypto')
5+
6+
const passwordsEqual = (a, b) => {
7+
return a && b && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
8+
}
9+
410
const routes = {
511
getRoot: (req, res) => {
612
res.send('Hey 👋')
713
},
814
postAlerts: async (req, res) => {
9-
let authorized = false;
10-
let expectedSecret = process.env.APP_ALERTMANAGER_SECRET;
15+
let authorized = false
16+
let expectedSecret = process.env.APP_ALERTMANAGER_SECRET
17+
18+
if (!expectedSecret) {
19+
console.error("APP_ALERTMANAGER_SECRET is not configured, unable to authenticate requests")
20+
res.status(500).end()
21+
return
22+
}
1123

12-
if (req.query.secret === expectedSecret) {
13-
authorized = true;
24+
if (passwordsEqual(req.query.secret, expectedSecret)) {
25+
authorized = true
1426
}
1527

16-
if (req.get('authorization') === `Bearer ${expectedSecret}`) {
17-
authorized = true;
28+
if (passwordsEqual(req.get('authorization'), `Bearer ${expectedSecret}`)) {
29+
authorized = true
1830
}
1931

2032
if (!authorized) {

0 commit comments

Comments
 (0)