use super::*; use dever_runtime::{ auth::{self, Claims, Identity, Provider}, job::Failure, value::secret::Secret, }; #[test] fn persisted_identity_rejects_malformed_duplicate_and_oversized_input_without_leaking_it() { let file = File::new("job-identity-input"); task::run(true, 32, async |scope| { let database = prepare(scope, &file).await; let mut executor = database.acquire().await?; job::enqueue(&mut executor, scope, submission("identity", 0), 0).await?; let mut claim = std::sync::Arc::try_unwrap(job::claim(&mut executor, scope, 0, 10000).await?.unwrap()) .unwrap(); assert!( claim .revalidate(None, async |_, _| panic!("System does not verify a user")) .await .is_ok() ); let secret = "identity-value-must-not-leak"; let inputs = [ format!(r#"{{"kind":"System","tenant":"{secret}"}}"#), r#"{"kind":"System","tenant":null,"tenant":1}"#.into(), format!(r#"{{"kind":"System","tenant":null,"extra":"{secret}"}}"#), r#"{"kind":"System","kind":"User"}"#.into(), r#"{"kind":"System","tenant":null} {}"#.into(), format!( r#"{{"kind":"System","tenant":null,"extra":"{}"}}"#, "x".repeat(dever_runtime::wire::MAX_BYTES) ), ]; for input in inputs { claim.execution = input; let Err(error) = claim .revalidate(None, async |_, _| { panic!("invalid identity must not invoke a Provider") }) .await else { panic!("invalid persisted identity was accepted"); }; assert!(matches!(error, Failure::Blocked(_))); assert!(!error.to_string().contains(secret)); } Ok::<_, Error>(()) }) .unwrap() .unwrap(); } #[test] fn queued_user_is_verified_again_and_tenant_changes_are_blocked() { let file = File::new("job-identity"); task::run(true, 32, async |scope| { let database = prepare(scope, &file).await; let provider = Provider::new("accounts".into(), Secret::from_input(vec![42; 32]), 60).unwrap(); let claims = Claims { subject: "u".into(), session: "s".into(), tenant: Some("tenant".into()), site: "admin".into(), }; let token = provider.issue(&claims, 100).unwrap(); let user = auth::authenticate(&provider, &token, "admin", 101, async |_| { Ok(Identity { id: "u".into(), user_id: Some(7), tenant_id: Some(1), }) }) .await .unwrap(); let execution = Execution::user(&user, "content.article.admin.publish".into()); let mut input = submission("verify", 0); input.execution = &execution; let mut executor = database.acquire().await?; job::enqueue(&mut executor, scope, input, 0).await?; let mut claim = std::sync::Arc::try_unwrap(job::claim(&mut executor, scope, 0, 10000).await?.unwrap()) .unwrap(); assert_eq!( serde_json::from_str::(&claim.execution).unwrap(), serde_json::json!({ "kind":"User", "provider":"accounts", "subject":"u", "session":"s", "site":"admin", "tenant":"tenant", "tenant_id":1, "permission":"content.article.admin.publish" }) ); let (verified, permission) = claim .revalidate(Some(1), async |provider, claims| { assert_eq!(provider, "accounts"); assert_eq!(claims.session, "s"); Ok(Identity { id: "u".into(), user_id: Some(7), tenant_id: Some(1), }) }) .await .unwrap(); assert_eq!(verified.unwrap().user(), Some(7)); assert_eq!(permission.as_deref(), Some("content.article.admin.publish")); assert!(matches!( claim .revalidate(Some(1), async |_, _| Err(auth::Error::Unauthorized)) .await, Err(Failure::Blocked(_)) )); assert!(matches!( claim .revalidate(Some(1), async |_, _| Ok(Identity { id: "u".into(), user_id: Some(7), tenant_id: Some(2) })) .await, Err(Failure::Blocked(_)) )); assert!(matches!( claim .revalidate(Some(2), async |_, _| panic!( "wrong physical tenant cannot invoke verifier" )) .await, Err(Failure::Blocked(_)) )); let prefix = claim.execution.strip_suffix('}').unwrap().to_owned(); for extra in [r#","extra":true}"#, r#","subject":"other"}"#] { claim.execution = format!("{prefix}{extra}"); assert!(matches!( claim .revalidate(Some(1), async |_, _| panic!( "invalid user record must not invoke a Provider" )) .await, Err(Failure::Blocked(_)) )); } Ok::<_, Error>(()) }) .unwrap() .unwrap(); }