Skip to content

Commit ae72891

Browse files
committed
fix(ai-apis): reject text inputs that are longer than 64K chars
Signed-off-by: Julien Veyssier <[email protected]>
1 parent 3d7b3ad commit ae72891

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

core/Controller/TextProcessingApiController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public function taskTypes(): DataResponse {
102102
#[AnonRateLimit(limit: 5, period: 120)]
103103
#[ApiRoute(verb: 'POST', url: '/schedule', root: '/textprocessing')]
104104
public function schedule(string $input, string $type, string $appId, string $identifier = ''): DataResponse {
105+
if (strlen($input) > 64_000) {
106+
return new DataResponse(['message' => $this->l->t('Input text is too long')], Http::STATUS_BAD_REQUEST);
107+
}
105108
try {
106109
$task = new Task($type, $input, $appId, $this->userId, $identifier);
107110
} catch (InvalidArgumentException) {

core/Controller/TextToImageApiController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public function isAvailable(): DataResponse {
7878
#[UserRateLimit(limit: 20, period: 120)]
7979
#[ApiRoute(verb: 'POST', url: '/schedule', root: '/text2image')]
8080
public function schedule(string $input, string $appId, string $identifier = '', int $numberOfImages = 8): DataResponse {
81+
if (strlen($input) > 64_000) {
82+
return new DataResponse(['message' => $this->l->t('Input text is too long')], Http::STATUS_BAD_REQUEST);
83+
}
8184
$task = new Task($input, $appId, $numberOfImages, $this->userId, $identifier);
8285
try {
8386
try {

core/Controller/TranslationApiController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function languages(): DataResponse {
6767
#[AnonRateLimit(limit: 10, period: 120)]
6868
#[ApiRoute(verb: 'POST', url: '/translate', root: '/translation')]
6969
public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
70+
if (strlen($text) > 64_000) {
71+
return new DataResponse(['message' => $this->l10n->t('Input text is too long')], Http::STATUS_BAD_REQUEST);
72+
}
7073
try {
7174
$translation = $this->translationManager->translate($text, $fromLanguage, $toLanguage);
7275

lib/public/TaskProcessing/EShapeType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ private function validateNonFileType(mixed $value): void {
8282
*/
8383
public function validateInput(mixed $value): void {
8484
$this->validateNonFileType($value);
85+
if ($this === EShapeType::Text && is_string($value) && strlen($value) > 64_000) {
86+
throw new ValidationException('Text is too long');
87+
}
8588
if ($this === EShapeType::Image && !is_numeric($value)) {
8689
throw new ValidationException('Non-image item provided for Image slot');
8790
}

0 commit comments

Comments
 (0)