aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2024-07-09 12:43:31 +0200
committerMarcel Klehr <mklehr@gmx.net>2024-07-17 13:55:55 +0200
commit5c457c64e88c4c7140c25a580c0964463b7c1094 (patch)
tree0d492ea5701f3ee54cd936088f3548f9ad4ec216 /lib
parent4ac1ac673e99ef067406b543c24d4c1e903238a4 (diff)
downloadnextcloud-server-5c457c64e88c4c7140c25a580c0964463b7c1094.tar.gz
nextcloud-server-5c457c64e88c4c7140c25a580c0964463b7c1094.zip
fix: Validate output properly
Differentiate between output with file IDs and output with File data Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/TaskProcessing/Manager.php37
-rw-r--r--lib/public/TaskProcessing/EShapeType.php35
2 files changed, 67 insertions, 5 deletions
diff --git a/lib/private/TaskProcessing/Manager.php b/lib/private/TaskProcessing/Manager.php
index 234534936d4..4ec8dd9cad2 100644
--- a/lib/private/TaskProcessing/Manager.php
+++ b/lib/private/TaskProcessing/Manager.php
@@ -456,7 +456,7 @@ class Manager implements IManager {
* @return void
* @throws ValidationException
*/
- private function validateOutput(array $spec, array $io, bool $optional = false): void {
+ private function validateOutputWithFileIds(array $spec, array $io, bool $optional = false): void {
foreach ($spec as $key => $descriptor) {
$type = $descriptor->getShapeType();
if (!isset($io[$key])) {
@@ -466,7 +466,31 @@ class Manager implements IManager {
throw new ValidationException('Missing key: "' . $key . '"');
}
try {
- $type->validateOutput($io[$key]);
+ $type->validateOutputWithFileIds($io[$key]);
+ } catch (ValidationException $e) {
+ throw new ValidationException('Failed to validate output key "' . $key . '": ' . $e->getMessage());
+ }
+ }
+ }
+
+ /**
+ * @param ShapeDescriptor[] $spec
+ * @param array $io
+ * @param bool $optional
+ * @return void
+ * @throws ValidationException
+ */
+ private function validateOutputWithFileData(array $spec, array $io, bool $optional = false): void {
+ foreach ($spec as $key => $descriptor) {
+ $type = $descriptor->getShapeType();
+ if (!isset($io[$key])) {
+ if ($optional) {
+ continue;
+ }
+ throw new ValidationException('Missing key: "' . $key . '"');
+ }
+ try {
+ $type->validateOutputWithFileData($io[$key]);
} catch (ValidationException $e) {
throw new ValidationException('Failed to validate output key "' . $key . '": ' . $e->getMessage());
}
@@ -651,8 +675,13 @@ class Manager implements IManager {
$optionalOutputShape = $taskTypes[$task->getTaskTypeId()]['optionalOutputShape'];
try {
// validate output
- $this->validateOutput($outputShape, $result);
- $this->validateOutput($optionalOutputShape, $result, true);
+ if (!$isUsingFileIds) {
+ $this->validateOutputWithFileData($outputShape, $result);
+ $this->validateOutputWithFileData($optionalOutputShape, $result, true);
+ } else {
+ $this->validateOutputWithFileIds($outputShape, $result);
+ $this->validateOutputWithFileIds($optionalOutputShape, $result, true);
+ }
$output = $this->removeSuperfluousArrayKeys($result, $outputShape, $optionalOutputShape);
// extract raw data and put it in files, replace it with file ids
if (!$isUsingFileIds) {
diff --git a/lib/public/TaskProcessing/EShapeType.php b/lib/public/TaskProcessing/EShapeType.php
index d66de6e01a8..ade78fda71a 100644
--- a/lib/public/TaskProcessing/EShapeType.php
+++ b/lib/public/TaskProcessing/EShapeType.php
@@ -89,7 +89,7 @@ enum EShapeType: int {
* @throws ValidationException
* @since 30.0.0
*/
- public function validateOutput(mixed $value) {
+ public function validateOutputWithFileData(mixed $value): void {
$this->validateNonFileType($value);
if ($this === EShapeType::Image && !is_string($value)) {
throw new ValidationException('Non-image item provided for Image slot');
@@ -118,6 +118,39 @@ enum EShapeType: int {
}
/**
+ * @param mixed $value
+ * @return void
+ * @throws ValidationException
+ */
+ public function validateOutputWithFileIds(mixed $value): void {
+ $this->validateNonFileType($value);
+ if ($this === EShapeType::Image && !is_numeric($value)) {
+ throw new ValidationException('Non-image item provided for Image slot');
+ }
+ if ($this === EShapeType::ListOfImages && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
+ throw new ValidationException('Non-image list item provided for ListOfImages slot');
+ }
+ if ($this === EShapeType::Audio && !is_string($value)) {
+ throw new ValidationException('Non-audio item provided for Audio slot');
+ }
+ if ($this === EShapeType::ListOfAudios && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
+ throw new ValidationException('Non-audio list item provided for ListOfAudio slot');
+ }
+ if ($this === EShapeType::Video && !is_string($value)) {
+ throw new ValidationException('Non-video item provided for Video slot');
+ }
+ if ($this === EShapeType::ListOfVideos && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
+ throw new ValidationException('Non-video list item provided for ListOfTexts slot');
+ }
+ if ($this === EShapeType::File && !is_string($value)) {
+ throw new ValidationException('Non-file item provided for File slot');
+ }
+ if ($this === EShapeType::ListOfFiles && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
+ throw new ValidationException('Non-audio list item provided for ListOfFiles slot');
+ }
+ }
+
+ /**
* @param EShapeType $type
* @return EShapeType
* @since 30.0.0