Skip to content

Commit 9c72402

Browse files
committed
Validate POST payloads from Jenkins (nodejs#40)
1 parent b59205c commit 9c72402

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎lib/push-jenkins-update.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const githubClient = require('./github-client')
44

5-
function pushJenkinsUpdate (options, build) {
5+
function push (options, build) {
66
const statusOpts = extendWithBuildData(options, build)
77
const createGhStatus = createGhStatusFn(statusOpts)
88

@@ -43,4 +43,10 @@ function prInfoStr (options) {
4343
return `${options.owner}/${options.repoName}/${shortSha}`
4444
}
4545

46-
module.exports = pushJenkinsUpdate
46+
function validate (payload) {
47+
const isString = (param) => typeof (payload[param]) === 'string'
48+
return ['identifier', 'status', 'message', 'commit', 'url'].every(isString)
49+
}
50+
51+
exports.validate = validate
52+
exports.push = push

‎scripts/node-jenkins-status.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const pushJenkinsUpdate = require('../lib/push-jenkins-update')
44

55
module.exports = function (app) {
66
app.post('/node/jenkins', (req, res) => {
7-
pushJenkinsUpdate({
7+
const isValid = pushJenkinsUpdate.validate(req.body)
8+
9+
if (!isValid) {
10+
return res.status(400).end('Invalid payload')
11+
}
12+
13+
pushJenkinsUpdate.push({
814
owner: 'nodejs',
915
repoName: 'node'
1016
}, req.body)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"identifier": "test/osx",
3+
"status": "pending",
4+
"url": "https://ci.nodejs.org/job/node-test-commit-osx/3157/"
5+
}

‎test/push-jenkins-update.test.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ tap.test('Forwards payload provided in incoming POST to GitHub status API', (t)
5252
})
5353
})
5454

55+
tap.test('Responds with 400 / "Bad request" when incoming request has invalid payload', (t) => {
56+
const fixture = readFixture('invalid-payload.json')
57+
58+
// don't care about the results, just want to prevent any HTTP request ever being made
59+
nock('https://api.github.com')
60+
61+
t.plan(1)
62+
63+
supertest(app)
64+
.post('/node/jenkins')
65+
.send(fixture)
66+
.expect(400, 'Invalid payload')
67+
.end((err, res) => {
68+
t.equal(err, null)
69+
})
70+
})
71+
5572
function ignoreQueryParams (pathAndQuery) {
5673
return url.parse(pathAndQuery, true).pathname
5774
}

0 commit comments

Comments
 (0)