FG
📡 Networking

allow ressource filters/callbacks

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score54%
54%

Problem

as discussed on twitter, it would be very interesting to be able to hack proxied ressources before sending them back to the client. something like ressource filtering where we'll be able to change the response body/headers wonder how to integrate this into node http proxy

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Resource Filtering in Node HTTP Proxy

Medium Risk

The current Node HTTP proxy implementation does not allow for modification of response bodies or headers before they are sent to the client. This limitation prevents developers from applying custom logic to responses, such as filtering or transforming data based on specific criteria.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Install Required Packages

    Ensure you have the necessary packages installed for HTTP proxying and response manipulation.

    bash
    npm install http-proxy
  2. 2

    Set Up Basic Proxy Server

    Create a basic HTTP proxy server using the http-proxy package to handle incoming requests.

    javascript
    const http = require('http');
    const httpProxy = require('http-proxy');
    
    const proxy = httpProxy.createProxyServer({});
    
    const server = http.createServer((req, res) => {
      proxy.web(req, res, { target: 'http://example.com' });
    });
    
    server.listen(3000);
  3. 3

    Add Response Filtering Logic

    Implement a response filter that allows modification of the response body and headers before sending it back to the client. Use the 'proxy.on('proxyRes')' event to achieve this.

    javascript
    proxy.on('proxyRes', (proxyRes, req, res) => {
      let body = '';
      proxyRes.on('data', (chunk) => {
        body += chunk;
      });
      proxyRes.on('end', () => {
        // Modify response body
        body = body.replace(/example/g, 'modified');
        res.writeHead(proxyRes.statusCode, proxyRes.headers);
        res.end(body);
      });
    });
  4. 4

    Test the Proxy with Resource Filtering

    Send a request to the proxy server and verify that the response body has been modified as expected. Use tools like Postman or curl to test the endpoint.

    bash
    curl http://localhost:3000/some-endpoint

Validation

Confirm that the response from the proxy server reflects the modifications made in the filtering logic. Check that the original response body contains the term 'example' and that it has been replaced with 'modified'.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.jsfeature-request