Day 21

A Rust URL Security Scanner

Unfortunately, you can't run this in the browser here

This program reads from stdin and uses the url crate, so it must be run locally — it cannot run in the browser.

Run cargo run in the project directory and enter a URL when prompted.


Description


A Rust CLI tool that validates a user-provided URL and enforces HTTPS. It parses the input with the url crate, rejects any URL that does not use the HTTPS scheme, and prints a confirmation or error message.

View the source code


use std::io::{self, Write};
use url::Url;

struct Args {
 url: String,
}

fn process_url(url: String) -> Result<Url, String> {
 let parsed = Url::parse(&url)
  .map_err(|e| format!("Invalid URL: {}", e))?;
 if parsed.scheme() != "https" {
  return Err("Security Error: HTTPS not used".to_string());
 }
 Ok(parsed)
}

fn main() {
 println!("Enter the URL to scan: ");
 io::stdout().flush().unwrap();
 let mut url = String::new();
 io::stdin().read_line(&mut url).expect("Failed to read line");
 let args = Args { url: url.trim().to_string() };
 match process_url(args.url.clone()) {
  Ok(valid_url) => println!("Scanning URL: {}", valid_url),
  Err(e) => println!("Error: {}", e),
 }
}

Previous Day
Next Day