Skip to content

Commit 237e5da

Browse files
authored
Merge pull request #6334 from kenjis/fix-random_string-crypto
fix: random_string('crypto') may return string less than $len or ErrorException
2 parents d840ee2 + b55b3e7 commit 237e5da

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

system/Helpers/text_helper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ function random_string(string $type = 'alnum', int $len = 8): string
573573
return sha1(uniqid((string) mt_rand(), true));
574574

575575
case 'crypto':
576+
if ($len % 2 !== 0) {
577+
throw new InvalidArgumentException(
578+
'You must set an even number to the second parameter when you use `crypto`.'
579+
);
580+
}
581+
576582
return bin2hex(random_bytes($len / 2));
577583
}
578584
// 'basic' type treated as default

tests/system/Helpers/TextHelperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Helpers;
1313

1414
use CodeIgniter\Test\CIUnitTestCase;
15+
use InvalidArgumentException;
1516

1617
/**
1718
* @internal
@@ -113,6 +114,19 @@ public function testRandomString()
113114
$this->assertSame(40, strlen($random = random_string('sha1')));
114115
}
115116

117+
/**
118+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6330
119+
*/
120+
public function testRandomStringCryptoOddNumber()
121+
{
122+
$this->expectException(InvalidArgumentException::class);
123+
$this->expectExceptionMessage(
124+
'You must set an even number to the second parameter when you use `crypto`'
125+
);
126+
127+
random_string('crypto', 9);
128+
}
129+
116130
public function testIncrementString()
117131
{
118132
$this->assertSame('my-test_1', increment_string('my-test'));

user_guide_src/source/changelogs/v4.2.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ BREAKING
1616
- The method signature of ``CodeIgniter\Debug\Exceptions::__construct()`` has been changed. The ``IncomingRequest`` typehint on the ``$request`` parameter was removed. Extending classes should likewise remove the parameter so as not to break LSP.
1717
- The method signature of ``BaseBuilder.php::insert()`` and ``BaseBuilder.php::update()`` have been changed. The ``?array`` typehint on the ``$set`` parameter was removed.
1818
- A bug that caused pages to be cached before after filters were executed when using page caching has been fixed. Adding response headers or changing the response body in after filters now caches them correctly.
19+
- Due to a bug fix, now :php:func:`random_string` with the first parameter ``'crypto'`` throws ``InvalidArgumentException`` if the second parameter ``$len`` is an odd number.
1920

2021
Enhancements
2122
************

user_guide_src/source/helpers/text_helper.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ The following functions are available:
4242
- **sha1**: An encrypted random number based on ``sha1()`` (fixed length of 40).
4343
- **crypto**: A random string based on ``random_bytes()``.
4444

45+
.. note:: When you use **crypto**, you must set an even number to the second parameter.
46+
Since v4.2.2, if you set an odd number, ``InvalidArgumentException`` will be thrown.
47+
4548
Usage example:
4649

4750
.. literalinclude:: text_helper/002.php

user_guide_src/source/installation/upgrade_422.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Others
3232

3333
- The method ``Forge::createTable()`` no longer executes a ``CREATE TABLE IF NOT EXISTS``. If table is not found in ``$db->tableExists($table)`` then ``CREATE TABLE`` is executed.
3434
- The second parameter ``$ifNotExists`` of ``Forge::_createTable()`` is deprecated. It is no longer used and will be removed in a future release.
35+
- When you use :php:func:`random_string` with the first parameter ``'crypto'``, now if you set the second parameter ``$len`` to an odd number, ``InvalidArgumentException`` will be thrown. Change the parameter to an even number.
3536

3637
Breaking Enhancements
3738
*********************

0 commit comments

Comments
 (0)