Skip to content

Instantly share code, notes, and snippets.

@ormaaj ormaaj/pattest.py
Last active Sep 12, 2019

Embed
What would you like to do?
Shell pattern quote/escape fuzzer.
#!/usr/bin/env python3
import subprocess, itertools
class Shell(list):
def __init__(self, shell, cmds):
self.shell = shell
super().__init__([(x, self.__run(x)) for x in cmds])
def __iter__(self):
while True:
try:
self[0][1].communicate()
yield (lambda x: (x[0], x[1].returncode, self.shell))(self.pop(0))
except IndexError:
raise StopIteration()
def __run(self, cmd):
return subprocess.Popen([self.shell, "-c", cmd])
def main():
template = r'x=\\; case \\x in {0}{1}x) :;; *) false; esac'
tests = [template.format(*x) for x in itertools.product(['"${x}"', '${x}', r'"\\"', r'\\'], repeat=2)]
shells = [Shell(x, tests) for x in ["bash", "dash", "ksh", "mksh", "zsh", "bb", "posh", "jsh"]]
print(" " * 54, " ".join(x.shell for x in shells))
for row in zip(*shells):
print("{0:55}{1}".format(row[0][0], "".join(str(test) + (" " * len(shell)) for x, test, shell in row)))
if __name__ == "__main__":
main()
# bash dash ksh mksh zsh bb posh jsh
# x=\\; case \\x in "${x}""${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in "${x}"${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1
# x=\\; case \\x in "${x}""\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in "${x}"\\x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in ${x}"${x}"x) :;; *) false; esac 1 0 1 1 1 1 1 1
# x=\\; case \\x in ${x}${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1
# x=\\; case \\x in ${x}"\\"x) :;; *) false; esac 1 0 1 1 1 1 1 1
# x=\\; case \\x in ${x}\\x) :;; *) false; esac 1 0 1 1 1 1 1 1
# x=\\; case \\x in "\\""${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in "\\"${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1
# x=\\; case \\x in "\\""\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in "\\"\\x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in \\"${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in \\${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1
# x=\\; case \\x in \\"\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1
# x=\\; case \\x in \\\\x) :;; *) false; esac 1 1 1 1 1 1 1 1
# vim: set fenc=utf-8 ff=unix ft=python ts=4 sts=4 sw=4 nowrap et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.