Neural Mastery
← Practice
Detect Shell Metacharacters in Tool Argumentsmedium

Detect Shell Metacharacters in Tool Arguments

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement shell-metacharacter detection, a real, critical input check before a tool argument is EVER passed to a shell command (the exact class of bug behind real command-injection vulnerabilities).

Task

Implement has_shell_metacharacters(arg): return True if arg contains any of these characters: ;, |, &, $, backtick, >, <, \n, (, ).

Function Signature

has_shell_metacharacters(arg: str) -> bool

Examples

Example 1: clean argument
Input: arg = 'myfile.txt'
Output: False
Example 2: command chaining attempt
Input: arg = 'file.txt; rm -rf /'
Output: True
Example 3: pipe attempt
Input: arg = 'file.txt | cat /etc/passwd'
Output: True

Constraints

  • arg is a string
Python3Saved ✓
def has_shell_metacharacters(arg):
# Your implementation here
pass

Case 1: clean argument
Input: arg = 'myfile.txt'
Expected: False
Case 2: command chaining attempt
Input: arg = 'file.txt; rm -rf /'
Expected: True
Case 3: pipe attempt
Input: arg = 'file.txt | cat /etc/passwd'
Expected: True