Skip to content

feat: yt-dlp YouTube import has no URL validation before invoking the binary β€” passing a malformed URL or a non-YouTube URL causes a cryptic yt-dlp error that surfaces as an unhandled exception in the Rust backendΒ #10

Description

@prince-pokharna

πŸ› Problem Statement

Knowte's YouTube import feature accepts a URL pasted by the user and passes it directly to the bundled yt-dlp binary. If the user pastes:

  • A non-URL string (my lecture notes)
  • A non-YouTube URL (https://vimeo.com/123456)
  • A private/unavailable video URL

...yt-dlp exits with a non-zero code. Without explicit error handling on the yt-dlp process exit, this either panics in Rust or returns an opaque error to the frontend.

πŸ’‘ Proposed Fix

1. Validate URL format and domain before invoking yt-dlp (Rust)

// src-tauri/src/downloader.rs

use url::Url; // add `url` crate to Cargo.toml

const ALLOWED_DOMAINS: &[&str] = &[
    "youtube.com",
    "youtu.be",
    "www.youtube.com",
    "m.youtube.com",
];

pub fn validate_youtube_url(raw_url: &str) -> Result<String, String> {
    let parsed = Url::parse(raw_url)
        .map_err(|_| "Invalid URL format. Please paste a valid YouTube link.".to_string())?;

    let host = parsed
        .host_str()
        .unwrap_or("")
        .trim_start_matches("www.");

    if !ALLOWED_DOMAINS.iter().any(|d| d.trim_start_matches("www.") == host) {
        return Err(format!(
            "'{}' is not a supported source. Only YouTube URLs are currently supported.",
            host
        ));
    }

    Ok(raw_url.to_string())
}

2. Handle yt-dlp non-zero exit codes explicitly

let output = Command::new(&yt_dlp_path)
    .args(["-x", "--audio-format", "mp3", "-o", &output_path, &validated_url])
    .output()
    .map_err(|e| format!("Failed to launch yt-dlp: {}", e))?;

if !output.status.success() {
    let stderr = String::from_utf8_lossy(&output.stderr);
    return Err(format!("Download failed: {}", stderr));
}

πŸ“ Files to Modify

File Change
src-tauri/src/downloader.rs Add URL validation + yt-dlp exit code handling
src-tauri/Cargo.toml Add url crate dependency
src/ Display user-friendly error message from Tauri

Suggested labels: bug, tauri, ux, validation

I would like to work on this. Could you please assign it to me?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions