11//! The saved Builderlab browser sign-in credential of a build whose
22//! Builderlab plugin does not use NIP-FI, so that sign-in survives restarts.
3- //! It is one OS secure item of its own per profile, never another item.
3+ //! It is one OS secure item of its own per profile and compiled Builderlab
4+ //! deployment, never another item, and only that deployment's caller uses it.
45use zeroize:: Zeroizing ;
56
67#[ cfg_attr( test, allow( dead_code) ) ]
@@ -11,9 +12,36 @@ const SERVICE: &str = if cfg!(debug_assertions) {
1112} ;
1213const LIMIT : usize = 4096 ;
1314
15+ /// The item's account: the profile and the deployment's API base, so a build
16+ /// for another Builderlab never reads, sends or deletes this one's credential.
17+ fn account_for ( profile : & str , origin : & str , api_base_path : & str ) -> String {
18+ format ! ( "{profile} {origin}{api_base_path}" )
19+ }
20+
1421#[ cfg_attr( test, allow( dead_code) ) ]
1522fn account ( ) -> String {
16- std:: env:: var ( "BUZZODZ_PROFILE" ) . unwrap_or_else ( |_| "default" . into ( ) )
23+ let profile = std:: env:: var ( "BUZZODZ_PROFILE" ) . unwrap_or_else ( |_| "default" . into ( ) ) ;
24+ account_for (
25+ & profile,
26+ buzzodz_plugins:: BUILDERLAB_ORIGIN ,
27+ buzzodz_plugins:: BUILDERLAB_API_BASE_PATH ,
28+ )
29+ }
30+
31+ /// The caller's sign-in target (`oauthTarget`) must be this build's.
32+ fn bound ( target : & str , origin : & str , api_base_path : & str ) -> Result < ( ) , String > {
33+ if origin. is_empty ( ) || target != format ! ( "{origin}{}" , api_base_path. trim_end_matches( '/' ) ) {
34+ return Err ( "This saved sign-in belongs to another Builderlab" . into ( ) ) ;
35+ }
36+ Ok ( ( ) )
37+ }
38+
39+ fn this_build ( target : & str ) -> Result < ( ) , String > {
40+ bound (
41+ target,
42+ buzzodz_plugins:: BUILDERLAB_ORIGIN ,
43+ buzzodz_plugins:: BUILDERLAB_API_BASE_PATH ,
44+ )
1745}
1846
1947#[ cfg( all(
@@ -83,7 +111,8 @@ async fn blocking<T: Send + 'static>(
83111
84112/// The saved credential record, or `None` when there is none.
85113#[ tauri:: command]
86- pub ( crate ) async fn builderlab_session_load ( ) -> Result < Option < String > , String > {
114+ pub ( crate ) async fn builderlab_session_load ( target : String ) -> Result < Option < String > , String > {
115+ this_build ( & target) ?;
87116 let value = blocking ( platform:: read) . await ?;
88117 value
89118 . map ( |bytes| {
@@ -93,7 +122,8 @@ pub(crate) async fn builderlab_session_load() -> Result<Option<String>, String>
93122}
94123
95124#[ tauri:: command]
96- pub ( crate ) async fn builderlab_session_save ( value : String ) -> Result < ( ) , String > {
125+ pub ( crate ) async fn builderlab_session_save ( target : String , value : String ) -> Result < ( ) , String > {
126+ this_build ( & target) ?;
97127 let value = Zeroizing :: new ( value) ;
98128 if value. is_empty ( ) || value. len ( ) > LIMIT {
99129 return Err ( "Invalid Builderlab sign-in" . into ( ) ) ;
@@ -102,6 +132,44 @@ pub(crate) async fn builderlab_session_save(value: String) -> Result<(), String>
102132}
103133
104134#[ tauri:: command]
105- pub ( crate ) async fn builderlab_session_clear ( ) -> Result < ( ) , String > {
135+ pub ( crate ) async fn builderlab_session_clear ( target : String ) -> Result < ( ) , String > {
136+ this_build ( & target) ?;
106137 blocking ( platform:: delete) . await
107138}
139+
140+ #[ cfg( test) ]
141+ mod tests {
142+ use super :: * ;
143+
144+ #[ test]
145+ fn each_builderlab_deployment_has_its_own_item_and_callers ( ) {
146+ let public = account_for ( "default" , "https://app.builderlab.xyz" , "/api/goose/" ) ;
147+ assert_ne ! (
148+ public,
149+ account_for( "default" , "https://builderlab.example" , "/api/goose/" )
150+ ) ;
151+ assert_ne ! (
152+ public,
153+ account_for( "default" , "https://app.builderlab.xyz" , "/team/api/goose/" )
154+ ) ;
155+ assert_ne ! (
156+ public,
157+ account_for( "work" , "https://app.builderlab.xyz" , "/api/goose/" )
158+ ) ;
159+ let origin = "https://app.builderlab.xyz" ;
160+ assert ! ( bound(
161+ "https://app.builderlab.xyz/api/goose" ,
162+ origin,
163+ "/api/goose/"
164+ )
165+ . is_ok( ) ) ;
166+ for other in [
167+ "https://builderlab.example/api/goose" ,
168+ "https://app.builderlab.xyz/team/api/goose" ,
169+ "" ,
170+ ] {
171+ assert ! ( bound( other, origin, "/api/goose/" ) . is_err( ) , "{other}" ) ;
172+ }
173+ assert ! ( bound( "/api/goose" , "" , "" ) . is_err( ) ) ;
174+ }
175+ }
0 commit comments