Skip to content

Commit cbe896d

Browse files
authored
Merge pull request #2713 from testssl/sanitze_better_http_header.3.0
Sanitze HTTP header early and better
2 parents 5a320df + 520604d commit cbe896d

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

testssl.sh

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,21 @@ connectivity_problem() {
21952195
fi
21962196
}
21972197

2198+
sanitze_http_header() {
2199+
# Quit on first empty line to catch 98% of the cases. Next pattern is there because the SEDs tested
2200+
# so far seem not to be fine with header containing x0d x0a (CRLF) which is the usual case.
2201+
# So we also trigger also on any sign on a single line which is not alphanumeric (plus _)
2202+
#
2203+
# Also we use tr here to remove any crtl chars which the server side offers --> possible security problem
2204+
# Only allowed now is LF + CR. See #2337
2205+
# awk, see above, doesn't seem to care
2206+
sed -e '/^$/q' -e '/^[^a-zA-Z_0-9]$/q' $HEADERFILE | tr -d '\000-\011\013\014\016-\037' >$HEADERFILE.tmp
2207+
sed -e '/^$/q' -e '/^[^a-zA-Z_0-9]$/q' $HEADERFILE | tr -d '\000-\010\013\014\016-\037' >$HEADERFILE.tmp
2208+
# Now to be more sure we delete from '<' or '{' maybe with a leading blank until the end
2209+
sed -e '/^ *<.*$/d' -e '/^ *{.*$/d' $HEADERFILE.tmp >$HEADERFILE
2210+
debugme echo -e "---\n $(< $HEADERFILE) \n---"
2211+
}
2212+
21982213

21992214
#problems not handled: chunked
22002215
run_http_header() {
@@ -2224,16 +2239,14 @@ run_http_header() {
22242239
# Doing it again in the foreground to get an accurate header time
22252240
printf "$GET_REQ11" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") >$HEADERFILE 2>$ERRFILE
22262241
NOW_TIME=$(date "+%s")
2227-
HTTP_TIME=$(awk -F': ' '/^date:/ { print $2 } /^Date:/ { print $2 }' $HEADERFILE)
2228-
HTTP_AGE=$(awk -F': ' '/^[aA][gG][eE]: / { print $2 }' $HEADERFILE)
22292242
HAD_SLEPT=0
2243+
sanitze_http_header
22302244
else
2245+
sanitze_http_header
22312246
# 1st GET request hung and needed to be killed. Check whether it succeeded anyway:
22322247
if grep -Eiaq "XML|HTML|DOCTYPE|HTTP|Connection" $HEADERFILE; then
22332248
# correct by seconds we slept, HAD_SLEPT comes from wait_kill()
22342249
NOW_TIME=$(($(date "+%s") - HAD_SLEPT))
2235-
HTTP_TIME=$(awk -F': ' '/^date:/ { print $2 } /^Date:/ { print $2 }' $HEADERFILE)
2236-
HTTP_AGE=$(awk -F': ' '/^[aA][gG][eE]: / { print $2 }' $HEADERFILE)
22372250
else
22382251
prln_warning " likely HTTP header requests failed (#lines: $(wc -l $HEADERFILE | awk '{ print $1 }'))"
22392252
[[ "$DEBUG" -lt 1 ]] && outln "Rerun with DEBUG>=1 and inspect $HEADERFILE\n"
@@ -2242,6 +2255,8 @@ run_http_header() {
22422255
((NR_HEADER_FAIL++))
22432256
fi
22442257
fi
2258+
HTTP_TIME=$(awk -F': ' '/^date:/ { print $2 } /^Date:/ { print $2 }' $HEADERFILE)
2259+
HTTP_AGE=$(awk -F': ' '/^[aA][gG][eE]: / { print $2 }' $HEADERFILE)
22452260
if [[ ! -s $HEADERFILE ]]; then
22462261
((NR_HEADER_FAIL++))
22472262
if [[ $NR_HEADER_FAIL -ge $MAX_HEADER_FAIL ]]; then
@@ -2269,19 +2284,6 @@ run_http_header() {
22692284
[[ -n "$HTTP_TIME" ]] && HTTP_TIME="$(strip_lf "$HTTP_TIME")"
22702285
debugme echo "NOW_TIME: $NOW_TIME | HTTP_AGE: $HTTP_AGE | HTTP_TIME: $HTTP_TIME"
22712286

2272-
# Quit on first empty line to catch 98% of the cases. Next pattern is there because the SEDs tested
2273-
# so far seem not to be fine with header containing x0d x0a (CRLF) which is the usual case.
2274-
# So we also trigger also on any sign on a single line which is not alphanumeric (plus _)
2275-
#
2276-
# Also we use tr here to remove any crtl chars which the server side offers --> possible security problem
2277-
# Only allowed now is LF + CR. See #2337
2278-
# awk, see above, doesn't seem to care
2279-
sed -e '/^$/q' -e '/^[^a-zA-Z_0-9]$/q' $HEADERFILE | tr -d '\000-\011\013\014\016-\037' >$HEADERFILE.tmp
2280-
sed -e '/^$/q' -e '/^[^a-zA-Z_0-9]$/q' $HEADERFILE | tr -d '\000-\010\013\014\016-\037' >$HEADERFILE.tmp
2281-
# Now to be more sure we delete from '<' or '{' maybe with a leading blank until the end
2282-
sed -e '/^ *<.*$/d' -e '/^ *{.*$/d' $HEADERFILE.tmp >$HEADERFILE
2283-
debugme echo -e "---\n $(< $HEADERFILE) \n---"
2284-
22852287
HTTP_STATUS_CODE=$(awk '/^HTTP\// { print $2 }' $HEADERFILE 2>>$ERRFILE)
22862288
msg_thereafter=$(awk -F"$HTTP_STATUS_CODE" '/^HTTP\// { print $2 }' $HEADERFILE 2>>$ERRFILE) # dirty trick to use the status code as a
22872289
msg_thereafter=$(strip_lf "$msg_thereafter") # field separator, otherwise we need a loop with awk

0 commit comments

Comments
 (0)