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

Fixed: $naxsi_request_id (issue #142) #154

Merged
merged 7 commits into from
Jul 7, 2024
Merged

Conversation

lubomudr
Copy link
Contributor

Hi

I propose a solution to issue #142

The first commit is simply a fix for $naxsi_request_id not being available in an internal redirect

The second one has been fixed: now $naxsi_request_id is inherited by NGINX $request_id and is calculated only when necessary, for example, logging an error or directly accessing a variable.

Since the size of $request_id is fixed in NGINX and is always 16 random bytes, in hexadecimal, the size of the string is explicitly specified in the code

naxsi_src/naxsi_skeleton.c Outdated Show resolved Hide resolved
naxsi_src/naxsi_skeleton.c Outdated Show resolved Hide resolved
naxsi_src/naxsi_skeleton.c Outdated Show resolved Hide resolved
naxsi_src/naxsi_skeleton.c Outdated Show resolved Hide resolved
Copy link
Owner

@wargio wargio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, address the changes.

@lubomudr
Copy link
Contributor Author

Ok

I specifically removed the "request_id" size constants because I believed that since this is inherited from NGINX, then these are internal structures and should not be changed in any way in the NAXSI code

But if you maintain compatibility with NGINX, when it does not yet contain the “request_id” variable, you will have to implement it yourself and these constants are needed. So I got excited here 😄

But in any case, calculating naxsi_request_id for EVERY request is expensive. The algorithm has been slightly modified - it is calculated only by demand and only if the "request_id" variable is not defined. Otherwise naxsi_request_id is equal to request_id.

Copy link
Owner

@wargio wargio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but now i wonder if it would just make more sense to replace rid with just request_id and implement a check for the NGINX version and add the extra code just for the older versions.

@lubomudr
Copy link
Contributor Author

Information about naxsi _request_id has already been leaked 😄

@wargio
Copy link
Owner

wargio commented Jun 28, 2024

Information about naxsi _request_id has already been leaked 😄

could be just an alias.

Copy link
Owner

@wargio wargio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, i'm very ok with this optimization, but i just checked the code that nginx uses for generating the value and is essentially the same as ours.

ngx_http_variable_request_id(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{
    u_char  *id;

#if (NGX_OPENSSL)
    u_char   random_bytes[16];
#endif

    id = ngx_pnalloc(r->pool, 32);
    if (id == NULL) {
        return NGX_ERROR;
    }

    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;

    v->len = 32;
    v->data = id;

#if (NGX_OPENSSL)

    if (RAND_bytes(random_bytes, 16) == 1) {
        ngx_hex_dump(id, random_bytes, 16);
        return NGX_OK;
    }

    ngx_ssl_error(NGX_LOG_ERR, r->connection->log, 0, "RAND_bytes() failed");

#endif

    ngx_sprintf(id, "%08xD%08xD%08xD%08xD",
                (uint32_t) ngx_random(), (uint32_t) ngx_random(),
                (uint32_t) ngx_random(), (uint32_t) ngx_random());

    return NGX_OK;
}

@lubomudr
Copy link
Contributor Author

To be clear, i'm very ok with this optimization, but i just checked the code that nginx uses for generating the value and is essentially the same as ours.

Yes
The optimization is that this is not calculated for EVERY request, regardless of whether the value is needed or not

Copy link
Owner

@wargio wargio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last two small changes and we can merge

@lubomudr lubomudr requested a review from wargio July 7, 2024 03:10
@wargio wargio merged commit aa3b6c7 into wargio:main Jul 7, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants