Security Flashcards
What is an CSRF attack?
Cross-site request forgery attacks allow malicious site creators to input something in your application in the name of an authenticated user. For example, imagine if your application has a /user/email route that accepts a POST request to change the authenticated user’s email address. Most likely, this route expects an email input field to contain the email address the user would like to begin using. Without CSRF protection, a malicious website could create an HTML form that points to your application’s /user/email route and submits the malicious user’s own email address to take over the account of the user. If the malicious website automatically submits the form when the page is loaded, the malicious user only needs to lure an unsuspecting user of your application to visit their website and their email address will be changed in your application.
How does Laravel prevent CSRF attacks?
Laravel automatically generates a CSRF “token” for each active user session managed by your application. This token is used to verify that the authenticated user is the person actually making the requests to the application. Since this token is stored in the user’s session and changes each time the session is regenerated, a malicious application is unable to access it. The current session’s CSRF token cab be accessed via the request’s session or via the csrf_token helper function:
Route::get('/token', function (Request $request) { $token = $request->session()->token(); $token = csrf_token(); // ... });
Sometimes you may wish to exclude a set of URIs from CSRF protection, for example for processing payments. How can you exclude specific sites / applications from your CSRF protection?
You may exclude specific routes by providing their URIs to the validateCsrfTokens method in your application’s bootstrap/app.php file:
->withMiddleware(function (Middleware $middleware) { $middleware->validateCsrfTokens(except: [ 'stripe/*', 'http://example.com/example/*', ]); });
This would now allow stripe and example.com/example/anything after the / to interact with your application.
When using Laravel’s encrypter, you must set the key configuration option in your config/app.php configuration file. How can you use Artisan to generate keys for you?
php artisan key:generate
What will happen if you change your applications encryption key and how can you prevent these things from becoming problems?
If you change your application’s encryption key, all authenticated user sessions will be logged out of your application. This is because every cookie, including session cookies, are encrypted by Laravel. In addition, it will no longer be possible to decrypt any data that was encrypted with your previous encryption key. To mitigate this issue, Laravel allows you to list your previous encryption keys in your application’s APP_PREVIOUS_KEYS environment variable. This variable may contain a comma-delimited list of all of your previous encryption keys. When you set this environment variable, Laravel will always use the “current” encryption key when encrypting values. However, when decrypting values, Laravel will first try the current key, and if decryption fails using the current key, Laravel will try all previous keys until one of the keys is able to decrypt the value.
How can you encrypt a string?
You may encrypt a value using the encryptString method provided by the Crypt facade. All encrypted values are encrypted using OpenSSL and the AES-256-CBC cipher. Furthermore, all encrypted values are signed with a message authentication code (MAC). The integrated message authentication code will prevent the decryption of any values that have been tampered with by malicious users.
How can you decrypt a string and what happens, if the string couldn’t correctly be decrypted?
You may decrypt values using the decryptString method provided by the Crypt facade. If the value can not be properly decrypted, such as when the message authentication code is invalid, an Illuminate\Contracts\Encryption\DecryptException will be thrown.
At its core, Laravel’s authentication facilities are made up of “guards” and “providers”. What do they do?
Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies.
Providers define how users are retrieved from your persistent storage. Laravel ships with support for retrieving users using Eloquent and the database query builder. However, you are free to define additional providers as needed for your application.
Where is the application’s authentication configuration file located?
config/auth.php
What is Laravel Breeze?
Laravel Breeze is a simple, minimal implementation of all of Laravel’s authentication features, including login registration, password reset, email verification, and password confirmation. Laravel Breeze’s view layer is compromised of simple Blade templates styled with Tailwind CSS.
What is Laravel Fortify?
Laravel Fortify is a headless authentication backend for Laravel that implements many features including cookie based authentication as well as two-factor authentication and email verification. Fortify provides the authentication backend for Laravel Jetstream or may be used independetly in combination with Laravel Sanctum to provide authentication for an SPA that needs to authenticate with Laravel.
What is Laravel Jetstream?
Laravel Jetstream is a robust application starter kit that consumes and exposes Laravel Fortify’s authentication services with a beautiful, modern UI powered by Tailwind CSS, Livewire, and / or Inertia. Laravel Jetstream includes optional support for two-factor authentication, team support, browser session management, profile management, and built-in integration with Laravel Sanctum to offer API token authentication.
What is Laravel Passport?
Passport is an OAuth2 authentication provider, offering a variety of OAuth2 “grant types” which allow you to issue various types of tokens. In general, this is a robust and complex package for API authentication. However, most applications do not require the complex features offered by the OAuth 2 spec, which can be confusing for both users and developers. In addition, developers have been historically confused about how to authenticate SPA applications or mobile applications using OAuth2 authentication providers like Passport.
What is Laravel Sanctum?
Simpler, more streamlined authentication package, that could handle both first-party web requests from a web-browser and API requests via tokens. It’s a hybrid web / API authentication package that can manage your application’s entire authentication process. This is possible because when Sanctum based applications receive a request, Sanctum will first determine if the request includes a session cookie that references an authenticated session. Sanctum accomplishes this by calling Laravel’s built-in authentication services. If the request is not being authenticated via a session cookie, Sanctum will inspect the request for an API token. If an API token is present, Sanctum will authenticate the request using that token.
After installing and using an authentication starter kit from Laravel and allowing users to register and authenticate your application, you will often need to interact with the currently authenticated user. How can you access the user while handling an incoming request?
By using the user method of the Auth facade, for example:
// Retrieve the currently authenticated user... $user = Auth::user(); // Retrieve the currently authenticated user's ID... $id = Auth::id();
Alternatively, once a user is authenticated, you may access the authenticated user via an illuminate\Http\Request instance, for example:
public function update(Request $request): RedirectResponse { $user = $request->user(); ...
How can you check if a user is logged in?
By using the check method of the Auth facade, for example:
if (Auth::check()) { // The user is logged in... }
What do you need to do to make a route only accessible to logged in users?
Route::get('/example', function() { // Only authenticated users may access this route... })->middleware('auth');
How can you redirect unauthenticated users to the login page if they try to access something, that guests are not supposed to access?
In bootstrap/app.php: ->withMiddleware(function (Middleware $middleware) { $middleware->redirectGuestTo('/login'); })