Skip to content

Commit cbc986d

Browse files
committed
jenkins: whitelist IPs allowed to push status changes
This is needed to ensure not everyone on the internet can push an inline status to any PR if they know the bot URL.
1 parent 1705f22 commit cbc986d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
1818
The webhook secret that GitHub signs the POSTed payloads with. This is created when the webhook is defined. The default is `hush-hush`.
1919
- **`TRAVIS_CI_TOKEN`**<br>
2020
For scripts that communicate with Travis CI. Your Travis token is visible on [yourprofile](https://travis-ci.org/profile) page, by clicking the "show token" link. Also See: https://blog.travis-ci.com/2013-01-28-token-token-token
21+
- **`JENKINS_SLAVE_IPS`**<br>
22+
List of valid Jenkins slave IPs allowed to push PR status updates, split by comma: `192.168.1.100,192.168.1.101`.
2123
- **`JENKINS_API_CREDENTIALS`** (optional)<br>
2224
For scripts that communicate with Jenkins on http://ci.nodejs.org. The Jenkins API token is visible on
2325
your own profile page `https://ci.nodejs.org/user/<YOUR_GITHUB_USERNAME>/configure`, by clicking the

scripts/jenkins-status.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
const pushJenkinsUpdate = require('../lib/push-jenkins-update')
44
const enabledRepos = ['citgm', 'node']
55

6+
const jenkinsIpWhiteliste = process.env.JENKINS_SLAVE_IPS ? process.env.JENKINS_SLAVE_IPS.split(',') : []
7+
8+
function isJenkinsIpWhitelisted (req) {
9+
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
10+
11+
if (jenkinsIpWhiteliste.length) {
12+
if (!jenkinsIpWhiteliste.includes(ip)) {
13+
req.log.warn({ ip }, 'Ignoring, not allowed to push Jenkins updates')
14+
return false
15+
}
16+
}
17+
18+
return true
19+
}
20+
621
module.exports = function (app) {
722
app.post('/:repo/jenkins/start', (req, res) => {
823
const isValid = pushJenkinsUpdate.validate(req.body)
@@ -16,6 +31,10 @@ module.exports = function (app) {
1631
return res.status(400).end('Invalid repository')
1732
}
1833

34+
if (!isJenkinsIpWhitelisted(req)) {
35+
return res.status(401).end('Invalid Jenkins IP')
36+
}
37+
1938
pushJenkinsUpdate.pushStarted({
2039
owner: 'nodejs',
2140
repo,
@@ -37,6 +56,10 @@ module.exports = function (app) {
3756
return res.status(400).end('Invalid repository')
3857
}
3958

59+
if (!isJenkinsIpWhitelisted(req)) {
60+
return res.status(401).end('Invalid Jenkins IP')
61+
}
62+
4063
req.log.debug({ payload: req.body }, 'Jenkins / Github PR end status incoming')
4164

4265
pushJenkinsUpdate.pushEnded({

0 commit comments

Comments
 (0)