Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

8.2 KB
Raw Blame
# Trust X-Forwarded-Proto only from loopback/private/link-local proxies
# (matching _forwarded_headers_trusted in backend/__init__.py), or from any
# peer when TRUST_PROXY_HEADERS=true (or none when TRUST_PROXY_HEADERS=false).
geo $warracker_trusted_proxy {
    default __NGINX_TRUST_ALL_PROXIES__;
    127.0.0.0/8 __NGINX_TRUST_PRIVATE_PROXIES__;
    10.0.0.0/8 __NGINX_TRUST_PRIVATE_PROXIES__;
    172.16.0.0/12 __NGINX_TRUST_PRIVATE_PROXIES__;
    192.168.0.0/16 __NGINX_TRUST_PRIVATE_PROXIES__;
    169.254.0.0/16 __NGINX_TRUST_PRIVATE_PROXIES__;
    ::1/128 __NGINX_TRUST_PRIVATE_PROXIES__;
    fc00::/7 __NGINX_TRUST_PRIVATE_PROXIES__;
    fe80::/10 __NGINX_TRUST_PRIVATE_PROXIES__;
}

map "$warracker_trusted_proxy:$http_x_forwarded_proto" $warracker_forwarded_proto {
    default $scheme;
    "~^1:(https?)$" $1;
}

server {
    listen 80;
    listen [::]:80;
    server_name localhost;
    # Point root to the frontend directory where static assets reside inside the container
    root /var/www/html;
    index index.html;
    
    # Security headers (CFG-07a/b/d). These apply to the pages nginx
    # serves directly; the Flask app sets the same headers on API
    # responses, which pass through the proxy untouched. Keep the CSP
    # value identical to _CONTENT_SECURITY_POLICY in backend/__init__.py.
    # Locations below that define their own add_header directives do
    # not inherit these, so the HTML location repeats them and static
    # locations repeat X-Content-Type-Options: nosniff.
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https://images.unsplash.com; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' https://api.github.com; frame-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header X-Content-Type-Options "nosniff" always;

    # Suppress the nginx version number in error pages and the Server
    # header (CFG-08). Valid in the server context; this file is
    # included as the default server block.
    server_tokens off;

    # Send nginx errors to stderr so they appear in `docker logs`
    # alongside the app output; access logs stay in their file.
    error_log /dev/stderr warn;
    access_log /var/log/nginx/access.log;
    
    # Global settings
    gzip on;
    gzip_types text/plain text/css application/javascript application/json;
    client_max_body_size __NGINX_MAX_BODY_SIZE_CONFIG_VALUE__; # Configurable global limit
    
    # MIME types - fixed duplicate js entry
    types {
        text/html                             html htm shtml;
        text/css                              css;
        application/javascript                js;
        image/png                             png;
        image/jpeg                            jpg jpeg;
        image/gif                             gif;
        image/svg+xml                         svg svgz;
        application/pdf                       pdf;
        image/x-icon                          ico;
        application/json                      json;
        application/manifest+json             webmanifest manifest;
        font/woff2                            woff2;
        font/woff                             woff;
        font/ttf                              ttf;
    }
    
    # Specific location for locales JSON files
    location /locales/ {
        alias /var/www/html/locales/;
        try_files $uri =404;
        add_header Content-Type "application/json; charset=utf-8";
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        add_header X-Content-Type-Options "nosniff" always;
        expires -1;
    }
    
    # API requests - proxy to backend (fixed upstream host)
    # Using ^~ to ensure this prefix match takes precedence over regex extension matches
    location ^~ /api/ {
        proxy_pass http://127.0.0.1:5000; # Removed trailing slash to pass /api/ prefix
        # Use $http_host (not $host) so the original client port is preserved.
        # $host drops the port, which broke OIDC redirect_uri generation
        # (issue #238) for anyone serving Warracker on a nonstandard port.
        proxy_set_header Host $http_host;
        # Pass through the outer proxy's scheme only from trusted proxy peers
        # (issues #190/#97, H-03); direct public clients always get $scheme.
        proxy_set_header X-Forwarded-Proto $warracker_forwarded_proto;
        # Clear spoofable forwarded headers not used by Warracker and
        # contradictory scheme headers (empty values are omitted by nginx).
        proxy_set_header X-Forwarded-Host "";
        proxy_set_header X-Forwarded-Prefix "";
        proxy_set_header X-Forwarded-Ssl "";
        proxy_set_header X-Forwarded-Protocol "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size __NGINX_MAX_BODY_SIZE_CONFIG_VALUE__; # Configurable limit for API location
        
        # Pass Authorization header to backend
        proxy_set_header Authorization $http_authorization;
        
        # Enhanced proxy settings for file handling
        proxy_buffering off; # Disable buffering for file downloads to prevent content-length mismatches
        proxy_request_buffering off; # Disable request buffering for uploads
        proxy_read_timeout 300s; # Increased timeout for large file transfers
        proxy_connect_timeout 30s;
        proxy_send_timeout 300s;
        
        # Prevent proxy from modifying response headers that could cause issues
        proxy_set_header Connection "";
        proxy_http_version 1.1;
    }
    
    # Uploads - serve files from uploads directory
    location /uploads/ {
        return 403 "Access forbidden";
    }
    
    # HTML files - ensure proper content type
    location ~ \.html$ {
        add_header Content-Type "text/html; charset=utf-8";
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        # Repeated: locations with their own add_header do not inherit
        # the server-level security headers (CFG-07a/b/d).
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https://images.unsplash.com; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' https://api.github.com; frame-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'" always;
        add_header Referrer-Policy "strict-origin-when-cross-origin" always;
        add_header X-Content-Type-Options "nosniff" always;
    }
    
    # Favicon - specific handling
    location = /favicon.ico {
        log_not_found off;
        access_log off;
        try_files $uri =404;
        expires 7d;
        add_header Cache-Control "public, max-age=604800";
        add_header X-Content-Type-Options "nosniff" always;
    }
    
    # Service Worker - no cache to ensure updates
    location = /sw.js {
        try_files $uri =404;
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        add_header X-Content-Type-Options "nosniff" always;
        expires -1;
    }

    # JavaScript files - short cache with revalidation so module updates are picked up promptly
    location ~* \.js$ {
        try_files $uri =404;
        expires -1;
        add_header Cache-Control "no-cache, must-revalidate";
        add_header X-Content-Type-Options "nosniff" always;
    }

    # Static assets (CSS, images) - long cache is fine since these change rarely
    location ~* \.(css|jpg|jpeg|png|gif|ico|svg)$ {
        try_files $uri =404;
        expires 7d;
        add_header Cache-Control "public, max-age=604800";
        add_header X-Content-Type-Options "nosniff" always;
    }

    # Specific handling for manifest.json
    location = /manifest.json {
        alias /var/www/html/manifest.json; # Serve from this specific path
    }
    
    # Default location
    location / {
        try_files $uri $uri/ /index.html;
    }
}