getOldestPossibleReplayAttackDate()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 0
1
<?php
2
/**
3
 * Helper for cron tasks.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Helper_Message
12
{
13
    /**
14
     * Checks whether a message is at risk of an replay attack.
15
     *
16
     * If the message is not at risk, it is usually just too old, so
17
     * that date checking mechanisms would prevent it to be delivered.
18
     *
19
     * @param array $messageMetaData the message data including, at least, the
20
     *                               meta data.
21
     *
22
     * @return bool
23
     */
24
    public static function isAtRiskOfReplayAttack(array $messageMetaData)
25
    {
26
        // in case the time is no valid/positive number, better return true
27
        if (!$messageMetaData['date_received']) {
28
            return true;
29
        }
30
31
        /* @var XenForo_Options $options */
32
        $xenOptions = XenForo_Application::getOptions();
33
34
        // when the hardened mode is activated, always return true
35
        if ($xenOptions->threema_gateway_harden_reply_attack_protection) {
36
            return true;
37
        }
38
39
        // if message has not been send at least 2 weeks ago (by default), it is attackable
40
        if ($messageMetaData['date_received'] >= self::getOldestPossibleReplayAttackDate()) {
41
            return true;
42
        }
43
44
        // older messages are fine
45
        return false;
46
    }
47
48
    /**
49
     * Returns the date/time where a message would still be accepted although
50
     * it is outdated.
51
     *
52
     * Note that for doing the actual replay attack check, this method *must not*
53
     * be used, but the option should rather be used directly.
54
     *
55
     * @return int
56
     */
57
    public static function getOldestPossibleReplayAttackDate()
58
    {
59
        /** @var XenForo_Options $options */
60
        $options = XenForo_Application::getOptions();
61
        /* @var int $rejectOldDefault the maximum age of a message as hardcoded/predefined fallback */
62
        $rejectOldDefault = strtotime('-14 days', XenForo_Application::$time);
63
        /* @var int $rejectOldOption the maximum age of a message according to the options */
64
        $rejectOldOption = $rejectOldDefault;
65
        if ($options->threema_gateway_verify_receive_time &&
66
            $options->threema_gateway_verify_receive_time['enabled']
67
        ) {
68
            $rejectOldOption = strtotime($options->threema_gateway_verify_receive_time['time'], XenForo_Application::$time);
69
        }
70
71
        return min($rejectOldDefault, $rejectOldOption);
72
    }
73
}
74