Skip to content

Commit

Permalink
fix: Incorrect method name in detect-buffer-noassert.
Browse files Browse the repository at this point in the history
Closes #63
Closes #80
  • Loading branch information
nzakas committed Apr 18, 2022
1 parent 78292e0 commit 313c0c6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 51 deletions.
76 changes: 42 additions & 34 deletions rules/detect-buffer-noassert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@

'use strict';

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------

const read = [
'readUInt8',
'readUInt16LE',
'readUInt16BE',
'readUInt32LE',
'readUInt32BE',
'readInt8',
'readInt16LE',
'readInt16BE',
'readInt32LE',
'readInt32BE',
'readFloatLE',
'readFloatBE',
'readDoubleLE',
'readDoubleBE',
];

const write = [
'writeUInt8',
'writeUInt16LE',
'writeUInt16BE',
'writeUInt32LE',
'writeUInt32BE',
'writeInt8',
'writeInt16LE',
'writeInt16BE',
'writeInt32LE',
'writeInt32BE',
'writeFloatLE',
'writeFloatBE',
'writeDoubleLE',
'writeDoubleBE',
];

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -18,42 +56,12 @@ module.exports = {
recommended: true,
url: 'https://github.com/nodesecurity/eslint-plugin-security#detect-buffer-noassert',
},
__methodsToCheck: {
read,
write,
},
},
create: function (context) {
const read = [
'readUInt8',
'readUInt16LE',
'readUInt16BE',
'readUInt32LE',
'readUInt32BE',
'readInt8',
'readInt16LE',
'readInt16BE',
'readInt32LE',
'readInt32BE',
'readFloatLE',
'readFloatBE',
'readDoubleL',
'readDoubleBE',
];

const write = [
'writeUInt8',
'writeUInt16LE',
'writeUInt16BE',
'writeUInt32LE',
'writeUInt32BE',
'writeInt8',
'writeInt16LE',
'writeInt16BE',
'writeInt32LE',
'writeInt32BE',
'writeFloatLE',
'writeFloatBE',
'writeDoubleLE',
'writeDoubleBE',
];

return {
MemberExpression: function (node) {
let index;
Expand Down
35 changes: 18 additions & 17 deletions test/detect-buffer-noassert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester();

const ruleName = 'detect-buffer-noassert';
const Rule = require(`../rules/${ruleName}`);
const rule = require(`../rules/${ruleName}`);

const invalid = 'a.readUInt8(0, true);';
const allMethodNames = [...rule.meta.__methodsToCheck.read, ...rule.meta.__methodsToCheck.write];

tester.run(ruleName, Rule, {
valid: [{ code: 'a.readUInt8(0);' }],
tester.run(ruleName, rule, {
valid: [...allMethodNames.map((methodName) => `a.${methodName}(0)`), ...allMethodNames.map((methodName) => `a.${methodName}(0, false)`)],
invalid: [
{
code: invalid,
errors: [{ message: 'Found Buffer.readUInt8 with noAssert flag set true' }]
}
]
});
...rule.meta.__methodsToCheck.read.map((methodName) => ({
code: `a.${methodName}(0, true)`,
errors: [{ message: `Found Buffer.${methodName} with noAssert flag set true` }],
})),

tester.run(`${ruleName} (false)`, Rule, {
valid: [{ code: 'a.readUInt8(0, false);' }],
invalid: [
...rule.meta.__methodsToCheck.write.map((methodName) => ({
code: `a.${methodName}(0, 0, true)`,
errors: [{ message: `Found Buffer.${methodName} with noAssert flag set true` }],
})),

// hard-coded test to ensure #63 is fixed
{
code: invalid,
errors: [{ message: 'Found Buffer.readUInt8 with noAssert flag set true' }]
}
]
code: 'a.readDoubleLE(0, true);',
errors: [{ message: 'Found Buffer.readDoubleLE with noAssert flag set true' }],
},
],
});

0 comments on commit 313c0c6

Please # to comment.