Hide passwords containing regex metachars
- Created new escape_regex_metachars function.
- Had register_passwords function call new escape_regex_metachars function.
Signed-off-by: David Shaw <dlshaw@us.ibm.com>
Change-Id: Ibc1e3841cc6fac1d9c9c3529e913c786b449f751
diff --git a/lib/escape.tcl b/lib/escape.tcl
index e854792..d5f36e4 100755
--- a/lib/escape.tcl
+++ b/lib/escape.tcl
@@ -112,3 +112,27 @@
}
+proc escape_regex_metachars { buffer } {
+
+ # Escape every regex metacharacter found in buffer and return the result.
+
+ # Example code:
+
+ # set var1 {john*sm(]ith}
+ # print_vars var1
+ # set var1 [escape_regex_metachars $var1]
+ # print_vars var1
+
+ # Example output:
+
+ # var1: john*sm(]ith
+ # var1: john\*sm\(\]ith
+
+ # Description of argument(s):
+ # buffer The string whose regex metacharacters are to be escaped.
+
+ set escape_chars_regex {[\\\^\$\/\(\)\|\?\+\*\[\]\{\}\,\.]}
+ regsub -all ${escape_chars_regex} ${buffer} {\\\0} buffer
+ return ${buffer}
+
+}
diff --git a/lib/print.tcl b/lib/print.tcl
index 3959088..aab6a62 100755
--- a/lib/print.tcl
+++ b/lib/print.tcl
@@ -2,7 +2,7 @@
# This file provides many valuable print procedures such as sprint_var, sprint_time, sprint_error, etc.
-my_source [list data_proc.tcl call_stack.tcl]
+my_source [list data_proc.tcl call_stack.tcl escape.tcl]
# Need "Expect" package for trap procedure.
package require Expect
@@ -89,10 +89,9 @@
# Skip already-registered passwords.
if { [lsearch -exact $hidden_text $password] != -1 } { continue }
# Put the password into the global hidden_text list.
- lappend hidden_text $password
+ lappend hidden_text [escape_regex_metachars $password]
}
- # TODO: Escape metachars in the password_regex.
set password_regex [join $hidden_text |]
}