Skip to content

Commit cb070cb

Browse files
committed
Remove check
1 parent 3fd8f84 commit cb070cb

5 files changed

Lines changed: 69 additions & 92 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,16 +2051,10 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
20512051

20522052
// Array operators
20532053
case Operator::TYPE_ARRAY_APPEND:
2054-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2055-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2056-
}
20572054
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
20582055
return "{$quotedColumn} = JSON_MERGE_PRESERVE(IFNULL({$quotedColumn}, JSON_ARRAY()), :$bindKey)";
20592056

20602057
case Operator::TYPE_ARRAY_PREPEND:
2061-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2062-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2063-
}
20642058
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
20652059
return "{$quotedColumn} = JSON_MERGE_PRESERVE(:$bindKey, IFNULL({$quotedColumn}, JSON_ARRAY()))";
20662060

@@ -2094,9 +2088,6 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
20942088
), JSON_ARRAY())";
20952089

20962090
case Operator::TYPE_ARRAY_INTERSECT:
2097-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2098-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2099-
}
21002091
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
21012092
return "{$quotedColumn} = IFNULL((
21022093
SELECT JSON_ARRAYAGG(jt1.value)
@@ -2108,9 +2099,6 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
21082099
), JSON_ARRAY())";
21092100

21102101
case Operator::TYPE_ARRAY_DIFF:
2111-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2112-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2113-
}
21142102
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
21152103
return "{$quotedColumn} = IFNULL((
21162104
SELECT JSON_ARRAYAGG(jt1.value)

src/Database/Adapter/MySQL.php

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ class MySQL extends MariaDB
1616
{
1717
/**
1818
* Set max execution time
19-
* @param int $milliseconds
20-
* @param string $event
21-
* @return void
19+
*
2220
* @throws DatabaseException
2321
*/
2422
public function setTimeout(int $milliseconds, string $event = Database::EVENT_ALL): void
2523
{
26-
if (!$this->getSupportForTimeouts()) {
24+
if (! $this->getSupportForTimeouts()) {
2725
return;
2826
}
2927
if ($milliseconds <= 0) {
@@ -44,29 +42,28 @@ public function setTimeout(int $milliseconds, string $event = Database::EVENT_AL
4442

4543
/**
4644
* Get size of collection on disk
47-
* @param string $collection
48-
* @return int
45+
*
4946
* @throws DatabaseException
5047
*/
5148
public function getSizeOfCollectionOnDisk(string $collection): int
5249
{
5350
$collection = $this->filter($collection);
54-
$collection = $this->getNamespace() . '_' . $collection;
51+
$collection = $this->getNamespace().'_'.$collection;
5552
$database = $this->getDatabase();
56-
$name = $database . '/' . $collection;
57-
$permissions = $database . '/' . $collection . '_perms';
53+
$name = $database.'/'.$collection;
54+
$permissions = $database.'/'.$collection.'_perms';
5855

59-
$collectionSize = $this->getPDO()->prepare("
56+
$collectionSize = $this->getPDO()->prepare('
6057
SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE)
6158
FROM INFORMATION_SCHEMA.INNODB_TABLESPACES
6259
WHERE NAME = :name
63-
");
60+
');
6461

65-
$permissionsSize = $this->getPDO()->prepare("
62+
$permissionsSize = $this->getPDO()->prepare('
6663
SELECT SUM(FS_BLOCK_SIZE + ALLOCATED_SIZE)
6764
FROM INFORMATION_SCHEMA.INNODB_TABLESPACES
6865
WHERE NAME = :permissions
69-
");
66+
');
7067

7168
$collectionSize->bindParam(':name', $name);
7269
$permissionsSize->bindParam(':permissions', $permissions);
@@ -76,7 +73,7 @@ public function getSizeOfCollectionOnDisk(string $collection): int
7673
$permissionsSize->execute();
7774
$size = $collectionSize->fetchColumn() + $permissionsSize->fetchColumn();
7875
} catch (PDOException $e) {
79-
throw new DatabaseException('Failed to get collection size: ' . $e->getMessage());
76+
throw new DatabaseException('Failed to get collection size: '.$e->getMessage());
8077
}
8178

8279
return $size;
@@ -85,14 +82,8 @@ public function getSizeOfCollectionOnDisk(string $collection): int
8582
/**
8683
* Handle distance spatial queries
8784
*
88-
* @param Query $query
89-
* @param array<string, mixed> $binds
90-
* @param string $attribute
91-
* @param string $type
92-
* @param string $alias
93-
* @param string $placeholder
94-
* @return string
95-
*/
85+
* @param array<string, mixed> $binds
86+
*/
9687
protected function handleDistanceSpatialQueries(Query $query, array &$binds, string $attribute, string $type, string $alias, string $placeholder): string
9788
{
9889
$distanceParams = $query->getValues()[0];
@@ -115,17 +106,19 @@ protected function handleDistanceSpatialQueries(Query $query, array &$binds, str
115106
$operator = '<';
116107
break;
117108
default:
118-
throw new DatabaseException('Unknown spatial query method: ' . $query->getMethod());
109+
throw new DatabaseException('Unknown spatial query method: '.$query->getMethod());
119110
}
120111

121112
if ($useMeters) {
122-
$attr = "ST_SRID({$alias}.{$attribute}, " . Database::DEFAULT_SRID . ")";
113+
$attr = "ST_SRID({$alias}.{$attribute}, ".Database::DEFAULT_SRID.')';
123114
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0", null);
115+
124116
return "ST_Distance({$attr}, {$geom}, 'metre') {$operator} :{$placeholder}_1";
125117
}
126118
// need to use srid 0 because of geometric distance
127-
$attr = "ST_SRID({$alias}.{$attribute}, " . 0 . ")";
119+
$attr = "ST_SRID({$alias}.{$attribute}, ". 0 .')';
128120
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0", 0);
121+
129122
return "ST_Distance({$attr}, {$geom}) {$operator} :{$placeholder}_1";
130123
}
131124

@@ -139,7 +132,7 @@ public function getSupportForIndexArray(): bool
139132

140133
public function getSupportForCastIndexArray(): bool
141134
{
142-
if (!$this->getSupportForIndexArray()) {
135+
if (! $this->getSupportForIndexArray()) {
143136
return false;
144137
}
145138

@@ -173,29 +166,25 @@ protected function processException(PDOException $e): \Exception
173166

174167
return parent::processException($e);
175168
}
169+
176170
/**
177171
* Does the adapter includes boundary during spatial contains?
178-
*
179-
* @return bool
180172
*/
181173
public function getSupportForBoundaryInclusiveContains(): bool
182174
{
183175
return false;
184176
}
177+
185178
/**
186179
* Does the adapter support order attribute in spatial indexes?
187-
*
188-
* @return bool
189-
*/
180+
*/
190181
public function getSupportForSpatialIndexOrder(): bool
191182
{
192183
return false;
193184
}
194185

195186
/**
196187
* Does the adapter support calculating distance(in meters) between multidimension geometry(line, polygon,etc)?
197-
*
198-
* @return bool
199188
*/
200189
public function getSupportForDistanceBetweenMultiDimensionGeometryInMeters(): bool
201190
{
@@ -204,51 +193,52 @@ public function getSupportForDistanceBetweenMultiDimensionGeometryInMeters(): bo
204193

205194
/**
206195
* Spatial type attribute
207-
*/
196+
*/
208197
public function getSpatialSQLType(string $type, bool $required): string
209198
{
210199
switch ($type) {
211200
case Database::VAR_POINT:
212201
$type = 'POINT SRID 4326';
213-
if (!$this->getSupportForSpatialIndexNull()) {
202+
if (! $this->getSupportForSpatialIndexNull()) {
214203
if ($required) {
215204
$type .= ' NOT NULL';
216205
} else {
217206
$type .= ' NULL';
218207
}
219208
}
209+
220210
return $type;
221211

222212
case Database::VAR_LINESTRING:
223213
$type = 'LINESTRING SRID 4326';
224-
if (!$this->getSupportForSpatialIndexNull()) {
214+
if (! $this->getSupportForSpatialIndexNull()) {
225215
if ($required) {
226216
$type .= ' NOT NULL';
227217
} else {
228218
$type .= ' NULL';
229219
}
230220
}
231-
return $type;
232221

222+
return $type;
233223

234224
case Database::VAR_POLYGON:
235225
$type = 'POLYGON SRID 4326';
236-
if (!$this->getSupportForSpatialIndexNull()) {
226+
if (! $this->getSupportForSpatialIndexNull()) {
237227
if ($required) {
238228
$type .= ' NOT NULL';
239229
} else {
240230
$type .= ' NULL';
241231
}
242232
}
233+
243234
return $type;
244235
}
236+
245237
return '';
246238
}
247239

248240
/**
249241
* Does the adapter support spatial axis order specification?
250-
*
251-
* @return bool
252242
*/
253243
public function getSupportForSpatialAxisOrder(): bool
254244
{
@@ -263,8 +253,6 @@ public function getSupportForObjectIndexes(): bool
263253
/**
264254
* Get the spatial axis order specification string for MySQL
265255
* MySQL with SRID 4326 expects lat-long by default, but our data is in long-lat format
266-
*
267-
* @return string
268256
*/
269257
protected function getSpatialAxisOrderSpec(): string
270258
{
@@ -273,8 +261,6 @@ protected function getSpatialAxisOrderSpec(): string
273261

274262
/**
275263
* Adapter supports optional spatial attributes with existing rows.
276-
*
277-
* @return bool
278264
*/
279265
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
280266
{
@@ -285,30 +271,21 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
285271
* Get SQL expression for operator
286272
* Override for MySQL-specific operator implementations
287273
*
288-
* @param string $column
289-
* @param \Utopia\Database\Operator $operator
290-
* @param array<string, mixed> $binds
291-
* @return ?string
274+
* @param array<string, mixed> $binds
292275
*/
293-
protected function getOperatorSQL(string $column, \Utopia\Database\Operator $operator, array &$binds): ?string
276+
protected function getOperatorSQL(string $column, Operator $operator, array &$binds): ?string
294277
{
295278
$quotedColumn = $this->quote($column);
296279
$method = $operator->getMethod();
297280
$values = $operator->getValues();
298281

299282
switch ($method) {
300-
case Operator::TYPE_ARRAY_APPEND:
301-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
302-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
303-
}
304-
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
283+
case Operator::TYPE_ARRAY_APPEND: $bindKey = $this->registerOperatorBind($binds, json_encode($values));
284+
305285
return "{$quotedColumn} = JSON_MERGE_PRESERVE(IFNULL({$quotedColumn}, JSON_ARRAY()), :$bindKey)";
306286

307-
case Operator::TYPE_ARRAY_PREPEND:
308-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
309-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
310-
}
311-
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
287+
case Operator::TYPE_ARRAY_PREPEND: $bindKey = $this->registerOperatorBind($binds, json_encode($values));
288+
312289
return "{$quotedColumn} = JSON_MERGE_PRESERVE(:$bindKey, IFNULL({$quotedColumn}, JSON_ARRAY()))";
313290

314291
case Operator::TYPE_ARRAY_UNIQUE:

src/Database/Adapter/SQLite.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,9 +2127,6 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
21272127
// Array operators
21282128
case Operator::TYPE_ARRAY_APPEND:
21292129
$values = $operator->getValues();
2130-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2131-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2132-
}
21332130
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
21342131
// SQLite: merge arrays by using json_group_array on extracted elements
21352132
// We use json_each to extract elements from both arrays and combine them
@@ -2144,9 +2141,6 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
21442141

21452142
case Operator::TYPE_ARRAY_PREPEND:
21462143
$values = $operator->getValues();
2147-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2148-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2149-
}
21502144
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
21512145
// SQLite: prepend by extracting and recombining with new elements first
21522146
return "{$quotedColumn} = (
@@ -2215,9 +2209,6 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
22152209

22162210
case Operator::TYPE_ARRAY_INTERSECT:
22172211
$values = $operator->getValues();
2218-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2219-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2220-
}
22212212
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
22222213
// SQLite: keep only values that exist in both arrays
22232214
return "{$quotedColumn} = (
@@ -2228,9 +2219,6 @@ protected function getOperatorSQL(string $column, Operator $operator, array &$bi
22282219

22292220
case Operator::TYPE_ARRAY_DIFF:
22302221
$values = $operator->getValues();
2231-
if (\count($values) > Operator::MAX_ARRAY_OPERATOR_SIZE) {
2232-
throw new DatabaseException("Array size " . \count($values) . " exceeds maximum allowed size of " . Operator::MAX_ARRAY_OPERATOR_SIZE . " for array operations");
2233-
}
22342222
$bindKey = $this->registerOperatorBind($binds, json_encode($values));
22352223
// SQLite: remove values that exist in the comparison array
22362224
return "{$quotedColumn} = (

src/Database/Validator/Operator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ private function validateOperatorForAttribute(
150150
$type = $attribute instanceof Document ? $attribute->getAttribute('type') : $attribute['type'];
151151
$isArray = $attribute instanceof Document ? ($attribute->getAttribute('array') ?? false) : ($attribute['array'] ?? false);
152152

153+
// Array operators that carry a caller-supplied value list are capped to guard against
154+
// memory exhaustion. Enforced here so every adapter rejects an oversized list the same way.
155+
if (
156+
\in_array($method, [
157+
DatabaseOperator::TYPE_ARRAY_APPEND,
158+
DatabaseOperator::TYPE_ARRAY_PREPEND,
159+
DatabaseOperator::TYPE_ARRAY_INTERSECT,
160+
DatabaseOperator::TYPE_ARRAY_DIFF,
161+
], true)
162+
&& \count($values) > DatabaseOperator::MAX_ARRAY_OPERATOR_SIZE
163+
) {
164+
$this->message = "Array size " . \count($values) . " exceeds maximum allowed size of " . DatabaseOperator::MAX_ARRAY_OPERATOR_SIZE . " for array operations";
165+
return false;
166+
}
167+
153168
switch ($method) {
154169
case DatabaseOperator::TYPE_INCREMENT:
155170
case DatabaseOperator::TYPE_DECREMENT:

0 commit comments

Comments
 (0)