Skip to content

Commit 5a0c761

Browse files
authored
fix(security): use random_int instead of rand (#7819)
1 parent f504e9e commit 5a0c761

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

app/Console/Commands/Local/SetupDummyAccount.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ private function createVaults(): void
140140
{
141141
$this->info('☐ Create vaults');
142142

143-
for ($i = 0; $i < rand(3, 5); $i++) {
143+
for ($i = 0; $i < random_int(3, 5); $i++) {
144144
(new CreateVault)->execute([
145145
'account_id' => $this->firstUser->account_id,
146146
'author_id' => $this->firstUser->id,
147147
'type' => Vault::TYPE_PERSONAL,
148148
'name' => $this->faker->firstName,
149-
'description' => rand(1, 2) == 1 ? $this->faker->sentence() : null,
149+
'description' => random_int(1, 2) == 1 ? $this->faker->sentence() : null,
150150
]);
151151
}
152152
}
@@ -160,7 +160,7 @@ private function createContacts(): void
160160
->where('internal_type', ContactImportantDate::TYPE_BIRTHDATE)
161161
->first();
162162

163-
for ($i = 0; $i < rand(2, 13); $i++) {
163+
for ($i = 0; $i < random_int(2, 13); $i++) {
164164
$date = $this->faker->dateTimeThisCentury();
165165
$birthDate = Carbon::parse($date);
166166

@@ -170,7 +170,7 @@ private function createContacts(): void
170170
'vault_id' => $vault->id,
171171
'first_name' => $this->faker->firstName(),
172172
'last_name' => $this->faker->lastName(),
173-
'middle_name' => rand(1, 2) == 1 ? $this->faker->lastName() : null,
173+
'middle_name' => random_int(1, 2) == 1 ? $this->faker->lastName() : null,
174174
'nickname' => null,
175175
'maiden_name' => null,
176176
'listed' => true,
@@ -184,7 +184,7 @@ private function createContacts(): void
184184
'label' => 'Birthdate',
185185
'day' => $birthDate->day,
186186
'month' => $birthDate->month,
187-
'year' => rand(1, 2) == 1 ? $birthDate->year : null,
187+
'year' => random_int(1, 2) == 1 ? $birthDate->year : null,
188188
'contact_important_date_type_id' => $birthDateType->id,
189189
]);
190190
}
@@ -196,8 +196,8 @@ private function createGroups(): void
196196
$this->info('☐ Create groups');
197197

198198
foreach (Vault::all() as $vault) {
199-
for ($i = 0; $i < rand(2, 5); $i++) {
200-
$groupType = rand(1, 2) == 1 ? $this->firstUser->account->groupTypes->random() : null;
199+
for ($i = 0; $i < random_int(2, 5); $i++) {
200+
$groupType = random_int(1, 2) == 1 ? $this->firstUser->account->groupTypes->random() : null;
201201
$group = (new CreateGroup)->execute([
202202
'account_id' => $this->firstUser->account_id,
203203
'vault_id' => $vault->id,
@@ -209,11 +209,11 @@ private function createGroups(): void
209209
// Add random contacts to the group
210210
$contacts = Contact::where('vault_id', $vault->id)
211211
->inRandomOrder()
212-
->take(rand(1, 5))
212+
->take(random_int(1, 5))
213213
->get();
214214

215215
foreach ($contacts as $contact) {
216-
$groupTypeRoles = $groupType !== null && rand(1, 2) == 1 ? $groupType->groupTypeRoles : null;
216+
$groupTypeRoles = $groupType !== null && random_int(1, 2) == 1 ? $groupType->groupTypeRoles : null;
217217
if ($groupTypeRoles !== null && $groupTypeRoles->isNotEmpty()) {
218218
$role = $groupTypeRoles->random();
219219
} else {
@@ -244,7 +244,7 @@ private function createNotes(): void
244244
'author_id' => $this->firstUser->id,
245245
'vault_id' => $contact->vault_id,
246246
'contact_id' => $contact->id,
247-
'title' => rand(1, 2) == 1 ? $this->faker->sentence(rand(3, 6)) : null,
247+
'title' => random_int(1, 2) == 1 ? $this->faker->sentence(random_int(3, 6)) : null,
248248
'body' => $this->faker->paragraph(),
249249
]);
250250
}
@@ -262,7 +262,7 @@ private function createTasks(): void
262262
'author_id' => $this->firstUser->id,
263263
'vault_id' => $contact->vault_id,
264264
'contact_id' => $contact->id,
265-
'label' => $this->faker->sentence(rand(3, 6)),
265+
'label' => $this->faker->sentence(random_int(3, 6)),
266266
'description' => null,
267267
]);
268268
}
@@ -281,7 +281,7 @@ private function createGoals(): void
281281
]);
282282

283283
foreach (Contact::all() as $contact) {
284-
foreach ($goals->take(rand(1, 4)) as $goal) {
284+
foreach ($goals->take(random_int(1, 4)) as $goal) {
285285
$goal = (new CreateGoal)->execute([
286286
'account_id' => $this->firstUser->account_id,
287287
'author_id' => $this->firstUser->id,
@@ -292,8 +292,8 @@ private function createGoals(): void
292292

293293
for ($i = 0; $i < 4; $i++) {
294294
$date = Carbon::now()->subYears(2);
295-
for ($j = 0; $j < rand(1, 20); $j++) {
296-
$date = $date->addDays(rand(1, 3));
295+
for ($j = 0; $j < random_int(1, 20); $j++) {
296+
$date = $date->addDays(random_int(1, 3));
297297

298298
try {
299299
(new ToggleStreak)->execute([
@@ -325,16 +325,16 @@ private function createJournals(): void
325325
]);
326326

327327
foreach (Vault::all() as $vault) {
328-
foreach ($journals->take(rand(1, 4)) as $journal) {
328+
foreach ($journals->take(random_int(1, 4)) as $journal) {
329329
$journal = (new CreateJournal)->execute([
330330
'account_id' => $this->firstUser->account_id,
331331
'author_id' => $this->firstUser->id,
332332
'vault_id' => $vault->id,
333333
'name' => $journal,
334-
'description' => rand(1, 2) == 1 ? $this->faker->sentence() : null,
334+
'description' => random_int(1, 2) == 1 ? $this->faker->sentence() : null,
335335
]);
336336

337-
for ($j = 0; $j < rand(1, 20); $j++) {
337+
for ($j = 0; $j < random_int(1, 20); $j++) {
338338
(new CreatePost)->execute([
339339
'account_id' => $this->firstUser->account_id,
340340
'author_id' => $this->firstUser->id,

database/factories/GenderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function definition()
2323
return [
2424
'account_id' => Account::factory(),
2525
'name' => $this->faker->name(),
26-
'type' => Gender::LIST[rand(0, 4)],
26+
'type' => Gender::LIST[random_int(0, 4)],
2727
];
2828
}
2929
}

0 commit comments

Comments
 (0)