Przeglądaj źródła

fix: escape annotation/error messages

Sam Jaffe 3 tygodni temu
rodzic
commit
5e1e556b38
1 zmienionych plików z 14 dodań i 0 usunięć
  1. 14 0
      include/jvalidate/validation_result.h

+ 14 - 0
include/jvalidate/validation_result.h

@@ -263,6 +263,7 @@ private:
     if (std::visit([](auto const & v) { return v.empty(); }, message)) {
       return;
     }
+    std::visit([](auto &msg) { sanitize(msg); }, message);
     results_[where][schema_path].errors.emplace(name, std::move(message));
   }
 
@@ -280,7 +281,20 @@ private:
     if (std::visit([](auto const & v) { return v.empty(); }, message)) {
       return;
     }
+    std::visit([](auto &msg) { sanitize(msg); }, message);
     results_[where][schema_path].annotations.emplace(name, std::move(message));
   }
+
+  static void sanitize(std::string &message) {
+    for (auto it = message.begin(); it != message.end(); ++it) {
+      if (*it == '"' || *it == '\\') {
+        it = ++message.insert(it, '\\');
+      }
+    }
+  }
+
+  static void sanitize(std::vector<std::string> &message) {
+    std::ranges::for_each(message, [](std::string &val) { sanitize(val); });
+  }
 };
 }