Skip to content

Commit 559c089

Browse files
authored
Merge pull request #2887 from testssl/fix_http_age_3.2
Fix garbled screen when HTTP Age is not a non-negative int (branch 3.2)
2 parents a11ad94 + 7aa9d30 commit 559c089

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

testssl.sh

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,8 @@ service_detection() {
24352435
wait_kill $! $HEADER_MAXSLEEP
24362436
was_killed=$?
24372437
fi
2438+
# make sure that we don't have non-printable chars sneaked in -- relevant only in debug mode level 2
2439+
sanitze_http_header $TMPFILE
24382440
head $TMPFILE | grep -aq '^HTTP/' && SERVICE=HTTP
24392441
[[ -z "$SERVICE" ]] && head $TMPFILE | grep -Ewaq "SMTP|ESMTP|Exim|IdeaSmtpServer|Kerio Connect|Postfix" && SERVICE=SMTP # I know some overlap here
24402442
[[ -z "$SERVICE" ]] && head $TMPFILE | grep -Ewaq "POP|POP3|Gpop|OK Dovecot" && SERVICE=POP # I know some overlap here
@@ -2509,14 +2511,17 @@ connectivity_problem() {
25092511
fi
25102512
}
25112513

2514+
# arg1: filename (global)
2515+
# return: sanitzes arg1. output only when debugging
2516+
#
25122517
sanitze_http_header() {
25132518
# sed implementations tested were sometime not fine with header containing x0d x0a (CRLF) which is the usual
25142519
# case. Also we use tr here to remove any crtl chars which the server side offers --> possible security problem
25152520
# Only allowed now is LF + CR. See #2337. awk, see above, doesn't seem to care -- but not under MacOS.
2516-
sed -e '/^$/q' -e '/^[^a-zA-Z_0-9]$/q' $HEADERFILE | tr -d '\000-\011\013\014\016-\037' >$HEADERFILE.tmp
2521+
sed -e '/^$/q' -e '/^[^a-zA-Z_0-9]$/q' $1 | tr -d '\000-\011\013\014\016-\037' >$1.tmp
25172522
# Now to be more sure we delete from '<' or '{' maybe with a leading blank until the end
2518-
sed -e '/^ *<.*$/d' -e '/^ *{.*$/d' $HEADERFILE.tmp >$HEADERFILE
2519-
debugme echo -e "---\n $(< $HEADERFILE) \n---"
2523+
sed -e '/^ *<.*$/d' -e '/^ *{.*$/d' $1.tmp >$1
2524+
debugme echo -e "---\n $(< $1) \n---"
25202525
}
25212526

25222527

@@ -2550,9 +2555,9 @@ run_http_header() {
25502555
tm_out "$GET_REQ11" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") >$HEADERFILE 2>$ERRFILE
25512556
NOW_TIME=$(date "+%s")
25522557
HAD_SLEPT=0
2553-
sanitze_http_header
2558+
sanitze_http_header $HEADERFILE
25542559
else
2555-
sanitze_http_header
2560+
sanitze_http_header $HEADERFILE
25562561
# 1st GET request hung and needed to be killed. Check whether it succeeded anyway:
25572562
if grep -Eiaq "XML|HTML|DOCTYPE|HTTP|Connection" $HEADERFILE; then
25582563
# correct by seconds we slept, HAD_SLEPT comes from wait_kill()
@@ -2565,6 +2570,7 @@ run_http_header() {
25652570
((NR_HEADER_FAIL++))
25662571
fi
25672572
fi
2573+
25682574
HTTP_TIME=$(awk -F': ' '/^date:/ { print $2 } /^Date:/ { print $2 }' $HEADERFILE)
25692575
HTTP_AGE=$(awk -F': ' '/^[aA][gG][eE]: / { print $2 }' $HEADERFILE)
25702576
if [[ ! -s $HEADERFILE ]]; then
@@ -2592,6 +2598,10 @@ run_http_header() {
25922598
# Populate vars for HTTP time
25932599
[[ -n "$HTTP_AGE" ]] && HTTP_AGE="$(strip_lf "$HTTP_AGE")"
25942600
[[ -n "$HTTP_TIME" ]] && HTTP_TIME="$(strip_lf "$HTTP_TIME")"
2601+
if [[ -n "$HTTP_AGE" ]] && [[ ! "$HTTP_AGE" =~ ^[0-9]+$ ]]; then
2602+
HTTP_AGE="NaN"
2603+
fi
2604+
25952605
debugme echo "NOW_TIME: $NOW_TIME | HTTP_AGE: $HTTP_AGE | HTTP_TIME: $HTTP_TIME"
25962606

25972607
HTTP_STATUS_CODE=$(awk '/^HTTP\// { print $2 }' $HEADERFILE 2>>$ERRFILE)
@@ -2722,13 +2732,20 @@ run_http_date() {
27222732
outln
27232733
pr_bold " HTTP Age"
27242734
out " (RFC 7234) $HTTP_AGE"
2725-
fileout "HTTP_headerAge" "INFO" "$HTTP_AGE seconds"
2735+
if [[ "$HTTP_AGE" = NaN ]]; then
2736+
out ", "
2737+
# https://www.rfc-editor.org/rfc/rfc7234#section-1.2.1
2738+
pr_svrty_low "RFC 7234, sec 1.2.1. requires numbers"
2739+
fileout "HTTP_headerAge" "LOW" "$HTTP_AGE was not a non-negative integer, see RFC 7234, sec 1.2.1."
2740+
else
2741+
fileout "HTTP_headerAge" "INFO" "$HTTP_AGE seconds"
2742+
fi
27262743
fi
27272744
else
27282745
out "Got no HTTP time, maybe try different URL?";
27292746
fileout "$jsonID" "INFO" "Got no HTTP time, maybe try different URL?"
27302747
fi
2731-
debugme tm_out ", HTTP_TIME + HTTP_AGE in epoch: $HTTP_TIME / $HTTP_AGE"
2748+
debugme tm_out ", HTTP_TIME | HTTP_AGE: $HTTP_TIME | $HTTP_AGE"
27322749
outln
27332750
match_ipv4_httpheader "$1"
27342751
return 0

0 commit comments

Comments
 (0)