How to Fix Insecure Cookies Warning
In this article
An insecure cookies warning appears when a website sets a cookie without the Secure flag, meaning the cookie can be transmitted over unencrypted HTTP connections. This exposes session data to interception. Fixing it involves configuring your web server or application to set the Secure attribute on all cookies, and often also the HttpOnly and SameSite attributes. This guide covers the most common scenarios: Apache, Nginx, and application-level fixes.
What You'll Need
- Access to your web server configuration files (Apache or Nginx) or your application's code.
- Ability to restart or reload your web server after changes.
- Basic understanding of HTTP headers and cookies.
Step-by-Step
- Identify the source of the warning. Open your browser's developer tools (F12), go to the Network tab, reload the page, and click on any request. Look at the Set-Cookie response header. If the cookie lacks the
Secureflag, that's the cause. Also check if the page is served over HTTPS—if not, fix that first. - Ensure HTTPS is enabled. The
Secureflag only works over HTTPS. If your site is still on HTTP, obtain an SSL/TLS certificate (e.g., from Let's Encrypt) and configure HTTPS. Most hosts offer free certificates. - For Apache servers: Edit your virtual host configuration or an
.htaccessfile. Add a header directive to set theSecureflag on all cookies. For example, in yourhttpd.confor.htaccess:Header always edit Set-Cookie (.*) "$1; Secure"
This appendsSecureto everySet-Cookieheader. If you also wantHttpOnlyandSameSite, use:Header always edit Set-Cookie (.*) "$1; Secure; HttpOnly; SameSite=Lax"
Then restart Apache (sudo systemctl restart apache2orsudo service apache2 restart). - For Nginx servers: In your server block, add a
proxy_hide_headerandadd_headerdirective. However, Nginx cannot modify existing cookies easily. The recommended approach is to set the flags in your backend application. If you must do it at the Nginx level, you can use themore_set_headersmodule (if installed) or rewrite the header with a map. A simpler method is to use theproxy_cookie_flagsdirective (available in Nginx Plus or with thengx_http_proxy_modulein newer versions). Example:proxy_cookie_flags ~ secure;
This sets theSecureflag on all cookies. For more control, you can specify a pattern. Reload Nginx withsudo nginx -s reload. - For application-level fixes: If you're using a framework like PHP, Python (Django/Flask), Node.js, or Java, set the
Secureflag in the cookie creation code. For example in PHP:setcookie('name', 'value', [ 'secure' => true, 'httponly' => true, 'samesite' => 'Lax', ]);In Django, setSESSION_COOKIE_SECURE = TrueandCSRF_COOKIE_SECURE = Truein settings. In Flask, usesession.cookie_secure = True. In Express (Node.js), setres.cookie('name', 'value', { secure: true }). - Test the fix. After making changes, clear your browser cache and cookies, then reload the page. Go back to the Network tab and inspect the
Set-Cookieheader again—it should now includeSecure. Also check that the warning is gone in the console or security tab.
Common Mistakes
- Forgetting to reload/restart the server after changes.
- Applying the
Secureflag to cookies over HTTP—this will cause cookies to be ignored by browsers, breaking sessions. - Using a blanket rule that breaks third-party cookies (e.g., setting
SameSite=NonewithoutSecure). - Only fixing the cookie, not the underlying HTTP to HTTPS redirect.
Tips
- Also set the
HttpOnlyflag to prevent JavaScript access to cookies, reducing XSS risk. - Use
SameSite=LaxorStrictto mitigate CSRF attacks. - Test with a private browsing window to avoid cached cookies.
- If you have many cookies, consider using a security scanner to audit your headers.
Troubleshooting
- Warning persists after changes: Clear all cookies for your domain, then reload. If the warning remains, check that you're viewing the HTTPS version of the site (not HTTP). Also verify that the server is actually sending the modified header—use curl -I to inspect.
- Cookies not being set at all: If you set
Secureover HTTP, browsers will reject the cookie. Ensure you are testing over HTTPS. - Apache header edit not working: Make sure mod_headers is enabled (
sudo a2enmod headers). Also check that yourHeaderdirective is in the correct scope (server, vhost, or directory). - Nginx proxy_cookie_flags not working: This directive is only available in Nginx Plus or with certain third-party modules. If you're using open-source Nginx, you may need to use the
more_set_headersmodule or do the fix in the app. - Application framework overrides your header: Some frameworks set cookies automatically (e.g., session cookies). Ensure you configure the framework's settings, not just your custom cookies.
Frequently Asked Questions
What does the 'Secure' flag do?
It instructs the browser to only send the cookie over HTTPS connections, preventing interception over plain HTTP.
Can I set the Secure flag on cookies for a subdomain?
Yes, the Secure flag is independent of domain. You can set it on any cookie, but it only takes effect when the request is over HTTPS.
Will setting Secure break my site on HTTP?
Yes, if you set Secure on a cookie and then access the site over HTTP, the cookie won't be sent, which can break sessions. That's why HTTPS is essential.
What is the difference between Secure and HttpOnly?
Secure ensures the cookie is only sent over HTTPS. HttpOnly prevents JavaScript from accessing the cookie, reducing XSS risks.
Does this fix the 'insecure cookies' warning in all browsers?
Yes, all major browsers (Chrome, Firefox, Safari, Edge) show warnings for missing Secure flag. Fixing it resolves the warning in all of them.
I use a shared hosting without server config access. Can I still fix it?
Yes, you can often add the header via an .htaccess file (Apache) or through your application's code. If neither is possible, contact your host.