Skip to content

Commit 09b2393

Browse files
committed
Improve validation logic
1 parent 7ce935b commit 09b2393

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/Utopia/Messaging/Adapter/Chat/Discord.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Utopia\Messaging\Adapter;
66
use Utopia\Messaging\Messages\Discord as DiscordMessage;
77
use Utopia\Messaging\Response;
8+
use InvalidArgumentException;
89

910
class Discord extends Adapter
1011
{
@@ -15,16 +16,36 @@ class Discord extends Adapter
1516

1617
/**
1718
* @param string $webhookURL Your Discord webhook URL.
19+
* @throws InvalidArgumentException When webhook URL is invalid
1820
*/
1921
public function __construct(
2022
private string $webhookURL
2123
) {
22-
$parts = explode('/webhooks/', $webhookURL);
24+
// Validate URL format
25+
if (!filter_var($webhookURL, FILTER_VALIDATE_URL)) {
26+
throw new InvalidArgumentException('Invalid Discord webhook URL format.');
27+
}
28+
29+
// Validate URL uses https scheme
30+
$urlParts = parse_url($webhookURL);
31+
if (!isset($urlParts['scheme']) || $urlParts['scheme'] !== 'https') {
32+
throw new InvalidArgumentException('Discord webhook URL must use HTTPS scheme.');
33+
}
34+
35+
// Validate host is discord.com
36+
if (!isset($urlParts['host']) || $urlParts['host'] !== 'discord.com') {
37+
throw new InvalidArgumentException('Discord webhook URL must use discord.com as host.');
38+
}
39+
40+
// Extract and validate webhook ID
41+
$parts = explode('/webhooks/', $urlParts['path']);
2342
if (count($parts) >= 2) {
2443
$webhookParts = explode('/', $parts[1]);
2544
$this->webhookId = $webhookParts[0];
2645
}
27-
46+
if (empty($this->webhookId)) {
47+
throw new InvalidArgumentException('Discord webhook ID cannot be empty.');
48+
}
2849
}
2950

3051
public function getName(): string

0 commit comments

Comments
 (0)