Crate safebrowsing

Source
Expand description

§Safe Browsing API Client for Rust

This crate provides a Rust implementation of the Google Safe Browsing Update API (v4). It allows you to check URLs against Google’s constantly updated lists of unsafe web resources.

§Features

  • Asynchronous API using tokio
  • Pluggable database backends
  • Built-in caching with TTL support
  • URL canonicalization and pattern generation
  • Support for all Safe Browsing threat types

§Example

use safebrowsing::{SafeBrowser, Config, ThreatDescriptor, ThreatType, PlatformType, ThreatEntryType};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config {
        api_key: "your-api-key".to_string(),
        client_id: "your-client-id".to_string(),
        client_version: "1.0.0".to_string(),
        update_period: Duration::from_secs(1800), // 30 minutes
        threat_lists: vec![
            ThreatDescriptor {
                threat_type: ThreatType::Malware,
                platform_type: PlatformType::AnyPlatform,
                threat_entry_type: ThreatEntryType::Url,
            },
            ThreatDescriptor {
                threat_type: ThreatType::SocialEngineering,
                platform_type: PlatformType::AnyPlatform,
                threat_entry_type: ThreatEntryType::Url,
            },
        ],
        ..Default::default()
    };

    let mut sb = SafeBrowser::new(config).await?;
    sb.wait_until_ready().await?;

    let urls = vec!["http://example.com/suspicious"];
    let threats = sb.lookup_urls(&urls).await?;

    for (url, threat_matches) in urls.iter().zip(threats.iter()) {
        if !threat_matches.is_empty() {
            println!("⚠️  {} is unsafe: {:?}", url, threat_matches);
        } else {
            println!("✅ {} is safe", url);
        }
    }

    sb.close().await?;
    Ok(())
}

Re-exports§

pub use crate::cache::Cache;
pub use crate::error::Error;
pub use crate::error::Result;
pub use safebrowsing_api;
pub use safebrowsing_db;
pub use safebrowsing_hash;
pub use safebrowsing_proto;
pub use safebrowsing_url;

Modules§

cache
Caching for Safe Browsing API responses
database
Re-export database types from the safebrowsing-db crate.
error
Error types for the Safe Browsing library
types
Core types for the Safe Browsing library

Structs§

ConcurrentDatabase
Thread-safe wrapper around an in-memory database
Config
Configuration for the Safe Browsing client
DatabaseStats
Database statistics
HashPrefix
HashSet
HashSet for fast hash prefix lookups
InMemoryDatabase
In-memory database implementation
SafeBrowser
Main Safe Browsing client
SafeBrowsingApi
Safe Browsing API client
Stats
Statistics about Safe Browsing operations
ThreatDescriptor
A threat descriptor describes a specific threat list
URLThreat
Information about a URL that matched a threat list

Enums§

DatabaseType
Type of database to use
PlatformType
Platform types in the Safe Browsing API
ThreatEntryType
Types of threat entries in the Safe Browsing API
ThreatType
Types of threats in the Safe Browsing API

Constants§

DEFAULT_CLIENT_ID
Default client ID
DEFAULT_CLIENT_VERSION
Default client version
DEFAULT_REQUEST_TIMEOUT
Default request timeout
DEFAULT_SERVER_URL
Default Safe Browsing API server URL
DEFAULT_UPDATE_PERIOD
Default update period for threat lists (30 minutes)

Traits§

Database
Database interface for Safe Browsing

Functions§

canonicalize_url
Canonicalize a URL according to the Safe Browsing specification
generate_patterns
Generate URL patterns for lookup in the Safe Browsing database
validate_url
Check if a URL string is potentially valid