How it works...

Let's understand our script in detail:

  1. First, we begin by creating the bad_input.sh script—it takes $1 (or argument 1) and runs the list or ls command.
  2. Running the following commands, we can either list everything in the directory, subdirectory, or even traverse directories backwards! This is clearly not good and security vulnerabilities have even allowed malicious hackers to traverse through a web server—the idea is to contain the input for predictable results and to control input instead of allowing everything:
$ touch TEST.txt$ mkdir new_dir/$ bash bad_input.sh "."...$ bash bad_input.sh "../"../all the files backwards
  1. In the second script, better_input.sh, the input is sanitized by the following steps. Additionally, ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.