diff --git a/include/class.import.php b/include/class.import.php
index dc9a027c1d4af5606a6f9f2df293f9e097d5b72f..7e887e7690a3d042505f0b4f9e8a74e1b47dc93d 100644
--- a/include/class.import.php
+++ b/include/class.import.php
@@ -39,7 +39,7 @@ class CsvImporter {
             rewind($this->stream);
         }
         else {
-            throw new ImportError(__('Unable to parse submitted csv: ').print_r($stream, true));
+            throw new ImportError(__('Unable to parse submitted csv: ').print_r(Format::htmlchars($stream), true));
         }
     }
 
@@ -59,7 +59,7 @@ class CsvImporter {
             throw new ImportError(__('Whoops. Perhaps you meant to send some CSV records'));
 
         $headers = array();
-        foreach ($data as $h) {
+        foreach (Format::htmlchars($data) as $h) {
             $h = trim($h);
             $found = false;
             foreach ($all_fields as $f) {
@@ -68,7 +68,7 @@ class CsvImporter {
                     $found = true;
                     if (!$f->get('name'))
                         throw new ImportError(sprintf(__(
-                            '%s: Field must have `variable` set to be imported'), $h));
+                            '%s: Field must have `variable` set to be imported'), Format::htmlchars($h)));
                     $headers[$f->get('name')] = $f->get('label');
                     break;
                 }
@@ -85,7 +85,7 @@ class CsvImporter {
                 }
                 else {
                     throw new ImportError(sprintf(
-                                __('%s: Unable to map header to the object field'), $h));
+                                __('%s: Unable to map header to the object field'), Format::htmlchars($h)));
                 }
             }
         }
diff --git a/include/class.staff.php b/include/class.staff.php
index 2b0471a4cabca3f7c254482ce1f0fcd878e1b0c9..b8235be0646115c886a73e89fc6fe13e00077cfe 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -1019,8 +1019,8 @@ implements AuthenticatedUser, EmailContact, TemplateVariable, Searchable {
                 }
                 else {
                     throw new ImportError(sprintf(__('Unable to import (%s): %s'),
-                        $data['username'],
-                        print_r($errors, true)
+                        Format::htmlchars($data['username']),
+                        print_r(Format::htmlchars($errors), true)
                     ));
                 }
                 $imported++;
diff --git a/include/class.user.php b/include/class.user.php
index 36e1df54848c17b7e8de08268135820cb0079ad0..e70749626b6a565f7d6bb8035ff30ba5a47056c3 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -512,7 +512,7 @@ implements TemplateVariable, Searchable {
                     throw new ImportError('Both `name` and `email` fields are required');
                 if (!($user = static::fromVars($data, true, true)))
                     throw new ImportError(sprintf(__('Unable to import user: %s'),
-                        print_r($data, true)));
+                        print_r(Format::htmlchars($data), true)));
                 $imported++;
             }
             db_autocommit(true);