Skip to content

fix: remove shell injection vulnerabilities in hdfs_utils.py - #253

Closed
fwh888 wants to merge 1 commit into
ONSdigital:mainfrom
fwh888:fix-shell-injection
Closed

fix: remove shell injection vulnerabilities in hdfs_utils.py#253
fwh888 wants to merge 1 commit into
ONSdigital:mainfrom
fwh888:fix-shell-injection

Conversation

@fwh888

@fwh888 fwh888 commented Aug 30, 2026

Copy link
Copy Markdown

Summary

This PR fixes three shell injection vulnerabilities in rdsa_utils/cdp/helpers/hdfs_utils.py identified during a code audit of the CDP module (see issue #246).

Vulnerabilities Fixed

1. create_txt_from_string() — shell injection via string_to_write and path

Before:

subprocess.call([f'echo "{string_to_write}" | hadoop fs -put - {path}'], shell=True)

After: Uses stdin-based piping via subprocess.Popen:

proc = subprocess.Popen([hadoop, fs, -put, -, path], stdin=subprocess.PIPE, ...)
proc.communicate(input=string_to_write.encode("utf-8"), timeout=15)

2. get_date_modified() — shell injection via filepath

Before:

subprocess.Popen(f"hadoop fs -stat %y {filepath}", shell=True)

After: List-based command:

subprocess.Popen(["hadoop", "fs", "-stat", "%y", filepath], ...)

3. read_dir_files_recursive() — shell injection via path

Before: Shell pipeline with grep/tr/cut:

subprocess.Popen(f"hadoop fs -ls -R {path} | grep -v ^d | tr -s ' ' | cut -d ' ' -f 8-", shell=True)

After: Pure Python parsing of hadoop fs -ls -R output.

Testing

  • All 20 existing tests pass (verified locally)
  • Tests updated to verify list-based commands instead of shell=True
  • Added test case for read_dir_files_recursive output parsing

Three shell injection vulnerabilities fixed by removing shell=True:

1. create_txt_from_string(): Replaced echo | hadoop fs -put - with
   stdin-based subprocess.Popen, piping the string directly to the
   hadoop process via stdin.

2. get_date_modified(): Replaced f-string + shell=True with list-based
   subprocess.Popen(['hadoop', 'fs', '-stat', '%y', filepath]).

3. read_dir_files_recursive(): Replaced shell pipeline (grep/tr/cut)
   with pure Python parsing of 'hadoop fs -ls -R' output, eliminating
   the shell injection vector in the path parameter.

Tests updated to verify list-based commands and stdin piping.
@coatet

coatet commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Closed, as we do not accept submissions from users outside the ONS and I suspect this may be an LLM.

@coatet coatet closed this Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants