Fix CORS Vulnerabilities in Nginx: Enforce Secure Origin Checks

This article explains why an insecure cross‑origin setup in Nginx violates security standards, demonstrates how to reproduce the vulnerability with custom Origin headers, and provides a complete Nginx configuration using a map directive to whitelist origins, add proper CORS headers, and return 403 for disallowed requests.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Fix CORS Vulnerabilities in Nginx: Enforce Secure Origin Checks

According to the required security level, the existing cross‑origin configuration is insecure and must be fixed.

The solution is to inspect the Origin request header and apply a strict CORS policy that returns a 403 response for any illegal origin.

Vulnerability reproduction

Send a request with a custom Origin header; if the request succeeds, the server does not validate the header and the vulnerability is present.

curl -H 'Origin:http://test.com' http://192.168.15.32:80

Fix implementation

In the Nginx configuration, use a map directive to create a whitelist and set $allow_cors to 0 for disallowed origins. Then add the appropriate Access-Control-Allow-* headers and return 403 when $allow_cors equals 0.

http {
    map $http_origin $allow_cors {
        default 1;
        "~^https?://.*?\.tripwolf\.com.*$" 1;
        "~^(https?://(dmp.finerice.cn)?)$" 1;
        "~*" 0;
    }
    server {
        add_header Access-Control-Allow-Origin $http_origin;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
        location / {
            if ($allow_cors = 0) {
                return 403;
            }
            root /mnt/data;
        }
    }
}

Verification

Use tools such as POSTMAN to send requests with different Origin values and verify that illegal origins receive a 403 response.

POSTMAN CORS test
POSTMAN CORS test

If CORS is not required, simply remove the add_header Access-Control-Allow-Origin and related directives.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationSecurityHTTPCORSCross-Origin
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.