Skip to main content

systemprompt_analytics/repository/fingerprint/
mod.rs

1//! Browser-fingerprint reputation tracking for abuse detection.
2//!
3//! [`FingerprintRepository`] upserts and scores `fingerprint_reputation`
4//! rows — session counts, request velocity, flags, and reputation score —
5//! used to detect and ban abusive clients. Read queries live in `queries`,
6//! state changes in `mutations`; the threshold constants here define the
7//! abuse-detection policy.
8//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12mod mutations;
13mod queries;
14
15use std::sync::Arc;
16
17use crate::Result;
18use sqlx::PgPool;
19use systemprompt_database::DbPool;
20
21pub const MAX_SESSIONS_PER_FINGERPRINT: i32 = 5;
22pub const HIGH_REQUEST_THRESHOLD: i64 = 100;
23pub const HIGH_VELOCITY_RPM: f32 = 10.0;
24pub const SUSTAINED_VELOCITY_MINUTES: i32 = 60;
25pub const ABUSE_THRESHOLD_FOR_BAN: i32 = 3;
26
27#[derive(Clone)]
28pub struct FingerprintRepository {
29    pool: Arc<PgPool>,
30    write_pool: Arc<PgPool>,
31    sessions: systemprompt_traits::DynSessionStore,
32}
33
34impl FingerprintRepository {
35    pub fn new(db: &DbPool, sessions: systemprompt_traits::DynSessionStore) -> Result<Self> {
36        let pool = db.pool_arc()?;
37        let write_pool = db.write_pool_arc()?;
38        Ok(Self {
39            pool,
40            write_pool,
41            sessions,
42        })
43    }
44}
45
46impl std::fmt::Debug for FingerprintRepository {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        f.debug_struct("FingerprintRepository")
49            .finish_non_exhaustive()
50    }
51}