Njs: r.variables.request_body_file is empty

Created on 21 Oct 2020  Â·  3Comments  Â·  Source: nginx/njs

nginx -v
nginx version: nginx/1.18.0
njs -v
0.4.4

I have a case where the body request is too big and therefore is saved in the file. With the njs I am trying to read the request_body_file

As I understood, we can access to nginx variables with r.variables.request_body_file but on my case this is empty.

answered question

Most helpful comment

@yvmarques

As I understood, we can access to nginx variables with r.variables.request_body_file but on my case this is empty.

No, it's not empty.

But, it doesn't seem to work properly yet.

$ curl -X POST -d @../../Downloads/.directory 'http://localhost:10000/discard'
body length: 56, body in file: false
$ curl -X POST -d @../../Downloads/lord_of_the_rings_3_974.rar 'http://localhost:10000/discard'
bad file: /var/cache/nginx/client_temp/0000000003
Error: No such file or directory
    at fs.readFileSync (native)
    at discard (recaptcha.js:13)

$ curl -X POST -d @../../Downloads/.directory 'http://localhost:10000/discard1'
body length: 56, body in file: true
$ curl -X POST -d @../../Downloads/lord_of_the_rings_3_974.rar 'http://localhost:10000/discard1'
body length: 274146, body in file: true
$

```nginx
location /discard {
js_content test.discard;
}

location /discard1 {
    client_body_in_file_only clean;
    js_content test.discard;
}
```js
function discard(r) {
    var in_file = false;
    try {
        var body = r.requestBody || '';

    } catch (e) {
        r.log(e);
        var file = r.variables.request_body_file;

        try {
            body = require('fs').readFileSync(file);
            in_file = true;

        } catch (e) {
            r.log(e);
            return r.return(500, `bad file: ${file}\n${njs.dump(e)}\n`);
        }
    }

    return r.return(200, `body length: ${body.length}, body in file: ${in_file}\n`);
}

All 3 comments

@yvmarques

please share your code, request_body_file may be empty if it is accessed in early processing phases (when request body was not read yet).

Hello @xeioex ,

Here is my server config

server {
    listen 443 ssl;
    resolver 1.1.1.1 ipv6=off;

    server_name domain.tld;

    root index;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    proxy_redirect off;
    proxy_buffer_size          128k;
    proxy_buffers              4 256k;
    proxy_busy_buffers_size    256k;
    subrequest_output_buffer_size 1M;

    location = /check {
        js_content http.check;
    }

    location = /sandbox {
        proxy_pass https://domain.tld/sandbox;
    }

    location = /prod {
        proxy_pass https://domain.tld/production;
    }

    location / {
        deny all;
    }
}

And the njs script http.js

function check(r) {
    try {
        var body = JSON.parse(r.requestBody);   
    } catch (error) {
        var fs = require('fs');
        var body = JSON.parse(fs.readFileSync(r.variables.request_body_file));
    }

    if (!body) {
        r.error("Unable to parse the body");
        r.return(400);
    }

    if (!body['data']) {
        r.warn("Missing parameters for the request");
        r.return(400, "Missing parameters");
    }
    r.headersOut['Content-Type'] = 'application/json';

    r.subrequest("/prod", {"method": "POST"})
        .then(reply => JSON.parse(reply.responseBody))
        .then(response => {
            if (response.status !== true) {
                r.log("production succeeded");
                r.return(200, JSON.stringify(response));
                return;
            }
            r.log("production verification failed — trying sandbox");
            r.subrequest("/sandbox", {"method": "POST"})
                .then(reply => { r.return(200, reply.responseBody); return; })
                .catch(_ => r.return(500, "Sandbox"));
        })
        .catch(_ => r.return(500, "Prod"));

}

export default {check};

And the nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;
load_module modules/ngx_http_js_module.so;

events {
    worker_connections 768;
}

http {

    ##
    # Basic Settings
    ##

    client_header_timeout 3000;
    client_body_timeout 3000;
    fastcgi_read_timeout 3000;
    client_max_body_size 32m;
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 128k;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

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

    ##
    # Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    js_import http.js;
}

@yvmarques

As I understood, we can access to nginx variables with r.variables.request_body_file but on my case this is empty.

No, it's not empty.

But, it doesn't seem to work properly yet.

$ curl -X POST -d @../../Downloads/.directory 'http://localhost:10000/discard'
body length: 56, body in file: false
$ curl -X POST -d @../../Downloads/lord_of_the_rings_3_974.rar 'http://localhost:10000/discard'
bad file: /var/cache/nginx/client_temp/0000000003
Error: No such file or directory
    at fs.readFileSync (native)
    at discard (recaptcha.js:13)

$ curl -X POST -d @../../Downloads/.directory 'http://localhost:10000/discard1'
body length: 56, body in file: true
$ curl -X POST -d @../../Downloads/lord_of_the_rings_3_974.rar 'http://localhost:10000/discard1'
body length: 274146, body in file: true
$

```nginx
location /discard {
js_content test.discard;
}

location /discard1 {
    client_body_in_file_only clean;
    js_content test.discard;
}
```js
function discard(r) {
    var in_file = false;
    try {
        var body = r.requestBody || '';

    } catch (e) {
        r.log(e);
        var file = r.variables.request_body_file;

        try {
            body = require('fs').readFileSync(file);
            in_file = true;

        } catch (e) {
            r.log(e);
            return r.return(500, `bad file: ${file}\n${njs.dump(e)}\n`);
        }
    }

    return r.return(200, `body length: ${body.length}, body in file: ${in_file}\n`);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fishioon picture fishioon  Â·  3Comments

drsm picture drsm  Â·  4Comments

drsm picture drsm  Â·  3Comments

drsm picture drsm  Â·  3Comments

porunov picture porunov  Â·  3Comments