activity icon indicating copy to clipboard operation
activity copied to clipboard

Don't fail with invalid timezones

Open PVince81 opened this issue 2 years ago • 2 comments

Note: the steps are assumed based on log entries I found with this error.

Steps to reproduce

  1. Have a user select the timezone "Etc/Unknown" (not sure how, I'll need to check)
  2. Have the user generate some activities
  3. Wait for activities to be sent by email

Expected behaviour

Activities sent using UTC timezone.

Actual behaviour

Errors in log and likely no emails sent.

Server configuration

Nextcloud 21.0.7

Logs

{
  "reqId": "XXXXX",
  "level": 3,
  "time": "2021-12-10T07:40:02+00:00",
  "remoteAddr": "",
  "user": "--",
  "app": "activity",
  "method": "",
  "url": "--",
  "message": {
    "Exception": "Exception",
    "Message": "DateTimeZone::__construct(): Unknown or bad timezone (Etc/Unknown)",
    "Code": 0,
    "Trace": [
      {
        "file": "/var/www/html/apps/activity/lib/MailQueueHandler.php",
        "line": 354,
        "function": "__construct",
        "class": "DateTimeZone",
        "type": "->",
        "args": [
          "Etc/Unknown"
        ]
      },
      {
        "file": "/var/www/html/apps/activity/lib/MailQueueHandler.php",
        "line": 163,
        "function": "sendEmailToUser",
        "class": "OCA\\Activity\\MailQueueHandler",
        "type": "->",
        "args": [
          "",
          "",
          "en",
          "Etc/Unknown",
          1639122001
        ]
      },
      {
        "file": "/var/www/html/apps/activity/lib/BackgroundJob/EmailNotification.php",
        "line": 61,
        "function": "sendEmails",
        "class": "OCA\\Activity\\MailQueueHandler",
        "type": "->",
        "args": [
          500,
          1639122001
        ]
      },
      {
        "file": "/var/www/html/lib/private/BackgroundJob/Job.php",
        "line": 52,
        "function": "run",
        "class": "OCA\\Activity\\BackgroundJob\\EmailNotification",
        "type": "->",
        "args": [
          null
        ]
      },
      {
        "file": "/var/www/html/lib/private/BackgroundJob/TimedJob.php",
        "line": 59,
        "function": "execute",
        "class": "OC\\BackgroundJob\\Job",
        "type": "->",
        "args": [
          {
            "__class__": "OC\\BackgroundJob\\JobList"
          },
          {
            "__class__": "OC\\Log"
          }
        ]
      },
      {
        "file": "/var/www/html/cron.php",
        "line": 128,
        "function": "execute",
        "class": "OC\\BackgroundJob\\TimedJob",
        "type": "->",
        "args": [
          {
            "__class__": "OC\\BackgroundJob\\JobList"
          },
          {
            "__class__": "OC\\Log"
          }
        ]
      }
    ],
    "File": "/var/www/html/apps/activity/lib/MailQueueHandler.php",
    "Line": 354,
    "CustomMessage": "Failed sending activity email to user \"{user}\""
  },
  "userAgent": "--",
  "version": "21.0.7.0"
}

Notes

I suggest that we initialize the DateTimeZone earlier and catch its error. In case of error, fall back to "UTC" which is way better than failing and never sending the email.

PVince81 avatar Dec 13 '21 14:12 PVince81

hmm, so it seems the timezone is set by the browser and not configurable, and not validated, so depending what is sent there could be garbage there.

and also set at login time: https://github.com/nextcloud/server/blob/master/lib/private/Authentication/Login/SetUserTimezoneCommand.php#L47

PVince81 avatar Dec 13 '21 14:12 PVince81

so maybe this is now slowly transforming into a core issue where we should:

  1. not accept invalid timezones at login and fall back to UTC
  2. not accept invalid timezones in user_saml and fall back to UTC
  3. a migration that converts the unknown timezone to UTC: update oc_preferences set configvalue='UTC' where appid='core' and configkey='timezone' and configvalue='Etc/Unknown'; (might also affect other time zones, but I suspect that this one is the most common)

PVince81 avatar Dec 13 '21 14:12 PVince81