atuin-common 18.23.0

common library for atuin
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Utilities for operating on databases.

#[cfg(feature = "sqlite")]
pub mod sqlite;

use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sqlx::query::{Query, QueryAs, QueryScalar};
use sqlx::{Database, FromRow, SqlSafeStr};
use thiserror::Error;
use url::Url;

use crate::string::FormatSafeUrlExt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BannedPattern {
    StarGlob,
    Alter,
}

impl BannedPattern {
    fn test(sql: &str) -> Option<Self> {
        sql.split_whitespace().find_map(|raw| {
            let token = raw.trim_matches(|c: char| matches!(c, ',' | '(' | ')' | ';'));
            let upper = token.to_ascii_uppercase();

            if upper == "ALTER" {
                Some(Self::Alter)
            } else if token == "*" || upper.ends_with(".*") {
                Some(Self::StarGlob)
            } else {
                None
            }
        })
    }

    fn reason(self) -> &'static str {
        match self {
            Self::StarGlob => "`*` is banned in queries due to being a footgun. be explicit.",
            Self::Alter => "`ALTER` is banned in queries due to being a footgun. use migrations.",
        }
    }
}

/// Utility designed to prevent foot-gunny SQL queries.
#[track_caller]
fn debug_sanity_check_query(sql: &str) {
    if cfg!(debug_assertions)
        && let Some(pattern) = BannedPattern::test(sql)
    {
        panic!("{}.\n  query: {sql}", pattern.reason());
    }
}

/// Equivalent to [`sqlx::query()`].
pub fn query<'a, DB>(sql: impl SqlSafeStr) -> Query<'a, DB, <DB as Database>::Arguments>
where
    DB: Database,
{
    let sql = sql.into_sql_str();
    debug_sanity_check_query(sql.as_str());
    #[allow(clippy::disallowed_methods)]
    sqlx::query(sql)
}

/// Equivalent to [`sqlx::query_as()`].
pub fn query_as<'q, DB, O>(sql: impl SqlSafeStr) -> QueryAs<'q, DB, O, <DB as Database>::Arguments>
where
    DB: Database,
    O: for<'r> FromRow<'r, DB::Row>,
{
    let sql = sql.into_sql_str();
    debug_sanity_check_query(sql.as_str());
    #[allow(clippy::disallowed_methods)]
    sqlx::query_as(sql)
}

/// Equivalent to [`sqlx::query_scalar()`].
pub fn query_scalar<'q, DB, O>(
    sql: impl SqlSafeStr,
) -> QueryScalar<'q, DB, O, <DB as Database>::Arguments>
where
    DB: Database,
    (O,): for<'r> FromRow<'r, DB::Row>,
{
    let sql = sql.into_sql_str();
    debug_sanity_check_query(sql.as_str());
    #[allow(clippy::disallowed_methods)]
    sqlx::query_scalar(sql)
}

#[macro_export]
macro_rules! __atuin_db_migrate {
    ($pool:expr, $dir:literal) => {{
        let pool = $pool;
        async move {
            #[allow(clippy::disallowed_macros)]
            ::sqlx::migrate!($dir).run(pool).await?;
            let mut conn = ::sqlx::Acquire::acquire(pool)
                .await
                .map_err(::sqlx::migrate::MigrateError::Execute)?;
            // Unfortunately this is necessary. Sqlx caches statements, so if you do something like
            // "SELECT * FROM foobar" where foobar has columns foo and bar, sqlx will cache the
            // "compiled" statement.
            //
            // This results in nastiness with migrations -- you _just_ ran a migration, so you need
            // to effectively clear the cached statements:
            //
            // See https://github.com/transact-rs/sqlx/issues/2517
            ::sqlx::Connection::clear_cached_statements(&mut *conn).await?;
            ::core::result::Result::<(), ::sqlx::migrate::MigrateError>::Ok(())
        }
    }};
}

pub use __atuin_db_migrate as migrate;

/// A sqlite connection string.
#[derive(Clone, PartialEq, Eq)]
pub struct SqliteDbUrl<T: Borrow<str> = String>(pub T);

/// A postgres connection URL.
#[derive(Clone, PartialEq, Eq)]
pub struct PostgresDbUrl<T: Borrow<Url> = Url>(pub T);

/// A mysql connection URL.
#[derive(Clone, PartialEq, Eq)]
pub struct MysqlDbUrl<T: Borrow<Url> = Url>(pub T);

impl<T: Borrow<str>> SqliteDbUrl<T> {
    /// The connection string as sqlx expects it, byte-for-byte.
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.borrow()
    }
}

impl<T: Borrow<Url>> PostgresDbUrl<T> {
    #[must_use]
    pub fn url(&self) -> &Url {
        self.0.borrow()
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.borrow().as_str()
    }
}

impl<T: Borrow<Url>> MysqlDbUrl<T> {
    #[must_use]
    pub fn url(&self) -> &Url {
        self.0.borrow()
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.borrow().as_str()
    }
}

impl<T: Borrow<str>> fmt::Debug for SqliteDbUrl<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.0.borrow(), f)
    }
}

impl<T: Borrow<Url>> fmt::Debug for PostgresDbUrl<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.borrow().format_safe(f)
    }
}

impl<T: Borrow<Url>> fmt::Debug for MysqlDbUrl<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.borrow().format_safe(f)
    }
}

/// A database connection URL, tagged by backend.
#[derive(Clone, PartialEq, Eq, derive_more::Debug)]
pub enum DbUrl<S = String, U = Url>
where
    S: Borrow<str>,
    U: Borrow<Url>,
{
    #[debug("{_0:?}")]
    Sqlite(SqliteDbUrl<S>),
    #[debug("{_0:?}")]
    Postgres(PostgresDbUrl<U>),
    #[debug("{_0:?}")]
    Mysql(MysqlDbUrl<U>),
}

/// An owned [`DbUrl`] — what [`FromStr`], serde, and config produce.
pub type OwnedDbUrl = DbUrl;

/// A borrowed view into an [`OwnedDbUrl`], produced by [`OwnedDbUrl::as_view`].
pub type DbUrlView<'a> = DbUrl<&'a str, &'a Url>;

impl<S: Borrow<str>, U: Borrow<Url>> DbUrl<S, U> {
    /// The connection string as sqlx expects it, byte-for-byte.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Sqlite(SqliteDbUrl(s)) => s.borrow(),
            Self::Postgres(PostgresDbUrl(u)) | Self::Mysql(MysqlDbUrl(u)) => u.borrow().as_str(),
        }
    }
}

impl<S: Borrow<str>, U: Borrow<Url>> Deref for DbUrl<S, U> {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl OwnedDbUrl {
    /// Borrow this URL as a [`DbUrlView`], without cloning.
    #[must_use]
    pub fn as_view(&self) -> DbUrlView<'_> {
        match self {
            Self::Sqlite(SqliteDbUrl(s)) => DbUrl::Sqlite(SqliteDbUrl(s.as_str())),
            Self::Postgres(PostgresDbUrl(u)) => DbUrl::Postgres(PostgresDbUrl(u)),
            Self::Mysql(MysqlDbUrl(u)) => DbUrl::Mysql(MysqlDbUrl(u)),
        }
    }
}

/// The scheme of a database URL was not one we recognise.
#[derive(Debug, Error)]
pub enum DbUrlParseError {
    #[error("unrecognised database scheme)")]
    UnknownScheme,
    #[error("invalid database url: {_0}")]
    InvalidUrl(#[from] url::ParseError),
}

impl FromStr for OwnedDbUrl {
    type Err = DbUrlParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.starts_with("postgres://") || s.starts_with("postgresql://") {
            Ok(Self::Postgres(PostgresDbUrl(Url::parse(s)?)))
        } else if s.starts_with("mysql://") {
            Ok(Self::Mysql(MysqlDbUrl(Url::parse(s)?)))
        } else if s.starts_with("sqlite:") {
            Ok(Self::Sqlite(SqliteDbUrl(s.to_owned())))
        } else {
            Err(DbUrlParseError::UnknownScheme)
        }
    }
}

