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

Add more valid domains for webhooks URL (#2491) #2499

Merged
merged 4 commits into from
Aug 6, 2023
Merged
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
25 changes: 20 additions & 5 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ pub fn parse_quotes(s: impl AsRef<str>) -> Vec<String> {
args
}

/// Discord's official domains. This is used in [`parse_webhook`] and in its corresponding test.
const DOMAINS: &[&str] = &[
"discord.com",
"canary.discord.com",
"ptb.discord.com",
"discordapp.com",
"canary.discordapp.com",
"ptb.discordapp.com",
];

/// Parses the id and token from a webhook url. Expects a [`url::Url`] object rather than a [`&str`].
///
/// # Examples
Expand All @@ -431,7 +441,7 @@ pub fn parse_quotes(s: impl AsRef<str>) -> Vec<String> {
pub fn parse_webhook(url: &Url) -> Option<(u64, &str)> {
let (webhook_id, token) = url.path().strip_prefix("/api/webhooks/")?.split_once('/')?;
if !["http", "https"].contains(&url.scheme())
|| !["discord.com", "discordapp.com"].contains(&url.domain()?)
|| !DOMAINS.contains(&url.domain()?)
|| !(17..=20).contains(&webhook_id.len())
|| !(60..=68).contains(&token.len())
{
Expand Down Expand Up @@ -504,9 +514,14 @@ mod test {

#[test]
fn test_webhook_parser() {
let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV".parse().unwrap();
let (id, token) = parse_webhook(&url).unwrap();
assert_eq!(id, 245037420704169985);
assert_eq!(token, "ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV");
for domain in DOMAINS {
let url = format!("https://{}/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV", domain).parse().unwrap();
let (id, token) = parse_webhook(&url).unwrap();
assert_eq!(id, 245037420704169985);
assert_eq!(
token,
"ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV"
);
}
}
}
Loading