Latest posts

Browse topics

How To Configure Trusted Host settings for Drupal 8 Website

In today's Drupal tutorial series I will talk about Trusted host settings, what they are and how to configure them.

In short, Trusted host settings protects against HTTP HOST Header attacks (prevent your site from thinking it is someone else).

Drupal 7 added a new feature into the core that is not user-facing directly but is sometimes called poor man's cron. The feature triggers the periodic tasks of a Drupal site like emptying log files, sending e-mails, and clearing out caches. This feature, when combined with dynamic detection of the "base url" (added in Drupal 4.7), can lead to some screwy situations.

Let's start with the oblivious, first noticed a warning in a Drupal 8 website, saying:

The trusted_host_patterns setting is not configured in settings.php. This can lead to security vulnerabilities. It is highly recommended that you configure this. See Protecting against HTTP HOST Header attacks for more information.

Warning about Trusted Host Settings in Drupal 8

Warning about Trusted Host Settings in Drupal 8

in case you are running a single website, the solution is simple, open settings.php file and add following code:

$settings['trusted_host_patterns'] = [
  '^www\.example\.com$',
];

For more complicated use, please see following article:  Trusted Host settings

How To Redirect in Nginx all Domain Name Versions to https://www

As you might already know search engines (Google) sees www.domain.com and domain.com as two separate domain names. If you are not using 301 redirects (www to non-www or vice verse)you might get penalized for duplicate content. 

In Ngnix, there is a simple solution how to make such redirect happen, see: Ngnix Redirect To WWW

Now, things get a bit complicated when https versions are involved. See: Linode: How To Secure Nginx with Let's Encrypt on Ubuntu 12.04

Once your website has https enabled, technically speaking you might be dealing with for different domain names:

  1. domain.com
  2. www.domain.com
  3. https://domain.com
  4. https://www.domain.com

What we are looking is to redirect all versions to one, I prefer the version with https://www, here is how to achieve that with Nginx server blocks.

Assuming you have already configured server blocks pointing your domain name to https://www open your server config:

sudo nano /opt/etc/nginx/sites-available/reinisfischer.com

And at the end of the code add:

server{
  listen 80;
  server_name www.reinisfischer.com reinisfischer.com;
  return 301 https://$server_name$request_uri;
}
server{
  listen 443;
  server_name reinisfischer.com;
  return 301 https://www.$server_name$request_uri;
}

Now, restart Nginx

sudo /etc/init.d/nginx restart

Hope it helps!

How To Update Drupal Modules via SSH (Putty)

To strengthen Drupal websites security I decided to disable one-click automatic module updates from Drupal dashboard, unfortunately, it makes it harder not even for bad guys to compromise Drupal website but it also makes it harder for Drupal devs.

Now, there are several options you might choose instead, composer being a top of them. But for this article, I will write down how to update modules using SSH.

For Windows, we are going to use Putty:

From your Drupal installation go to the modules folder

cd /sites/all/modules

now, using wget download desired modules and extract:

sudo wget https://ftp.drupal.org/files/projects/file_entity-7.x-2.21.tar.gz
sudo tar -xvzf file_entity-7.x-2.21.tar.gz

Go back to the Drupal website and run updates. Next, you would like to delete tar.gz file from the modules folder

sudo rm file_entity-7.x-2.21.tar.gz

Tapping Birch Sap in the Backyard

Can't decide on a new hobby? Think about tapping birch sap in early spring. Depending on the region you are located and availability of birch trees you might even enjoy this drink - a birch sap.

In today's article a few photos from tapping a birch sap in early spring in our backyard using Libertu Birch Sap Kit.

You can purchase this kit at sula.lv to get your own tree sap. If you have maple or birch trees in your yard or forest you can get juice very easy. 

Libertu Birch Sap Kit

Libertu Birch Sap Kit

Bought this kit at the local DIY shop. It was a fun, trying to install it and trying to squeeze a juice from this birch

Plastic bag for collecting birch sap

Plastic bag for collecting birch sap

We left this kit in a tree for about two weeks to collect birch sap.

Securing Drupal Files and Folders (Chmod, Security Review, Hacked)

There are a couple of great helper modules for Drupal to learn more about failing security holes - Security Review and Hacked. 

Security Review

The Security Review module automates testing for many of the easy-to-make mistakes that render your site insecure.

Security Review runs the following checks:

  • Safe file system permissions (protecting against arbitrary code execution)
  • Text formats don't allow dangerous tags (protecting against XSS)
  • PHP or Javascript in content (nodes and comments and fields in Drupal 7)
  • Safe error reporting (avoiding information disclosure)
  • Secure private files
  • Only safe upload extensions
  • A large number of database errors (could be a sign of SQLi attempts)
  • A large amount of failed logins (could be a sign of brute-force attempts)
  • Responsible Drupal admin permissions (protecting against access misconfiguration)
  • Username as password (protecting against brute-force)
  • Password included in user emails (avoiding information disclosure)
  • PHP execution (protecting against arbitrary code execution)
  • Base URL set / D8 Trusted hosts (protecting against some phishing attempts)
  • Views access controlled (protecting against information disclosure)

This module does not automatically make changes to your site. You should use the results of the checklist and its resources to manually secure your site. The results of some checks may be incorrect depending on unique factors of your site.

Here are the results showing issues with a Drupal 7 installation

Security Review

Security Review

Hacked

This module scans the currently installed Drupal, contributed modules and themes, re-downloads them, and determines if they have been changed. Changes are marked clearly and if the diff module is installed then Hacked! will allow you to see the exact lines that have changed.

This module does not and will not prevent your site from being 'hacked'.

Chmod permissions for files and folders

The Drupal security guide has a section on securing your site with a useful list of items to work through.

There are several approaches to set file permissions for a Drupal website, most common include chmod 755 for sites/default/folder and chmod 644 for public files

If you are using Security Review module,you should investigate step by step, but here is an example for securing your drupal website public_html folder

sudo chmod -R 555 public_html

Let me know!