lib/utils: Check for invalid UTF-8 in filenames
authorSimon McVittie <smcv@debian.org>
Thu, 26 Oct 2017 11:08:15 +0000 (12:08 +0100)
committerSimon McVittie <smcv@debian.org>
Thu, 26 Oct 2017 23:19:45 +0000 (00:19 +0100)
In case a filename contains invalid UTF-8 characters, libostree will
pass it to g_variant_builder_add() in create_tree_variant_from_hashes()
anyway, which leads to a critical warning from glib and an invalid
commit. This commit makes ostree print a useful error and exit instead.

Closes: #1271
Approved by: cgwalters
Origin: upstream, 2017.13, commit:2a9c5efe1d10b79681b0ee638994ead375be6597

Gbp-Pq: Topic 2017.13
Gbp-Pq: Name lib-utils-Check-for-invalid-UTF-8-in-filenames.patch

src/libotutil/ot-unix-utils.c
tests/basic-test.sh

index d6c5ee6d67b7c698c59baf8fa5598935852af691..1446cde7374aa8facd88f144b628e7e750ba91e5 100644 (file)
@@ -50,6 +50,8 @@ ot_util_filename_validate (const char *name,
     {
       return glnx_throw (error, "Invalid / in filename %s", name);
     }
+  if (!g_utf8_validate (name, -1, NULL))
+    return glnx_throw (error, "Invalid UTF-8 in filename %s", name);
   return TRUE;
 }
 
index a01f437aa1ed40efc17ab13565800bcc8731fe91..52bbe521bcfc506450b32bf57455dc14d6ff7f25 100644 (file)
@@ -19,7 +19,7 @@
 
 set -euo pipefail
 
-echo "1..$((73 + ${extra_basic_tests:-0}))"
+echo "1..$((74 + ${extra_basic_tests:-0}))"
 
 CHECKOUT_U_ARG=""
 CHECKOUT_H_ARGS="-H"
@@ -334,6 +334,14 @@ echo "ok commit from ref with modifier"
 $OSTREE commit ${COMMIT_ARGS} -b trees/test2 -s 'ref with / in it' --tree=ref=test2
 echo "ok commit ref with /"
 
+mkdir badutf8
+echo "invalid utf8 filename" > badutf8/$(printf '\x80')
+if $OSTREE commit ${COMMIT_ARGS} -b badutf8 --tree=dir=badutf8 2>err.txt; then
+    assert_not_reached "commit filename with invalid UTF-8"
+fi
+assert_file_has_content err.txt "Invalid UTF-8 in filename"
+echo "ok commit bad UTF-8"
+
 old_rev=$($OSTREE rev-parse test2)
 $OSTREE ls -R -C test2
 $OSTREE commit ${COMMIT_ARGS} --skip-if-unchanged -b trees/test2 -s 'should not be committed' --tree=ref=test2