Http-proxy-middleware: Edit proxy request/response POST parameters

Created on 4 Apr 2016  路  13Comments  路  Source: chimurai/http-proxy-middleware

Hello Chimurai,

I was hoping you might have some suggestions on how to edit the POST parameters prior to submitting the request to the target and redacting/removing sensitive parameters in the response prior to forwarding to the client? I can find a number of options to trap the body content using modules like getRawBody or using the req.on('data',..) to capture the form parameters but I can't find anything that would allow for the manipulation of the rawBody for both the request or response.

Hoping you might be able to shed some light on this problem?

Respectfully,

Warren.

question

Most helpful comment

Ok Here's the final solution.

The following example allows you to inject POST parameters prior to forwarding to the proxy target. You don't have scrub any parameters from the proxy target response - as far as I can tell - because it maintains a copy of the original POST request.

Example Node Express Route File:

'use strict';

var express = require('express');
var router = express.Router();

var proxy_filter = function (path, req) {
    return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
};

var proxy_options = {
    target: 'http://localhost:8080',
    pathRewrite: {
        '^/docs' : '/java/rep/server1' // Host path & target path conversion
    },
    onError(err, req, res) {
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('Something went wrong. And we are reporting a custom error message.' + err);
    },
    onProxyReq(proxyReq, req, res) {
        if ( req.method == "POST" && req.body ) {
            // Add req.body logic here if needed....

           // ....

            // Remove body-parser body object from the request
            if ( req.body ) delete req.body;

            // Make any needed POST parameter changes
            let body = new Object();

            body.filename = 'reports/statistics/summary_2016.pdf';
            body.routeid = 's003b012d002';
            body.authid = 'bac02c1d-258a-4177-9da6-862580154960';

            // URI encode JSON object
            body = Object.keys( body ).map(function( key ) {
                return encodeURIComponent( key ) + '=' + encodeURIComponent( body[ key ])
            }).join('&');

            // Update header
            proxyReq.setHeader( 'content-type', 'application/x-www-form-urlencoded' );
            proxyReq.setHeader( 'content-length', body.length );

            // Write out body changes to the proxyReq stream
            proxyReq.write( body );
            proxyReq.end();
        }
    }
};

// Proxy configuration
var proxy = require( 'http-proxy-middleware' )( proxy_filter, proxy_options );

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Node.js Express Proxy Test' });
});

router.all('/document', proxy );

module.exports = router;

All 13 comments

I should have added that I need to be able to inject sensitive routing parameters that need to be kept internal to the organization and will need to scrub this information from the POST data on the response.

Unfortunately I can't find a clean way to do this and was hoping you could point me in the right direction.

Many Thanks,

Warren.

Hi @flackenstein

Great question. I can definitely see some interesting use-cases for this.
Too bad I don't have the answer...

Could you post this question on StackOverflow with a trackback url to this thread; In case an answer pops up over there or here.

Will do - thanks for the quick reply.

Cheers.

Please post a link of the StackOverflow question. Thanks

Here's the link on Stack Overflow - http://stackoverflow.com/questions/36416129/edit-post-parameters-prior-to-forwarding-to-a-proxy-target-and-sending-response

So far no response, but I am making progress on my own. Currently looking into re-piping the express/http request readable stream so I can make changes to the raw body before passing on to the proxy.

On a side note - I was able to get http-proxy-middleware to work with the body-parser module by setting up onProxyReq with the following format:

    onProxyReq(proxyReq, req, res) {
            if ( req.method == "POST" && req.body ) {
                let body = req.body;

                // URI encode JSON object
                body = Object.keys( body ).map(function( key ) {
                    return encodeURIComponent( key ) + '=' + encodeURIComponent( body[ key ])
                }).join('&');

                proxyReq.write( body );
                proxyReq.end();
            }
    }

Because the POST data from the form has the context-type of application/x-www-form-urlencoded it needed to be converted back prior to forwarding the request.

I have a solution worked out for the request - and as soon as I have the response side wrapped up I'll send out the complete solution.

Thanks

Ok Here's the final solution.

The following example allows you to inject POST parameters prior to forwarding to the proxy target. You don't have scrub any parameters from the proxy target response - as far as I can tell - because it maintains a copy of the original POST request.

Example Node Express Route File:

'use strict';

var express = require('express');
var router = express.Router();

var proxy_filter = function (path, req) {
    return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
};

var proxy_options = {
    target: 'http://localhost:8080',
    pathRewrite: {
        '^/docs' : '/java/rep/server1' // Host path & target path conversion
    },
    onError(err, req, res) {
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('Something went wrong. And we are reporting a custom error message.' + err);
    },
    onProxyReq(proxyReq, req, res) {
        if ( req.method == "POST" && req.body ) {
            // Add req.body logic here if needed....

           // ....

            // Remove body-parser body object from the request
            if ( req.body ) delete req.body;

            // Make any needed POST parameter changes
            let body = new Object();

            body.filename = 'reports/statistics/summary_2016.pdf';
            body.routeid = 's003b012d002';
            body.authid = 'bac02c1d-258a-4177-9da6-862580154960';

            // URI encode JSON object
            body = Object.keys( body ).map(function( key ) {
                return encodeURIComponent( key ) + '=' + encodeURIComponent( body[ key ])
            }).join('&');

            // Update header
            proxyReq.setHeader( 'content-type', 'application/x-www-form-urlencoded' );
            proxyReq.setHeader( 'content-length', body.length );

            // Write out body changes to the proxyReq stream
            proxyReq.write( body );
            proxyReq.end();
        }
    }
};

// Proxy configuration
var proxy = require( 'http-proxy-middleware' )( proxy_filter, proxy_options );

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Node.js Express Proxy Test' });
});

router.all('/document', proxy );

module.exports = router;

Awesome! Thanks for sharing your solution!

Could you create a PR with your final example and add it to the http-proxy-middleware recipes?

Think it would be great addition.

You can create a new markdown file like recipes/modify-post.md or something to hold the example.

Thanks chimural - recipe/modify-post.md has been added.

Cheers.

Thanks!
But I don't see any commit in your branch yet. Did you git commit and git push the recipe?
After that you should be able to send a PR so it can be merged.

Sorry bout that - I forgot to submit the pull request. Thanks again for the great proxy module.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pwlerke picture pwlerke  路  6Comments

DylanPiercey picture DylanPiercey  路  6Comments

georgyfarniev picture georgyfarniev  路  7Comments

joshiste picture joshiste  路  4Comments

minhduchcm picture minhduchcm  路  7Comments