Repository navigation
Use safe cryptographic functions - #652
Merged
Merged
Conversation
hxdimpf
approved these changes
Oct 19, 2026
hxdimpf
left a comment
Contributor
There was a problem hiding this comment.
Looks good to me. All three changes are correct, and the output formats stay the same.
create_uuid():&binds tighter than|, so the version and variant bits are set right.str_split(…, 4)with the 8-4-4-4-12 format gives a valid lowercase v4 UUID.generate_key(): same alphabet and lengths, onlyrand()→random_int().generate_nonce(): still 32 hex characters. The server side hashes nonces anyway (OkapiDataStore::lookup_nonce), so nothing depends on the old format.
Tested:
- PHP 7.4 and 8.4: 200,000 UUIDs each, all unique and matching the v4 pattern (version 4, variant 8/9/a/b). Keys alphanumeric with the right length, the digits-only verifier 8 digits, nonce 32 hex. No notices or deprecations with
error_reporting=-1. - On the VM (this commit on the current master), OCDE and OCPL on MariaDB 10.11 and 10.0, with the suite from #653, read-only and
--write: 0 failures. HMAC requests and the wrong-secret rejection work. On both OCPL installationslogs/submitreturned v4 UUIDs (e.g.279f12cc-e73b-4b54-9930-0ae8790fdbc1), and the logs could be edited and deleted.
One note on the merge order: random_int() / random_bytes() need PHP 7.0, while master's composer.json still says >=5.6.0. So this should go in together with or after #651. All nodes we know of run 7.4 or newer, so in practice nothing breaks.
jrmajor
marked this pull request as ready for review
October 28, 2026 12:38
hxdimpf
added a commit
to OpencachingDeutschland/okapi
that referenced
this pull request
Oct 28, 2026
Sync with opencaching/okapi: safe random functions (opencaching#652), rcmds_received (opencaching#646)
hxdimpf
added a commit
that referenced
this pull request
Nov 2, 2026
Entries in `etc/changes.xml` for the changes merged since the last changelog PR (#665) that matter to client developers or site admins: #649, #654, #646, #651, #682, #680, #666, #676, #672, #671, #667. Version numbers and times were filled in by `etc/update_changes.php`. #652 has no visible change for clients, so it has no entry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OKAPI generated credentials and identifiers with non-cryptographic PRNGs:
Okapi::generate_key()usedrand(). It produces consumer keys and secrets, request and access tokens, and OAuth verifiers.Okapi::create_uuid()fell back tomt_rand()when/proc/sys/kernel/random/uuidwas unavailable (a comment notes a real duplicate in production).OAuthRequest::generate_nonce()usedmd5(microtime() . mt_rand()).They now use
random_int()/random_bytes():generate_key():random_int(); same alphabet and lengths.create_uuid(): RFC 4122 v4 UUID built fromrandom_bytes(16)on allplatforms; the
/procspecial case is gone. The format is unchanged(lowercase, dashed).
generate_nonce():bin2hex(random_bytes(16)); still 32 hex chars.Output formats are unchanged.
Testing
End to end on local OCPL and OCDE installs: consumer signup, request token, access token and signed calls. Nonce replay is still rejected over HTTP. On OCPL,
logs/submitandlogs/images/addreturn valid v4 UUIDs that round-trip throughlogs/entry.