Skip to content

Commit 9d0744e

Browse files
committed
Introducing --overwrite option
Sometimes it is needed to overwrite existing output files. This has been requested in the past (#927). For safety reasons it was not implemented. However I realized that it could be useful. It requires some responsible usage though. Code added, help() and manpages added -- warnings added too.
1 parent 6558631 commit 9d0744e

4 files changed

Lines changed: 35 additions & 8 deletions

File tree

doc/testssl.1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ whole 9 yards
496496
\fB\-\-severity <severity>\fR For CSV and both JSON outputs this will only add findings to the output file if a severity is equal or higher than the \fBseverity\fR value specified\. Allowed are \fB<LOW|MEDIUM|HIGH|CRITICAL>\fR\. WARN is another level which translates to a client\-side scanning error or problem\. Thus you will always see them in a file if they occur\.
497497
.
498498
.P
499-
\fB\-\-append\fR Normally, if an output file already exists and it has a file size greater zero, testssl\.sh will prompt you to manually remove the file exit with an error\. \fB\-\-append\fR however will append to this file, without a header\. The environment variable APPEND does the same\. Be careful using this switch/variable\. A complementary option which overwrites an existing file doesn\'t exist per design\.
499+
\fB\-\-append\fR Normally, if an output file already exists and it has a file size greater zero, testssl\.sh will prompt you to manually remove the file and then exit with an error\. \fB\-\-append\fR however will append to this file, without a header\. The environment variable APPEND does the same\. Be careful using this switch/variable\.
500+
.
501+
.P
502+
\fB\-\-overwrite\fR Normally, if an output file already exists and it has a file size greater zero, testssl\.sh will not allow you to overwrite this file\. This option will do that \fBwithout any warning\fR\. The environment variable OVERWRITE does the same\. Be careful, you have been warned!
500503
.
501504
.P
502505
\fB\-\-outprefix <fname_prefix>\fR Prepend output filename prefix \fIfname_prefix\fR before \'${NODE}\-\'\. You can use as well the environment variable FNAME_PREFIX\. Using this any output files will be named \fB<fname_prefix>\-${NODE}\-p${port}${YYYYMMDD\-HHMM}\.<format>\fR when no file name of the respective output option was specified\. If you do not like the separator \'\-\' you can as well supply a \fB<fname_prefix>\fR ending in \'\.\', \'_\' or \',\'\. In this case or if you already supplied \'\-\' no additional \'\-\' will be appended to \fB<fname_prefix>\fR\.

doc/testssl.1.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/testssl.1.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ Rating automatically gets disabled, to not give a wrong or misleading grade, whe
327327

328328
`--severity <severity>` For CSV and both JSON outputs this will only add findings to the output file if a severity is equal or higher than the `severity` value specified. Allowed are `<LOW|MEDIUM|HIGH|CRITICAL>`. WARN is another level which translates to a client-side scanning error or problem. Thus you will always see them in a file if they occur.
329329

330-
`--append` Normally, if an output file already exists and it has a file size greater zero, testssl.sh will prompt you to manually remove the file exit with an error. `--append` however will append to this file, without a header. The environment variable APPEND does the same. Be careful using this switch/variable. A complementary option which overwrites an existing file doesn't exist per design.
330+
`--append` Normally, if an output file already exists and it has a file size greater zero, testssl.sh will prompt you to manually remove the file and exit with an error. `--append` however will append to this file, without a header. The environment variable APPEND does the same. Be careful using this switch/variable. A complementary option which overwrites an existing file doesn't exist per design.
331+
332+
`--overwrite` Normally, if an output file already exists and it has a file size greater zero, testssl.sh will not allow you to overwrite this file. This option will do that **without any warning**. The environment variable OVERWRITE does the same. Be careful, you have been warned!
331333

332334
`--outprefix <fname_prefix>` Prepend output filename prefix <fname_prefix> before '${NODE}-'. You can use as well the environment variable FNAME_PREFIX. Using this any output files will be named `<fname_prefix>-${NODE}-p${port}${YYYYMMDD-HHMM}.<format>` when no file name of the respective output option was specified. If you do not like the separator '-' you can as well supply a `<fname_prefix>` ending in '.', '_' or ','. In this case or if you already supplied '-' no additional '-' will be appended to `<fname_prefix>`.
333335

testssl.sh

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ CSVFILE="${CSVFILE:-""}" # csvfile if used
179179
HTMLFILE="${HTMLFILE:-""}" # HTML if used
180180
FNAME=${FNAME:-""} # file name to read commands from
181181
FNAME_PREFIX=${FNAME_PREFIX:-""} # output filename prefix, see --outprefix
182-
APPEND=${APPEND:-false} # append to csv/json file instead of overwriting it
182+
APPEND=${APPEND:-false} # append to csv/json/html/log file
183+
OVERWRITE=${OVERWRITE:-false} # overwriting csv/json/html/log file
183184
[[ -z "$NODNS" ]] && declare NODNS # If unset it does all DNS lookups per default. "min" only for hosts or "none" at all
184185
HAS_IPv6=${HAS_IPv6:-false} # if you have OpenSSL with IPv6 support AND IPv6 networking set it to yes
185186
ALL_CLIENTS=${ALL_CLIENTS:-false} # do you want to run all client simulation form all clients supplied by SSLlabs?
@@ -1349,7 +1350,10 @@ json_header() {
13491350
if "$APPEND"; then
13501351
JSONHEADER=false
13511352
else
1352-
[[ -s "$JSONFILE" ]] && fatal "non-empty \"$JSONFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1353+
if [[ -s "$JSONFILE" ]]; then
1354+
"$OVERWRITE" || fatal "non-empty \"$JSONFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1355+
cp /dev/null "$JSONFILE"
1356+
fi
13531357
"$do_json" && echo "[" > "$JSONFILE"
13541358
"$do_pretty_json" && echo "{" > "$JSONFILE"
13551359
fi
@@ -1390,7 +1394,10 @@ csv_header() {
13901394
if "$APPEND"; then
13911395
CSVHEADER=false
13921396
else
1393-
[[ -s "$CSVFILE" ]] && fatal "non-empty \"$CSVFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1397+
if [[ -s "$CSVFILE" ]]; then
1398+
"$OVERWRITE" || fatal "non-empty \"$CSVFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1399+
cp /dev/null "$CSVFILE"
1400+
fi
13941401
touch "$CSVFILE"
13951402
if "$GIVE_HINTS"; then
13961403
fileout_csv_finding "id" "fqdn/ip" "port" "severity" "finding" "cve" "cwe" "hint"
@@ -1440,7 +1447,10 @@ html_header() {
14401447
if "$APPEND"; then
14411448
HTMLHEADER=false
14421449
else
1443-
[[ -s "$HTMLFILE" ]] && fatal "non-empty \"$HTMLFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1450+
if [[ -s "$HTMLFILE" ]]; then
1451+
"$OVERWRITE" || fatal "non-empty \"$HTMLFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1452+
cp /dev/null "$HTMLFILE"
1453+
fi
14441454
html_out "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
14451455
html_out "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
14461456
html_out "<!-- This file was created with testssl.sh. https://testssl.sh -->\n"
@@ -1499,7 +1509,10 @@ prepare_logging() {
14991509
fi
15001510

15011511
if ! "$APPEND"; then
1502-
[[ -s "$LOGFILE" ]] && fatal "non-empty \"$LOGFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1512+
if [[ -s "$LOGFILE" ]]; then
1513+
"$OVERWRITE" || fatal "non-empty \"$LOGFILE\" exists. Either use \"--append\" or (re)move it" $ERR_FCREATE
1514+
cp /dev/null "$LOGFILE"
1515+
fi
15031516
fi
15041517
tmln_out "## Scan started as: \"$PROG_NAME $CMDLINE\"" >>"$LOGFILE"
15051518
tmln_out "## at $HNAME:$OPENSSL_LOCATION" >>"$LOGFILE"
@@ -19260,6 +19273,7 @@ file output options (can also be preset via environment variables)
1926019273
--hints additional hints to findings
1926119274
--severity <severity> severities with lower level will be filtered for CSV+JSON, possible values <LOW|MEDIUM|HIGH|CRITICAL>
1926219275
--append if (non-empty) <logfile>, <csvfile>, <jsonfile> or <htmlfile> exists, append to file. Omits any header
19276+
--overwrite if <logfile>, <csvfile>, <jsonfile> or <htmlfile> exists it overwrites it without any warning
1926319277
--outprefix <fname_prefix> before '\${NODE}.' above prepend <fname_prefix>
1926419278

1926519279

@@ -22118,7 +22132,12 @@ parse_cmd_line() {
2211822132
do_csv=true
2211922133
do_logging=true
2212022134
;;
22135+
--overwrite)
22136+
"$APPEND" && fatal "using --overwrite and --append is contradicting" $ERR_CMDLINE
22137+
OVERWRITE=true
22138+
;;
2212122139
--append)
22140+
"$OVERWRITE" && fatal "using --append and --overwrite is contradicting" $ERR_CMDLINE
2212222141
APPEND=true
2212322142
;;
2212422143
--outprefix)

0 commit comments

Comments
 (0)