Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read "data" from js_body_filter #488

Closed
joaosobreira opened this issue Mar 22, 2022 · 1 comment
Closed

Cannot read "data" from js_body_filter #488

joaosobreira opened this issue Mar 22, 2022 · 1 comment

Comments

@joaosobreira
Copy link

joaosobreira commented Mar 22, 2022

I'm trying to hash the content of a file (sent in chunks) in a response from the frontend.

Log of "data" in js_body_filter show unreadable data that I can't decode as shown below.
nginx

default.conf

server_tokens off;

#frontend.
server {
    listen 443 ssl;
    server_name 132.226.195.22;
    ssl_certificate      /home/zerossl/certificateTRUE.crt; 
    ssl_certificate_key  /home/zerossl/private.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    
    add_header X-Frame-Options "SAMEORIGIN";
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
    add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

    location / {
        js_body_filter main.audit;
        proxy_pass https://138.3.245.53:3000/;
    }
}

nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;

load_module modules/ngx_http_js_module.so;
load_module modules/ngx_stream_js_module.so;


events {
    worker_connections  1024;
}


http {
    js_path "/etc/nginx/njs/";
    js_import main from hash.js;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
}

hash.js

function audit(r, data, flags) {
    var cr = require('crypto');
    var hash = cr.createHash('md5');
    let response = '';
    let responseHash = '';
    let finalHash = '';
    const refHash = '8978cf8669edb983c3bad1973fce0155';

    if (r.uri == "/static/js/main.chunk.js") {
        r.log(data)
        if (flags.last) {
            let finalHash = hash.update(responseHash).digest('hex');
            r.log(response)
            if (finalHash == refHash) {
                r.log(response)
                r.return(502);
            }
        }
    }

    r.sendBuffer(data, flags);
}

export default { audit }
@xeioex
Copy link
Contributor

xeioex commented Apr 6, 2022

Hi @joaosobreira,

Most probably you are seeing a compressed response from your backend.
I suggest to play around with Accept-encoding header and proxy_set_directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants