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 sqlx::PgPool;
18use systemprompt_database::DbPool;
19
20pub const MAX_SESSIONS_PER_FINGERPRINT: i32 = 5;
21pub const HIGH_REQUEST_THRESHOLD: i64 = 100;
22pub const HIGH_VELOCITY_RPM: f32 = 10.0;
23pub const SUSTAINED_VELOCITY_MINUTES: i32 = 60;
24pub const ABUSE_THRESHOLD_FOR_BAN: i32 = 3;
25
26#[derive(Clone)]
27pub struct FingerprintRepository {
28    pool: Arc<PgPool>,
29    write_pool: Arc<PgPool>,
30    sessions: systemprompt_traits::DynSessionStore,
31}
32
33impl FingerprintRepository {
34    pub fn new(db: &DbPool, sessions: systemprompt_traits::DynSessionStore) -> Self {
35        let pool = db.pool();
36        let write_pool = db.write_pool();
37        Self {
38            pool,
39            write_pool,
40            sessions,
41        }
42    }
43}
44
45impl std::fmt::Debug for FingerprintRepository {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        f.debug_struct("FingerprintRepository")
48            .finish_non_exhaustive()
49    }
50}