Skip to content

Unexpected type of js_body_filter data argument #999

@lonerr

Description

@lonerr

Hi,

I'm experiencing strange behavior of js_body_filter when combined with the conditional if block.
Unexpectedly, the conversion of the data argument to String type stops when if conditional evaluates to true.

Below is a minimal example demonstrating the issue:

load_module modules/ngx_http_js_module.so;

events {
    worker_connections 1024;
}

http {
    js_import lib.js;

    server {
        listen 80;

        location /origin/ {
            return 200 payload\n;
        }

        location /t1/ {
            proxy_pass http://127.0.0.1/origin/;

            # always false
            if ($remote_addr = nonexistent) { set $x 1; }

            js_header_filter lib.headerFilter;
            js_body_filter   lib.bodyFilter;
        }

        location /t2/ {
            proxy_pass http://127.0.0.1/origin/;

            # always true
            if ($remote_addr) { set $x 1; }

            js_header_filter lib.headerFilter;
            js_body_filter   lib.bodyFilter;
        }

        location /t3/ {
            proxy_pass http://127.0.0.1/origin/;

            # always true, empty block
            if ($remote_addr) { }

            js_header_filter lib.headerFilter;
            js_body_filter   lib.bodyFilter;
        }
    }
}

Where lib.js has the following content:

function bodyFilter(r, data, flags) {
    if (flags.last)
        return r.sendBuffer(data, flags);

    r.sendBuffer(`[${data.constructor.name}] ${data}`);
}

function headerFilter(r) {
    delete r.headersOut['Content-Length'];
}

export default { bodyFilter, headerFilter }

The first result meets expectations:

% curl http://127.0.0.1/t1/
[String] payload

But the second and third ones are not:

% curl http://127.0.0.1/t2/
[Buffer] payload

% curl http://127.0.0.1/t3/
[Buffer] payload

The case is reproducible with the latest packages from official repo on Ubuntu 22.04.
Thank you!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions