Skip to content

python: Use argument lists instead of shell=True for subprocess - #7703

Merged
cwhite911 merged 2 commits into
OSGeo:mainfrom
cwhite911:subprocess-no-shell
Jul 15, 2026
Merged

cwhite911 merged 2 commits into
OSGeo:mainfrom
cwhite911:subprocess-no-shell

Conversation

@cwhite911

Copy link
Copy Markdown
Contributor

Fixes the 6 Bandit B602 (shell=True, error severity) code scanning alerts in non-vendored code, as a robustness and portability cleanup:

  • grass.imaging.images2avi.writeAvi: the ffmpeg command was assembled by string concatenation and run through the shell. Now built as an argument list, with the caller-provided inputOptions/outputOptions strings split via shlex.split(), preserving the previous argument semantics.
  • grass.imaging.images2avi.readAvi: the command was a plain string passed without shell=True, which raises FileNotFoundError on non-Windows platforms (No such file or directory: 'ffmpeg -i input.avi im%d.jpg'), so this also fixes readAvi being broken on Linux and macOS.
  • grass.pygrass.modules.grid: set_region() built a g.region command string for the shell; now an explicit argument list. cmd_exe() passed shell=(sys.platform == "win32"); it now uses grass.script.core.Popen, which resolves the executable with shutil.which() and handles script tools on Windows, so the platform flag is no longer needed.

Verified locally:

  • GridModule (r.slope.aspect, 4 tiles, 2 processes) produces output bit-identical to a non-tiled run (r.mapcalc diff, max abs diff 0)
  • writeAvi/readAvi round trip with real ffmpeg (5 frames, correct shapes), exercising the shlex option splitting
  • direct set_region() call confirmed to apply the region
  • bandit 1.9.4 reports no findings in the two files

This is part of a larger effort to work through the open code scanning alerts, grouped into small PRs by issue type.

Written with the assistance of Claude Code.

In grass.imaging, build the ffmpeg command for writeAvi as an argument
list, splitting the caller-provided option strings with shlex. Convert
the readAvi command to a list as well; as a plain string without a
shell it failed with FileNotFoundError on non-Windows platforms. In
pygrass grid, build the g.region command in set_region as a list and
run tile commands in cmd_exe through grass.script.core.Popen, which
resolves the executable and handles script tools on Windows, instead
of passing shell=True or a platform-dependent shell flag.

Fixes 6 Bandit B602 (subprocess_popen_with_shell_equals_true) alerts.
@github-actions github-actions Bot added Python Related code is in Python libraries labels Jul 14, 2026
cwhite911 added a commit to cwhite911/grass that referenced this pull request Jul 15, 2026
Address review feedback: instead of skipping Bandit B607 globally
(which would also stop catching newly introduced violations), suppress
it inline at each existing call site. These are developer, build, and
test helpers that invoke known tools (git, gh, svn, msgfmt, mdl, man,
grass) by name, intentionally resolved through PATH, the same rationale
as the already-skipped B603.

The g.region call in pygrass grid is addressed separately in the
shell=True cleanup (OSGeo#7703).
Address review feedback: replace the "from grass.script import core as
gcore" module-alias import with a direct Popen import, and update
cmd_exe()'s three call sites accordingly.

Also switch set_region()'s two subprocess.Popen calls to the same
grass.script Popen used by cmd_exe(). That Popen resolves the executable
via shutil.which() and re-enables shell=True on Windows for script tools
like g.region, which the removed shell=True previously provided; plain
subprocess.Popen would fail to launch g.region on Windows.

Assisted by Claude Code.
@cwhite911
cwhite911 requested a review from petrasovaa July 15, 2026 13:20
@cwhite911 cwhite911 self-assigned this Jul 15, 2026
@cwhite911
cwhite911 merged commit 22df76e into OSGeo:main Jul 15, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants