@@ -6,19 +6,27 @@ import { DatabaseSync } from 'node:sqlite';
66import { setTimeout as delay } from 'node:timers/promises' ;
77import { promisify } from 'node:util' ;
88import { Fault , relativeName } from '../core/util.js' ;
9- import { restrictToOwner , runWindowsProfileScript } from '../core/win-acl.js' ;
9+ import { protectWindowsStage , runWindowsProfileScript } from '../core/win-acl.js' ;
1010
1111const exec = promisify ( execFile ) ;
12- const conflict = ( ) =>
12+ const conflict = ( reason : string ) =>
1313 new Fault (
1414 'profile_config_conflict' ,
15- 'The profile configuration changed, is busy or cannot retain its protections . Reload it before retrying.' ,
15+ `Profile update stopped: ${ reason } . Reload the configuration before retrying.` ,
1616 409 ,
1717 ) ;
1818const info = ( file : string ) => lstat ( file , { bigint : true } ) ;
1919const identical = ( a : BigIntStats , b : BigIntStats ) => a . dev === b . dev && a . ino === b . ino ;
20- const unchanged = ( a : BigIntStats , b : BigIntStats ) =>
21- identical ( a , b ) && a . size === b . size && a . ctimeNs === b . ctimeNs && a . mtimeNs === b . mtimeNs ;
20+ const revisionChange = ( a : BigIntStats , b : BigIntStats ) =>
21+ ! identical ( a , b )
22+ ? 'file identity'
23+ : a . size !== b . size
24+ ? 'file size'
25+ : a . mtimeNs !== b . mtimeNs
26+ ? 'modification time'
27+ : a . ctimeNs !== b . ctimeNs
28+ ? 'change time'
29+ : undefined ;
2230
2331const creatingGuards = new Map < string , Promise < void > > ( ) ;
2432async function prepareGuard ( guard : string ) {
@@ -53,7 +61,7 @@ export async function withProfileWriter<T>(
5361 initial . nlink !== 1n ||
5462 ( process . getuid && ( initial . uid !== BigInt ( process . getuid ( ) ) || ( initial . mode & 0o022n ) !== 0n ) )
5563 )
56- throw conflict ( ) ;
64+ throw conflict ( 'writer guard has unexpected type or permissions' ) ;
5765 const db = new DatabaseSync ( guard ) ;
5866 try {
5967 db . exec ( 'PRAGMA busy_timeout=0' ) ;
@@ -74,13 +82,13 @@ export async function withProfileWriter<T>(
7482 'Another caller-key setup is still active; retry without deleting its files.' ,
7583 503 ,
7684 ) ;
77- throw conflict ( ) ;
85+ throw conflict ( 'another configuration writer is still active' ) ;
7886 }
7987 await delay ( pause ) ;
8088 if ( subject === 'caller-key' ) pause = Math . min ( 250 , pause * 2 ) ;
8189 }
8290 }
83- if ( ! identical ( initial , await info ( guard ) ) ) throw conflict ( ) ;
91+ if ( ! identical ( initial , await info ( guard ) ) ) throw conflict ( 'writer guard was replaced' ) ;
8492 return await apply ( ) ;
8593 } finally {
8694 db . close ( ) ;
@@ -89,14 +97,17 @@ export async function withProfileWriter<T>(
8997
9098async function snapshot ( file : string , expected : Buffer ) : Promise < BigIntStats > {
9199 const named = await info ( file ) ;
92- if ( ! named . isFile ( ) || named . size !== BigInt ( expected . byteLength ) ) throw conflict ( ) ;
100+ if ( ! named . isFile ( ) || named . size !== BigInt ( expected . byteLength ) )
101+ throw conflict ( 'source type or size differs from the reviewed file' ) ;
93102 const handle = await open (
94103 file ,
95104 constants . O_RDWR | ( constants . O_NOFOLLOW ?? 0 ) | ( constants . O_NONBLOCK ?? 0 ) ,
96105 ) ;
97106 try {
98107 const s = await handle . stat ( { bigint : true } ) ;
99- if ( ! unchanged ( named , s ) || ! s . isFile ( ) ) throw conflict ( ) ;
108+ const opening = revisionChange ( named , s ) ;
109+ if ( opening || ! s . isFile ( ) )
110+ throw conflict ( `source changed while opening (${ opening ?? 'type' } )` ) ;
100111 const buf = Buffer . alloc ( Math . min ( expected . byteLength , 65536 ) ) ;
101112 for ( let offset = 0 ; offset < expected . length ; ) {
102113 const { bytesRead } = await handle . read (
@@ -109,10 +120,11 @@ async function snapshot(file: string, expected: Buffer): Promise<BigIntStats> {
109120 ! bytesRead ||
110121 ! buf . subarray ( 0 , bytesRead ) . equals ( expected . subarray ( offset , offset + bytesRead ) )
111122 )
112- throw conflict ( ) ;
123+ throw conflict ( 'source bytes differ from the reviewed configuration' ) ;
113124 offset += bytesRead ;
114125 }
115- if ( ! unchanged ( s , await info ( file ) ) ) throw conflict ( ) ;
126+ const closing = revisionChange ( s , await info ( file ) ) ;
127+ if ( closing ) throw conflict ( `source changed while reading (${ closing } )` ) ;
116128 return s ;
117129 } finally {
118130 await handle . close ( ) ;
@@ -122,7 +134,8 @@ async function snapshot(file: string, expected: Buffer): Promise<BigIntStats> {
122134async function destination ( file : string , allowSymlink : boolean ) : Promise < string > {
123135 if ( ! allowSymlink ) {
124136 relativeName ( path . basename ( file ) ) ;
125- if ( ( await lstat ( file ) ) . isSymbolicLink ( ) ) throw conflict ( ) ;
137+ if ( ( await lstat ( file ) ) . isSymbolicLink ( ) )
138+ throw conflict ( 'this writer cannot follow a configuration symlink' ) ;
126139 }
127140 return realpath ( file ) ;
128141}
@@ -142,7 +155,13 @@ function copyFlags(): Promise<string[]> {
142155
143156async function prepare ( source : string , stage : string , next : string , original : BigIntStats ) {
144157 if ( process . platform === 'win32' ) {
145- if ( ! ( await restrictToOwner ( path . dirname ( stage ) ) ) ) throw conflict ( ) ;
158+ const dir = path . dirname ( stage ) ,
159+ expected = await info ( dir ) ;
160+ const privacy = await protectWindowsStage ( dir , dir ) ;
161+ if ( privacy !== 'private' )
162+ throw conflict ( `temporary directory permissions were not confirmed (${ privacy } )` ) ;
163+ if ( ! expected . isDirectory ( ) || ! identical ( expected , await info ( dir ) ) )
164+ throw conflict ( 'temporary directory was replaced' ) ;
146165 } else {
147166 if ( process . platform === 'darwin' ) await exec ( '/bin/chmod' , [ '-N' , path . dirname ( stage ) ] ) ;
148167 // Node copyFile drops ACLs and extended attributes on macOS. Preserve source metadata.
@@ -157,20 +176,28 @@ async function prepare(source: string, stage: string, next: string, original: Bi
157176 copy . gid !== original . gid ||
158177 copy . mode !== original . mode
159178 )
160- throw conflict ( ) ;
179+ throw conflict ( 'temporary copy did not preserve source metadata' ) ;
161180 }
162181 const out = await open (
163182 stage ,
164183 process . platform === 'win32' ? 'wx' : constants . O_RDWR | ( constants . O_NOFOLLOW ?? 0 ) ,
165184 0o600 ,
166185 ) ;
167186 try {
168- if ( process . platform === 'win32' && ! ( await restrictToOwner ( stage ) ) ) throw conflict ( ) ;
187+ if ( process . platform === 'win32' ) {
188+ const expected = await out . stat ( { bigint : true } ) ;
189+ const privacy = await protectWindowsStage ( stage , path . dirname ( stage ) ) ;
190+ if ( privacy !== 'private' )
191+ throw conflict ( `temporary file permissions were not confirmed (${ privacy } )` ) ;
192+ const named = await info ( stage ) ;
193+ if ( ! named . isFile ( ) || named . nlink !== 1n || ! identical ( expected , named ) )
194+ throw conflict ( 'temporary file was replaced' ) ;
195+ }
169196 await out . truncate ( 0 ) ;
170197 await out . writeFile ( next , 'utf8' ) ;
171198 await out . sync ( ) ;
172199 if ( process . platform !== 'win32' && ( await out . stat ( { bigint : true } ) ) . mode !== original . mode )
173- throw conflict ( ) ;
200+ throw conflict ( 'temporary file mode changed while staging' ) ;
174201 } finally {
175202 await out . close ( ) ;
176203 }
@@ -206,11 +233,10 @@ export async function replaceProfileConfig(
206233 await prepare ( target , stage , next , original ) ;
207234 const proposed = await info ( stage ) ;
208235 await withProfileWriter ( dir , async ( ) => {
209- if (
210- ( await destination ( file , allowSymlink ) ) !== target ||
211- ! unchanged ( original , await snapshot ( target , before ) )
212- )
213- throw conflict ( ) ;
236+ if ( ( await destination ( file , allowSymlink ) ) !== target )
237+ throw conflict ( 'configuration path now resolves elsewhere' ) ;
238+ const change = revisionChange ( original , await snapshot ( target , before ) ) ;
239+ if ( change ) throw conflict ( `source changed before replacement (${ change } )` ) ;
214240 try {
215241 await replace ( stage , target ) ;
216242 } catch ( error ) {
0 commit comments