2

I have a library that accepts plugins. The plugins are shell scripts. I want to prevent the plugins from changing the current working directory for any command in the script.

One thing I could do is somehow prevent changing directory by making cd or similar into some sort of no-op. This seems like a bad-idea and perhaps tyrannical / too heavy-handed.

The other thing I could do is force the plugins to pass a suite of tests, and in those tests somehow detect a change of directory in the script. (And fail the test if a change directory command was issued).

My question is - is there a good way to prevent cd'ing (and similar commands... what are they?), or detecting a cd within a shell script?

  • Can the plugin code be run only in a subshell? – thrig Jul 6 '17 at 21:49
  • @thrig sure, I could "force" my plugin authors to run the script in a subshell, I imagine. – Alexander Mills Jul 6 '17 at 21:57
3

With a plugins-run-in-subshell approach (though this has drawbacks mostly around whether plugins need to mangle state in the parent) then the plugin can issue whatever chdir calls it wants, as that will not change the working directory of the parent:

assumethisistheplugincode() {
    cd /tmp
    pwd
}

cd /var/tmp || exit 1
printf "before "
pwd

# and this is how we run a "plugin", in a subshell...
( assumethisistheplugincode )

printf "after "
pwd
  • the plugins definitely don't have to change state in the parent, they just need to do some side-effect i/o (that's why they can be plugins, because they don't affect program state). Although I guess I am totally unfamiliar with how a child script could change the environment of a parent script? Is there some explanation for that? I frankly was not aware that child scripts could change parent script state. – Alexander Mills Jul 6 '17 at 23:29
  • I guess I need to elaborate - the entire script (the entire .sh file) is the plugin. The .sh file is invoked by a parent process. Nowhere in the .sh file code do I want the pwd/cwd to be changed. – Alexander Mills Jul 6 '17 at 23:31
  • 1
    Invoked how? If you source or . the code runs within the parent process. – thrig Jul 6 '17 at 23:36
  • right, maybe I mistaken but in the case of source child.sh, the child process and parent process are one and the same. I was under the impression that a child process or subshell could never modify the parent's env. So to say "and then the child process won't modify the parent process" is something should never be said, because it's misleading. – Alexander Mills Jul 7 '17 at 4:11
1

a simpler approach

copy the script inside a directory where it cannot 'escape'

execute th script :

chroot /directory /bin/bash ./script.sh

  • might work. however the requirement is that it runs in a particular directory X within the user's home dir...I want to prevent cd away from X. But X has to be their project root. – Alexander Mills Jul 7 '17 at 0:12
  • you can also switch user chroot /chroot_dir /bin/bash -c "su ...options... -c ./startup.sh" – Massimo Jul 7 '17 at 0:21
  • Thanks, can you explain in English how this works and what exactly is going on? Just need little bit more explanation and I will try it. – Alexander Mills Jul 7 '17 at 3:28
  • I cannot, see stackoverflow.com/questions/8157931/… – Massimo Jul 10 '17 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.