@@ -50,6 +50,25 @@ const BUILD_INPUTS = [
5050 "icon.svg" ,
5151] ;
5252
53+ /** Packaged files stay writable by their owner only (a umask of 002 leaves them group-writable). */
54+ function dropGroupOtherWrite ( dir : string ) : void {
55+ for ( const entry of fs . readdirSync ( dir , { withFileTypes : true } ) ) {
56+ const file = path . join ( dir , entry . name ) ;
57+ if ( entry . isSymbolicLink ( ) ) {
58+ continue ;
59+ }
60+ const mode = fs . statSync ( file ) . mode & 0o7777 ;
61+ if ( mode & 0o022 ) {
62+ fs . chmodSync ( file , mode & ~ 0o022 ) ;
63+ }
64+ if ( entry . isDirectory ( ) ) {
65+ dropGroupOtherWrite ( file ) ;
66+ }
67+ }
68+ const mode = fs . statSync ( dir ) . mode & 0o7777 ;
69+ fs . chmodSync ( dir , mode & ~ 0o022 ) ;
70+ }
71+
5372/** Rust's `std::env::consts::ARCH`, used in the metadata file name the runtime looks for. */
5473const RUNTIME_ARCH : Record < string , string > = {
5574 x64 : "x86_64" ,
@@ -297,15 +316,27 @@ export class LinuxProjectService
297316
298317 if ( release ) {
299318 const archive = path . join ( outputRoot , `${ name } .tar.gz` ) ;
300- await this . $childProcess . spawnFromEvent (
301- "tar" ,
302- [ "-czf" , archive , "-C" , outputRoot , name ] ,
303- "close" ,
304- { } ,
305- {
306- throwError : true ,
307- } ,
308- ) ;
319+ dropGroupOtherWrite ( stage ) ;
320+ // entries owned by root: extracting as root mustn't hand the files to the builder's uid
321+ const owner = [ "--owner=0" , "--group=0" , "--numeric-owner" ] ;
322+ const tar = ( extra : string [ ] ) =>
323+ this . $childProcess . spawnFromEvent (
324+ "tar" ,
325+ [ "-czf" , archive , ...extra , "-C" , outputRoot , "--" , name ] ,
326+ "close" ,
327+ { } ,
328+ { throwError : false } ,
329+ ) ;
330+ let result = await tar ( owner ) ;
331+ if ( result . exitCode !== 0 ) {
332+ // not GNU tar
333+ result = await tar ( [ ] ) ;
334+ }
335+ if ( result . exitCode !== 0 ) {
336+ throw new Error (
337+ `tar failed (exit code ${ result . exitCode } ): ${ result . stderr . trim ( ) } ` ,
338+ ) ;
339+ }
309340 this . $logger . info ( `Linux package: ${ archive } ` ) ;
310341 }
311342 }
0 commit comments