Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Security and WordPress Last updated: April 11, 2023
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Protect WordPress website from XSS, Clickjacking, and some other attacks

Securing your site is essential for your online business presence. Over the weekend, I did a security scan on my WordPress website through Acunetix and Netsparker and found the following vulnerabilities.

  • Missing X-Frame-Options Header
  • Cookie Not Marked as HttpOnly
  • Cookie without Secure flag set

If you are on dedicated Cloud or VPS hosting, you can directly inject these headers in Apache or Nginx to mitigate it. However, to do this directly in WordPress – you can do the following.

Note: post-implementation, you can use the Secure Headers Test tool to verify the results.

X-Frame-Options Header in WordPress

Having this injected into the Header will prevent Clickjacking attacks. Below was discovered by Netsparker.

X-Frame-Options Header in WordPress

Solution:

  • Go to the path where WordPress is installed. If you are on shared hosting, you can log into cPanel >> File Manager
  • Take a backup of wp-config.php
  • Edit the file and add the following line
header('X-Frame-Options: SAMEORIGIN');
  • Save and refresh your website to verify.

Having Cookie with HTTPOnly instructs the browser to trust the cookie only by the server, which adds a layer of protection against XSS attacks.

httponly-cookie-wordpress

The secure flag in the cookie instructs the browser that the cookie is accessible over secure SSL channels, which add a layer of protection for the session cookie.

cookie-secure-flag

Note: This would work on the HTTPS website. If you are still on HTTP, you may consider switching to HTTPS for better security.

Solution:

  • Take a backup of wp-config.php
  • Edit the file and add the following line
@ini_set('session.cookie_httponly', true); 
@ini_set('session.cookie_secure', true); 
@ini_set('session.use_only_cookies', true);
  • Save the file and refresh your website to verify it.

If you don’t like to hack the code, then alternatively, you can use Shield plugin, which will help you to block iFrames & and protect from XSS attacks.

Once you install the plugin, go to HTTP headers and enable them.

shield-http-headers

I hope the above helps you in mitigating WordPress vulnerabilities.

Wait before you go…

Are you looking to implement more secure headers?

There are 10 OWASP recommended secure headers, and if using VPS or Cloud, check out this implementation guide for Apache and Nginx. However, if on shared hosting or want to do it within WordPress, then try this plugin.

Conclusion

Securing a site is challenging, and it requires ongoing efforts. If you are looking to offload the security headache to the expert, then you may try SUCURI WAF, which looks after complete website protection and performance for you.

  • Chandan Kumar
    Author
    As the founder of Geekflare, I’ve helped millions to excel in the digital realm. Passionate about technology, I’m on a mission to explore the world and amplify growth for professionals and businesses alike.
Thanks to our Sponsors
More great readings on Security
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Monday.com is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder