Skip to content

Commit b84e182

Browse files
authored
Merge pull request #2332 from drwetter/sanitize_fileout
Make sure control chars from HTTP header don't end up in html,csv,json
2 parents 83f67b4 + cacd8c5 commit b84e182

3 files changed

Lines changed: 46 additions & 14 deletions

File tree

.github/workflows/codespell.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
- uses: codespell-project/actions-codespell@master
1414
with:
1515
skip: ca_hashes.txt,tls_data.txt,*.pem,OPENSSL-LICENSE.txt
16-
ignore_words_list: borken,gost,ciph,ba,bloc,isnt,chello,fo,alle
16+
ignore_words_list: borken,gost,ciph,ba,bloc,isnt,chello,fo,alle,aNULL

t/32_isHTML_valid.t

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ my $out="";
1515
my $html="";
1616
my $debughtml="";
1717
my $edited_html="";
18-
my $check2run="--ip=one --ids-friendly --color 0 --htmlfile tmp.html";
18+
my $htmlfile="tmp.html";
19+
my $check2run="--ip=one --ids-friendly --color 0 --htmlfile $htmlfile";
1920
my $diff="";
2021
die "Unable to open $prg" unless -f $prg;
2122

2223
printf "\n%s\n", "Doing HTML output checks";
23-
unlink 'tmp.html';
24+
unlink $htmlfile;
2425

2526
#1
2627
printf "%s\n", " .. running $prg against \"$uri\" to create HTML and terminal outputs (may take ~2 minutes)";
2728
# specify a TERM_WIDTH so that the two calls to testssl.sh don't create HTML files with different values of TERM_WIDTH
2829
$out = `TERM_WIDTH=120 $prg $check2run $uri`;
29-
$html = `cat tmp.html`;
30+
$html = `cat $htmlfile`;
3031
# $edited_html will contain the HTML with formatting information removed in order to compare against terminal output
3132
# Start by removing the HTML header.
32-
$edited_html = `tail -n +11 tmp.html`;
33-
unlink 'tmp.html';
33+
$edited_html = `tail -n +11 $htmlfile`;
34+
unlink $htmlfile;
3435

3536
# Remove the HTML footer
3637
$edited_html =~ s/\n\<\/pre\>\n\<\/body\>\n\<\/html\>//;
@@ -51,12 +52,13 @@ $tests++;
5152
$diff = diff \$edited_html, \$out;
5253
printf "\n%s\n", "$diff";
5354

55+
5456
#2
5557
printf "\n%s\n", " .. running again $prg against \"$uri\", now with --debug 4 to create HTML output (may take another ~2 minutes)";
5658
# Redirect stderr to /dev/null in order to avoid some unexplained "date: invalid date" error messages
5759
$out = `TERM_WIDTH=120 $prg $check2run --debug 4 $uri 2> /dev/null`;
58-
$debughtml = `cat tmp.html`;
59-
unlink 'tmp.html';
60+
$debughtml = `cat $htmlfile`;
61+
unlink $htmlfile;
6062

6163
# Remove date information from the Start and Done banners in the two HTML files, since they were created at different times
6264
$html =~ s/Start 2[0-9][0-9][0-9]-[0-3][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/Start XXXX-XX-XX XX:XX:XX/;
@@ -72,6 +74,7 @@ $debughtml =~ s/HTTP clock skew \+?-?[0-9]* /HTTP clock skew
7274
$debughtml =~ s/ Pre-test: .*\n//g;
7375
$debughtml =~ s/.*OK: below 825 days.*\n//g;
7476
$debughtml =~ s/.*DEBUG:.*\n//g;
77+
$debughtml =~ s/No engine or GOST support via engine with your.*\n//g;
7578

7679
cmp_ok($debughtml, "eq", $html, "HTML file created with --debug 4 matches HTML file created without --debug");
7780
$tests++;

testssl.sh

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ show_finding() {
534534
html_reserved(){
535535
local output
536536
"$do_html" || return 0
537-
#sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/"/\&quot;/g' -e "s/'/\&apos;/g" <<< "$1"
538537
output="${1//&/$'&'amp;}"
539538
output="${output//</$'&'lt;}"
540539
output="${output//>/$'&'gt;}"
@@ -545,8 +544,26 @@ html_reserved(){
545544
}
546545

547546
html_out() {
547+
local outstr="$1"
548+
548549
"$do_html" || return 0
549-
[[ -n "$HTMLFILE" ]] && [[ ! -d "$HTMLFILE" ]] && printf -- "%b" "$1" >> "$HTMLFILE"
550+
if [[ -n "$HTMLFILE" ]] && [[ ! -d "$HTMLFILE" ]]; then
551+
if [[ "$outstr" =~ [[:cntrl:]] ]]; then
552+
outstr="$(sanitize_fileout "$outstr")"
553+
fi
554+
printf -- "%b" "$outstr" >> "$HTMLFILE"
555+
fi
556+
}
557+
558+
# Removes non-printable chars in CSV, JSON, HTML, see #2330
559+
sanitize_fileout() {
560+
tr -d '\000-\011\013-\037' <<< "$1"
561+
}
562+
563+
# Removes non-printable chars in terminal output (log files)
564+
# We need to keep the color ANSI escape code x1b, o33, see #2330
565+
sanitize_termout() {
566+
tr -d '\000-\011\013-\032\034-\037' <<< "$1"
550567
}
551568

552569
# This is intentionally the same.
@@ -1227,6 +1244,9 @@ fileout_json_print_parameter() {
12271244
spaces=" " || \
12281245
spaces=" "
12291246
if [[ -n "$value" ]] || [[ "$parameter" == finding ]]; then
1247+
if [[ "$value" =~ [[:cntrl:]] ]]; then
1248+
value="$(sanitize_fileout "$value")"
1249+
fi
12301250
printf -- "%b%b%b%b" "$spaces" "\"$parameter\"" "$filler" ": \"$value\"" >> "$JSONFILE"
12311251
"$not_last" && printf ",\n" >> "$JSONFILE"
12321252
fi
@@ -1350,12 +1370,19 @@ fileout_insert_warning() {
13501370
fi
13511371
}
13521372

1373+
# args: "id" "fqdn/ip" "port" "severity" "finding" "cve" "cwe" "hint"
1374+
#
13531375
fileout_csv_finding() {
1376+
local finding="$5"
1377+
1378+
if [[ "$finding" =~ [[:cntrl:]] ]]; then
1379+
finding="$(sanitize_fileout "$finding")"
1380+
fi
13541381
safe_echo "\"$1\"," >> "$CSVFILE"
13551382
safe_echo "\"$2\"," >> "$CSVFILE"
13561383
safe_echo "\"$3\"," >> "$CSVFILE"
13571384
safe_echo "\"$4\"," >> "$CSVFILE"
1358-
safe_echo "\"$5\"," >> "$CSVFILE"
1385+
safe_echo "\"$finding\"," >> "$CSVFILE"
13591386
safe_echo "\"$6\"," >> "$CSVFILE"
13601387
if "$GIVE_HINTS"; then
13611388
safe_echo "\"$7\"," >> "$CSVFILE"
@@ -3102,16 +3129,18 @@ run_server_banner() {
31023129
grep -ai '^Server' $HEADERFILE >$TMPFILE
31033130
if [[ $? -eq 0 ]]; then
31043131
serverbanner=$(sed -e 's/^Server: //' -e 's/^server: //' $TMPFILE)
3105-
if [[ "$serverbanner" == $'\n' ]] || [[ "$serverbanner" == $'\r' ]] || [[ "$serverbanner" == $'\n\r' ]] || [[ -z "$serverbanner" ]]; then
3132+
serverbanner=${serverbanner//$'\r'}
3133+
serverbanner=${serverbanner//$'\n'}
3134+
if [[ -z "$serverbanner" ]]; then
31063135
outln "exists but empty string"
31073136
fileout "$jsonID" "INFO" "Server banner is empty"
31083137
else
31093138
emphasize_stuff_in_headers "$serverbanner"
31103139
fileout "$jsonID" "INFO" "$serverbanner"
31113140
if [[ "$serverbanner" == *Microsoft-IIS/6.* ]] && [[ $OSSL_VER == 1.0.2* ]]; then
3112-
prln_warning " It's recommended to run another test w/ OpenSSL 1.0.1 !"
3141+
prln_warning " It's recommended to run another test w/ OpenSSL >= 1.0.1 !"
31133142
# see https://github.com/PeterMosmans/openssl/issues/19#issuecomment-100897892
3114-
fileout "${jsonID}" "WARN" "IIS6_openssl_mismatch: Recommended to rerun this test w/ OpenSSL 1.0.1. See https://github.com/PeterMosmans/openssl/issues/19#issuecomment-100897892"
3143+
fileout "${jsonID}" "WARN" "IIS6_openssl_mismatch: Recommended to rerun this test w/ OpenSSL >= 1.0.1. See https://github.com/PeterMosmans/openssl/issues/19#issuecomment-100897892"
31153144
fi
31163145
fi
31173146
# mozilla.github.io/server-side-tls/ssl-config-generator/

0 commit comments

Comments
 (0)