impl Serialize for OwnedDbUrl {
    fn serialize<Ser: Serializer>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for OwnedDbUrl {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(deserializer)?;
        raw.parse().map_err(serde::de::Error::custom)
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod tests {
    use proptest::prelude::*;
    use rstest::{fixture, rstest};
    use sqlx::Sqlite;
    use sqlx::pool::PoolConnection;

    use super::*;
    use crate::db::sqlite::Sqlite as AtuinSqlite;

    #[rstest]
    #[case::postgres_password(
        "postgres://user:hunter2@host:5432/atuin",
        r#""postgres://user:****@host:5432/atuin""#
    )]
    #[case::mysql_password(
        "mysql://root:hunter2@127.0.0.1/atuin",
        r#""mysql://root:****@127.0.0.1/atuin""#
    )]
    // A URL without a password is printed as-is; `****` is never fabricated.
    #[case::postgres_no_password("postgres://host/atuin", r#""postgres://host/atuin""#)]
    // sqlite is never parsed as a URL, so nothing is redacted or reshaped.
    #[case::sqlite(
        "sqlite:///var/lib/atuin/atuin.db?mode=rwc",
        r#""sqlite:///var/lib/atuin/atuin.db?mode=rwc""#
    )]
    fn db_url_debug_redacts_passwords(#[case] uri: &str, #[case] expected_debug: &str) {
        let url: OwnedDbUrl = uri.parse().unwrap();
        assert_eq!(format!("{url:?}"), expected_debug);
        assert!(!format!("{url:?}").contains("hunter2"), "password leaked into Debug");
    }

    #[rstest]
    #[case::postgres("postgres://u:p@h/db")]
    #[case::mysql("mysql://u:p@h/db")]
    #[case::sqlite("sqlite://:memory:")]
    fn db_url_as_str_matches_view(#[case] uri: &str) {
        let owned: OwnedDbUrl = uri.parse().unwrap();
        assert_eq!(owned.as_str(), owned.as_view().as_str());
    }

    #[rstest]
    #[case::path("sqlite:///var/lib/atuin/atuin.db?mode=rwc")]
    #[case::memory("sqlite://:memory:")]
    fn sqlite_url_reaches_sqlx_byte_for_byte(#[case] uri: &str) {
        let url: OwnedDbUrl = uri.parse().unwrap();
        assert_eq!(url.as_str(), uri);
    }

    #[rstest]
    #[case::redis("redis://localhost")]
    #[case::bare("not-a-database-url")]
    fn db_url_rejects_unknown_scheme(#[case] uri: &str) {
        assert!(matches!(uri.parse::<OwnedDbUrl>(), Err(DbUrlParseError::UnknownScheme)));
    }

    #[rstest]
    fn db_url_serde_round_trips_through_the_real_connection_string() {
        let owned: OwnedDbUrl = "postgres://user:hunter2@host/atuin".parse().unwrap();
        let json = serde_json::to_string(&owned).unwrap();
        // serialized form is the real connection string, not the redacted debug
        assert_eq!(json, r#""postgres://user:hunter2@host/atuin""#);
        assert_eq!(serde_json::from_str::<OwnedDbUrl>(&json).unwrap(), owned);
    }

    #[rstest]
    #[case::bare_star("select * from history", Some(BannedPattern::StarGlob))]
    #[case::many_spaces_star("select      *  from history", Some(BannedPattern::StarGlob))]
    #[case::tab_star("select\t*\tfrom history", Some(BannedPattern::StarGlob))]
    #[case::newline_star("select\n  *\n  from history", Some(BannedPattern::StarGlob))]
    #[case::qualified_star("select h.* from history h", Some(BannedPattern::StarGlob))]
    #[case::star_amongst_columns("select id, * from history", Some(BannedPattern::StarGlob))]
    #[case::alter("alter table history add column x integer", Some(BannedPattern::Alter))]
    #[case::alter_upper("ALTER TABLE history ADD COLUMN x integer", Some(BannedPattern::Alter))]
    #[case::explicit("select id, timestamp, command from history", None)]
    #[case::count_star("select count(*) from history", None)]
    #[case::pragma("PRAGMA wal_checkpoint(RESTART)", None)]
    #[case::scalar_fn("select sqlite_version()", None)]
    #[case::insert("insert into t (a, b) values (?1, ?2)", None)]
    #[case::migration_only_add("add column shell text", None)]
    fn banned_pattern_classifies(#[case] sql: &str, #[case] expected: Option<BannedPattern>) {
        assert_eq!(BannedPattern::test(sql), expected);
    }

    proptest! {
        #[rstest]
        fn explicit_column_lists_are_allowed(cols in prop::collection::vec("[b-z][a-z0-9_]{0,7}", 1..8)) {
            let sql = format!("select {} from t", cols.join(", "));
            prop_assert_eq!(BannedPattern::test(&sql), None);
        }

        #[rstest]
        fn a_bare_star_selector_is_always_rejected(lead in prop::collection::vec("[b-z][a-z0-9_]{0,7}", 0..4)) {
            let mut fields = lead;
            fields.push("*".to_owned());
            let sql = format!("select {} from t", fields.join(", "));
            prop_assert_eq!(BannedPattern::test(&sql), Some(BannedPattern::StarGlob));
        }

        #[rstest]
        fn a_qualified_star_is_always_rejected(alias in "[b-z][a-z0-9_]{0,7}") {
            let sql = format!("select {alias}.* from t {alias}");
            prop_assert_eq!(BannedPattern::test(&sql), Some(BannedPattern::StarGlob));
        }
    }

    struct PrimedDb {
        _sqlite: AtuinSqlite,
        conn: PoolConnection<Sqlite>,
    }

    #[fixture]
    async fn primed_db() -> PrimedDb {
        let sqlite = AtuinSqlite::builder_in_memory().open().await.unwrap();
        let mut conn = sqlite.pool().acquire().await.unwrap();

        query::<Sqlite>("create table t (a integer, b integer)").execute(&mut *conn).await.unwrap();
        query::<Sqlite>("insert into t (a, b) values (1, 2)").execute(&mut *conn).await.unwrap();
        query_as::<Sqlite, (i64, i64)>("select a, b from t").fetch_all(&mut *conn).await.unwrap();

        PrimedDb {
            _sqlite: sqlite,
            conn,
        }
    }

    #[allow(clippy::disallowed_methods)]
    async fn add_columns(conn: &mut PoolConnection<Sqlite>, columns: &[&str]) {
        for column in columns {
            sqlx::query(sqlx::AssertSqlSafe(format!("alter table t add column {column} integer")))
                .execute(&mut **conn)
                .await
                .unwrap();
        }
    }

    #[rstest]
    #[case::add_one(&["c"])]
    #[case::add_several(&["c", "d", "e"])]
    #[tokio::test]
    async fn explicit_columns_survive_add_column(
        #[future] primed_db: PrimedDb,
        #[case] added: &[&str],
    ) {
        let mut db = primed_db.await;

        add_columns(&mut db.conn, added).await;

        let rows: Vec<(i64, i64)> = query_as::<Sqlite, (i64, i64)>("select a, b from t")
            .fetch_all(&mut *db.conn)
            .await
            .unwrap();
        assert_eq!(rows, vec![(1, 2)]);
    }
}