Skip to content

Commit 255ea45

Browse files
authored
Merge pull request #13 from cleverage/bugfix/fix-decrypt-from-mcrypt
Fix a bug when decrypting from mcrypt as the fallback decryption is not triggered
2 parents 92e1ef9 + b008e0f commit 255ea45

File tree

10 files changed

+26
-34
lines changed

10 files changed

+26
-34
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ install:
1313

1414
script:
1515
- bin/phpunit
16+
- bin/var-dump-check --symfony --exclude vendor src
1617

1718
env:
1819
- SYMFONY_VERSION=4.4.*

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ install:
77

88
tests:
99
bin/phpunit
10+
bin/var-dump-check --symfony --exclude vendor src

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"doctrine/orm": "*",
2828
"symfony/security":"^4.4|^5.1",
2929
"symfony/monolog-bundle": "^3.6",
30-
"symfony/string": "^5.2"
30+
"symfony/string": "^5.2",
31+
"php-parallel-lint/php-var-dump-check": "^0.5.0"
3132
},
3233
"require-dev": {
3334
"symfony/stopwatch": "^4.4|^5.1",

src/DependencyInjection/Compiler/EncryptionAdapterCompilerPass.php

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function process(ContainerBuilder $container): void
4141
[
4242
new Reference($adapterId),
4343
new Reference(CipherKeyStorageInterface::class),
44-
$container->getParameter('sidus.encryption.throw_exceptions'),
4544
]
4645
);
4746
$managerDefinition->addTag('sidus.encryption.manager');

src/Doctrine/ValueEncrypter.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ class ValueEncrypter implements ValueEncrypterInterface
1414
private EncryptionManagerRegistryInterface $registry;
1515
private LoggerInterface $logger;
1616
private EncryptionEnablerInterface $encryptionEnabler;
17+
private bool $throwExceptions;
1718

1819
public function __construct(
1920
EncryptionManagerRegistryInterface $registry,
2021
LoggerInterface $logger,
21-
EncryptionEnablerInterface $encryptionEnabler
22+
EncryptionEnablerInterface $encryptionEnabler,
23+
bool $throwExceptions
2224
) {
2325
$this->registry = $registry;
2426
$this->logger = $logger;
2527
$this->encryptionEnabler = $encryptionEnabler;
28+
$this->throwExceptions = $throwExceptions;
2629
}
2730

2831
public function encrypt(string $value): string
@@ -55,6 +58,10 @@ public function decrypt(string $value): string
5558
} catch (Exception $exception) {
5659
}
5760
}
61+
62+
if ($this->throwExceptions) {
63+
throw $exception;
64+
}
5865
}
5966

6067
return $value;

src/Manager/EncryptionManager.php

+8-26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Sidus\EncryptionBundle\Manager;
1212

13-
use Exception;
1413
use Sidus\EncryptionBundle\Encryption\EncryptionAdapterInterface;
1514
use Sidus\EncryptionBundle\Entity\UserEncryptionProviderInterface;
1615
use Sidus\EncryptionBundle\Exception\EmptyCipherKeyException;
@@ -101,16 +100,7 @@ public function encryptString(string $string, string $nonce = null): string
101100
if (null === $nonce) {
102101
$nonce = $this->encryptionAdapter->generateNonce();
103102
}
104-
105-
try {
106-
$encrypted = $this->encryptionAdapter->encrypt($string, $nonce, $this->cipherKeyStorage->getCipherKey());
107-
} catch (Exception $exception) {
108-
if ($this->throwExceptions) {
109-
throw $exception;
110-
}
111-
112-
return '';
113-
}
103+
$encrypted = $this->encryptionAdapter->encrypt($string, $nonce, $this->cipherKeyStorage->getCipherKey());
114104

115105
return $nonce.$encrypted;
116106
}
@@ -125,22 +115,14 @@ public function decryptString(string $encryptedString, string $nonce = null): st
125115
return '';
126116
}
127117

128-
try {
129-
if ($nonce === null) {
130-
$nonce = $this->encryptionAdapter->parseNonce($encryptedString);
131-
}
132-
$decrypted = $this->encryptionAdapter->decrypt(
133-
$encryptedString,
134-
$nonce,
135-
$this->cipherKeyStorage->getCipherKey()
136-
);
137-
} catch (Exception $exception) {
138-
if ($this->throwExceptions) {
139-
throw $exception;
140-
}
141-
142-
return '';
118+
if ($nonce === null) {
119+
$nonce = $this->encryptionAdapter->parseNonce($encryptedString);
143120
}
121+
$decrypted = $this->encryptionAdapter->decrypt(
122+
$encryptedString,
123+
$nonce,
124+
$this->cipherKeyStorage->getCipherKey()
125+
);
144126

145127
return rtrim($decrypted, "\0");
146128
}

src/Resources/config/services/encryption.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ services:
2323
class: Sidus\EncryptionBundle\Encryption\Enabler\EncryptionEnabler
2424
public: false
2525

26+
Sidus\EncryptionBundle\Doctrine\ValueEncrypterInterface: '@Sidus\EncryptionBundle\Doctrine\ValueEncrypter'
2627
Sidus\EncryptionBundle\Doctrine\ValueEncrypter:
2728
class: Sidus\EncryptionBundle\Doctrine\ValueEncrypter
2829
public: false
2930
arguments:
3031
- '@Sidus\EncryptionBundle\Registry\EncryptionManagerRegistryInterface'
3132
- '@logger'
3233
- '@Sidus\EncryptionBundle\Encryption\Enabler\EncryptionEnablerInterface'
33-
34-
Sidus\EncryptionBundle\Doctrine\ValueEncrypterInterface: '@Sidus\EncryptionBundle\Doctrine\ValueEncrypter'
34+
- '%sidus.encryption.throw_exceptions%'
35+

tests/PHPUnit/Doctrine/Type/EncryptStringTypeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function setUp(): void
6161
$registry = new EncryptionManagerRegistry(
6262
XChaChaPolySodiumEncryptionAdapter::getCode(), new \ArrayIterator($managers)
6363
);
64-
$encrypter = new ValueEncrypter($registry, new Logger('encryption'), $enabler);
64+
$encrypter = new ValueEncrypter($registry, new Logger('encryption'), $enabler, true);
6565
$this->type = new EncryptStringType();
6666
$this->type->setValueEncrypter($encrypter);
6767
}

tests/PHPUnit/Doctrine/Type/EncryptTextTypeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function setUp(): void
6161
$registry = new EncryptionManagerRegistry(
6262
XChaChaPolySodiumEncryptionAdapter::getCode(), new \ArrayIterator($managers)
6363
);
64-
$encrypter = new ValueEncrypter($registry, new Logger('encryption'), $enabler);
64+
$encrypter = new ValueEncrypter($registry, new Logger('encryption'), $enabler, true);
6565
$this->type = new EncryptTextType();
6666
$this->type->setValueEncrypter($encrypter);
6767
}

tests/PHPUnit/Doctrine/ValueEncrypterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ protected function setUp(string $defaultAdapterCode = null): void
7373
$this->registry = new EncryptionManagerRegistry(
7474
XChaChaPolySodiumEncryptionAdapter::getCode(), new \ArrayIterator($managers)
7575
);
76-
$this->encrypter = new ValueEncrypter($this->registry, new Logger('encryption'), $enabler);
76+
$this->encrypter = new ValueEncrypter($this->registry, new Logger('encryption'), $enabler, true);
7777
}
7878
}

0 commit comments

Comments
 (0)