Skip to content

Commit 765b585

Browse files
authored
Merge pull request #2386 from drwetter/strict_parsing_HSTS
Strict parser for HSTS
2 parents c55207d + 01ab3ac commit 765b585

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

testssl.sh

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,28 +2716,40 @@ run_hsts() {
27162716
match_httpheader_key "Strict-Transport-Security" "HSTS" "$spaces" "true"
27172717
if [[ $? -ne 0 ]]; then
27182718
echo "$HEADERVALUE" >$TMPFILE
2719-
hsts_age_sec="${HEADERVALUE//[^0-9]/}"
2719+
# strict parsing now as suggested in #2381
2720+
hsts_age_sec="${HEADERVALUE#*=}"
2721+
hsts_age_sec=${hsts_age_sec%%;*}
2722+
if [[ $hsts_age_sec =~ \" ]]; then
2723+
# remove first an last " in $hsts_age_sec (borrowed from strip_trailing_space/strip_leading_space):
2724+
hsts_age_sec=$(printf "%s" "${hsts_age_sec#"${hsts_age_sec%%[!\"]*}"}")
2725+
hsts_age_sec=$(printf "%s" "${hsts_age_sec%"${hsts_age_sec##*[!\"]}"}")
2726+
fi
27202727
debugme echo "hsts_age_sec: $hsts_age_sec"
2721-
if [[ -n $hsts_age_sec ]]; then
2722-
hsts_age_days=$(( hsts_age_sec / 86400))
2723-
else
2724-
hsts_age_days=-1
2725-
fi
2726-
if [[ $hsts_age_days -eq -1 ]]; then
2727-
pr_svrty_medium "misconfiguration: HSTS max-age (recommended > $HSTS_MIN seconds = $((HSTS_MIN/86400)) days ) is required but missing"
2728-
fileout "${jsonID}_time" "MEDIUM" "misconfiguration, parameter max-age (recommended > $HSTS_MIN seconds = $((HSTS_MIN/86400)) days) missing"
2729-
set_grade_cap "A" "HSTS max-age is misconfigured"
2730-
elif [[ $hsts_age_sec -eq 0 ]]; then
2731-
pr_svrty_low "HSTS max-age is set to 0. HSTS is disabled"
2732-
fileout "${jsonID}_time" "LOW" "0. HSTS is disabled"
2733-
set_grade_cap "A" "HSTS is disabled"
2734-
elif [[ $hsts_age_sec -ge $HSTS_MIN ]]; then
2735-
pr_svrty_good "$hsts_age_days days" ; out "=$hsts_age_sec s"
2736-
fileout "${jsonID}_time" "OK" "$hsts_age_days days (=$hsts_age_sec seconds) > $HSTS_MIN seconds"
2728+
if ! is_number "$hsts_age_sec"; then
2729+
pr_svrty_medium "misconfiguration: \'"$hsts_age_sec"\' is not a valid max-age specification"
2730+
fileout "${jsonID}_time" "MEDIUM" "misconfiguration, specified not a number for max-age"
27372731
else
2738-
pr_svrty_medium "$hsts_age_sec s = $hsts_age_days days is too short ( >= $HSTS_MIN seconds recommended)"
2739-
fileout "${jsonID}_time" "MEDIUM" "max-age too short. $hsts_age_days days (=$hsts_age_sec seconds) < $HSTS_MIN seconds"
2740-
set_grade_cap "A" "HSTS max-age is too short"
2732+
if [[ -n $hsts_age_sec ]]; then
2733+
hsts_age_days=$(( hsts_age_sec / 86400))
2734+
else
2735+
hsts_age_days=-1
2736+
fi
2737+
if [[ $hsts_age_days -eq -1 ]]; then
2738+
pr_svrty_medium "misconfiguration: HSTS max-age (recommended > $HSTS_MIN seconds = $((HSTS_MIN/86400)) days ) is required but missing"
2739+
fileout "${jsonID}_time" "MEDIUM" "misconfiguration, parameter max-age (recommended > $HSTS_MIN seconds = $((HSTS_MIN/86400)) days) missing"
2740+
set_grade_cap "A" "HSTS max-age is misconfigured"
2741+
elif [[ $hsts_age_sec -eq 0 ]]; then
2742+
pr_svrty_low "HSTS max-age is set to 0. HSTS is disabled"
2743+
fileout "${jsonID}_time" "LOW" "0. HSTS is disabled"
2744+
set_grade_cap "A" "HSTS is disabled"
2745+
elif [[ $hsts_age_sec -ge $HSTS_MIN ]]; then
2746+
pr_svrty_good "$hsts_age_days days" ; out "=$hsts_age_sec s"
2747+
fileout "${jsonID}_time" "OK" "$hsts_age_days days (=$hsts_age_sec seconds) > $HSTS_MIN seconds"
2748+
else
2749+
pr_svrty_medium "$hsts_age_sec s = $hsts_age_days days is too short ( >= $HSTS_MIN seconds recommended)"
2750+
fileout "${jsonID}_time" "MEDIUM" "max-age too short. $hsts_age_days days (=$hsts_age_sec seconds) < $HSTS_MIN seconds"
2751+
set_grade_cap "A" "HSTS max-age is too short"
2752+
fi
27412753
fi
27422754
if includeSubDomains "$TMPFILE"; then
27432755
fileout "${jsonID}_subdomains" "OK" "includes subdomains"

0 commit comments

Comments
 (0)