Skip to content

Commit 7e5659c

Browse files
RichB-DLSndg63276
andauthored
LIMS-1612: Replace uniqid with openssl random pseudo bytes (#903)
* LIMS-1612 - add util fn for generating md5. * test/LIMS-1612 - Setup Basic param tests. * feat/LIMS-1612 - implement the new md5 util across all uses of uniqId * fix/LIMS-1612 - Fix PSALM faiure * Merge pre-release/2025-R1.2 into master (#904) * LIMS-1590: Dont allow name editing for lab contacts when login is set (#887) * LIMS-1558: Add functionality for SMILES code for any sample (#880) * LIMS-1490: Use callback URL for incoming dewars via shipping service (#852) * LIMS-1570: Remove unused app file (#874) * LIMS-1537: Add more options to reprocessing (#876) * LIMS-1466: Disable dispatch form validation when using the shipping service (#891) * fix/LIMS-1612 compatibility with LIMS-1490 --------- Co-authored-by: Mark W <24956497+ndg63276@users.noreply.github.com>
1 parent b8c7a7e commit 7e5659c

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

api/src/Page/Cal.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SynchWeb\Page;
44

55
use SynchWeb\Page;
6+
use SynchWeb\Utils;
67

78
class Cal extends Page
89
{
@@ -24,11 +25,11 @@ function _external_link() {
2425
if (sizeof($args)) {
2526
$this->_output('/cal/ics/h/'.$args[0]['HASH'].'/calendar.ics');
2627
} else {
27-
$h = md5(uniqid());
28+
$md5 = Utils::generateRandomMd5();
2829
$this->db->pq("INSERT INTO calendarhash (calendarhashid,ckey,hash,beamline)
29-
VALUES (s_calendarhash.nextval, :1, :2, :3)", array($arg, $h, $this->has_arg('bl') ? 1 : 0));
30+
VALUES (s_calendarhash.nextval, :1, :2, :3)", array($arg, $md5, $this->has_arg('bl') ? 1 : 0));
3031

31-
$this->_output('/cal/ics/h/'.$h.'/calendar.ics');
32+
$this->_output('/cal/ics/h/'.$md5.'/calendar.ics');
3233
}
3334
}
3435
}

api/src/Page/Download.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\HttpFoundation\Response;
1313
use ZipStream\Option\Archive;
1414
use ZipStream\ZipStream;
15+
use SynchWeb\Utils;
1516

1617
ini_set('max_execution_time', 0); // To allow large file downloads
1718

@@ -66,7 +67,7 @@ function _sign_url()
6667
{
6768
if (!$this->has_arg('validity'))
6869
$this->_error('No validity specified');
69-
$token = md5(uniqid());
70+
$token = Utils::generateRandomMd5();
7071

7172
$this->db->pq("INSERT INTO SW_onceToken (token, validity, proposalid, personid) VALUES (:1, :2, :3, :4)", array($token, $this->arg('validity'), $this->proposalid, $this->user->personId));
7273
$this->_output(array('token' => $token));

api/src/Page/Shipment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ function _dispatch_dewar_shipment_request($dewar)
10661066
$proposal = $dewar['PROPOSAL'];
10671067
$external_id = (int) $dewar['DEWARID'];
10681068
$shipping_id = (int) $dewar['SHIPPINGID'];
1069-
$token = md5(uniqid());
1069+
$token = Utils::generateRandomMd5();
10701070
$this->db->pq(
10711071
"UPDATE dewar SET extra = JSON_SET(IFNULL(extra, '{}'), '$.token', :1 ) WHERE dewarid=:2",
10721072
array($token, $external_id)
@@ -3266,7 +3266,7 @@ function _create_shipment_shipment_request($shipment, array $dewars): int
32663266

32673267
$shipping_id = (int) $shipment['SHIPPINGID'];
32683268

3269-
$token = md5(openssl_random_pseudo_bytes(7));
3269+
$token = Utils::generateRandomMd5(7);
32703270
$this->db->pq(
32713271
"UPDATE dewar SET extra = JSON_SET(IFNULL(extra, '{}'), '$.token', :1 ) WHERE shippingid=:2",
32723272
array($token, $shipping_id)

api/src/Utils.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SynchWeb;
44

5+
use InvalidArgumentException;
6+
57
class Utils
68
{
79
public static $exitOnError = true;
@@ -17,6 +19,20 @@ public static function returnError($title, $msg)
1719
}
1820
}
1921

22+
/**
23+
* Generate a random 32 hex md5 string from a random byteString. Utilises openssl_random_pseudo_bytes under the hood.
24+
* @uses [open_ssl_random_pseudo_bytes](https://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php)
25+
* @param int $length = 13 Specify the bytes of the random val. defaults to 13 as per uniqID(). This is likely enough for most uses.
26+
* @return string
27+
* @throws InvalidArgumentException if $length <= 0
28+
* @throws \Exception if openSSL fails
29+
*/
30+
public static function generateRandomMd5(int $length = 13): string {
31+
if ($length <= 0) throw new InvalidArgumentException('byteLength must be > 0');
32+
$bytes = openssl_random_pseudo_bytes($length);
33+
return md5(bin2hex($bytes));
34+
}
35+
2036
public static function shouldLogUserActivityToDB($loginId): bool
2137
{
2238
global $log_activity_to_ispyb;

api/tests/Utils/UtilsTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SynchWeb;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class UtilsTest extends TestCase {
9+
public function testMd5CreationFailsWithInvalidArgValue(): void {
10+
$this->expectException(InvalidArgumentException::class);
11+
12+
Utils::generateRandomMd5(-10);
13+
}
14+
15+
public function testMd5CreationSucceedsWithValidArgValue(): void {
16+
17+
$md5Hash = Utils::generateRandomMd5(5);
18+
19+
$this->assertNotEmpty($md5Hash );
20+
$this->assertIsString($md5Hash );
21+
$this->assertLessThanOrEqual(32, strlen($md5Hash));
22+
}
23+
24+
25+
public function testMd5CreationSucceedsWithEmptyArgValue(): void {
26+
27+
$md5Hash = Utils::generateRandomMd5();
28+
29+
$this->assertNotEmpty($md5Hash );
30+
$this->assertIsString($md5Hash );
31+
$this->assertLessThanOrEqual(32, strlen($md5Hash));
32+
}
33+
34+
35+
}
36+

0 commit comments

Comments
 (0)