Drupal 8: Poking A Hole In The Shield Module

25th August 2018

The Shield module prevents access to a Drupal site by putting a Apache authentication system across the entire website. This means that to access the site you need a username and password.

This is useful in a number of different situations, but I use it most for protecting dev and staging sites from access. It's not the most complex authentication system in the world, but it's enough to prevent the embarrassment caused by having staging sites being spidered by search engines.

One problem it does introduce is when developing API endpoints. Because the Shield module prevents access to the entire site this means that the only way to test API endpoints is to turn off the module, exposing the whole site. Even if we leave Shield on there is a problem in that API endpoints usually have their own authentication systems they can often get confused between the shield authentication and their own authentication, which can cause a few headaches.

Thankfully, Drupal 8 allows us to override this functionality and poke holes in the Shield module to allow certain endpoints to be exposed without authentication. What we need to do is override the service definition for the Shield module and introduce our own class. Our custom class will check the incoming request and check it against certain conditions. If the conditions are met then we allow the request to occur normally. If they do not pass then we send the request onto the Shield module and the request goes through the authentication system.

I should point out that disabling the shield authentication leaves the routes open to access. As a result you should either be prepared for these endpoints being accessed anonymously or have another authentication system in place.

When loading the services on a site Drupal will automatically load a service provider class that can be used to alter the services. When the cache is cleared these classes can be used to alter the services in some way. These classes will only be loaded if they fulfil certain requirements. These requirements are as follows:

  • The machine name of the module is translated to remove all spaces and capitalise the first letter of every word.
  • The class name is kept in the /src directory of the module.
  • The class name must be suffixed by ServiceProvider.
  • The class must extend ServiceProviderBase.

In order to facilitate this we need to create a module, for this example we'll call it "Shield Hole" and give it the machine name of "shield_hole". Our class therefore needs to be called ShieldHoleServiceProvider.

  1. <?php
  2.  
  3. namespace Drupal\shield_hole;
  4.  
  5. use Drupal\Core\DependencyInjection\ContainerBuilder;
  6. use Drupal\Core\DependencyInjection\ServiceProviderBase;
  7.  
  8. /**
  9.  * Class ShieldHoleServiceProvider.
  10.  *
  11.  * @package Drupal\shield_hole
  12.  */
  13. class ShieldHoleServiceProvider extends ServiceProviderBase {
  14.  
  15. /**
  16.   * {@inheritdoc}
  17.   */
  18. public function alter(ContainerBuilder $container) {
  19. // Decorate the shield module to prevent it from triggering on certain
  20. // routes.
  21. $definition = $container->getDefinition('shield.middleware');
  22. $definition->setClass('Drupal\shield_hole\ShieldOverride');
  23. }
  24.  
  25. }

When we clear the caches Drupal loads this class and runs the alter() method, passing in all of the defined services as the $container paramter. In this case we find the definition of the shield.midderware class and swaps it out for our own ShieldOverride class.

The ShieldOverride class is pretty simple, but it only has one job to do. By extending the ShieldMiddleware class we can either use it or bypass it altogether. The handle() method is called when a page request comes in and so it has access to all of the meta data for that page request. We then detect the correct page that we need to disable the authentication for. If this is the case then we handle the request with a normal Drupal response (done via the HTTP kernel object).

  1. <?php
  2.  
  3. namespace Drupal\shield_hole;
  4.  
  5. use Drupal\shield\ShieldMiddleware;
  6. use Symfony\Component\HttpFoundation\Request;
  7.  
  8. /**
  9.  * Class ShieldOverride.
  10.  *
  11.  * @package Drupal\shield_hole
  12.  */
  13. class ShieldOverride extends ShieldMiddleware {
  14.  
  15. /**
  16.   * {@inheritdoc}
  17.   */
  18. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
  19. // Get the current request URI.
  20. $currentPath = $request->getRequestUri();
  21.  
  22. // Get the current method (e.g. GET or POST).
  23. $currentMethod = $request->getMethod();
  24.  
  25. if (($currentMethod == 'POST' || $currentMethod == 'GET') && strstr($currentPath, '/services/some_api') !== FALSE) {
  26. // If we are attempting to access the service then we handle the
  27. // request without invoking the Shield module.
  28. return $this->httpKernel->handle($request, $type, $catch);
  29. }
  30.  
  31. // Always handle the request using the default Shield behaviour.
  32. return parent::handle($request, $type, $catch);
  33. }
  34.  
  35. }

The default behaviour is to handle the request using the parent objects handle() method. As we have extended the ShieldMiddleware class the parent handle() method is essentially the authentication system. This means that if the request doesn't meet our requirements then it will be handled by the shield module at it should be and require the user to enter a password.

This example of using a ServiceProvider class to alter an existing service has many uses in Drupal 8. The Shield module override is a good example though as it shows how to alter a simple service to provide a known outcome. You just need to be aware of the security implications of doing this.

I have used ServiceProvider classes a few times to overload existing classes in contributed modules when I have needed to alter the way in which a module functions. This is especially useful when performing tests. If you are attempting to run some tests, especially against an API, then using a ServiceProvider to mock the service is a great way of providing your application with a known dataset without actually using the API.

For more information about this subject you can see the Drupal 8 service provider documentation.

Add new comment

The content of this field is kept private and will not be shown publicly.