Skip to content

Commit 313c0c6

Browse files
committed
fix: Incorrect method name in detect-buffer-noassert.
Closes #63 Closes #80
1 parent 78292e0 commit 313c0c6

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed

Diff for: rules/detect-buffer-noassert.js

+42-34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,44 @@
55

66
'use strict';
77

8+
//-----------------------------------------------------------------------------
9+
// Helpers
10+
//-----------------------------------------------------------------------------
11+
12+
const read = [
13+
'readUInt8',
14+
'readUInt16LE',
15+
'readUInt16BE',
16+
'readUInt32LE',
17+
'readUInt32BE',
18+
'readInt8',
19+
'readInt16LE',
20+
'readInt16BE',
21+
'readInt32LE',
22+
'readInt32BE',
23+
'readFloatLE',
24+
'readFloatBE',
25+
'readDoubleLE',
26+
'readDoubleBE',
27+
];
28+
29+
const write = [
30+
'writeUInt8',
31+
'writeUInt16LE',
32+
'writeUInt16BE',
33+
'writeUInt32LE',
34+
'writeUInt32BE',
35+
'writeInt8',
36+
'writeInt16LE',
37+
'writeInt16BE',
38+
'writeInt32LE',
39+
'writeInt32BE',
40+
'writeFloatLE',
41+
'writeFloatBE',
42+
'writeDoubleLE',
43+
'writeDoubleBE',
44+
];
45+
846
//------------------------------------------------------------------------------
947
// Rule Definition
1048
//------------------------------------------------------------------------------
@@ -18,42 +56,12 @@ module.exports = {
1856
recommended: true,
1957
url: 'https://github.com/nodesecurity/eslint-plugin-security#detect-buffer-noassert',
2058
},
59+
__methodsToCheck: {
60+
read,
61+
write,
62+
},
2163
},
2264
create: function (context) {
23-
const read = [
24-
'readUInt8',
25-
'readUInt16LE',
26-
'readUInt16BE',
27-
'readUInt32LE',
28-
'readUInt32BE',
29-
'readInt8',
30-
'readInt16LE',
31-
'readInt16BE',
32-
'readInt32LE',
33-
'readInt32BE',
34-
'readFloatLE',
35-
'readFloatBE',
36-
'readDoubleL',
37-
'readDoubleBE',
38-
];
39-
40-
const write = [
41-
'writeUInt8',
42-
'writeUInt16LE',
43-
'writeUInt16BE',
44-
'writeUInt32LE',
45-
'writeUInt32BE',
46-
'writeInt8',
47-
'writeInt16LE',
48-
'writeInt16BE',
49-
'writeInt32LE',
50-
'writeInt32BE',
51-
'writeFloatLE',
52-
'writeFloatBE',
53-
'writeDoubleLE',
54-
'writeDoubleBE',
55-
];
56-
5765
return {
5866
MemberExpression: function (node) {
5967
let index;

Diff for: test/detect-buffer-noassert.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ const RuleTester = require('eslint').RuleTester;
44
const tester = new RuleTester();
55

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

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

11-
tester.run(ruleName, Rule, {
12-
valid: [{ code: 'a.readUInt8(0);' }],
11+
tester.run(ruleName, rule, {
12+
valid: [...allMethodNames.map((methodName) => `a.${methodName}(0)`), ...allMethodNames.map((methodName) => `a.${methodName}(0, false)`)],
1313
invalid: [
14-
{
15-
code: invalid,
16-
errors: [{ message: 'Found Buffer.readUInt8 with noAssert flag set true' }]
17-
}
18-
]
19-
});
14+
...rule.meta.__methodsToCheck.read.map((methodName) => ({
15+
code: `a.${methodName}(0, true)`,
16+
errors: [{ message: `Found Buffer.${methodName} with noAssert flag set true` }],
17+
})),
2018

21-
tester.run(`${ruleName} (false)`, Rule, {
22-
valid: [{ code: 'a.readUInt8(0, false);' }],
23-
invalid: [
19+
...rule.meta.__methodsToCheck.write.map((methodName) => ({
20+
code: `a.${methodName}(0, 0, true)`,
21+
errors: [{ message: `Found Buffer.${methodName} with noAssert flag set true` }],
22+
})),
23+
24+
// hard-coded test to ensure #63 is fixed
2425
{
25-
code: invalid,
26-
errors: [{ message: 'Found Buffer.readUInt8 with noAssert flag set true' }]
27-
}
28-
]
26+
code: 'a.readDoubleLE(0, true);',
27+
errors: [{ message: 'Found Buffer.readDoubleLE with noAssert flag set true' }],
28+
},
29+
],
2930
});

0 commit comments

Comments
 (0)