@@ -64,12 +64,20 @@ describe("extension relay host-local secret", () => {
6464 } ) ;
6565
6666 it . each ( [
67- { name : "simultaneous first callers" , writerStarted : false } ,
68- { name : "a writer already in progress" , writerStarted : true } ,
69- ] ) ( "adopts the first writer's token with $name" , async ( { writerStarted } ) => {
67+ { name : "simultaneous first callers" , writerStarted : false , symlink : false } ,
68+ { name : "a writer already in progress" , writerStarted : true , symlink : false } ,
69+ { name : "a writer in progress through a directory alias" , writerStarted : true , symlink : true } ,
70+ ] ) ( "adopts the first writer's token with $name" , async ( { writerStarted, symlink } ) => {
7071 const writerToken = "a1" . repeat ( 32 ) ;
71- fs . mkdirSync ( path . dirname ( secretPath ) , { mode : 0o700 } ) ;
72- const fd = writerStarted ? fs . openSync ( secretPath , "wx" , 0o600 ) : undefined ;
72+ const credentials = path . dirname ( secretPath ) ;
73+ const target = symlink ? path . join ( stateDir , "credential-target" ) : credentials ;
74+ fs . mkdirSync ( target , { mode : 0o700 } ) ;
75+ if ( symlink ) {
76+ fs . symlinkSync ( target , credentials , "junction" ) ;
77+ }
78+ const fd = writerStarted
79+ ? fs . openSync ( path . join ( target , path . basename ( secretPath ) ) , "wx" , 0o600 )
80+ : undefined ;
7381 try {
7482 const before = fd === undefined ? undefined : fs . fstatSync ( fd ) ;
7583 const tokens = Promise . all ( [ ensureExtensionRelayToken ( env ) , ensureExtensionRelayToken ( env ) ] ) ;
@@ -80,6 +88,7 @@ describe("extension relay host-local secret", () => {
8088 const [ first , second ] = await tokens ;
8189 expect ( second ) . toBe ( first ) ;
8290 expect ( readExtensionRelayToken ( env ) ) . toBe ( first ) ;
91+ expect ( fs . lstatSync ( credentials ) . isSymbolicLink ( ) ) . toBe ( symlink ) ;
8392 if ( before ) {
8493 expect ( first ) . toBe ( writerToken ) ;
8594 expect ( fs . readFileSync ( secretPath , "utf8" ) ) . toBe ( `${ writerToken } \n` ) ;
@@ -92,6 +101,15 @@ describe("extension relay host-local secret", () => {
92101 }
93102 } ) ;
94103
104+ it ( "rejects creating a missing key through a symlinked credential directory" , async ( ) => {
105+ const target = path . join ( stateDir , "credential-target" ) ;
106+ fs . mkdirSync ( target , { mode : 0o700 } ) ;
107+ fs . symlinkSync ( target , path . dirname ( secretPath ) , "junction" ) ;
108+ await expect ( ensureExtensionRelayToken ( env ) ) . rejects . toThrow ( "must not be a symlink" ) ;
109+ expect ( fs . readdirSync ( target ) ) . toEqual ( [ ] ) ;
110+ expect ( fs . lstatSync ( path . dirname ( secretPath ) ) . isSymbolicLink ( ) ) . toBe ( true ) ;
111+ } ) ;
112+
95113 it . each ( [
96114 { name : "empty" , content : "" } ,
97115 { name : "whitespace" , content : " \n\t" } ,
@@ -100,7 +118,9 @@ describe("extension relay host-local secret", () => {
100118 fs . mkdirSync ( path . dirname ( secretPath ) , { mode : 0o700 } ) ;
101119 fs . writeFileSync ( secretPath , content , { flag : "wx" , mode : 0o600 } ) ;
102120 const before = fs . statSync ( secretPath ) ;
103- await expect ( ensureExtensionRelayToken ( env ) ) . rejects . toThrow ( ) ;
121+ await expect ( ensureExtensionRelayToken ( env ) ) . rejects . toMatchObject ( {
122+ cause : expect . any ( Error ) ,
123+ } ) ;
104124 expect ( fs . readFileSync ( secretPath , "utf8" ) ) . toBe ( content ) ;
105125 expect ( fs . statSync ( secretPath ) ) . toMatchObject ( { dev : before . dev , ino : before . ino } ) ;
106126 } ) ;
0 commit comments