Autoenv rewrite, security and scripting#2083
Autoenv rewrite, security and scripting#2083sophiajt merged 176 commits intonushell:masterfrom samhedin:direnv-rewrite
Conversation
However, it does not use existing code that well.
|
Alright! Another commit, another try. The format has now changed. The script section and its keys (entryscripts and exitscripts) are optional. [env]
k = "v"
[scriptvars]
myscript = "echo 'myval'"
[scripts]
entryscripts = ["touch hello.txt"]
exitscripts = ["touch bye.txt"] |
|
Question: Should autoenv give an error message if any section is missing (i.e |
|
@samhedin - I'd say it should be quiet and only work with what it is given. |
|
@samhedin - gave it a go. If I do: I'm able to trust the current .nu-env, and it looks like it works great without a restart. I ran into an issue if I try to untrust in the same session: If I |
|
As I said in the original post, I didn't want to hot-reload after But things changed - now there are checks to make sure autoenv is only run once per directory change, so maybe it is fine to do IO each time. Have a look at the latest commit, changes from |
|
@samhedin Cool, will keep an eye open for performance issues. I was just trying the same test again, and getting the same result. If you're in a trusted directory, and you untrust it, do you still see the same environment variables? Feels like we're close. |
|
Did it work out alright? Untrusting a directory will not immediately unset the variables - you need to leave the directory as usual. Untrust only prevents any new variables and scripts from running. Is this alright behavior? |
|
I think the behavior of Direnv is that it actively strips any env vars that it controls when you "un-allow" a .envrc (directory) |
|
Huh, interesting. I would have expected the current behavior to be the norm. I don't believe that this would be a trivial change, but if it's requested I will of course give it a proper go. |
|
@samhedin - I think it's worth giving it a try, and seeing if it's doable. |
|
If you run |
|
I'd lean towards (b). I know that could leave things in a weird state, but I think when we trust a directory, we trust it to run the scripts. When we untrust, we take back that trust and probably shouldn't run the scripts. I'd start there, at least. |
|
I agree. I want to add that for a while I didn't even consider adding One issue remains: Vars set by [edit] |
|
@samhedin - is it possible that it's going up into a parent directory and finding something? |
|
This was my initial thought too, but nope. I can't reproduce the behavior anymore though... |
|
Did some testing with the trust/untrust, scriptvars, and scripts. Things look good enough we should land this so we can use it and continue improving it based on how it feels in practice. Thanks again for putting all this together! @samhedin - I did notice that the paths in my nu-env.toml weren't canonical at one point but then couldn't reproduce it. Maybe you fixed something along the way? |
Super exciting! I am ready for a stream of bug reports.
Yes! At some point I started using |
Autoenv
You no longer have to restart nushell after using
autoenv trust. On startup, nushell readsnu-env.tomland stores the trusted files and their hashes. It uses thesha2crate to allow for predictable hash values, sinceDefaultHashercan change between versions. This was by request, and if it would be better to avoid adding the dependency, it is simple to revert back toDefaultHasher.Autoenv trust/untrust
You mark a
.nu-envfile as trusted or untrusted by runningautoenv trustorautoenv untrustin the directory it is in, or by giving the commands the dir path.If you enter a directory with a
.nu-envfile which was either not trusted on startup, or has a different hash than it had on startup,nu-env.tomlis read again and the.nu-envhash is checked against it.You still need to restart after using
autoenv untrust, as I could not come up with a good way to reload this. Just re-readingnu-env.tomlall the time would work, but it also feels wrong to read a file all the time like that.Scripting
In addition to
env, each.nu-envfile may now contain the linesentryscripts,exitscriptsand the sectionscriptvars.The keys in
scriptvarsmust point to a valid command which resolves to a value. After that, the key and value are tracked and removed by nushell like regular environment variables listed underenv.Commands in
entryscriptsandexitscriptsare run when you enter and after you exit a directory. Nushell takes no responsibility for these and does not even show their output. Note thatexitscriptsare not run inside the directory they are declared in.[edit: new toml format]
All comments appreciated! Please do test it if you can.