Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
als-body-parser
Advanced tools
A flexible, efficient body parsing middleware for Node.js, supporting JSON, URL-encoded, and plain text, with customizable options for request handling.
als-body-parser
is a flexible middleware for parsing incoming request bodies in a Node.js environment, making it easy to integrate with frameworks like Express or with the native HTTP server. It adds a req.body
property to the incoming request object, which can be a JSON object, URL-encoded string, or plain text, depending on the Content-Type of the request.
Install using npm:
npm install als-body-parser
const http = require('http');
const bodyParser = require('als-body-parser')({});
// Create HTTP server and use bodyParser middleware
http.createServer((req, res) => {
bodyParser(req, res, () => {
if (req.body) {
// Send parsed body back as JSON
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(req.body));
} else {
// Handle cases where no body could be parsed
res.writeHead(400);
res.end('No body parsed');
}
});
}).listen(3000);
const express = require('express');
const bodyParser = require('als-body-parser')({});
const app = express();
app.use(bodyParser);
app.post('/data', (req, res) => {
res.json(req.body);
});
app.listen(3000);
als-body-parser
can be customized with several options:
supportedMethods
: Array of HTTP methods to parse (default: ['POST', 'PUT', 'PATCH']
).supportedCt
: Array of supported content types (default: ['application/x-www-form-urlencoded', 'application/json', 'text/plain']
).limit
: Maximum allowed size of the request body in bytes (default: 1048576
).timeout
: Timeout in milliseconds for receiving the request body (default: 5000
).logger
: Function to log errors (default: console.log
).httpErrorHandler
: Function to handle HTTP errors, expects (res, status, message)
(default handles by setting response headers and ending the request).const bodyParser = require('als-body-parser')({
supportedMethods: ['POST'],
supportedCt: ['application/json']
});
app.use(bodyParser);
const bodyParser = require('als-body-parser')({
httpErrorHandler: (res, status, message) => {
res.status(status).send({ error: message });
},
logger: (error) => customLogger.error(error)
});
app.use(bodyParser);
The middleware can terminate the request with the following status codes under certain conditions:
413
(Content Too Large): If the body exceeds the specified limit
.400
(Bad Request): If the body cannot be parsed correctly (e.g., malformed JSON).408
(Request Timeout): If the full body is not received within the specified timeout
.Depending on the Content-Type
, req.body
may be:
FAQs
A flexible, efficient body parsing middleware for Node.js, supporting JSON, URL-encoded, and plain text, with customizable options for request handling.
We found that als-body-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.