From 930c408a80584beeb1718a2eee5c3a8b15672fd2 Mon Sep 17 00:00:00 2001 From: "Florine W. Dekker" Date: Sun, 3 Dec 2023 17:28:55 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=83=20mommy=20escapes=20confusing=20sc?= =?UTF-8?q?enarios~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + README.md | 6 +++--- src/main/sh/mommy | 27 +++++++++++++++++---------- src/test/sh/unit_spec.sh | 8 ++++++++ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15d4e68..218817e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### fixed * 🚒 mommy fixes the description url in her manual page~ ([#81](https://github.com/FWDekker/mommy/issues/81)) ([#82](https://github.com/FWDekker/mommy/issues/82)) * 🕰️ mommy uses the correct types of changelogs in the right places ([#83](https://github.com/FWDekker/mommy/issues/83)) +* 🏃 mommy no longer crashes when using `\` or `&` in variables~ ([#84](https://github.com/FWDekker/mommy/issues/84)) ([#87](https://github.com/FWDekker/mommy/issues/87)) ## [1.2.6] -- 2023-11-29 diff --git a/README.md b/README.md index 168c0c4..aeeba25 100644 --- a/README.md +++ b/README.md @@ -491,10 +491,10 @@ elements that contain whitespace only, and elements that start with a `#` are ig then mommy will never use templates that contain `cat`, and will never use templates that contain `dog`~ ### 🧬 custom templates -you can add a [list](#-lists) of your own compliments to either `MOMMY_COMPLIMENTS` or `MOMMY_COMPLIMENTS_EXTRA`, but -there is a slight difference: +you can add a [list](#-lists) of your own compliments to either `MOMMY_COMPLIMENTS` or `MOMMY_COMPLIMENTS_EXTRA`. +there is a slight difference between the two lists: -* if you want both the default _and_ your own compliments, add your own compliments to `MOMMY_COMPLIMENTS_EXTRA`, but +* if you want both the default _and_ your own compliments, add your own compliments to `MOMMY_COMPLIMENTS_EXTRA`~ * if you want your own compliments and _not_ the default compliments, add your own compliments to `MOMMY_COMPLIMENTS`~ and similarly so for encouragements~ diff --git a/src/main/sh/mommy b/src/main/sh/mommy index 004940c..11efb57 100755 --- a/src/main/sh/mommy +++ b/src/main/sh/mommy @@ -90,7 +90,7 @@ list_contains_any() { # Takes the list in stdin and (1) removes all lines starting with `#`, (2) replaces each `/` with a newline, (3) removes # all blank lines, and (4) removes all entries that contain any of the entries in the sanitized list `$1` as a # substring. -list_sanitize() { +list_normalize() { cat | grep -v "^#" | tr "/" "$n" | @@ -113,6 +113,13 @@ else } fi +# Escapes stdin to be used as a replacement string in sed. +# +# Only the characters `\` and `&` need replacement, since we assume that `/` does not occur. +escape_sed_replacement() { + cat | sed -e 's/\\/\\\\/g' -e 's/&/\\\&/g' +} + ## Functions # Prints `$2`, but with color depending on `$1`. If `$1` equals `lolcat`, stdin is piped to `lolcat`. If `$1` is empty, @@ -170,12 +177,12 @@ split_pronouns() { # 4. prepends `$4` and appends `$5`; and # 5. writes to stdout. fill_template() { - sweetie="$(echo "$1" | list_sanitize | list_choose)" - split_pronouns "$(echo "$2" | list_sanitize | list_choose)" - caregiver="$(echo "$3" | list_sanitize | list_choose)" + sweetie="$(echo "$1" | list_normalize | list_choose | escape_sed_replacement)" + split_pronouns "$(echo "$2" | list_normalize | list_choose | escape_sed_replacement)" + caregiver="$(echo "$3" | list_normalize | list_choose | escape_sed_replacement)" - prefix="$(echo "$4" | list_sanitize | list_choose)" - suffix="$(echo "$5" | list_sanitize | list_choose)" + prefix="$(echo "$4" | list_normalize | list_choose | escape_sed_replacement)" + suffix="$(echo "$5" | list_normalize | list_choose | escape_sed_replacement)" template="$(cat | sed -e "s/%%SWEETIE%%/$sweetie/g" \ -e "s/%%THEY%%/$they/g" \ @@ -254,7 +261,7 @@ else fi # Choose and fill template (if enabled) - if list_contains_any "$command_exit_code" "$(echo "$MOMMY_IGNORED_STATUSES" | list_sanitize)"; then + if list_contains_any "$command_exit_code" "$(echo "$MOMMY_IGNORED_STATUSES" | list_normalize)"; then exit "$command_exit_code" elif [ "$command_exit_code" -eq 0 ] && [ "$MOMMY_COMPLIMENTS_ENABLED" = "1" ]; then templates="$MOMMY_COMPLIMENTS/$MOMMY_COMPLIMENTS_EXTRA" @@ -264,11 +271,11 @@ else exit "$command_exit_code" fi - color="$(echo "$MOMMY_COLOR" | list_sanitize | list_choose)" - forbidden_words="$(echo "$MOMMY_FORBIDDEN_WORDS" | list_sanitize)" + color="$(echo "$MOMMY_COLOR" | list_normalize | list_choose)" + forbidden_words="$(echo "$MOMMY_FORBIDDEN_WORDS" | list_normalize)" response="$(echo "$templates" | - list_sanitize "$forbidden_words" | + list_normalize "$forbidden_words" | list_choose | fill_template "$MOMMY_SWEETIE" "$MOMMY_PRONOUNS" "$MOMMY_CAREGIVER" "$MOMMY_PREFIX" \ "$MOMMY_SUFFIX" "$MOMMY_CAPITALIZE")" diff --git a/src/test/sh/unit_spec.sh b/src/test/sh/unit_spec.sh index 01df62d..6520340 100755 --- a/src/test/sh/unit_spec.sh +++ b/src/test/sh/unit_spec.sh @@ -342,6 +342,14 @@ Describe "mommy" End Describe "template variables" + It "escapes sed-specific characters" + set_config "MOMMY_COMPLIMENTS='>%%SWEETIE%%<';MOMMY_SWEETIE='&\\'" + + When run "$MOMMY_EXEC" -c "$MOMMY_CONFIG_FILE" true + The error should equal ">&\\<" + The status should be success + End + It "replaces %%SWEETIE%%" set_config "MOMMY_COMPLIMENTS='>%%SWEETIE%%<';MOMMY_SWEETIE='attempt'"