Skip to content

Commit

Permalink
jenkins: whitelist IPs allowed to push status changes
Browse files Browse the repository at this point in the history
This is needed to ensure not everyone on the internet can push an inline
status to any PR if they know the bot URL.
  • Loading branch information
phillipj committed May 6, 2017
1 parent 1705f22 commit a016a67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
The webhook secret that GitHub signs the POSTed payloads with. This is created when the webhook is defined. The default is `hush-hush`.
- **`TRAVIS_CI_TOKEN`**<br>
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
- **`JENKINS_WORKER_IPS`**<br>
List of valid Jenkins slave IPs allowed to push PR status updates, split by comma: `192.168.1.100,192.168.1.101`.
- **`JENKINS_API_CREDENTIALS`** (optional)<br>
For scripts that communicate with Jenkins on http://ci.nodejs.org. The Jenkins API token is visible on
your own profile page `https://ci.nodejs.org/user/<YOUR_GITHUB_USERNAME>/configure`, by clicking the
Expand Down
23 changes: 23 additions & 0 deletions scripts/jenkins-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
const pushJenkinsUpdate = require('../lib/push-jenkins-update')
const enabledRepos = ['citgm', 'node']

const jenkinsIpWhitelist = process.env.JENKINS_WORKER_IPS ? process.env.JENKINS_WORKER_IPS.split(',') : []

function isJenkinsIpWhitelisted (req) {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress

if (jenkinsIpWhitelist.length) {
if (!jenkinsIpWhitelist.includes(ip)) {
req.log.warn({ ip }, 'Ignoring, not allowed to push Jenkins updates')
return false
}
}

return true
}

module.exports = function (app) {
app.post('/:repo/jenkins/start', (req, res) => {
const isValid = pushJenkinsUpdate.validate(req.body)
Expand All @@ -16,6 +31,10 @@ module.exports = function (app) {
return res.status(400).end('Invalid repository')
}

if (!isJenkinsIpWhitelisted(req)) {
return res.status(401).end('Invalid Jenkins IP')
}

pushJenkinsUpdate.pushStarted({
owner: 'nodejs',
repo,
Expand All @@ -37,6 +56,10 @@ module.exports = function (app) {
return res.status(400).end('Invalid repository')
}

if (!isJenkinsIpWhitelisted(req)) {
return res.status(401).end('Invalid Jenkins IP')
}

req.log.debug({ payload: req.body }, 'Jenkins / Github PR end status incoming')

pushJenkinsUpdate.pushEnded({
Expand Down

0 comments on commit a016a67

Please sign in to comment.