@@ -10,6 +10,7 @@ import {
1010import * as Effect from "effect/Effect" ;
1111import * as FileSystem from "effect/FileSystem" ;
1212import * as Path from "effect/Path" ;
13+ import * as PlatformError from "effect/PlatformError" ;
1314import * as Scope from "effect/Scope" ;
1415import * as Stream from "effect/Stream" ;
1516import { ChildProcess , ChildProcessSpawner } from "effect/unstable/process" ;
@@ -25,6 +26,7 @@ import {
2526 resolveCookieDatabase ,
2627 isSourceInstalled ,
2728 isSourceRunning ,
29+ isWindowsLockHeldError ,
2830 posixLockIsHeld ,
2931 listSourceProfiles ,
3032 sourcePathContext ,
@@ -54,6 +56,23 @@ describe("Linux Chromium secret applications", () => {
5456 } ) ;
5557} ) ;
5658
59+ const platformError = ( reasonTag : string ) : PlatformError . PlatformError =>
60+ ( { _tag : "PlatformError" , reason : { _tag : reasonTag } } ) as never ;
61+
62+ describe ( "Windows browser lock errors" , ( ) => {
63+ it ( "treats sharing and lock violations reported as Busy as held" , ( ) => {
64+ assert . isTrue ( isWindowsLockHeldError ( platformError ( "Busy" ) ) ) ;
65+ } ) ;
66+
67+ it ( "does not treat access denied as proof of an active lock" , ( ) => {
68+ assert . isFalse ( isWindowsLockHeldError ( platformError ( "PermissionDenied" ) ) ) ;
69+ } ) ;
70+
71+ it ( "does not treat a missing lock file as held" , ( ) => {
72+ assert . isFalse ( isWindowsLockHeldError ( platformError ( "NotFound" ) ) ) ;
73+ } ) ;
74+ } ) ;
75+
5776/** A scratch home with the source's user-data directory already created. */
5877const withSourceHome = Effect . fnUntraced ( function * ( ) {
5978 const fileSystem = yield * FileSystem . FileSystem ;
@@ -352,6 +371,20 @@ describe("isSourceInstalled", () => {
352371 ) ,
353372 ) ;
354373
374+ it . effect ( "detects a Chromium 127+ install with cookies under Network/" , ( ) =>
375+ run (
376+ Effect . gen ( function * ( ) {
377+ const fileSystem = yield * FileSystem . FileSystem ;
378+ const context = yield * withSourceHome ( ) ;
379+ const root = userDataDirectory ( context ) ;
380+
381+ yield * fileSystem . makeDirectory ( `${ root } /Default/Network` , { recursive : true } ) ;
382+ yield * fileSystem . writeFileString ( `${ root } /Default/Network/Cookies` , "db" ) ;
383+ assert . isTrue ( yield * isSourceInstalled ( helium , context ) ) ;
384+ } ) ,
385+ ) ,
386+ ) ;
387+
355388 it . effect ( "follows cookie database symlinks when detecting profiles" , ( ) =>
356389 run (
357390 Effect . gen ( function * ( ) {
@@ -460,6 +493,79 @@ describe("listSourceProfiles", () => {
460493 ) ,
461494 ) ;
462495
496+ it . effect ( "drops Firefox profiles that hold no cookie database" , ( ) =>
497+ run (
498+ Effect . gen ( function * ( ) {
499+ const fileSystem = yield * FileSystem . FileSystem ;
500+ const context = yield * withSourceHome ( ) ;
501+ const root = firefox . userDataDirectory ( context ) ! ;
502+ yield * fileSystem . makeDirectory ( root , { recursive : true } ) ;
503+ yield * fileSystem . writeFileString (
504+ `${ root } /profiles.ini` ,
505+ `[Profile0]
506+ Name=original
507+ IsRelative=1
508+ Path=Profiles/abcd.default-release
509+ Default=1
510+
511+ [Profile1]
512+ Name=empty
513+ IsRelative=1
514+ Path=Profiles/wxyz.empty
515+ ` ,
516+ ) ;
517+ yield * fileSystem . makeDirectory ( `${ root } /Profiles/abcd.default-release` , {
518+ recursive : true ,
519+ } ) ;
520+ yield * fileSystem . writeFileString (
521+ `${ root } /Profiles/abcd.default-release/cookies.sqlite` ,
522+ "db" ,
523+ ) ;
524+ yield * fileSystem . makeDirectory ( `${ root } /Profiles/wxyz.empty` , { recursive : true } ) ;
525+
526+ assert . deepEqual ( yield * listSourceProfiles ( firefox , context ) , [
527+ { directory : "Profiles/abcd.default-release" , name : "original" } ,
528+ ] ) ;
529+ } ) ,
530+ ) ,
531+ ) ;
532+
533+ it . effect ( "drops empty profiles when falling back to the Profiles/ scan" , ( ) =>
534+ run (
535+ Effect . gen ( function * ( ) {
536+ const fileSystem = yield * FileSystem . FileSystem ;
537+ const context = yield * withSourceHome ( ) ;
538+ const root = firefox . userDataDirectory ( context ) ! ;
539+ yield * fileSystem . makeDirectory ( `${ root } /Profiles/filled.default` , { recursive : true } ) ;
540+ yield * fileSystem . writeFileString ( `${ root } /Profiles/filled.default/cookies.sqlite` , "db" ) ;
541+ yield * fileSystem . makeDirectory ( `${ root } /Profiles/empty.default` , { recursive : true } ) ;
542+
543+ assert . deepEqual ( yield * listSourceProfiles ( firefox , context ) , [
544+ {
545+ directory : context . path . join ( "Profiles" , "filled.default" ) ,
546+ name : "filled.default" ,
547+ } ,
548+ ] ) ;
549+ } ) ,
550+ ) ,
551+ ) ;
552+
553+ it . effect ( "discovers profiles with cookies under Network/ (Chromium 127+)" , ( ) =>
554+ run (
555+ Effect . gen ( function * ( ) {
556+ const fileSystem = yield * FileSystem . FileSystem ;
557+ const context = yield * withSourceHome ( ) ;
558+ const root = userDataDirectory ( context ) ;
559+ yield * fileSystem . makeDirectory ( `${ root } /Default/Network` , { recursive : true } ) ;
560+ yield * fileSystem . writeFileString ( `${ root } /Default/Network/Cookies` , "db" ) ;
561+
562+ assert . deepEqual ( yield * listSourceProfiles ( helium , context ) , [
563+ { directory : "Default" , name : "Default" } ,
564+ ] ) ;
565+ } ) ,
566+ ) ,
567+ ) ;
568+
463569 it . effect ( "counts a profile's cookies without decrypting them" , ( ) =>
464570 run (
465571 Effect . gen ( function * ( ) {
@@ -474,6 +580,24 @@ describe("listSourceProfiles", () => {
474580 } ) ,
475581 ) ,
476582 ) ;
583+
584+ it . effect ( "falls through to the legacy database when Network/Cookies is a directory" , ( ) =>
585+ run (
586+ Effect . gen ( function * ( ) {
587+ const fileSystem = yield * FileSystem . FileSystem ;
588+ const paths = yield * withSourceHome ( ) ;
589+ const root = helium . userDataDirectory ( paths ) ;
590+ // A folder squatting on the preferred candidate path must not shadow
591+ // the real legacy database behind it.
592+ yield * fileSystem . makeDirectory ( `${ root } /Default/Network/Cookies` , { recursive : true } ) ;
593+ yield * writeCookieDatabase ( `${ root } /Default/Cookies` , 2 ) ;
594+
595+ const [ profile ] = yield * listSourceProfiles ( helium , paths ) ;
596+ assert . equal ( profile ?. directory , "Default" ) ;
597+ assert . equal ( profile ?. cookieCount , 2 ) ;
598+ } ) ,
599+ ) ,
600+ ) ;
477601} ) ;
478602
479603describe ( "cookieDatabaseCandidatePaths" , ( ) => {
@@ -512,6 +636,25 @@ describe("cookieDatabaseCandidatePaths", () => {
512636 } ) ,
513637 ) ,
514638 ) ;
639+
640+ it . effect ( "returns only cookies.sqlite for Firefox" , ( ) =>
641+ run (
642+ Effect . gen ( function * ( ) {
643+ const path = yield * Path . Path ;
644+ const context = yield * sourcePathContext . pipe (
645+ Effect . provideService ( HostProcessEnvironment , { HOME : "/tmp/test" } ) ,
646+ Effect . provideService ( HostProcessPlatform , "darwin" ) ,
647+ ) ;
648+ const candidates = cookieDatabaseCandidatePaths ( firefox , context , "Profiles/abc.default" ) ;
649+ assert . deepEqual ( candidates , [
650+ path . join (
651+ "/tmp/test" ,
652+ "Library/Application Support/Firefox/Profiles/abc.default/cookies.sqlite" ,
653+ ) ,
654+ ] ) ;
655+ } ) ,
656+ ) ,
657+ ) ;
515658} ) ;
516659
517660const firefox = BROWSER_IMPORT_SOURCES . find ( ( source ) => source . id === "firefox" ) ! ;
@@ -642,6 +785,41 @@ describe("listSourceProfiles Firefox fallback", () => {
642785 ) ;
643786 }
644787
788+ it . effect ( "scans for profiles when profiles.ini declares only ones without cookies" , ( ) =>
789+ run (
790+ Effect . gen ( function * ( ) {
791+ const fileSystem = yield * FileSystem . FileSystem ;
792+ const path = yield * Path . Path ;
793+ const home = yield * fileSystem . makeTempDirectoryScoped ( {
794+ prefix : "t3code-firefox-stale-ini-" ,
795+ } ) ;
796+ const context = yield * sourcePathContext . pipe (
797+ Effect . provideService ( HostProcessEnvironment , { HOME : home } ) ,
798+ Effect . provideService ( HostProcessPlatform , "darwin" ) ,
799+ ) ;
800+ const root = firefox . userDataDirectory ( context ) ! ;
801+ // `profiles.ini` names a profile that was never launched (no cookie
802+ // database), while the real cookies sit in an undeclared one.
803+ yield * fileSystem . makeDirectory ( path . join ( root , "Profiles" , "stale.default" ) , {
804+ recursive : true ,
805+ } ) ;
806+ const realDirectory = path . join ( root , "Profiles" , "real.default" ) ;
807+ yield * fileSystem . makeDirectory ( realDirectory , { recursive : true } ) ;
808+ yield * writeFirefoxCookieDatabase ( path . join ( realDirectory , "cookies.sqlite" ) , 3 , 0 ) ;
809+ yield * fileSystem . writeFileString (
810+ path . join ( root , "profiles.ini" ) ,
811+ [ "[Profile0]" , "Name=Stale" , "IsRelative=1" , "Path=Profiles/stale.default" ] . join ( "\n" ) ,
812+ ) ;
813+
814+ // Returning the empty declared list would hide the browser entirely.
815+ assert . deepEqual ( yield * listSourceProfiles ( firefox , context ) , [
816+ { directory : "Profiles/real.default" , name : "real.default" , cookieCount : 3 } ,
817+ ] ) ;
818+ assert . isTrue ( yield * isSourceInstalled ( firefox , context ) ) ;
819+ } ) ,
820+ ) ,
821+ ) ;
822+
645823 it . effect ( "counts only importable cookies for declared and fallback profiles" , ( ) =>
646824 run (
647825 Effect . gen ( function * ( ) {
@@ -809,6 +987,49 @@ describe("isSourceRunning for Firefox", () => {
809987 assert . isTrue ( yield * firefoxSymlinkLockIsHeld ( "10.0.0.7:+9999" , local , alive ) ) ;
810988 } ) ,
811989 ) ;
990+
991+ it . effect ( "does not treat a stale parent.lock file as a running browser" , ( ) =>
992+ run (
993+ Effect . gen ( function * ( ) {
994+ const fileSystem = yield * FileSystem . FileSystem ;
995+ const home = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3code-firefox-" } ) ;
996+ const context = yield * sourcePathContext . pipe (
997+ // Firefox's win32 root hangs off %APPDATA%; without it the root is
998+ // undefined and the fixture would escape the sandbox into the repo.
999+ Effect . provideService ( HostProcessEnvironment , {
1000+ HOME : home ,
1001+ APPDATA : `${ home } /AppData/Roaming` ,
1002+ } ) ,
1003+ Effect . provideService ( HostProcessPlatform , "win32" ) ,
1004+ ) ;
1005+ const root = firefox . userDataDirectory ( context ) ! ;
1006+ const profile = `${ root } /Profiles/gx7x7fqx.default-release` ;
1007+ yield * fileSystem . makeDirectory ( profile , { recursive : true } ) ;
1008+ yield * fileSystem . writeFileString ( `${ profile } /cookies.sqlite` , "db" ) ;
1009+
1010+ // On Windows, Firefox creates parent.lock as a regular file that
1011+ // persists after the process exits. The file is only locked while
1012+ // Firefox is running; the old stat-based check always found it.
1013+ yield * fileSystem . writeFileString ( `${ profile } /parent.lock` , "" ) ;
1014+ assert . isFalse ( yield * isSourceRunning ( firefox , context ) ) ;
1015+ } ) ,
1016+ ) ,
1017+ ) ;
1018+ } ) ;
1019+
1020+ describe ( "Windows user-data directories" , ( ) => {
1021+ it . effect ( "keeps app-bound Chromium forks unsupported on win32" , ( ) =>
1022+ Effect . sync ( ( ) => {
1023+ // Helium retains the older DPAPI-backed store. Other Chromium forks use
1024+ // App-Bound Encryption, so omitting win32 makes `unavailableReason`
1025+ // report `unsupportedPlatform` and keeps them out of the menu.
1026+ for ( const source of BROWSER_IMPORT_SOURCES ) {
1027+ if ( source . engine === "chromium" && source . id !== "helium" ) {
1028+ assert . notInclude ( source . platforms , "win32" ) ;
1029+ }
1030+ }
1031+ } ) ,
1032+ ) ;
8121033} ) ;
8131034
8141035describe ( "listSourceProfiles hardening" , ( ) => {
0 commit comments