modsecurity-crs-docker icon indicating copy to clipboard operation
modsecurity-crs-docker copied to clipboard

Adding CORS header 'Access-Control-Allow-Origin' with errors.

Open TafkaMax opened this issue 2 years ago • 9 comments

Hi

I am using modsecurity-crs:nginx as a proxy for my backend, which is an API.

On a totally different machine, there is a frontend JS application. Modern JS needs to have a CORS header set.

The proxy and API work fine, when everything is OK, but when a rule triggers the CORS header is not added.


Example:

The API allows users to POST data - up to 25MB. Currently the application checks the file size and also the proxy checks it.

When the proxy intercepts a max_body_size (or similar variable) that is larger than allowed it sends a 403 request, that does not include the CORS header. On the other hand the application sets it, when it encounters that denial.

I guess the easy way is for the application to block it, but it's better if it is intercepted earlier?

EDIT:

Currently my default.conf.template that I map to the container contains the following (which does not work):

# Nginx configuration for both HTTP and SSL

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

# https://serverfault.com/questions/958965/nginx-enabling-cors-for-multiple-subdomains

map $http_origin $allow_origin {
    ~^https?://(.*\.)?.mydomain.example(:\d+)?$ $http_origin;
    ~^https?://(.*\.)?localhost(:\d+)?$ $http_origin;
    default "";
}

server {
# http redirect
}

server {
# stuff
    add_header 'Access-Control-Allow-Origin' $allow_origin;
    location / {
# stuff
    }
}

TafkaMax avatar Jan 17 '23 12:01 TafkaMax

Thanks for the report @TafkaMax. We'll check this one soon.

fzipi avatar Feb 01 '23 12:02 fzipi

BTW, do you have an example that works?

fzipi avatar Feb 06 '23 15:02 fzipi

Unfortunately, not something that I can share.

  1. When I add something that return 403. image

When it is successful I don't get any error messages regarding CORS

TafkaMax avatar Feb 08 '23 09:02 TafkaMax

Ok, no worries, let me see what we can do.

fzipi avatar Feb 08 '23 09:02 fzipi

When sending the request that returns 403, it is intercepted by the modsec-crs-docker container.

image

When sending a successful request, the header from the backend app is forwarded.

image


As you can see, my previous attempts to modifying the functionality to add the header, do not work. (The code in the inital post)

TafkaMax avatar Feb 08 '23 09:02 TafkaMax

I started playing with this a bit. I don't think we can provide an answer for all cases, but this is what I'm trying:

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0).

So it will never add for errors 403.

load_module modules/ngx_http_headers_more_filter_module.so;
  • adding a new file includes/cors.conf.template with this temporary content:
more_set_headers -s 403 'Content-Type' 'text/plain';
more_set_headers -s 403 'Access-Control-Allow-Origin' '*';
more_set_headers -s 403 'Access-Control-Max-Age' '3600';
more_set_headers -s 403 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
more_set_headers -s 403 'Access-Control-Allow-Headers' '*';

We probably need to change the build step of the image to include the headers_more module on nginx and alpine. And also make those ☝️ configurable.

fzipi avatar Feb 26 '23 20:02 fzipi

Aha, thanks for the answer. I did not know the add headers were limited and not available to all responses without extra modifications.

TafkaMax avatar Feb 26 '23 20:02 TafkaMax

@TafkaMax Do you want to take a chance on this one?

fzipi avatar Jan 27 '24 23:01 fzipi

Currently not.

TafkaMax avatar Jan 28 '24 11:01 TafkaMax