Skip to content

Commit aef3c04

Browse files
committed
Safer create_uuid implementation
Fixes issue #336.
1 parent e35d2cf commit aef3c04

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

okapi/core.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,28 @@ class Okapi
933933

934934
private static $okapi_vars = null;
935935

936+
/** Return a new, random UUID. */
937+
public static function create_uuid()
938+
{
939+
/* If we're on Linux, then we'll use a system function for that. */
940+
941+
if (file_exists("/proc/sys/kernel/random/uuid")) {
942+
return trim(file_get_contents("/proc/sys/kernel/random/uuid"));
943+
}
944+
945+
/* On other systems (as well as on some other Linux distributions)
946+
* fall back to the original implementation (which is NOT safe - we had
947+
* one duplicate during 3 years of its running). */
948+
949+
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
950+
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
951+
mt_rand(0, 0xffff),
952+
mt_rand(0, 0x0fff) | 0x4000,
953+
mt_rand(0, 0x3fff) | 0x8000,
954+
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
955+
);
956+
}
957+
936958
/** Get a variable stored in okapi_vars. If variable not found, return $default. */
937959
public static function get_var($varname, $default = null)
938960
{

okapi/services/logs/submit.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -619,23 +619,12 @@ private static function increment_user_stats($user_internal_id, $logtype)
619619
}
620620
}
621621

622-
private static function create_uuid()
623-
{
624-
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
625-
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
626-
mt_rand(0, 0xffff),
627-
mt_rand(0, 0x0fff) | 0x4000,
628-
mt_rand(0, 0x3fff) | 0x8000,
629-
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
630-
);
631-
}
632-
633622
private static function insert_log_row(
634623
$consumer_key, $cache_internal_id, $user_internal_id, $logtype, $when,
635624
$formatted_comment, $text_html
636625
)
637626
{
638-
$log_uuid = self::create_uuid();
627+
$log_uuid = Okapi::create_uuid();
639628
Db::execute("
640629
insert into cache_logs (uuid, cache_id, user_id, type, date, text, text_html, last_modified, date_created, node)
641630
values (

0 commit comments

Comments
 (0)