1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02:00

wkd: Fix client issue with leading or trailing spaces in user-ids.

* common/recsel.c (recsel_parse_expr): Add flag -t.
* common/stringhelp.c: Remove assert.h.
(strtokenize): Factor code out to do_strtokenize.
(strtokenize_nt): New.
(do_strtokenize): Add arg trim to support the strtokenize_nt.
* common/t-stringhelp.c (test_strtokenize_nt): New test cases.

* tools/wks-util.c (wks_list_key): Use strtokenize_nt and the recsel
flag -t.
--

This fixes a bug with user ids with leading spaces because:

wks-client lists all mail addresses from the key and matches them to the
requested mail address.

If there are several user-ids all with the same mail address
wks-client picks one of them and then extracts exactly that user id.
However, here it does not match by the mail address but by the full
user-id so that we can be sure that there will be only one user-id in
the final key.

The filter built expression unfortunately strips leading blanks but
requires a verbatim match.  Thus it won't find the user id again and
errors out.

The new -t flag and a non-trimming strtokenize solves the problem.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2021-08-20 09:13:01 +02:00
parent 1565baa93a
commit b4345f7521
No known key found for this signature in database
GPG key ID: E3FDFF218E45B72B
6 changed files with 202 additions and 22 deletions

View file

@ -1,6 +1,6 @@
/* t-stringhelp.c - Regression tests for stringhelp.c
* Copyright (C) 2007 Free Software Foundation, Inc.
* 2015 g10 Code GmbH
* 2015, 2021 g10 Code GmbH
*
* This file is part of GnuPG.
*
@ -27,6 +27,7 @@
* You should have received a copies of the GNU General Public License
* and the GNU Lesser General Public License along with this program;
* if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
*/
#include <config.h>
@ -34,7 +35,6 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
@ -686,6 +686,144 @@ test_strtokenize (void)
}
static void
test_strtokenize_nt (void)
{
struct {
const char *s;
const char *delim;
const char *fields_expected[10];
} tv[] = {
{
"", ":",
{ "", NULL }
},
{
"a", ":",
{ "a", NULL }
},
{
":", ":",
{ "", "", NULL }
},
{
"::", ":",
{ "", "", "", NULL }
},
{
"a:b:c", ":",
{ "a", "b", "c", NULL }
},
{
"a:b:", ":",
{ "a", "b", "", NULL }
},
{
"a:b", ":",
{ "a", "b", NULL }
},
{
"aa:b:cd", ":",
{ "aa", "b", "cd", NULL }
},
{
"aa::b:cd", ":",
{ "aa", "", "b", "cd", NULL }
},
{
"::b:cd", ":",
{ "", "", "b", "cd", NULL }
},
{
"aa: : b:cd ", ":",
{ "aa", " ", " b", "cd ", NULL }
},
{
" aa: : b: cd ", ":",
{ " aa", " ", " b", " cd ", NULL }
},
{
" ", ":",
{ " ", NULL }
},
{
" :", ":",
{ " ", "", NULL }
},
{
" : ", ":",
{ " ", " ", NULL }
},
{
": ", ":",
{ "", " ", NULL }
},
{
": x ", ":",
{ "", " x ", NULL }
},
{
"a:bc:cde:fghi:jklmn::foo:", ":",
{ "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }
},
{
",a,bc,,def,", ",",
{ "", "a", "bc", "", "def", "", NULL }
},
{
" a ", " ",
{ "", "a", "", NULL }
},
{
" ", " ",
{ "", "", NULL }
},
{
"", " ",
{ "", NULL }
}
};
int tidx;
for (tidx = 0; tidx < DIM(tv); tidx++)
{
char **fields;
int field_count;
int field_count_expected;
int i;
for (field_count_expected = 0;
tv[tidx].fields_expected[field_count_expected];
field_count_expected ++)
;
fields = strtokenize_nt (tv[tidx].s, tv[tidx].delim);
if (!fields)
fail (tidx * 1000);
else
{
for (field_count = 0; fields[field_count]; field_count++)
;
if (field_count != field_count_expected)
fail (tidx * 1000);
else
{
for (i = 0; i < field_count_expected; i++)
if (strcmp (tv[tidx].fields_expected[i], fields[i]))
{
printf ("For field %d, expected '%s', but got '%s'\n",
i, tv[tidx].fields_expected[i], fields[i]);
fail (tidx * 1000 + i + 1);
}
}
}
xfree (fields);
}
}
static void
test_split_fields (void)
{
@ -724,7 +862,7 @@ test_split_fields (void)
for (tidx = 0; tidx < DIM(tv); tidx++)
{
nfields = tv[tidx].nfields;
assert (nfields <= DIM (fields));
log_assert (nfields <= DIM (fields));
/* Count the fields. */
for (field_count_expected = 0;
@ -799,7 +937,7 @@ test_split_fields_colon (void)
for (tidx = 0; tidx < DIM(tv); tidx++)
{
nfields = tv[tidx].nfields;
assert (nfields <= DIM (fields));
log_assert (nfields <= DIM (fields));
/* Count the fields. */
for (field_count_expected = 0;
@ -1070,6 +1208,7 @@ main (int argc, char **argv)
test_make_absfilename_try ();
test_strsplit ();
test_strtokenize ();
test_strtokenize_nt ();
test_split_fields ();
test_split_fields_colon ();
test_compare_version_strings ();