Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guest WiFi crendetials aren't guaranteed to be unique #540

Open
Trigary opened this issue Jun 4, 2024 · 3 comments
Open

Guest WiFi crendetials aren't guaranteed to be unique #540

Trigary opened this issue Jun 4, 2024 · 3 comments

Comments

@Trigary
Copy link
Collaborator

Trigary commented Jun 4, 2024

I received the following error when generating 10 000 users via a seeder:

  Database\Seeders\UsersTableSeeder .......................................................................... RUNNING  

   Illuminate\Database\UniqueConstraintViolationException 

  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'guest_yhxOYL' for key 'internet_accesses.internet_accesses_wifi_username_unique' (Connection: mysql, SQL: update `internet_accesses` set `wifi_password` = tFnMQeEO, `wifi_username` = guest_yhxOYL, `internet_accesses`.`updated_at` = 2024-06-04 16:19:30 where `user_id` = 8813)                                                    
                                                                                                                                                                                                        
  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:808                                                                                                                                
    804▕         // message to include the bindings with SQL, which will make this exception a                                                                                                          
    805▕         // lot more helpful to the developer instead of just the database's errors.                                                                                                            
    806▕         catch (Exception $e) {                                                                                                                                                                 
    807▕             if ($this->isUniqueConstraintError($e)) {                                                                                                                                          
  ➜ 808▕                 throw new UniqueConstraintViolationException(                                                                                                                                  
    809▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e                                                                                                            
    810▕                 );                                                                                                                                                                             
    811▕             }                                                                                                                                                                                  
    812▕                                                                                                                                                                                                
                                                                                                                                                                                                        
      +11 vendor frames                                                                                                                                                                                 
                                                                                                                                                                                                        
  12  app/Models/Internet/InternetAccess.php:109                                                                                                                                                        
      Illuminate\Database\Eloquent\Model::update()                                                                                                                                                      
                                                                                                                                                                                                        
  13  app/Models/User.php:140                                                                                                                                                                           
      App\Models\Internet\InternetAccess::setWifiCredentials()                                                                                                                                          
                                                                                                                                                                                                        
                                                                                                                                                                                                        
Process finished with exit code 1                                                                                                                                                                       

The culprit is the following line: https://github.com/EotvosCollegium/mars/blob/development/app/Models/Internet/InternetAccess.php#L107C1-L107C100

@machiato32
Copy link
Contributor

Math problem: If you generate N guest wifi usernames with the scheme defined at User.php:140, what is the probability of generating two identical ones in the process?
@kdmnk

@kdmnk
Copy link
Contributor

kdmnk commented Jun 4, 2024

We need to call Erika that we can't manage her guests anymore... :D

@horcsinbalint
Copy link
Member

My math is most probably off, but I think there are 62^6=56800235584 possible ids from Str::random(6).
According to this, it should have a 0.0008798 chance that the issue happens described by @Trigary. So we I will look into this a bit more.

This python script calculates the possibility of the problem described by @Trigary:

import decimal
result = 1
for i in range(0,10000):
     result *= decimal.Decimal(62**6-i)/decimal.Decimal(62**6)

print(decimal.Decimal(1)-result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants