detect-buffer-noassert

Try in Playground
javascript-security

oscar143

SecurityWarning

0

No tags

No CWE or CVE

security/detect-buffer-noassert

Detects calls to buffer with "noAssert" flag set

From the Node.js API docs: "Setting noAssert to true skips validation of the offset. This allows the offset to be beyond the end of the Buffer."

Ast Rule: function call


detect-buffer-noassert

How to write a rule
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',
];

function visit(node, filename, code) {
	let index;
	
	if (read.includes(node?.functionName?.name?.value)) {
		index = 0;
	} else if (write.includes(node?.functionName?.name?.value)) {
		index = 1;
	}
	
	if (index !== "undefined") {
		const argument = node.arguments?.values[index];
		
		if (argument.value.value === "true") {
			addError(buildError(
				node.start.line,
				node.start.col,
				node.end.line,
				node.end.col,
				`Found Buffer with noAssert flag set true`,
				"WARNING",
				"SECURITY",
			));
		}
	}
}

bad.js

Expected test result: has error

a.readUInt8(true)
a.writeFloatLE(0, true)

good.js

Expected test result: no error

a.readUInt8(0)
a.readUInt8(0, false)
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.