Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Stud.IP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan-Hendrik Willms
Stud.IP
Commits
f3757123
Commit
f3757123
authored
3 years ago
by
Jan Eberhardt
Browse files
Options
Downloads
Patches
Plain Diff
use random_bytes() instead of custom function
parent
7469feb3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/classes/CSRFProtection.php
+1
-56
1 addition, 56 deletions
lib/classes/CSRFProtection.php
with
1 addition
and
56 deletions
lib/classes/CSRFProtection.php
+
1
−
56
View file @
f3757123
...
...
@@ -125,7 +125,7 @@ class CSRFProtection
// create a token, if there is none
if
(
!
isset
(
$_SESSION
[
self
::
TOKEN
]))
{
$_SESSION
[
self
::
TOKEN
]
=
base64_encode
(
self
::
random
B
ytes
(
32
));
$_SESSION
[
self
::
TOKEN
]
=
base64_encode
(
random
_b
ytes
(
32
));
}
return
$_SESSION
[
self
::
TOKEN
];
...
...
@@ -149,59 +149,4 @@ class CSRFProtection
self
::
token
()
);
}
/**
* Returns a string of highly randomized bytes (over the full 8-bit range).
*
* This function is better than simply calling mt_rand() or any other
* built-in PHP function because it can return a long string of bytes
* (compared to < 4 bytes normally from mt_rand()) and uses the best
* available pseudo-random source.
*
* This function was copied from Drupal's includes/bootstrap.inc.
*
* @param integer $count The number of characters (bytes) to return in the string.
*/
private
static
function
randomBytes
(
$count
)
{
static
$random_state
,
$bytes
;
// Initialize on the first call. The contents of $_SERVER includes a mix of
// user-specific and system information that varies a little with each page.
if
(
!
isset
(
$random_state
))
{
$random_state
=
print_r
(
$_SERVER
,
TRUE
);
if
(
function_exists
(
'getmypid'
))
{
// Further initialize with the somewhat random PHP process ID.
$random_state
.
=
getmypid
();
}
$bytes
=
''
;
}
if
(
mb_strlen
(
$bytes
)
<
$count
)
{
// /dev/urandom is available on many *nix systems and is considered the
// best commonly available pseudo-random source.
if
(
$fh
=
@
fopen
(
'/dev/urandom'
,
'rb'
))
{
// PHP only performs buffered reads, so in reality it will always read
// at least 4096 bytes. Thus, it costs nothing extra to read and store
// that much so as to speed any additional invocations.
$bytes
.
=
fread
(
$fh
,
max
(
4096
,
$count
));
fclose
(
$fh
);
}
// If /dev/urandom is not available or returns no bytes, this loop will
// generate a good set of pseudo-random bytes on any system.
// Note that it may be important that our $random_state is passed
// through hash() prior to being rolled into $output, that the two hash()
// invocations are different, and that the extra input into the first one -
// the microtime() - is prepended rather than appended. This is to avoid
// directly leaking $random_state via the $output stream, which could
// allow for trivial prediction of further "random" numbers.
while
(
mb_strlen
(
$bytes
)
<
$count
)
{
$random_state
=
hash
(
'sha256'
,
microtime
()
.
mt_rand
()
.
$random_state
);
$bytes
.
=
hash
(
'sha256'
,
mt_rand
()
.
$random_state
,
TRUE
);
}
}
$output
=
mb_substr
(
$bytes
,
0
,
$count
);
$bytes
=
mb_substr
(
$bytes
,
$count
);
return
$output
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment