Merge pull request #377 from benmcollins/jansson-error-excerpt

jansson: leave the input excerpt out of parse error messages
diff --git a/libjwt/jansson/jwt-jansson.c b/libjwt/jansson/jwt-jansson.c
index 2d512b2..8fdec26 100644
--- a/libjwt/jansson/jwt-jansson.c
+++ b/libjwt/jansson/jwt-jansson.c
@@ -76,6 +76,37 @@
  * Internal: map jansson json_error_t to jwt_json_error_t
  * ================================================================ */
 
+#define UNICODE_MSG	"invalid Unicode"
+#define OPEN_MSG	"unable to open "
+
+/* Report Jansson's message without the input text it quotes, as the json-c
+ * backend does with its fixed descriptions. Most messages end in
+ * " near '<text being read>'": cut there. A " near end of file" ending quotes
+ * nothing and stays. The "invalid Unicode" messages quote the escape itself
+ * as well: cut those at their first single quote. "unable to open <path>:
+ * <reason>" has no " near" ending and stays whole, even when the path it
+ * names has " near '" in it. */
+static void map_error_text(char *dst, const char *text)
+{
+	const char *cut = strstr(text, " near '");
+	size_t len;
+
+	if (!strncmp(text, OPEN_MSG, sizeof(OPEN_MSG) - 1))
+		cut = NULL;
+	len = cut ? (size_t)(cut - text) : strlen(text);
+
+	if (!strncmp(text, UNICODE_MSG, sizeof(UNICODE_MSG) - 1)) {
+		cut = memchr(text, '\'', len);
+		if (cut) {
+			len = (size_t)(cut - text);
+			while (len > 0 && text[len - 1] == ' ')
+				len--;
+		}
+	}
+
+	snprintf(dst, JWT_JSON_ERROR_TEXT_LENGTH, "%.*s", (int)len, text);
+}
+
 static void map_error(jwt_json_error_t *dst, const json_error_t *src)
 {
 	if (!dst)
@@ -84,8 +115,7 @@
 	memset(dst, 0, sizeof(*dst));
 
 	if (src) {
-		snprintf(dst->text, JWT_JSON_ERROR_TEXT_LENGTH,
-			 "%s", src->text);
+		map_error_text(dst->text, src->text);
 		snprintf(dst->source, JWT_JSON_ERROR_SOURCE_LENGTH,
 			 "%s", src->source);
 		dst->line = src->line;
diff --git a/tests/jwt_jwks_errors.c b/tests/jwt_jwks_errors.c
index 6ae8e0f..5d7ed3f 100644
--- a/tests/jwt_jwks_errors.c
+++ b/tests/jwt_jwks_errors.c
@@ -29,6 +29,152 @@
 }
 END_TEST
 
+/*
+ * Load @json, which does not parse, with jwks_create() and with
+ * jwks_create_strn(). The set reports the error as "<source>: <text>", where
+ * the text says what is wrong without repeating the input: it has none of the
+ * characters in @chars, which only the input has. Under Jansson the message
+ * is exactly @jansson_msg; json-c has fixed descriptions of its own.
+ */
+static void check_parse_error(const char *json, const char *chars,
+			      const char *jansson_msg)
+{
+	int n;
+
+	for (n = 0; n < 2; n++) {
+		jwk_set_auto_t *jwk_set = NULL;
+		const char *msg, *text;
+
+		if (n == 0)
+			jwk_set = jwks_create(json);
+		else
+			jwk_set = jwks_create_strn(json, strlen(json));
+
+		ck_assert_ptr_nonnull(jwk_set);
+		ck_assert_int_ne(jwks_error(jwk_set), 0);
+
+		msg = jwks_error_msg(jwk_set);
+		ck_assert_ptr_nonnull(msg);
+
+		/* Some text after the source, with no space left at the end */
+		text = strstr(msg, ": ");
+		ck_assert_ptr_nonnull(text);
+		ck_assert_int_ne(text[2], '\0');
+		ck_assert_int_ne(msg[strlen(msg) - 1], ' ');
+
+		ck_assert_msg(strpbrk(msg, chars) == NULL,
+			      "\"%s\" has one of \"%s\"", msg, chars);
+
+#ifdef HAVE_JSON_C
+		(void)jansson_msg;
+#else
+		ck_assert_str_eq(msg, jansson_msg);
+#endif
+	}
+}
+
+/* A word that is not true, false or null */
+START_TEST(test_jwks_parse_error_bare_word)
+{
+	SET_OPS();
+
+	check_parse_error("[QWZKY]", "QWZKY", "<buffer>: invalid token");
+}
+END_TEST
+
+/* A control character inside a string. json-c takes such a string as it is,
+ * so the comma after it is left out too, for json-c to report. The second
+ * string has " near '" in it, so the cut has to be at the first one, where
+ * Jansson's own ending starts. */
+START_TEST(test_jwks_parse_error_control_char)
+{
+	SET_OPS();
+
+	check_parse_error("[\"QWZKY\x01\" 1]", "QWZKY",
+			  "<buffer>: control character 0x1");
+	check_parse_error("[\"Q near 'W\x01\" 1]", "QW",
+			  "<buffer>: control character 0x1");
+}
+END_TEST
+
+/* A Unicode escape whose fourth character is not a hex digit */
+START_TEST(test_jwks_parse_error_bad_escape)
+{
+	SET_OPS();
+
+	check_parse_error("[\"\\uFEDQ\"]", "FEDQ",
+			  "<buffer>: invalid escape");
+}
+END_TEST
+
+/* Unpaired surrogates: a high one at the end of the string, a low one on its
+ * own, and a high one followed by a Unicode escape that is not a low one.
+ * json-c puts U+FFFD in their place, so the comma after the string is left
+ * out too, for json-c to report. Past 20 bytes of string, Jansson leaves out
+ * the " near" ending, so in the last one only the "invalid Unicode" rule
+ * takes the escape out. */
+START_TEST(test_jwks_parse_error_surrogate)
+{
+	SET_OPS();
+
+	check_parse_error("[\"\\uDBFE\" 1]", "BDEF",
+			  "<buffer>: invalid Unicode");
+	check_parse_error("[\"\\uDCBA\" 1]", "ABCD",
+			  "<buffer>: invalid Unicode");
+	check_parse_error("[\"\\uDBFE\\uABCD\" 1]", "ABCDEF",
+			  "<buffer>: invalid Unicode");
+	check_parse_error("[\"QWZKYQWZKYQWZKYQWZKY\\uDBFE\" 1]",
+			  "BDEFKQWYZ", "<buffer>: invalid Unicode");
+}
+END_TEST
+
+/* Input that ends early. Jansson's own quotes and its "near end of file" stay
+ * in the message: they repeat nothing from the input. */
+START_TEST(test_jwks_parse_error_end_of_input)
+{
+	SET_OPS();
+
+	check_parse_error("[1, 2", "12",
+			  "<buffer>: ']' expected near end of file");
+}
+END_TEST
+
+/* An object key that is not a string. Jansson's own quotes stay in the
+ * message; only the " near" ending, which repeats the input, goes. */
+START_TEST(test_jwks_parse_error_object_key)
+{
+	SET_OPS();
+
+	check_parse_error("{QWZKY}", "QWZKY",
+			  "<buffer>: string or '}' expected");
+}
+END_TEST
+
+/* A file that cannot be opened, in a directory whose name has " near '" in
+ * it. Jansson names the path and the reason, with no " near" ending, and
+ * the message stays whole. */
+START_TEST(test_jwks_missing_file)
+{
+	const char path[] = "/nonexistent/a near 'QWZKY'/x.json";
+	jwk_set_auto_t *jwk_set = NULL;
+	char exp[256];
+
+	SET_OPS();
+
+	jwk_set = jwks_create_fromfile(path);
+	ck_assert_ptr_nonnull(jwk_set);
+	ck_assert_int_ne(jwks_error(jwk_set), 0);
+
+#ifdef HAVE_JSON_C
+	snprintf(exp, sizeof(exp), "%s: Unable to open file", path);
+#else
+	snprintf(exp, sizeof(exp), "%s: unable to open %s: %s", path, path,
+		 strerror(ENOENT));
+#endif
+	ck_assert_str_eq(jwks_error_msg(jwk_set), exp);
+}
+END_TEST
+
 START_TEST(test_jwks_unknown_kty)
 {
 	const char *json = "{\"kty\":\"INVALID\"}";
@@ -128,6 +274,13 @@
 
 	/* Core JWKS Error path tests */
 	tcase_add_loop_test(tc_core, test_jwks_bad_json, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_parse_error_bare_word, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_parse_error_control_char, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_parse_error_bad_escape, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_parse_error_surrogate, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_parse_error_end_of_input, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_parse_error_object_key, 0, i);
+	tcase_add_loop_test(tc_core, test_jwks_missing_file, 0, i);
 	tcase_add_loop_test(tc_core, test_jwks_empty, 0, i);
 	tcase_add_loop_test(tc_core, test_jwks_unknown_kty, 0, i);
 	tcase_add_loop_test(tc_core, test_jwks_missing_kty, 0, i);