From 92a9fe46eac06b98f1092428cc777f5cbfd30bbd Mon Sep 17 00:00:00 2001
From: Shawn <506895667@qq.com>
Date: Fri, 11 Mar 2022 20:16:03 +0800
Subject: [PATCH] fix(bridges): validate empty topics for mqtt bridge
---
.../src/mqtt/emqx_connector_mqtt_schema.erl | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl
index 2c7cfe012..7fb657ee6 100644
--- a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl
+++ b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl
@@ -28,6 +28,8 @@
, egress_desc/0
]).
+-export([non_empty_string/1]).
+
-import(emqx_schema, [mk_duration/2]).
roots() ->
@@ -101,6 +103,7 @@ fields("ingress") ->
[ {remote_topic,
sc(binary(),
#{ required => true
+ , validator => fun ?MODULE:non_empty_string/1
, desc => "Receive messages from which topic of the remote broker"
})}
, {remote_qos,
@@ -110,7 +113,8 @@ fields("ingress") ->
})}
, {local_topic,
sc(binary(),
- #{ desc => """
+ #{ validator => fun ?MODULE:non_empty_string/1
+ , desc => """
Send messages to which topic of the local broker.
Template with variables is allowed.
"""
@@ -135,10 +139,12 @@ fields("egress") ->
[ {local_topic,
sc(binary(),
#{ desc => "The local topic to be forwarded to the remote broker"
+ , validator => fun ?MODULE:non_empty_string/1
})}
, {remote_topic,
sc(binary(),
#{ default => <<"${topic}">>
+ , validator => fun ?MODULE:non_empty_string/1
, desc => """
Forward to which topic of the remote broker.
Template with variables is allowed.
@@ -233,5 +239,10 @@ Template with variables is allowed."""
qos() ->
hoconsc:union([emqx_schema:qos(), binary()]).
+non_empty_string(<<>>) -> {error, empty_string_not_allowed};
+non_empty_string("") -> {error, empty_string_not_allowed};
+non_empty_string(S) when is_binary(S); is_list(S) -> ok;
+non_empty_string(_) -> {error, invalid_string}.
+
sc(Type, Meta) -> hoconsc:mk(Type, Meta).
ref(Field) -> hoconsc:ref(?MODULE, Field).