|
| 1 | +stdisplay(1) -- Sanitize text to be safely printed to the terminal |
| 2 | +================================================================= |
| 3 | + |
| 4 | +<!-- |
| 5 | +# Copyright (C) 2025 Benjamin Grande M. S. <ben.grande.b@gmail.com> |
| 6 | +# Copyright (C) 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org> |
| 7 | +# See the file COPYING for copying conditions. |
| 8 | +--> |
| 9 | + |
| 10 | +## SYNOPSIS |
| 11 | + |
| 12 | +`stprint [TEXT...]`<br> |
| 13 | +`stecho [TEXT...]`<br> |
| 14 | +`stcat [FILE...]`<br> |
| 15 | +`sttee [FILE...]`<br> |
| 16 | +`stsponge [FILE]`<br> |
| 17 | + |
| 18 | +## DESCRIPTION |
| 19 | + |
| 20 | +`stdisplay` is a Python library used to safely print text from untrusted |
| 21 | +sources sanitizing non-ASCII characters and dangerous ANSI escape |
| 22 | +sequenes, from the latter, only a strict subset of SGR (Select Graphic |
| 23 | +Rendition) attributes, line feeds (`\n`) and horizontal tabs (`\t`) are |
| 24 | +allowed). |
| 25 | + |
| 26 | +The following tests are done in order to verify if SGR should be enabled |
| 27 | +and how large it's set should be: |
| 28 | + |
| 29 | +1. If the environment variable `$NO_COLOR` is set to a non-empty value, |
| 30 | + SGR is disabled. |
| 31 | +2. If the environment variable `$COLORTERM` is set to `truecolor`, |
| 32 | + 24-bit SGR is enabled. |
| 33 | +3. If none of the above matches, the terminal referenced by the |
| 34 | + environment variable `$TERM` is queried for its `colors` capability, |
| 35 | + which returns how many colors the terminal supports. |
| 36 | + |
| 37 | +Tools based on this library have no option parameters, everything is |
| 38 | +treated either as text or file, depending on the tool used, therefore, |
| 39 | +`--` is interpreted as text and not end of options. |
| 40 | + |
| 41 | +Each tool behaves as if their shell utility counterpart was used without |
| 42 | +any options: |
| 43 | + |
| 44 | +| Tool | Sanitizer | |
| 45 | +| -------- | --------- | |
| 46 | +| strint | printf | |
| 47 | +| stecho | echo | |
| 48 | +| stcat | cat | |
| 49 | +| sttee | tee | |
| 50 | +| stsponge | sponge | |
| 51 | + |
| 52 | +## RETURN VALUES |
| 53 | + |
| 54 | +* `0` Successfully printed text. |
| 55 | +* Any other return value is an error. |
| 56 | + |
| 57 | +## EXAMPLE: SGR CONTROL |
| 58 | + |
| 59 | +Enable 24-bit SGR if the terminfo database is outdated: |
| 60 | + |
| 61 | +<code> |
| 62 | +COLORTERM=truecolor stprint "$(printf '%b' "\033[38;2;0;0;255mSome color\033[m")"<br> |
| 63 | +</code> |
| 64 | + |
| 65 | +Disable SGR: |
| 66 | + |
| 67 | +<code> |
| 68 | +NO_COLOR=1 stprint "$(printf '%b' "\033[31mNo color\033[m")"<br> |
| 69 | +TERM=dumb stprint "$(printf '%b' "\033[31mNo color\033[m")"<br> |
| 70 | +</code> |
| 71 | + |
| 72 | +## EXAMPLE: STCAT |
| 73 | + |
| 74 | +Copy standard input to standard output: |
| 75 | + |
| 76 | +<code> |
| 77 | +printf '%s' "${untrusted_string}" | stcat<br> |
| 78 | +stcat < /untrusted/file<br> |
| 79 | +.<br> |
| 80 | +untrusted-cmd 2>&1 | stcat<br> |
| 81 | +# Or with Bash/Zsh syntax:<br> |
| 82 | +stcat < <(untrusted-cmd 2>&1) |
| 83 | +</code> |
| 84 | + |
| 85 | +Concatenate files: |
| 86 | + |
| 87 | +<code> |
| 88 | +stcat /untrusted/file /untrusted/log |
| 89 | +</code> |
| 90 | + |
| 91 | +Piping to a pager (can also be `sttee`): |
| 92 | + |
| 93 | +<code> |
| 94 | +data | stcat | less -R<br> |
| 95 | +GIT_PAGER="stcat | less -R" git log |
| 96 | +</code> |
| 97 | + |
| 98 | +Print a ownership restricted file with external programs: |
| 99 | + |
| 100 | +<code> |
| 101 | +sudo -- stcat /untrusted/log<br> |
| 102 | +</code> |
| 103 | + |
| 104 | +## EXAMPLE: STTEE/STSPONGE |
| 105 | + |
| 106 | +The tools `sttee` and `stsponge` have the same usage but differ when |
| 107 | +writing. While `sttee` always writes to standrd output as soon as |
| 108 | +it is read, `stsponge` only writes to standard output if no file is |
| 109 | +provided and the write is atomic. |
| 110 | + |
| 111 | +Copy standrd input to standard output and optionally to a file: |
| 112 | + |
| 113 | +<code> |
| 114 | +printf '%s' "${untrusted_string}" | sttee<br> |
| 115 | +printf '%s' "${untrusted_string}" | sttee /trusted/file<br> |
| 116 | +sttee /trusted/file < /untrusted/file</br> |
| 117 | +</code> |
| 118 | + |
| 119 | +Only `stsponge` can sanitize a file in-place: |
| 120 | + |
| 121 | +<code> |
| 122 | +stsponge /untrusted/file < /untrusted/file |
| 123 | +</code> |
| 124 | + |
| 125 | +## EXAMPLE: STPRINT/STECHO |
| 126 | + |
| 127 | +The tools `stprint` and `stecho` have the same usage but differ in |
| 128 | +formatting. While `stprint` does print text as is, `stecho` adds a space |
| 129 | +between each argument and a newline at the end of the string. |
| 130 | + |
| 131 | +Print a variable value is simple: |
| 132 | + |
| 133 | +<code> |
| 134 | +stprint "${untrusted_string}" |
| 135 | +</code> |
| 136 | + |
| 137 | +Note that items are joined without word-splitting (no space separation): |
| 138 | + |
| 139 | +<code> |
| 140 | +stprint "${untrusted_string}" "${another_untrusted_string}" |
| 141 | +</code> |
| 142 | + |
| 143 | +To have space separated items, simply add a space between them: |
| 144 | + |
| 145 | +<code> |
| 146 | +stprint "${untrusted_string} ${another_untrusted_string}"<br> |
| 147 | +stprint "${untrusted_string}" " ${another_untrusted_string}" |
| 148 | +</code> |
| 149 | + |
| 150 | +Print with heredoc to avoid quoting problems: |
| 151 | + |
| 152 | +<code> |
| 153 | +stprint <<EOF<br> |
| 154 | +${untrusted_string}<br> |
| 155 | +EOF |
| 156 | +</code> |
| 157 | + |
| 158 | +### EXAMPLE: STPRINT WITH VARIABLES |
| 159 | + |
| 160 | +Print a variable as is: |
| 161 | + |
| 162 | +<code> |
| 163 | +var="$(stprint "${untrusted_string}")"<br> |
| 164 | +## Or Bash/Zsh syntax:<br> |
| 165 | +printf -v var '%s' "$(stprint "${red}Hey${nocolor}: ${untrusted_string}")"<br> |
| 166 | +.<br> |
| 167 | +## Raw print:<br> |
| 168 | +printf '%s' "${var}" |
| 169 | +</code> |
| 170 | + |
| 171 | +Interpret wanted escapes before passing them: |
| 172 | + |
| 173 | +<code> |
| 174 | +red="$(printf '%b' "\033[31m")"<br> |
| 175 | +nocolor="$(printf '%b' "\033[m")"<br> |
| 176 | +## Or Bash/Zsh syntax:<br> |
| 177 | +red=$"\033[31m"<br> |
| 178 | +nocolor=$"\033[m"<br> |
| 179 | +.<br> |
| 180 | +## Raw assignment:<br> |
| 181 | +var="$(stprint "${red}Hey${nocolor}: ${untrusted_string}")" |
| 182 | +</code> |
| 183 | + |
| 184 | +### EXAMPLE: STPRINT MISUSE |
| 185 | + |
| 186 | +*Warning*: Reinterpreting the escapes from the data returned from |
| 187 | +`stprint` is insecure. Stack of escaped escape sequences not interpreted |
| 188 | +before will be evaluated. |
| 189 | + |
| 190 | +Do *NOT* reinterpret the escape sequences on variable assignment (dangerous |
| 191 | +when printing to the terminal later: |
| 192 | + |
| 193 | +<code> |
| 194 | +var="$(stprint "${untrusted_string}")" # OK<br> |
| 195 | +# Or with Bash/Zsh syntax:<br> |
| 196 | +printf -v var "$(stprint "${untrusted_string}")" # DANGER (format is '%b')<br> |
| 197 | +printf -v var '%b' "$(stprint "${untrusted_string}")" # DANGER |
| 198 | +</code> |
| 199 | + |
| 200 | +Do *NOT* reinterpret the escape sequences when printing a variable, one |
| 201 | +more layer of escapes will be interpreted: |
| 202 | + |
| 203 | +<code> |
| 204 | +var="$(stprint "${untrusted_string}")" # OK<br> |
| 205 | +printf "${var}" # DANGER (format is '%b')<br> |
| 206 | +printf '%b' "${var}" # DANGER<br> |
| 207 | +echo -e "${var}" # DANGER<br> |
| 208 | +echo "${var}" # DANGER (may default to use '-e')<br> |
| 209 | +echo -E "${var}" # DANGER (var may have '-e' prefix) |
| 210 | +</code> |
| 211 | + |
| 212 | +## AUTHOR |
| 213 | + |
| 214 | +This man page has been written by Benjamin Grand M. S. |
| 215 | +(ben.grande.b@gmail.com). |
0 commit comments