3

After reading some articles about the EmeraldWhale-malware collecting sensitive credentials through Git I am looking for best-practices to use environment variables in a Symfony project.

  1. A option would be to set the environment variables at runtime like: DB_NAME=my_database DB_USER=my_user DB_PASSWORD=my_password symfony server:start By using this method you wouldn't even ever have a .env-file for production, mitigating the risk of even ever publishing the credentials through accidentally pushing a .env file to your VCS.

  2. I also took a look at Symfony Secrets. This seems way more secure then using global CLI variables or a .env. But because both the private- and public-key for this asymmetric encryption is saved on the server, how exactly is this more secure? An intruder accessing the server could still use the private-key to get the credentials (same for CLI variables), but in this case the private key could still be pushed to a VCS by accident like a .env-file?

So, what is the most secure and best-practice way to store your credentials in a Symfony project?

1 Answer 1

4

As you correctly point out, the master key pair for the Symfony vault is stored as plaintext in a file. If this file gets compromised, then so do all secrets. Really the only benefit of this approach is that instead of having to protect multiple secrets scattered across configuration files, you only have to take care of a a single master secret at a clearly defined location.

Setting secrets when the application is started can mitigate some risks (e.g., you cannot accidentally commit them), but it's not perfect either. The plaintext keys are still accessible to the application and may also be logged as command line arguments.

A better approach is to use the key store provided by the operating system, e.g., the Linux Kernel Key Retention Service. This allows you to keep keys in kernel space. If you additionally perform all cryptographic operations with the Linux Kernel Crypto API, then you can avoid ever exposing the key to userspace (this is what Cloudflare does, for example). Using a Trusted Platform Module as a hardware-based solution would be another option. However, I'm not aware that PHP – let alone Symfony – supports any of this directly, so you'll need to implement some agent program or PHP extension. Whether the effort is justified depends entirely on the value of the application data and the risks you're facing (which are hard to assess without knowing the application and server setup).

5
  • Thanks for your answer! So by using the Linux Kernel Crypto API even when the webserver would be compromised, the keys would remain protected? Or at least more protected then saving both sides of the asymmetric key encryption in plain on the webserver.
    – O'Niel
    Commented Nov 13 at 9:52
  • @O'Niel: Yes, the attacker wouldn't be able to extract the key even if the application is compromised. But be aware that the implementation won't be easy. If you just want to prevent credentials from accidentally being commited, there are easier solutions like using Symfony Secrets and setting the private key through the SYMFONY_DECRYPTION_SECRET environment variable (instead of a file).
    – Ja1024
    Commented Nov 13 at 11:31
  • Yes that would seem to avoid accidentally committing the key. But by using your suggestion of using Linux Kernel API even with a compromised web-app the variables would remain relatively secure. The implementation indeed will not be easy but seems like a nice challenge. I'm going to write a custom PHP extension for it in Rust/C.
    – O'Niel
    Commented Nov 13 at 11:38
  • 1
    @O'Niel if you do write that extension, I strongly recommend that you ask for a review of it over on Code Review when it's finished and working to your satisfaction. I might be able to review the C parts (or C++ if you so choose). Commented Nov 13 at 16:29
  • Thanks, I'll edit my question here to the CodeReview question when finished. I don't think I'll need C. Seems Rust has crates to make syscalls or interact with the Linux KeyRing already.
    – O'Niel
    Commented Nov 13 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .