Skip to content

Commit 25bbbe3

Browse files
amabluea-maurice
authored andcommitted
Updated queries to error when given invalid query parameters.
PiperOrigin-RevId: 246582481
1 parent af15a1f commit 25bbbe3

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

database/src/desktop/query_desktop.cc

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414

1515
#include "database/src/desktop/query_desktop.h"
16+
1617
#include <sstream>
18+
1719
#include "app/memory/unique_ptr.h"
1820
#include "app/rest/transport_builder.h"
1921
#include "app/rest/transport_curl.h"
@@ -40,6 +42,47 @@ using callback::NewCallback;
4042
namespace database {
4143
namespace internal {
4244

45+
// This method validates that key index has been called with the correct
46+
// combination of parameters
47+
static bool ValidateQueryEndpoints(const QueryParams& params) {
48+
if (params.order_by == QueryParams::kOrderByKey) {
49+
const char message[] =
50+
"You must use StartAt(String value), EndAt(String value) or "
51+
"EqualTo(String value) in combination with orderByKey(). Other type of "
52+
"values or using the version with 2 parameters is not supported";
53+
if (HasStart(params)) {
54+
const Variant& start_node = GetStartValue(params);
55+
std::string start_name = GetStartName(params);
56+
if ((start_name != QueryParamsComparator::kMinKey) ||
57+
!(start_node.is_string())) {
58+
LogWarning(message);
59+
return false;
60+
}
61+
}
62+
if (HasEnd(params)) {
63+
const Variant& end_node = GetEndValue(params);
64+
std::string end_name = GetEndName(params);
65+
if ((end_name != QueryParamsComparator::kMaxKey) ||
66+
!(end_node.is_string())) {
67+
LogWarning(message);
68+
return false;
69+
}
70+
}
71+
} else {
72+
if (params.order_by == QueryParams::kOrderByPriority) {
73+
if ((HasStart(params) && !IsValidPriority(GetStartValue(params))) ||
74+
(HasEnd(params) && !IsValidPriority(GetEndValue(params)))) {
75+
LogWarning(
76+
"When using orderByPriority(), values provided to "
77+
"StartAt(), EndAt(), or EqualTo() must be valid "
78+
"priorities.");
79+
return false;
80+
}
81+
}
82+
}
83+
return true;
84+
}
85+
4386
QueryInternal::QueryInternal(DatabaseInternal* database,
4487
const QuerySpec& query_spec)
4588
: database_(database), query_spec_(query_spec) {
@@ -241,8 +284,15 @@ QueryInternal* QueryInternal::StartAt(const Variant& value) {
241284
query_spec_.path.c_str());
242285
return nullptr;
243286
}
287+
if (HasStart(query_spec_.params)) {
288+
LogWarning("Can't Call StartAt() or EqualTo() multiple times");
289+
return nullptr;
290+
}
244291
QuerySpec spec = query_spec_;
245292
spec.params.start_at_value = value;
293+
if (!ValidateQueryEndpoints(spec.params)) {
294+
return nullptr;
295+
}
246296
return new QueryInternal(database_, spec);
247297
}
248298

@@ -259,6 +309,9 @@ QueryInternal* QueryInternal::StartAt(const Variant& value,
259309
QuerySpec spec = query_spec_;
260310
spec.params.start_at_value = value;
261311
spec.params.start_at_child_key = child_key;
312+
if (!ValidateQueryEndpoints(spec.params)) {
313+
return nullptr;
314+
}
262315
return new QueryInternal(database_, spec);
263316
}
264317

@@ -270,8 +323,15 @@ QueryInternal* QueryInternal::EndAt(const Variant& value) {
270323
query_spec_.path.c_str());
271324
return nullptr;
272325
}
326+
if (HasEnd(query_spec_.params)) {
327+
LogWarning("Can't Call EndAt() or EqualTo() multiple times");
328+
return nullptr;
329+
}
273330
QuerySpec spec = query_spec_;
274331
spec.params.end_at_value = value;
332+
if (!ValidateQueryEndpoints(spec.params)) {
333+
return nullptr;
334+
}
275335
return new QueryInternal(database_, spec);
276336
}
277337

