• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-form-consent / 20317419334

17 Dec 2025 09:12PM UTC coverage: 94.737% (-0.8%) from 95.51%
20317419334

push

github

web-flow
Merge pull request #441 from eliashaeussler/feature/typo3-v14

13 of 21 new or added lines in 2 files covered. (61.9%)

792 of 836 relevant lines covered (94.74%)

15.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/Classes/Compatibility/Migration/HmacHashMigration.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "form_consent".
7
 *
8
 * Copyright (C) 2021-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3FormConsent\Compatibility\Migration;
25

26
use TYPO3\CMS\Core;
27
use TYPO3\CMS\Extbase;
28
use TYPO3\CMS\Form;
29

30
/**
31
 * HmacHashMigration
32
 *
33
 * @author Elias Häußler <elias@haeussler.dev>
34
 * @license GPL-2.0-or-later
35
 * @internal
36
 */
37
final readonly class HmacHashMigration
38
{
39
    private const LEGACY_HASH_LENGTH = 40;
40

41
    private Core\Information\Typo3Version $typo3Version;
42

43
    public function __construct(
26✔
44
        private Core\Crypto\HashService $hashService,
45
    ) {
46
        $this->typo3Version = new Core\Information\Typo3Version();
26✔
47
    }
48

49
    public function migrate(string $string, Extbase\Security\HashScope|Form\Security\HashScope $hashScope): string
19✔
50
    {
51
        // Early return if string does not have HMAC appended
52
        if (strlen($string) < self::LEGACY_HASH_LENGTH) {
19✔
53
            return $string;
1✔
54
        }
55

56
        // Validate hash
57
        try {
58
            $this->validateHashedString($string, $hashScope);
18✔
59

60
            // Hash is valid
61
            return $string;
11✔
62
        } catch (Core\Exception\Crypto\InvalidHashStringException) {
7✔
63
            // Hash is invalid and needs migration
64
        }
65

66
        /** @var non-empty-string $string */
67
        $string = substr($string, 0, -self::LEGACY_HASH_LENGTH);
7✔
68

69
        return $this->migrateString($string, $hashScope);
7✔
70
    }
71

72
    /**
73
     * @param non-empty-string $string
74
     */
75
    private function validateHashedString(string $string, Extbase\Security\HashScope|Form\Security\HashScope $hashScope): void
18✔
76
    {
77
        if ($this->typo3Version->getMajorVersion() === 13) {
18✔
78
            // @todo Remove once support for TYPO3 v13 is dropped
79
            $this->hashService->validateAndStripHmac($string, $hashScope->prefix());
18✔
80
        } else {
NEW
81
            $this->hashService->validateAndStripHmac($string, $hashScope->prefix(), $this->getRequiredHashAlgo($hashScope));
×
82
        }
83
    }
84

85
    /**
86
     * @param non-empty-string $string
87
     */
88
    private function migrateString(string $string, Extbase\Security\HashScope|Form\Security\HashScope $hashScope): string
7✔
89
    {
90
        // @todo Remove once support for TYPO3 v13 is dropped
91
        if ($this->typo3Version->getMajorVersion() === 13) {
7✔
92
            return $this->hashService->appendHmac($string, $hashScope->prefix());
7✔
93
        }
94

NEW
95
        return $this->hashService->appendHmac($string, $hashScope->prefix(), $this->getRequiredHashAlgo($hashScope));
×
96
    }
97

NEW
98
    private function getRequiredHashAlgo(Extbase\Security\HashScope|Form\Security\HashScope $hashScope): ?Core\Crypto\HashAlgo
×
99
    {
NEW
100
        if ($this->typo3Version->getMajorVersion() === 13) {
×
NEW
101
            return null;
×
102
        }
103

NEW
104
        if ($hashScope === Form\Security\HashScope::ResourcePointer) {
×
NEW
105
            return Core\Crypto\HashAlgo::SHA1;
×
106
        }
107

NEW
108
        return Core\Crypto\HashAlgo::SHA3_256;
×
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc