Skip to content

Commit

Permalink
feat: validate ignoredBy.email
Browse files Browse the repository at this point in the history
  • Loading branch information
joshje committed Oct 24, 2017
1 parent 6f06ccd commit 84a22f2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
11 changes: 11 additions & 0 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = add;

var debug = require('debug')('snyk:policy');
var emailValidator = require('email-validator');

var validReasonTypes = ['not-vulnerable', 'wont-fix', 'temporary-ignore'];

Expand All @@ -25,6 +26,16 @@ function add(policy, type, options) {
throw new Error('invalid reasonType ' + options[curr]);
}

if (curr === 'ignoredBy') {
if (typeof options[curr] !== 'object') {
throw new Error('ignoredBy must be an object');
}

if (!emailValidator.validate(options[curr].email)) {
throw new Error('ignoredBy.email must be a valid email address');
}
}

acc[curr] = options[curr];
return acc;
}, {});
Expand Down
4 changes: 1 addition & 3 deletions lib/parser/demunge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ function demunge(policy, apiRoot) {
res.reason = pathObj[path].reason;
res.expires =
pathObj[path].expires && new Date(pathObj[path].expires);
if (pathObj[path].disregardIfFixable) {
res.disregardIfFixable = pathObj[path].disregardIfFixable;
}
res.disregardIfFixable = pathObj[path].disregardIfFixable;
}

return res;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"dependencies": {
"debug": "^2.2.0",
"email-validator": "^1.1.1",
"es6-promise": "^3.1.2",
"js-yaml": "^3.5.3",
"lodash.clonedeep": "^4.3.1",
Expand Down
48 changes: 45 additions & 3 deletions test/unit/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ test('add ignore with valid reasonType', function (t) {
})
.then(function (policy) {
t.ok('error not thrown');
t.deepEqual(Object.keys(policy.ignore), ['a'], '`a` is the only root');
t.deepEqual(policy.ignore.a[0]['a > b'].reasonType, 'wont-fix',
'metadata saved');
})
Expand All @@ -63,14 +62,57 @@ test('add ignore with invalid reasonType', function (t) {
return policy.addIgnore({
id: 'a',
path: 'a > b',
reasonType: 'invalid',
reasonType: 'test',
});
})
.then(function () {
t.fail('error not thrown');
})
.catch(function (err) {
t.equal(err.message, 'invalid reasonType test',
'error is thrown');
});
});

test('add ignore with valid ignoredBy', function (t) {
var ignoredBy = {
name: 'Joe Bloggs',
email: 'joe@acme.org',
};
return create().then(function (policy) {
return policy.addIgnore({
id: 'a',
path: 'a > b',
ignoredBy: ignoredBy,
});
})
.then(function (policy) {
t.ok('error not thrown');
t.deepEqual(policy.ignore.a[0]['a > b'].ignoredBy, ignoredBy,
'metadata saved');
})
.catch(function () {
t.ok('error is thrown');
t.fail('error thrown thrown');
});
});

test('add ignore with invalid ignoredBy', function (t) {
var ignoredBy = {
name: 'Joe Bloggs',
email: 'joeacme.org',
};
return create().then(function (policy) {
return policy.addIgnore({
id: 'a',
path: 'a > b',
ignoredBy: ignoredBy,
});
})
.then(function () {
t.fail('error not thrown');
})
.catch(function (err) {
t.equal(err.message, 'ignoredBy.email must be a valid email address',
'error is thrown');
});
});

0 comments on commit 84a22f2

Please sign in to comment.