@@ -288,6 +348,9 @@ QueryInternal* QueryInternal::EndAt(const Variant& value,
288348
QuerySpec spec = query_spec_;
289349
spec.params.end_at_value = value;
290350
spec.params.end_at_child_key = child_key;
351+
if (!ValidateQueryEndpoints(spec.params)) {
352+
return nullptr;
353+
}
291354
return new QueryInternal(database_, spec);
292355
}
293356

@@ -301,6 +364,9 @@ QueryInternal* QueryInternal::EqualTo(const Variant& value) {
301364
}
302365
QuerySpec spec = query_spec_;
303366
spec.params.equal_to_value = value;
367+
if (!ValidateQueryEndpoints(spec.params)) {
368+
return nullptr;
369+
}
304370
return new QueryInternal(database_, spec);
305371
}
306372

@@ -316,6 +382,9 @@ QueryInternal* QueryInternal::EqualTo(const Variant& value,
316382
QuerySpec spec = query_spec_;
317383
spec.params.equal_to_value = value;
318384
spec.params.equal_to_child_key = child_key;
385+
if (!ValidateQueryEndpoints(spec.params)) {
386+
return nullptr;
387+
}
319388
return new QueryInternal(database_, spec);
320389
}
321390

database/src/desktop/util_desktop.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ const std::string& GetHash(const Variant& data, std::string* output) {
812812
return *output;
813813
}
814814

815+
bool IsValidPriority(const Variant& variant) {
816+
return variant.is_numeric() || variant.is_string();
817+
}
818+
815819
std::pair<Variant, Variant> MakePost(const QueryParams& params,
816820
const std::string& name,
817821
const Variant& value) {
@@ -866,15 +870,15 @@ std::string GetEndName(const QueryParams& params) {
866870
}
867871
}
868872

869-
Variant GetStartValue(const QueryParams& params) {
873+
const Variant& GetStartValue(const QueryParams& params) {
870874
FIREBASE_DEV_ASSERT_MESSAGE(
871875
HasStart(params),
872876
"Cannot get index start value if start has not been set");
873877
return params.equal_to_value.is_null() ? params.start_at_value
874878
: params.equal_to_value;
875879
}
876880

877-
Variant GetEndValue(const QueryParams& params) {
881+
const Variant& GetEndValue(const QueryParams& params) {
878882
FIREBASE_DEV_ASSERT_MESSAGE(
879883
HasEnd(params), "Cannot get index end value if end has not been set");
880884
return params.equal_to_value.is_null() ? params.end_at_value

database/src/desktop/util_desktop.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ std::pair<Variant, Variant> MakePost(const QueryParams& params,
242242
const std::string& name,
243243
const Variant& value);
244244

245+
bool IsValidPriority(const Variant& variant);
246+
245247
// Check whether the given params contain either a start_at_value or an
246248
// equal_to_value.
247249
bool HasStart(const QueryParams& params);
@@ -263,12 +265,12 @@ std::string GetEndName(const QueryParams& params);
263265
// Get the lower bound of the value for the earliest element that can appear in
264266
// an IndexedVariant associated with these QueryParams. This will either be the
265267
// start_at_value or the equal_to_value if either is set.
266-
Variant GetStartValue(const QueryParams& params);
268+
const Variant& GetStartValue(const QueryParams& params);
267269

268270
// Get the upper bound of the value for the latest element that can appear in
269271
// an IndexedVariant associated with these QueryParams. This will either be the
270272
// end_at_value or the equal_to_value if either is set.
271-
Variant GetEndValue(const QueryParams& params);
273+
const Variant& GetEndValue(const QueryParams& params);
272274

273275
// Get the earliest key/value pair that can appear in a given IndexedVariant,
274276
// based on the sorting order and range given in the QueryParams.

0 commit comments

Comments
 (0)