r/PHPhelp • u/elelec • Jun 03 '26
Newbie security question about game API with Laravel
Hey there, I am pretty new to laravel, and I have a basic security question.
So I'm primarily a Unity 3D developer, and I decided to look into setting up an API for a small game, mainly as a learning experience. For the API I'm using Laravel, and so far I've managed to do some simple GET and POST requests from inside Unity to interact with a local server.
Here's my concern, in order to manage to do requests from Unity, I've had to disable csrf and origin Request Forgery protections. I did that by going to the bootstrap/app.php file, and meddling with the Middleware part a bit.
->withMiddleware(function (Middleware $middleware): void {
$middleware->preventRequestForgery(
except: ["/*"]
);
})Is this too bad, or is it find for my use case? Should I do something different? What is a proper way to implement security for an API where the calls are coming from unrelated programs?
I'm not going to be using forms for data requesting at all, and soon I want to implement a user authentication as a check for any data creation and some data receiving. Would that suffice?
Thanks for your time, I'm still very new to the backend side of this, so any help would be very appreciated!
6
u/Lumethys Jun 03 '26
REST api isnt meant to be protected with CSRF, it is also the default if you use API route: https://laravel.com/docs/routing#api-routes. Is there a reason why you did not?
1
u/obstreperous_troll Jun 03 '26
Just disable the CSRF middleware entirely: a JSON API doesn't need it, and if your clients are all authenticated, that's as good as a token, since it's not something another browser tab can use anyway.
1
u/martinbean Jun 03 '26
Why have you had to disable CSRF? CSRF is not applied in the default
apimiddleware stack.0
u/[deleted] Jun 03 '26
[deleted] — view deleted comment