YouTubevideo· Jul 16, 2025· 20:38· 3,800 views on YouTube

Why Your Laravel APP_KEY is a Secret

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

49 moments
  1. So this is big news, but not in the good way because this news article released there was app key leaks for larva applications, 600 vulnerable applications using 260,000 exposed keys from GitHub. So yeah, that's not good news, but just how bad are these leaks? What happened? And then what can we do about it? Why don't we talk. but hey, sadly, no one has leaked their stripe API keys to me in support of this channel. So instead, uh, we're gonna turn to a sponsor.

  2. So here's the sponsor for today's video. I. All right and send. Perfect. Alright, user number six, just signed up. Hey sir, you set a timer for 10 minutes. We need to get this drip campaign out the door for new signup. Hey, what are you doing? Who else is gonna send these emails every single minute? I need to watch to make sure my users are signed up so that I can get them emails as promptly as possible. That's why I got timers.

  3. I can just use Bento. They have workflows and triggers specifically meant for those kind of automations. Yeah, that sounds better. Seriously, bento is fantastic, even if you only use a small part of it like I do. My favorite feature, being able to automate my email flow saying once something happens in my code, and of course there's SDKs for literally everything I can say.

  4. I want to wait a certain number of time and then I want to send or this email or start. This campaign, it just makes the things I want to do within my application easier, and it makes it better for my users because I don't have to bother them as much as I would otherwise because all the automation's built in and flexible for my use cases. So seriously, try Bento. Let them know Josh Siri sent you and get started with it today.

  5. So if you've seen anything on either X or Twitter about these two big articles, one from gi guardian.com, and then one from the hacker news.com. Not to be confused with hacker news.com, uh, about these exploited public app key. Leaks hundreds of Laravel applications. And what they say is there's 600 vulnerable applications that have been using a 260,000 exposed keys from GitHub.

  6. And now these numbers are a little skewed because it doesn't necessarily mean that there are 260,000 applications that are vulnerable. Right now, there are 600, but over 260,000 exposed keys from GitHub. And even that number is a little bit misleading, and we'll get into why. So before we dive into what exactly happened, it's important to know what is an app key within Laravel.

  7. So Laravel has an encryption service that provides this simple, convenient interface for encrypting and decrypting text via open SSL using a ES 2 56 a ES 1 28 encryption. So everything that goes through a layer of application is first passed through this encryption key an app. Key so that all of your sessions, all of your cookies, all of your data, uh, can't be modified or tampered.

  8. Once this encryption key has been set, because it has to be a match, that particular encryption key. So this is key to note, and not in pun kind of way. This app key has been set. And so then any sort of, you know, configuration, any sort of cookies, any sort of sessions that are set. Within your application are then encrypted through this app key, this environment variable that you put into your application.

  9. That's usually generated upon when you first create this layer of application or when you first deploy it to your server, but that key can be rotated. So what happened with these leaks when it comes to this app key, and what does it mean for people who their app keys were leaked? Was this user error? Was this something else? Let's see. So this blog mentions what the Laravel Docs kind of mentions, is that this app key? It's a randomly generated 32 byte symmetric encryption key.

  10. And Laravel by default uses, uh, byte 64 in order to do that, or base 64, I should say. And it's automatically utilized by this encrypt and decrypt functions so that you don't have to, um, have, uh, explicit developer intervention. If attackers obtain the app key and can invoke the decrypt function, that is, uh, that is key in this particular instance with a maliciously crafted payload, they can achieve remote code execution on the layer of a web server so they can make some nasty things happen.

  11. It goes on to say it's not far from being theoretical, but it does pose a real threat for several critical reasons. I'm not gonna read the whole article. It's good to know that this is not a Laravel problem. This is a user problem, but it is good to be aware of why some users might make this mistake and then what you can be, what you can make sure you don't do in the future.

  12. And while I do have a qualm with how some of these numbers have been presented, because, uh, for the most part, those 260,000 leaked app keys are probably not ever or were ever in production, but there are those 600 Laravel applications that, that were compromised. And so that is good to note.

  13. But this article dives into the process of what it would look like for this compromised aki in order to validate it, to decrypt it, and then send some remote code execution along with that decrypt function. And so it's good to note that that decryption has to succeed. Just knowing the app key is not necessarily good enough.

  14. This author explains that at GI Guardian, they've been monitoring GitHub activity in real time since 2018, and would likely have access to numerous exposed app keys. And so those would be app keys that have been pushed to GitHub, specifically in public repos. Our collaboration started right away and we defined patterns That allowed me the author to crunch data and finally extract 260,000 app keys.

  15. Again, not all in production app keys, but here this extended data set helped Remi and Michel to extend their result to 600 trivially compromised layer applications. That's the big number. Right here. Those were app keys that were found on public hub repos that then were able to be compromised because they were in production application keys.

  16. But like this article says, that analysis was just the beginning to reveal the full scope of the problem because it's really comes down to a user error. Or just a misunderstanding of how the app key works, how, uh, secret it should be because of how the Laravel application decrypts and encrypts specific sessions and what is possible when you know that app key, especially from malicious users.

  17. And here's the second thing that most malicious users would need within a layer of application, not just the app key pushed into a get repo, because for the most part, you probably don't know where that production application lives. And even in order to even test that, that application key is, is active. But the collaboration with syn active not only expanded the scale of vulnerable applications, but also revealed new attacked vectors during the app key extraction process.

  18. It became apparent that many developers were inadvertently exposing the app URL alongside their app key. So the app URL configuration defines the application's base URL, which Laravel uses to generate absolute links and asset URLs. And so if you have the both, the app, URL, and app key creates a powerful attack vector. Attackers can directly access the target application, retrieve session cookies, and attempt to decrypt them using the exposed key.

  19. And I do wanna make note of this particular sentence, attempt to decrypt them using the exposed key. While we'll get into why it is very bad to have your app key, especially for a production application out there exposed for everyone to see, you still do have to have a way to decrypt that application key in order for that, potentially malicious attack to occur. So what happens when you do have an app key that is either leaked or compromised?

  20. Well, don't do what these particular GI repositories did or what this author claims. The developers discovered their exposure, but chose to simply remove the commits rather than properly rotating the compromised keys.

  21. So there's a blog article that goes into this, but I want to first take a look at what does a from scratch Laravel application look like, and then what were some of these changes that these developers and you can potentially not make so that you don't have something like this happen to you? So if we were to pull up our terminal, we were to just run Laravel new app, key Gen, I'll just use a a blank live wire application.

  22. What happens within, within Git, and that's the big key here, because you don't want to expose your app key. Now of course you have an application. We see this here, application key set successfully. Laravel has a PHP Artisan command that is PHP Artisan key generated that allows.

  23. To then generate this base 64 key for your application to use when it any decrypts, any information in order to set session cookies, et cetera within your Laravel application. so we have our env and we have this app key right here. Of course, this is my development environment. Usually when I was, when I'll push this to, uh, let's say VO Cloud, or if I'm running on a server, most of the time you're going to run the.

  24. Artisan key generate command or have a particular key that you've already generated that you would put in your environment variables. If there's no key generated, then it's not going to work. Your application is not going to work. There is nothing to decrypt it against. So do we have a. Get ignore, and by lar by default, at least on this version of Laravel Laravel 12, and for most versions of Laravel in the past recent years, you do have a do get ignore.

  25. That includes any ENV files. So you have to be very specific in order to leak your NV key. That includes an app key. Now, here's what most people do. That is wrong, even with other environment variables. Let's say you had like an open ai, API key, and you had also a, stripe key.

  26. Now, most of the time, especially if you're working within a team, the tendency is to then, I'm just going to copy these and put them in the Dov example so that whenever my team clones this repo, downloads it to their local environment and then runs it there. Okay, you already have your API keys set, but even if this is a private repo, this is still a bad practice. This is something that you shouldn't do because it's not the best case scenario.

  27. That is what, uh, encrypted messaging is for. So whether that is. Just encrypted messaging between you and your team. It's using something like One Password or Doppler to send those specific API keys. Now those are just testing API keys and it's not as bad. Of course, that's kind of up to you, uh, when that comes or if you're just frequently rotating those testing keys as well. But for the most part, you probably shouldn't put anything in this.mv example or anything that is going to be hosted on Git.

  28. So what happens when this does happen? Well, if we were to pull up in our code, we have this PHP Artisan Key generate command. We can also pass in the dash dash show parameter. So this is going to generate a new base, 64, 32 character string, and that is what Laravel needs in order for that app key to be, um, a true app key. So I could just take this, put it in my MV file on my server, and now that app key is rotated.

  29. Of course, now your sessions are gonna be changed. If you have users logged in, they're gonna have to, they're not gonna be logged in anymore. They're gonna have to be logged in again. And so that is why Laravel. Now, in this case, for example, this is not something you should do if your app key is exposed. But you do have the ability to then gracefully rotate encryption keys because it's a good idea.

  30. Especially if you have a high profile layer of application, it's a good idea to rotate your encryption keys you can put your previous key in your environment variable. Again, this is on the server so that it's going to try the app key first. And if, uh, it doesn't work that way, it's going to grab the previous key to try to, uh, decrypt those variables from that user session or cookies.

  31. And then update them to the new encrypted variable sessions. This is a way to gracefully change your keys because that is a good practice, it wasn't just app key and really that, that that's what it, that's what this all comes down to, right? The, the ability as a user to know.

  32. What should be secret and what is okay to have a save for Env example where you're saying, okay, these, this is production info, but it's just maybe, you know, it's okay for it to be saved as a NV example. It's not necessarily a, a secret in that sense. And of course there are good ways to make things secret. That should be. Secret.

  33. But if this is something like an app key or API keys or anything that is remotely, able to be used in a malicious manner, that should be something is then never shown to someone. And even for the most part within your team, you probably don't need to have your team have these variables as well. That should be kept in like a secret manager place.

  34. Or within your ci, cd, um, or anything else remotely related to that. But if it's just for, hey, you can get this application up and running on your environment, that's what something like one password or again, Doppler or anything related to that where you can kind of share environment variables like that would be perfect for. And again, this article calls out layer applications, but even in this analysis of file names where it's like these app keys, it revealed that 63% originate from ENV files.

  35. Variants such ASV production. This indicates that exposures typically occur in configuration files containing diverse application secrets, not exclusively Laravel credentials. So again, not just API, uh, not just app keys, but also something like API key or database credentials, uh, god forbid. And yeah, it goes into mention database credentials and cloud storage tokens. Additional exposure credentials frequently include e-commerce platforms, customer support systems.

  36. All of these things are being stored in plain text within a MV dot. Blank or just a env that again, it's not just stored there, it's fine if it's stored locally, especially if you're using that, um, I don't know, to, to then push to production straight into your SS hing or something like that. But for the most part, everything should be kept as secret as possible, and that is includes not being just stored locally on your machine, even if you're a solo developer.

  37. One thing I couldn't quite figure out what they were mentioning in this article is they mentioned at the top there there was, 600 vulnerable applications using 260,000 exposed keys. But then as frequently asked question is, what's the scale of Laravel app key exposure? And it would be 400 app keys as functional, uh, with four confirmed cases of trivial remote code execution. So I don't know where that 200 additional came from.

  38. Maybe that's just they were functional at some point and maybe this 400 is more up to date in that sense. Like this frequently asked questions says if something is compromised, definitely rotate it. But how do you keep this from happening in the future? Well, one, I guess just don't add these to your, you know, your Git repository, first of all. Or don't share them publicly in any way, but then two. Use the features that Laravel provides.

  39. So not just gracefully rotating, which is a good idea, but you also have, uh, Laravel encrypted environment variables. Now, what does this mean? Just like we've said before, unencrypted environment variables or files should never be stored in source control, however. Laravel allows you to encrypt your environment files so that they may be safely added to source control with the rest of your application. This is a great way to share environment variables with the rest of your team. How do you do that?

  40. PHPs, artisan and Encrypt. Why don't we take a look? So if I was to say, Hey, I have these sensitive information now open AI API key, I have, um. You know, I have my, uh, database credentials, even just for my staging or testing environment. I have, uh, mail keys, I have whatever, fill in the blank of what secrets you usually have in most of your applications. Then you can run PHP Artisan M Encrypt.

  41. And so this is, gives you the option of either generate a random encryption key or providing an encryption key. I'm gonna generate one, and here's the key, the cipher, and then the encrypted file. So now I have this encrypted file. Let's see what we need to do with it. And the key here is that decryption key.

  42. Now, this is just as sensitive and just as secret, if not more so than your AI API key or your app key if it's for a production environment, because this is what you would need to then keep in a password manager or something like that, that you. Securely share with your team, or if this is a production environment, make sure that it is exists only on your server and not anywhere within any local environment, for example.

  43. So let's say I have that anytime I then, let's say I could push this into, uh, my get repo because this is encrypted now, and now I am, let's say I'm moving to a different. Workstation, I'm having my team use this information and they need this particular info. Then I can use BHP Artisan M decrypt where I can paste in that decryption key.

  44. And in this case, the environment file already exists, but if for some reason it didn't, in this case, we're going to delete that dot NV and I run this again. Then all of a sudden in that decrypted envi, that decrypted file now becomes available to me at my workstation. I don't have to, uh, then make sure that I have all of the variables kind of in place. It just exists within that one decryption key. Security, especially within an application, is nothing to take lightly.

  45. So it's not something that you should just be like, Hey, this isn't a big production application. I don't really care if my API keys are even in a private repo or public repo is even worse, but hopefully you are a little bit smarter than that. But the goal is that you can use the tools, the things that Laravel gives out of the box to say, Hey.

  46. You can rotate your keys and do it gracefully so that there's no session mishaps and users logged out when you don't want them to. You can decrypt and encrypt your environment variables so that you can safely push them to a get repo and, and have those available to you as long as you are storing your encrypted environment key in a secure place.

  47. being smart with how you build your application and how you store your application, how you push it to GitHub is being a responsible developer. It's just as important as something like authorization. I even have a video up here that you can find about how you can use authorization within live wire application to make sure your users are not seeing something that they shouldn't be seen. It becomes, it comes down to being smart as a user, being smart as a developer, and not just thinking, eh, someone else is not going to do this.

  48. They're not going to be bad. Everyone on the internet is bad. They are going to make bad choices. They're going to make bad mistakes. They're going to do things bad to your application, so guard against that.

  49. Until next time, keep creating.

just you here