WSOD TypeError when 'User Agent' is NULL

Created on 16 January 2024, 10 months ago
Updated 13 February 2024, 9 months ago

Problem/Motivation

When the 'User Agent' key of the request headers is NULL or not defined, for example with Varnish probe requests (in a reverse proxy setup) without the 'User Agent' key, the following fatal error occurs returning a 500 HTTP status code: 500 Internal Server Error
PHP error in the recent logs:
TypeError : DeviceDetector\DeviceDetector::__construct(): Argument #1 ($userAgent) must be of type string, null given, called in /universal_device_detection/src/Detector/DefaultDetector.php on line 51 dans DeviceDetector\DeviceDetector->__construct() (ligne 188 de /vendor/matomo/device-detector/DeviceDetector.php).

Steps to reproduce

Execute the following PHP code locally with development module Devel PHP and/or drush php:

use Drupal\universal_device_detection\Detector\DefaultDetector;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;

// Create a Symfony GET HTTP request with a NULL 'User-Agent'. 
$request = Request::create('/', 'GET', [], [], [], ['HTTP_USER_AGENT' => NULL]);

$stack= new RequestStack();
$stack->push($request);
dpm($stack);

// Trigger detection to display error.
$default_detector = new DefaultDetector($stack);
$res = $default_detector->detect();
dpm($default_detector);
dpm($res);

Proposed resolution

Add a default value for $userAgent if 'User-Agent' comes back empty from the request headers.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇫🇷France dydave

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024