News for hackers. Stuff that matters.
Re:I'm Switching to Python and Actually Liking It by bjourne on Wednesday July 16, 2025 @04:40PM (#44586575)
Neophytes take notice. Attention to details like this is what separates truly great programmers from merely good ones. That said, for scripts reusable by others you should use command line arguments . Environment variables in lieu of command line arguments is a huge code smell.
Re:I'm Switching to Python and Actually Liking It by shakna on Wednesday July 16, 2025 @05:09PM (#44586800)
You don't generally want API keys accidentally recorded into someone's bash history.
Re:I'm Switching to Python and Actually Liking It by zahlman on Wednesday July 16, 2025 @08:47PM (#44588521)
What exactly is your threat model, where the attacker can read ~/.bash_history but can't execute (or capture output from) /usr/bin/env?
Re:I'm Switching to Python and Actually Liking It by dapperdrake on Wednesday July 16, 2025 @11:36PM (#44589458)
Threat model: shell scripts
Re:I'm Switching to Python and Actually Liking It by pastage on Wednesday July 16, 2025 @10:09PM (#44589007)
You do NOT save passwords in shell history, it is insanely insecure. Lets begin with the passwords being readable by everything that can list tasks.

You can protect passwords in a password manager. You do not need to keep the passwords in env and I do not.

Re:I'm Switching to Python and Actually Liking It by zahlman on Wednesday July 16, 2025 @10:12PM (#44589025)
> Lets begin with the passwords being readable by everything that can list tasks.

Why are processes running that can do this, that I don't already fully trust?

> You can protect passwords in a password manager.

What's your plan for supplying the password to the program, given that people will want to automate use of the program?

Re:I'm Switching to Python and Actually Liking It by ftigis on Thursday July 17, 2025 @12:58AM (#44589830)
How about not accidentally showing tokens and credentials when live screen sharing?
Re:I'm Switching to Python and Actually Liking It by shakna on Thursday July 17, 2025 @03:07AM (#44590575)
CI and other build systems. Where tokens have been stolen in the past, by users not caring, and a VM not being properly cleaned.
Re:I'm Switching to Python and Actually Liking It by fc417fc802 on Friday July 18, 2025 @01:02AM (#44601370)
The threat model is that history is persistent while the environment isn't. That said, whenever possible you should handle secrets using file descriptors as opposed to environment variables.
Re:I'm Switching to Python and Actually Liking It by RadiozRadioz on Wednesday July 16, 2025 @05:12PM (#44586832)
For this example, don't just command line arguments. There's an API key there, you don't want an API key visible in your cmdline.
Re:I'm Switching to Python and Actually Liking It by bjourne on Wednesday July 16, 2025 @05:56PM (#44587235)
Then how would you SET the API key in the first place? :) The argument doesn't make any sense at all.
Re:I'm Switching to Python and Actually Liking It by medstrom on Wednesday July 16, 2025 @06:32PM (#44587534)
In some .profile or .envrc or what you'd call such a file, I suppose.
Re:I'm Switching to Python and Actually Liking It by NoGravitas on Thursday July 17, 2025 @01:55PM (#44596122)
And you expect someone will be able to read your bash_history, but not your .profile?
Re:I'm Switching to Python and Actually Liking It by medstrom on Friday July 18, 2025 @01:27PM (#44607486)
Fair, I wasn't thinking about the details of how someone would lift out the bash history.
Re:I'm Switching to Python and Actually Liking It by fc417fc802 on Friday July 18, 2025 @01:03AM (#44601376)
Using read or an equivalent, presumably. Just because you don't know why a practice is recommended against doesn't mean that there isn't a good alternative.
Re:I'm Switching to Python and Actually Liking It by connorbrinton on Thursday July 17, 2025 @09:00AM (#44592883)
Typer has a great feature that lets you optionally accept argument and flag values from environment variables by providing the environment variable name:

https://typer.tiangolo.com/tutorial/arguments/envvar/

It's especially nice for secrets. Best of both worlds :)

Re:I'm Switching to Python and Actually Liking It by bjourne on Thursday July 17, 2025 @01:19PM (#44595674)
No, that's an anti-feature. :) Sibling comments here claim that command line arguments "leak" whereas environment variables does not. It's plain wrong. An attacker with access to arbitrary processes' cmdline surely also has access to their environ. Store secrets in files, not in the environment. Now you can easily change secret by pointing the --secret-file parameter to a different file. The only reason people use BLABLA_API_KEY variables is because Heroku or something did it back in the day and everyone cargo-culted this terrible pattern.

One could write a huge treatise on everything that is wrong with environment variables. Avoid them like the plague. They are a huge usability PITA.

Re:I'm Switching to Python and Actually Liking It by fc417fc802 on Friday July 18, 2025 @01:13AM (#44601426)
This is bad advice. Please don't make claims about security if you're making it up as you go.

Environment variables are substantially more secure than plain text files because they are not persistent. There are utilities for entering secrets into them without leaking them into your shell history.

That said, you generally should not use an environment variable either. You should use a secure temporary file created by your shell and pass the associated file descriptor. Most shells make such functionality available but the details differ (ie there is no fully portable approach AFAIK).

The other situation that sometimes comes up is that you are okay having the secret on disk in plain text but you don't want it inadvertently commited to a repository. In those cases it makes sense to either do as you suggested and have a dedicated file, or alternatively to set an environment variable from ~/.bashrc or similar.

Re:I'm Switching to Python and Actually Liking It by bjourne on Friday July 18, 2025 @08:44PM (#44611476)
Good luck storing your private keys in environment variables!