@@ -1093,3 +1093,106 @@ describe("listSourceProfiles hardening", () => {
10931093 ) ,
10941094 ) ;
10951095} ) ;
1096+
1097+ describe ( "Safari profiles" , ( ) => {
1098+ const safari = BROWSER_IMPORT_SOURCES . find ( ( source ) => source . id === "safari" ) ! ;
1099+ const workUuid = "C561D071-67AD-4537-866F-54F65FB8E8DD" ;
1100+ const otherUuid = "2875EB19-B938-4E38-BE92-5AE97C256BDD" ;
1101+
1102+ const fixture = Effect . fnUntraced ( function * ( ) {
1103+ const context = yield * withSourceHome ( ) ;
1104+ const fileSystem = yield * FileSystem . FileSystem ;
1105+ const root = safari . userDataDirectory ( context ) ! ;
1106+ const library = context . path . dirname ( root ) ;
1107+ yield * fileSystem . makeDirectory ( root , { recursive : true } ) ;
1108+ yield * fileSystem . writeFileString ( context . path . join ( root , "Cookies.binarycookies" ) , "default" ) ;
1109+ const store = ( uuid : string ) =>
1110+ context . path . join ( library , "WebKit" , "WebsiteDataStore" , uuid . toLowerCase ( ) , "Cookies" ) ;
1111+ for ( const uuid of [ workUuid , otherUuid ] ) {
1112+ yield * fileSystem . makeDirectory ( store ( uuid ) , { recursive : true } ) ;
1113+ yield * fileSystem . writeFileString (
1114+ context . path . join ( store ( uuid ) , "Cookies.binarycookies" ) ,
1115+ uuid ,
1116+ ) ;
1117+ }
1118+ yield * fileSystem . makeDirectory ( context . path . join ( library , "Safari" ) , { recursive : true } ) ;
1119+ const metadata = context . path . join ( library , "Safari" , "SafariTabs.db" ) ;
1120+ return { context, root, store, metadata } ;
1121+ } ) ;
1122+
1123+ it . effect ( "discovers named profiles and resolves only the selected profile's cookies" , ( ) =>
1124+ run (
1125+ Effect . gen ( function * ( ) {
1126+ const { context, root, store, metadata } = yield * fixture ( ) ;
1127+ yield * Effect . sync ( ( ) => {
1128+ const database = new NodeSqlite . DatabaseSync ( metadata ) ;
1129+ try {
1130+ database . exec ( `CREATE TABLE bookmarks (
1131+ title TEXT, external_uuid TEXT, parent INTEGER DEFAULT 0,
1132+ type INTEGER DEFAULT 1, subtype INTEGER DEFAULT 2,
1133+ deleted INTEGER DEFAULT 0, order_index INTEGER DEFAULT 0
1134+ )` ) ;
1135+ const insert = database . prepare (
1136+ "INSERT INTO bookmarks (title, external_uuid, deleted) VALUES (?, ?, ?)" ,
1137+ ) ;
1138+ insert . run ( "" , "DefaultProfile" , 0 ) ;
1139+ insert . run ( "Ping" , workUuid , 0 ) ;
1140+ insert . run ( "Deleted" , otherUuid , 1 ) ;
1141+ insert . run ( "Unsafe" , "../../outside" , 0 ) ;
1142+ database . exec (
1143+ "INSERT INTO bookmarks (title, external_uuid, subtype) VALUES ('Tab group', 'group', 1)" ,
1144+ ) ;
1145+ } finally {
1146+ database . close ( ) ;
1147+ }
1148+ } ) ;
1149+ const profiles = yield * listSourceProfiles ( safari , context ) ;
1150+ assert . deepEqual ( profiles , [
1151+ { directory : "." , name : "Personal" } ,
1152+ { directory : store ( workUuid ) , name : "Ping" } ,
1153+ ] ) ;
1154+ assert . strictEqual (
1155+ yield * resolveCookieDatabase ( safari , context , "." ) ,
1156+ context . path . join ( root , "Cookies.binarycookies" ) ,
1157+ ) ;
1158+ const selected = yield * resolveCookieDatabase ( safari , context , profiles [ 1 ] ! . directory ) ;
1159+ assert . strictEqual ( selected , context . path . join ( store ( workUuid ) , "Cookies.binarycookies" ) ) ;
1160+ const fileSystem = yield * FileSystem . FileSystem ;
1161+ assert . strictEqual ( yield * fileSystem . readFileString ( selected ! ) , workUuid ) ;
1162+ yield * fileSystem . remove ( selected ! ) ;
1163+ assert . isUndefined ( yield * resolveCookieDatabase ( safari , context , profiles [ 1 ] ! . directory ) ) ;
1164+ assert . deepEqual ( yield * listSourceProfiles ( safari , context ) , profiles ) ;
1165+ } ) ,
1166+ ) ,
1167+ ) ;
1168+
1169+ for ( const metadataState of [ "missing" , "corrupt" ] as const ) {
1170+ it . effect ( `recovers separate cookie stores when metadata is ${ metadataState } ` , ( ) =>
1171+ run (
1172+ Effect . gen ( function * ( ) {
1173+ const { context, store, metadata } = yield * fixture ( ) ;
1174+ const fileSystem = yield * FileSystem . FileSystem ;
1175+ if ( metadataState === "corrupt" ) yield * fileSystem . writeFileString ( metadata , "invalid" ) ;
1176+ yield * fileSystem . remove ( context . path . join ( store ( otherUuid ) , "Cookies.binarycookies" ) ) ;
1177+ assert . deepEqual ( yield * listSourceProfiles ( safari , context ) , [
1178+ { directory : "." , name : "Safari" } ,
1179+ { directory : store ( workUuid ) , name : workUuid . toLowerCase ( ) } ,
1180+ ] ) ;
1181+ assert . isTrue ( yield * isSourceInstalled ( safari , context ) ) ;
1182+ } ) ,
1183+ ) ,
1184+ ) ;
1185+ }
1186+
1187+ it . effect ( "keeps Safari without profiles available" , ( ) =>
1188+ run (
1189+ Effect . gen ( function * ( ) {
1190+ const context = yield * withSourceHome ( ) ;
1191+ assert . deepEqual ( yield * listSourceProfiles ( safari , context ) , [
1192+ { directory : "." , name : "Safari" } ,
1193+ ] ) ;
1194+ assert . isFalse ( yield * isSourceInstalled ( safari , context ) ) ;
1195+ } ) ,
1196+ ) ,
1197+ ) ;
1198+ } ) ;
0 commit comments