Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable31] fix(bots): Fix default and restrict features of app bots when install with event #14231

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Command/Bot/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (!empty($input->getOption('feature'))) {
$featureFlags = Bot::featureLabelsToFlags($input->getOption('feature'));
if (str_starts_with($url, 'nextcloudapp://')) {
if (str_starts_with($url, Bot::URL_APP_PREFIX)) {
$featureFlags &= ~Bot::FEATURE_WEBHOOK;
}
} elseif (str_starts_with($url, 'nextcloudapp://')) {
} elseif (str_starts_with($url, Bot::URL_APP_PREFIX)) {
$featureFlags = Bot::FEATURE_EVENT;
} else {
$featureFlags = Bot::FEATURE_WEBHOOK + Bot::FEATURE_RESPONSE;
Expand Down
10 changes: 8 additions & 2 deletions lib/Events/BotInstallEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
protected string $secret,
protected string $url,
protected string $description = '',
protected int $features = Bot::FEATURE_WEBHOOK | Bot::FEATURE_RESPONSE,
protected ?int $features = null,
) {
parent::__construct();
}
Expand All @@ -39,6 +39,12 @@ public function getDescription(): string {
}

public function getFeatures(): int {
return $this->features;
if ($this->features !== null) {
return $this->features;
}
if (str_starts_with($this->url, Bot::URL_APP_PREFIX)) {
return Bot::FEATURE_EVENT;
}
return Bot::FEATURE_WEBHOOK | Bot::FEATURE_RESPONSE;
}
}
7 changes: 6 additions & 1 deletion lib/Listener/BotListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ protected function handleBotInstallEvent(BotInstallEvent $event): void {
} catch (DoesNotExistException) {
}

$features = $event->getFeatures();
if (str_starts_with($event->getUrl(), Bot::URL_APP_PREFIX)) {
$features &= ~Bot::FEATURE_WEBHOOK;
}

$bot = new BotServer();
$bot->setName($event->getName());
$bot->setDescription($event->getDescription());
$bot->setSecret($event->getSecret());
$bot->setUrl($event->getUrl());
$bot->setUrlHash(sha1($event->getUrl()));
$bot->setState(Bot::STATE_ENABLED);
$bot->setFeatures($event->getFeatures());
$bot->setFeatures($features);
$this->botServerMapper->insert($bot);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/Model/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Bot {
public const FEATURE_LABEL_WEBHOOK = 'webhook';
public const FEATURE_LABEL_RESPONSE = 'response';
public const FEATURE_LABEL_EVENT = 'event';
public const URL_APP_PREFIX = 'nextcloudapp://';

public const FEATURE_MAP = [
self::FEATURE_NONE => self::FEATURE_LABEL_NONE,
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/BotService.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public function validateBotParameters(string $name, string $secret, string $url,
throw new \InvalidArgumentException('The provided secret is too short (min. 40 chars, max. 128 chars)');
}

if (!$url || strlen($url) > 4000 || !(str_starts_with($url, 'http://') || str_starts_with($url, 'https://') || str_starts_with($url, 'nextcloudapp://'))) {
if (!$url || strlen($url) > 4000 || !(str_starts_with($url, 'http://') || str_starts_with($url, 'https://') || str_starts_with($url, Bot::URL_APP_PREFIX))) {
throw new \InvalidArgumentException('The provided URL is not a valid URL');
}

Expand Down
Loading