Skip to content

Commit 052f6ec

Browse files
Merge pull request #16 from ginkelsoft-development/fix/validate-pepper-configuration
fix: validate SEARCH_PEPPER configuration before token generation
2 parents 8360ba4 + ee2ef32 commit 052f6ec

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Support/Tokens.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ class Tokens
4646
*
4747
* @return string
4848
* Hex-encoded SHA-256 hash (64 characters).
49+
*
50+
* @throws \RuntimeException if pepper is empty
4951
*/
5052
public static function exact(string $normalized, string $pepper): string
5153
{
54+
if (empty($pepper)) {
55+
throw new \RuntimeException(
56+
'SEARCH_PEPPER is not configured. Set it in your .env file for security. ' .
57+
'Generate a random string: openssl rand -base64 32'
58+
);
59+
}
60+
5261
return hash('sha256', $normalized . $pepper);
5362
}
5463

@@ -71,9 +80,18 @@ public static function exact(string $normalized, string $pepper): string
7180
*
7281
* @return string[]
7382
* An array of hex-encoded SHA-256 prefix tokens.
83+
*
84+
* @throws \RuntimeException if pepper is empty
7485
*/
7586
public static function prefixes(string $normalized, int $maxDepth, string $pepper): array
7687
{
88+
if (empty($pepper)) {
89+
throw new \RuntimeException(
90+
'SEARCH_PEPPER is not configured. Set it in your .env file for security. ' .
91+
'Generate a random string: openssl rand -base64 32'
92+
);
93+
}
94+
7795
$out = [];
7896
$len = mb_strlen($normalized, 'UTF-8');
7997
$depth = min($maxDepth, $len);

0 commit comments

Comments
 (0)