Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
body-parser-xml
Advanced tools
Adds XML parsing to the body-parser library, so you can convert incoming XML data into a JSON representation.
This is really useful if you want to deal with plain old JavaScript objects, but you need to interface with XML APIs.
npm install express body-parser body-parser-xml
This library adds an xml
method to the body-parser
object.
Initialise like so:
const bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);
Once initialised, you can use it just like any other body-parser
middleware:
const app = require('express')();
app.use(bodyParser.xml());
This will parse any XML-based request and place it as a JavaScript object on req.body
for your route handlers to use.
An XML-based request is determined by the value of the Content-Type
header. By default, any Content-Type
header ending in /xml
or +xml
will be parsed as XML. For example, the following Content-Types will all match:
text/xml
application/xml
application/rss+xml
If you need to match against a custom Content-Type
header, pass in the type
to match as an option (see below).
You can also pass in options:
app.use(bodyParser.xml(options));
The options
object accepts any of the following keys:
Specify the default character set for the text content if the charset is not specified in the Content-Type
header of the request. Defaults to utf-8
.
When set to true
, then deflated (compressed) bodies will be inflated; when false
, deflated bodies are rejected. Defaults to true
.
Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'
.
The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like xml), a mime type (like application/xml), or a mime type with a wildcard (like / or _/xml). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to ['_/xml', '+xml']
.
The verify
option, if supplied, is called as verify(req, res, buf, encoding)
, where buf
is a Buffer
of the raw request body and encoding
is the encoding of the request. The parsing can be aborted by throwing an error.
This option controls the behaviour of the XML parser. You can pass any option that is supported by the xml2js library: see here for a list of these options.
const express = require('express');
const bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);
const app = express();
app.use(
bodyParser.xml({
limit: '1MB', // Reject payload bigger than 1 MB
xmlParseOptions: {
normalize: true, // Trim whitespace inside text nodes
normalizeTags: true, // Transform tags to lowercase
explicitArray: false, // Only put nodes in array if >1
},
}),
);
app.post('/users', function (req, res, body) {
// Any request with an XML payload will be parsed
// and a JavaScript object produced on req.body
// corresponding to the request payload.
console.log(req.body);
res.status(200).end();
});
This library was born out of a frustration that express-xml-bodyparser, the most popular XML-parsing library for express, doesn't support the regular body-parser
options - in particular, limiting the payload size.
This library was written to use body-parser
's text parser under the hood, and then passes the parsed string into the XML parser. We can therefore take advantage of body-parser
's regular options, and support limiting the payload size, amongst other things.
MIT
FAQs
XML parser middleware for express.js.
The npm package body-parser-xml receives a total of 56,627 weekly downloads. As such, body-parser-xml popularity was classified as popular.
We found that body-parser-xml demonstrated a not healthy version release cadence and project activity because the last version was released 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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.