Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
connect-injector
Advanced tools
A middleware to inject content into any HTTP response.
Install the module with: npm install connect-injector
The basic API looks like this:
var injector = require('connect-injector');
var middleware = injector(function when(req, res) {
// for this request and repsonse
// return whether or not to enable injecting
}, function converter(content, req, res, callback) {
content // the entire response buffer
req // the HTTP request
res // the HTTP response
callback // (error, data) with the injected data
});
And can be used like any other Connect and Express middleware. You need to make sure to use the injector middleware before the actual content is being written.
A very useful example for connect-injector is to add JSONP
support to any application/json
repsonse:
var injector = require('connect-injector');
var inject = injector(function(req, res) {
var isJSON = res.getHeader('content-type').indexOf('application/json') !== -1;
return isJSON && req.query.callback;
}, function(data, req, res, callback) {
callback(null, req.query.callback + '(' + data.toString() + ')');
});
// inject needs to be used before any middleware that writes to the response
connect().use(connect.query()).use(inject).use(/* your other middleware here */);
Now any application/json
response will be wrapped into a callback if given the
callback=xyz
query parameter.
Another use case would be to minify JavaScript files on the fly using UglifyJS:
var injector = require('connect-injector');
var connect = require('connect');
var UglifyJS = require('uglify-js');
// Cache for already uglified files
var cache = {};
// Function that uglifies JavaScript code
var uglify = function (code) {
var toplevel = UglifyJS.parse(code);
toplevel.figure_out_scope();
var compressor = UglifyJS.Compressor({
warnings: false
});
var compressed = toplevel.transform(compressor);
compressed.figure_out_scope();
compressed.compute_char_frequency();
compressed.mangle_names();
return compressed.print_to_string();
};
var inject = injector(function(req, res) {
return res.getHeader('content-type').indexOf('application/javascript') !== -1;
}, function(data, req, res, callback) {
// Check the cache, uglify the code if not and add it
if(!cache[req.url]) {
cache[req.url] = uglify(data.toString());
}
callback(null, cache[req.url]);
});
// inject needs to be used before any middleware that writes to the response
var app = connect().use(inject).use(connect.static(__dirname + '/../test'));
app.listen(8080);
connect-injector is tested to work with http-proxy you can rewrite existing proxied content:
var httpProxy = require('http-proxy');
var connect = require('connect');
var injector = require('connect-injector');
var proxy = httpProxy.createProxyServer();
var inject = injector(function(req, res) {
return res.getHeader('content-type').indexOf('text/html') === 0;
}, function(data, req, res, callback) {
callback(null, data.toString().replace('</body>', '<p>Powered by connect-injector</p></body>'));
});
var proxyMiddleware = function(req, res) {
// You need to rewrite your host in order to be able to hit virtual hosts
req.headers.host = 'daffl.github.io';
proxy.web(req, res, {
target: 'http://daffl.github.io'
});
};
var proxyApp = connect().use(inject).use(proxyMiddleware);
proxyApp.listen(8080);
After starting the server, check http://localhost:8080/connect-injector/dummycontent.html
to see the injected content.
0.4.2
0.4.1
0.4.0
0.3.0
0.2.3
0.2.2
0.2.1
isIntercepted
properly0.2.0
0.1.0
Copyright (c) 2016 David Luecke
Licensed under the MIT license.
v0.4.4 (2016-06-04)
Merged pull requests:
FAQs
A middleware to inject content into any HTTP response.
The npm package connect-injector receives a total of 20,025 weekly downloads. As such, connect-injector popularity was classified as popular.
We found that connect-injector 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.