use super::support::*;
use crate::runtime::native_catalog::native_model;
use uuid::Uuid;
async fn insert_webauthn_challenge(
pool: &sqlx::PgPool,
challenge_id: &str,
) -> Result<(), sqlx::Error> {
let model = native_model(
"udb.core.authn.entity.v1.WebAuthnChallenge",
&[
"challenge_id",
"user_id",
"ceremony",
"state_json",
"tenant_id",
"project_id",
"expires_at",
],
);
let rel = &model.relation;
let sql = format!(
"INSERT INTO {rel} ({}, {}, {}, {}, {}, {}, {}) \
VALUES ($1::UUID, $2::UUID, $3, $4::JSONB, $5, $6, to_timestamp($7::DOUBLE PRECISION))",
model.q("challenge_id"),
model.q("user_id"),
model.q("ceremony"),
model.q("state_json"),
model.q("tenant_id"),
model.q("project_id"),
model.q("expires_at"),
);
sqlx::query(&sql)
.bind(challenge_id)
.bind(Uuid::new_v4().to_string())
.bind("registration")
.bind("{}")
.bind("acme")
.bind("billing")
.bind(now_unix() as f64 + 300.0)
.execute(pool)
.await
.map(|_| ())
}
#[tokio::test]
#[ignore = "requires live Postgres; run with UDB_LIVE_AUTH_TESTS=1 cargo test --lib live_postgres_webauthn_duplicate_write_classified_no_leak -- --ignored --nocapture"]
async fn live_postgres_webauthn_duplicate_write_classified_no_leak() {
let _guard = live_auth_db_lock().lock().await;
let pool = live_pg_pool().await;
migrate_native_auth_db(&pool).await;
let challenge_id = Uuid::new_v4().to_string();
insert_webauthn_challenge(&pool, &challenge_id)
.await
.expect("first WebAuthn challenge insert should succeed");
let err = insert_webauthn_challenge(&pool, &challenge_id)
.await
.expect_err("duplicate WebAuthn challenge insert must fail");
let status = crate::runtime::executor_utils::sqlx_error_to_status(
"store WebAuthn challenge failed",
&err,
);
assert_eq!(
status.code(),
tonic::Code::AlreadyExists,
"duplicate native write must map to AlreadyExists, got {:?}: {}",
status.code(),
status.message()
);
let msg = status.message().to_ascii_lowercase();
for banned in [
"duplicate key",
"23505",
"sqlstate",
"key (",
"already exists with",
] {
assert!(
!msg.contains(banned),
"classified status leaked raw DB text ({banned}): {}",
status.message()
);
}
assert!(
msg.contains("already exists"),
"AlreadyExists status should carry a safe 'already exists' message, got: {}",
status.message()
);
cleanup_native_auth_db(&pool).await;
}