@@ -79,10 +79,16 @@ void totp_cli_command_add_docopt_options() {
7979 TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX ) " Show console user input as-is without masking\r\n" );
8080}
8181
82+ static void furi_string_secure_free (FuriString * str ) {
83+ for (long i = furi_string_size (str ) - 1 ; i >= 0 ; i -- ) {
84+ furi_string_set_char (str , i , '\0' );
85+ }
86+
87+ furi_string_free (str );
88+ }
89+
8290void totp_cli_command_add_handle (PluginState * plugin_state , FuriString * args , Cli * cli ) {
8391 FuriString * temp_str = furi_string_alloc ();
84- const char * temp_cstr ;
85-
8692 TokenInfo * token_info = token_info_alloc ();
8793
8894 // Reading token name
@@ -93,9 +99,9 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
9399 return ;
94100 }
95101
96- temp_cstr = furi_string_get_cstr (temp_str );
97- token_info -> name = malloc (strlen ( temp_cstr ) + 1 );
98- strcpy (token_info -> name , temp_cstr );
102+ size_t temp_cstr_len = furi_string_size (temp_str );
103+ token_info -> name = malloc (temp_cstr_len + 1 );
104+ strlcpy (token_info -> name , furi_string_get_cstr ( temp_str ), temp_cstr_len + 1 );
99105
100106 // Read optional arguments
101107 bool mask_user_input = true;
@@ -146,13 +152,15 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
146152 uint8_t c ;
147153 while (cli_read (cli , & c , 1 ) == 1 ) {
148154 if (c == CliSymbolAsciiEsc ) {
155+ // Some keys generating escape-sequences
156+ // We need to ignore them as we case about alpha-numerics only
149157 uint8_t c2 ;
150158 cli_read_timeout (cli , & c2 , 1 , 0 );
151159 cli_read_timeout (cli , & c2 , 1 , 0 );
152160 } else if (c == CliSymbolAsciiETX ) {
153161 TOTP_CLI_DELETE_CURRENT_LINE ();
154- TOTP_CLI_PRINTF ("Cancelled by user" );
155- furi_string_free (temp_str );
162+ TOTP_CLI_PRINTF ("Cancelled by user\r\n " );
163+ furi_string_secure_free (temp_str );
156164 token_info_free (token_info );
157165 return ;
158166 } else if ((c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' )) {
@@ -166,8 +174,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
166174 } else if (c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel ) {
167175 size_t temp_str_size = furi_string_size (temp_str );
168176 if (temp_str_size > 0 ) {
169- TOTP_CLI_PRINTF ("\b \b" );
170- fflush (stdout );
177+ TOTP_CLI_DELETE_LAST_CHAR ();
171178 furi_string_left (temp_str , temp_str_size - 1 );
172179 }
173180 } else if (c == CliSymbolAsciiCR ) {
@@ -176,25 +183,26 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
176183 }
177184 }
178185
179- temp_cstr = furi_string_get_cstr (temp_str );
180-
181186 TOTP_CLI_DELETE_LAST_LINE ();
182187
183188 if (!totp_cli_ensure_authenticated (plugin_state , cli )) {
184- furi_string_free (temp_str );
189+ furi_string_secure_free (temp_str );
185190 token_info_free (token_info );
186191 return ;
187192 }
188193
189- if (!token_info_set_secret (token_info , temp_cstr , strlen (temp_cstr ), plugin_state -> iv )) {
194+ if (!token_info_set_secret (
195+ token_info ,
196+ furi_string_get_cstr (temp_str ),
197+ furi_string_size (temp_str ),
198+ plugin_state -> iv )) {
190199 TOTP_CLI_PRINTF ("Token secret seems to be invalid and can not be parsed\r\n" );
191- furi_string_free (temp_str );
200+ furi_string_secure_free (temp_str );
192201 token_info_free (token_info );
193202 return ;
194203 }
195204
196- furi_string_reset (temp_str );
197- furi_string_free (temp_str );
205+ furi_string_secure_free (temp_str );
198206
199207 bool load_generate_token_scene = false;
200208 if (plugin_state -> current_scene == TotpSceneGenerateToken ) {
0 commit comments