Skip to content

Commit

Permalink
Allow to add to the existing environment in process instead of repl…
Browse files Browse the repository at this point in the history
…acing it (Gallopsled#1763)

* Add env_add to the process module

* Don't mutate passed `env` parameter dictionary

Co-authored-by: Arusekk <[email protected]>

* Update CHANGELOG

* Use a boolean flag instead of env_add

There should be preferably only one obvious way to pass env.

* Update CHANGELOG

---------

Co-authored-by: peace-maker <[email protected]>
Co-authored-by: peace-maker <[email protected]>
Co-authored-by: Arusekk <[email protected]>
  • Loading branch information
4 people authored Nov 24, 2023
1 parent 4ef4746 commit 65f9d57
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ The table below shows which release corresponds to each branch, and what date th
- [#2277][2277] elf: Resolve more relocations into GOT entries
- [#2281][2281] FIX: Getting right amount of data for search fix
- [#2293][2293] Add x86 CET status to checksec output
- [#1763][1763] Allow to add to the existing environment in `process` instead of replacing it

[2277]: https://github.com/Gallopsled/pwntools/pull/2277
[2281]: https://github.com/Gallopsled/pwntools/pull/2281
[2293]: https://github.com/Gallopsled/pwntools/pull/2293
[1763]: https://github.com/Gallopsled/pwntools/pull/1763

## 4.12.0 (`beta`)

Expand Down
19 changes: 11 additions & 8 deletions pwnlib/tubes/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class process(tube):
cwd(str):
Working directory. Uses the current working directory by default.
env(dict):
Environment variables. By default, inherits from Python's environment.
Environment variables to add to the environment.
ignore_environ(bool):
Ignore Python's environment. By default use Python's environment iff env not specified.
stdin(int):
File object or file descriptor number to use for ``stdin``.
By default, a pipe is used. A pty can be used instead by setting
Expand Down Expand Up @@ -224,6 +226,7 @@ def __init__(self, argv = None,
executable = None,
cwd = None,
env = None,
ignore_environ = None,
stdin = PIPE,
stdout = PTY,
stderr = STDOUT,
Expand Down Expand Up @@ -255,7 +258,7 @@ def __init__(self, argv = None,
original_env = env

if shell:
executable_val, argv_val, env_val = executable, argv, env
executable_val, argv_val, env_val = executable or '/bin/sh', argv, env
else:
executable_val, argv_val, env_val = self._validate(cwd, executable, argv, env)

Expand Down Expand Up @@ -287,14 +290,14 @@ def __init__(self, argv = None,
#: Full path to the executable
self.executable = executable_val

if ignore_environ is None:
ignore_environ = env is not None # compat

#: Environment passed on envp
self.env = os.environ if env is None else env_val
self.env = {} if ignore_environ else dict(getattr(os, "environb", os.environ))

if self.executable is None:
if shell:
self.executable = '/bin/sh'
else:
self.executable = which(self.argv[0], path=self.env.get('PATH'))
# Add environment variables as needed
self.env.update(env_val or {})

self._cwd = os.path.realpath(cwd or os.path.curdir)

Expand Down

0 comments on commit 65f9d57

Please sign in to comment.